diff --git a/src/Brush.py b/src/Brush.py index 9b22859..7c95d7a 100644 --- a/src/Brush.py +++ b/src/Brush.py @@ -85,30 +85,48 @@ def LayerApplied(self): # ----- for test ----- def DrawLine(self, x0, y0, x1, y1): + # print('draw', (x0, y0), (x1, y1)) + dx = x1 - x0 + dy = y1 - y0 + ab = 1 if dy > 0 else -1 + if dx == 0: + for y in range(y0, y1 + ab, ab): + self.pencil.OnMouseDown((x0, y), self._layer) + return if x0 > x1: x0, x1 = x1, x0 y0, y1 = y1, y0 - print(' ') - print((x0, y0), (x1, y1)) - dx = x1 - x0 - dy = y1 - y0 - 1 + dy = -dy + ab = -ab + # print(' ') + # print((x0, y0), (x1, y1)) slope = abs(dy / dx) - print(dx, dy) - print(slope) - ab = 1 if dy > 0 else -1 + # print(dx, dy) + # print(slope) error = 0 - y = y0 - for x in range(x0, x1 + 1): - print('x:', x) - self.pencil.OnMouseDown((x, y), self._layer) - print((x, y)) - error += slope - while error >= 0.5: - error -= 1 + if slope < 1: + y = y0 + for x in range(x0, x1 + 1): + # print((x, y), error) self.pencil.OnMouseDown((x, y), self._layer) - print((x, y)) - if y != y1: + error += slope + if error > 0.5: + error -= 1 + self.pencil.OnMouseDown((x, y), self._layer) + # print((x, y), error) y += ab + else: + slope = 1 / slope + x = x0 + for y in range(y0, y1 + ab, ab): + self.pencil.OnMouseDown((x, y), self._layer) + # print((x, y), error) + error += slope + if error > 0.5: + error -= 1 + # print((x, y), error) + self.pencil.OnMouseDown((x, y), self._layer) + x += 1 # if error > 0.5: # for _ in [0] * int(error - 0.5): # self.pencil.OnMouseDown((x, y), self._layer) diff --git a/src/Section.py b/src/Section.py index 7eb89ba..c33e4a1 100644 --- a/src/Section.py +++ b/src/Section.py @@ -80,8 +80,6 @@ class CanvasSection(Section): maxMagnification = 25 bgColor = (60, 63, 65) canvasOutlineWidth = 2 - # ----- for test ----- - first = None def SetupCanvas(self, w, h): self.sprite = Sprite.Empty(w, h) @@ -178,19 +176,11 @@ def PositionToPixel(self, x, y) -> (int, int, bool): def OnMouseDown(self, button, x, y): if button == 1: pass - _localX, _localY = self.LocalPosition((x, y)) - _pixelX, _pixelY, _valid = self.PositionToPixel(_localX, _localY) + # _localX, _localY = self.LocalPosition((x, y)) + # _pixelX, _pixelY, _valid = self.PositionToPixel(_localX, _localY) # if _valid: # Brush.OnMouseDown((_pixelX, _pixelY)) # self.Changed() - - # ----- for test ----- - if _valid: - if self.first is None: - self.first = (_pixelX, _pixelY) - else: - Brush.DrawLine(*self.first, _pixelX, _pixelY) - self.first = None elif button == 4: self.Magnify(1, self.LocalPosition((x, y))) elif button == 5: @@ -198,10 +188,13 @@ def OnMouseDown(self, button, x, y): def OnMouseDrag(self, button, x, y, _x, _y): if button == 1: - _localX, _localY = self.LocalPosition((x, y)) - _pixelX, _pixelY, _valid = self.PositionToPixel(_localX, _localY) - if _valid: - Brush.OnMouseDown((_pixelX, _pixelY)) + _pixelX, _pixelY, _valid = self.PositionToPixel(*self.LocalPosition((x, y))) + _prePixelX, _prePixelY, _preValid = self.PositionToPixel(*self.LocalPosition((_x, _y))) + if _valid and _preValid: + if abs(_prePixelX - _pixelX) > 1 or abs(_prePixelY - _pixelY) > 1: + Brush.DrawLine(_prePixelX, _prePixelY, _pixelX, _pixelY) + else: + Brush.OnMouseDown((_pixelX, _pixelY)) self.Changed() elif button == 2: self.MoveCanvas(x - _x, y - _y) diff --git a/src/Window.py b/src/Window.py index e8cb42a..b39b2bb 100644 --- a/src/Window.py +++ b/src/Window.py @@ -61,9 +61,9 @@ def MainLoop(self): self.clock.tick(self.fps) - # cnt += 1 - # if cnt % 200 == 0: - # print(self.clock.get_fps()) + cnt += 1 + if cnt % 200 == 0: + print(self.clock.get_fps()) def GetMouseSection(self): if CanvasSection.IsClicked((self.mouseX, self.mouseY)):