Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
Signed-off-by: Thxios <[email protected]>
  • Loading branch information
Thxios committed Mar 9, 2020
1 parent 3bb005d commit 28a75c3
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
52 changes: 35 additions & 17 deletions src/Brush.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 9 additions & 16 deletions src/Section.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -178,30 +176,25 @@ 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:
self.Magnify(-1, self.LocalPosition((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)
Expand Down
6 changes: 3 additions & 3 deletions src/Window.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down

0 comments on commit 28a75c3

Please sign in to comment.