From fe6b79a2ab8bdd7172042fc0aef9f4ceead540d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucio=20Andr=C3=A9s=20Illanes=20Albornoz?= Date: Thu, 26 Sep 2019 23:34:01 +0200 Subject: [PATCH] Implement {circle,rectangle} dragging w/ . assets/text/hotkeys.txt: updated. assets/text/TODO: updated. --- assets/text/TODO | 9 ++++---- assets/text/hotkeys.txt | 1 + libtools/ToolCircle.py | 37 ++++++++++++++++++++++++------ libtools/ToolRect.py | 51 ++++++++++++++++++++++++++++------------- 4 files changed, 70 insertions(+), 28 deletions(-) diff --git a/assets/text/TODO b/assets/text/TODO index 3436ffd..7eb2ce2 100644 --- a/assets/text/TODO +++ b/assets/text/TODO @@ -20,10 +20,9 @@ Release roadmap: 1) {copy,cut,delete,insert from,paste}, edit asset in new canvas, import from {canvas,object} 2) floating/dockable toolbar https://wxpython.org/Phoenix/docs/html/wx.aui.AuiManager.html -3) allow dragging {circle,rect,...} w/ irrespective of current brush size -4) reimplement cursor unmasking w/ simple list of points -5) operators: crop, scale, shift, slice -6) auto{load,save} & {backup,restore} -7) tools: unicode block elements +3) reimplement cursor unmasking w/ simple list of points +4) operators: crop, scale, shift, slice +5) auto{load,save} & {backup,restore} +6) tools: unicode block elements vim:ff=dos tw=0 diff --git a/assets/text/hotkeys.txt b/assets/text/hotkeys.txt index 9843e47..39d6a00 100644 --- a/assets/text/hotkeys.txt +++ b/assets/text/hotkeys.txt @@ -20,6 +20,7 @@ Canvas hotkeys: Apply current tool with background colour (with exceptions) Tool-specific hotkeys: +(Circle, rectangle) / Initiate circle/rectangle dragging irrespective of brush size (Erase) Erase background colour with foreground colour (Fill) // Fill entire region with foreground/background colour ignoring character cells (Line, object) / Initiate line drawing/selection diff --git a/libtools/ToolCircle.py b/libtools/ToolCircle.py index 3317768..0a03fb8 100644 --- a/libtools/ToolCircle.py +++ b/libtools/ToolCircle.py @@ -5,16 +5,15 @@ # from Tool import Tool +import wx class ToolCircle(Tool): name = "Circle" + TS_NONE = 0 + TS_ORIGIN = 1 - def onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown): - brushColours, brushSize, isCursor = list(brushColours), [brushSize[0] * 2, brushSize[1]], not (mouseLeftDown or mouseRightDown) - originPoint, radius = (brushSize[0] / 2, brushSize[0] / 2), brushSize[0] - if mouseRightDown: - brushColours = [brushColours[1], brushColours[0]] - cells = [] + def _drawCircle(self, brushColours, canvas, mapPoint, originPoint, radius): + cells, patches = [], [] for brushY in range(-radius, radius + 1): cells += [[]] for brushX in range(-radius, radius + 1): @@ -22,7 +21,6 @@ class ToolCircle(Tool): cells[-1] += [[mapPoint[i] + int(originPoint[i] + o) for i, o in zip((0, 1,), (brushX, brushY,))]] if cells[-1] == []: del cells[-1] - patches = [] for numRow in range(len(cells)): for numCol in range(len(cells[numRow])): if ((numRow == 0) or (numRow == (len(cells) - 1))) \ @@ -43,6 +41,31 @@ class ToolCircle(Tool): else: patch = [*cells[numRow][numCol], brushColours[1], brushColours[1], 0, " "] patches += [patch] + return patches + + def onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown): + brushColours = [brushColours[1], brushColours[0]] if mouseRightDown else brushColours + brushSize = [brushSize[0] * 2, brushSize[1]] + if self.toolState == self.TS_NONE: + originPoint, radius, targetPoint = list(mapPoint), brushSize[0], (brushSize[0] / 2, brushSize[0] / 2,) + if (keyModifiers == wx.MOD_CONTROL) and (mouseLeftDown or mouseRightDown): + self.brushColours, isCursor, self.originPoint, self.toolState = list(brushColours), True, originPoint, self.TS_ORIGIN + else: + isCursor = not (mouseLeftDown or mouseRightDown) + elif self.toolState == self.TS_ORIGIN: + brushColours, originPoint = self.brushColours, self.originPoint + if mapPoint[0] > self.originPoint[0]: + radius, targetPoint = brushSize[0] + (mapPoint[0] - self.originPoint[0]), ((brushSize[0] + (mapPoint[0] - self.originPoint[0])) / 2, (brushSize[0] + (mapPoint[0] - self.originPoint[0])) / 2,) + else: + radius, targetPoint = brushSize[0], (brushSize[0] / 2, brushSize[0] / 2,) + if keyModifiers != wx.MOD_CONTROL: + isCursor, self.brushColours, self.originPoint, self.toolState = False, None, None, self.TS_NONE + else: + isCursor = True + patches = self._drawCircle(brushColours, canvas, originPoint, targetPoint, radius) return True, patches if not isCursor else None, patches if isCursor else None + def __init__(self, *args): + super().__init__(*args); self.brushColours, self.originPoint, self.toolState = None, None, self.TS_NONE; + # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/libtools/ToolRect.py b/libtools/ToolRect.py index 6d7d33f..053707c 100644 --- a/libtools/ToolRect.py +++ b/libtools/ToolRect.py @@ -5,32 +5,51 @@ # from Tool import Tool +import wx class ToolRect(Tool): name = "Rectangle" + TS_NONE = 0 + TS_ORIGIN = 1 - def onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown): - brushColours, brushSize, isCursor, patches = list(brushColours), list(brushSize), not (mouseLeftDown or mouseRightDown), [] - if mouseRightDown: - brushColours = [brushColours[1], brushColours[0]] - if brushSize[0] > 1: - brushSize[0] *= 2 - for brushRow in range(brushSize[1]): - for brushCol in range(brushSize[0]): - if (brushCol in [0, brushSize[0] - 1]) \ - or (brushRow in [0, brushSize[1] - 1]): + def _drawRect(self, brushColours, canvas, rect): + patches = [] + for brushRow in range(rect[3] - rect[1]): + for brushCol in range(rect[2] - rect[0]): + if (brushCol in [0, (rect[2] - rect[0]) - 1]) or (brushRow in [0, (rect[3] - rect[1]) - 1]): patchColours = [brushColours[0]] * 2 - patch = [mapPoint[0] + brushCol, mapPoint[1] + brushRow, *patchColours, 0, " "] + patch = [rect[0] + brushCol, rect[1] + brushRow, *patchColours, 0, " "] elif brushColours[1] == -1: - if ((mapPoint[0] + brushCol) < canvas.size[0]) \ - and ((mapPoint[1] + brushRow) < canvas.size[1]): - patch = [mapPoint[0] + brushCol, mapPoint[1] + brushRow, *canvas.map[mapPoint[1] + brushRow][mapPoint[0] + brushCol]] + if ((rect[0] + brushCol) < canvas.size[0]) \ + and ((rect[1] + brushRow) < canvas.size[1]): + patch = [rect[0] + brushCol, rect[1] + brushRow, *canvas.map[rect[1] + brushRow][rect[0] + brushCol]] else: - patch = [mapPoint[0] + brushCol, mapPoint[1] + brushRow, -1, -1, 0, " "] + patch = [rect[0] + brushCol, rect[1] + brushRow, -1, -1, 0, " "] else: patchColours = [brushColours[1]] * 2 - patch = [mapPoint[0] + brushCol, mapPoint[1] + brushRow, *patchColours, 0, " "] + patch = [rect[0] + brushCol, rect[1] + brushRow, *patchColours, 0, " "] patches += [patch] + return patches + + def onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown): + brushColours = [brushColours[1], brushColours[0]] if mouseRightDown else brushColours + brushSize, patches = list(brushSize), []; brushSize[0] *= 2 if brushSize[0] > 1 else brushSize[0]; + if self.toolState == self.TS_NONE: + if (keyModifiers == wx.MOD_CONTROL) and (mouseLeftDown or mouseRightDown): + self.brushColours, isCursor, self.originPoint, self.toolState = list(brushColours), True, list(mapPoint), self.TS_ORIGIN + else: + isCursor = not (mouseLeftDown or mouseRightDown) + rect = [*mapPoint, *[a + b for a, b in zip(brushSize, mapPoint)]] + elif self.toolState == self.TS_ORIGIN: + rect = [*self.originPoint, *mapPoint] + if keyModifiers != wx.MOD_CONTROL: + brushColours, isCursor, self.brushColours, self.originPoint, self.toolState = self.brushColours, False, None, None, self.TS_NONE + else: + isCursor = True + patches = self._drawRect(brushColours, canvas, rect) return True, patches if not isCursor else None, patches if isCursor else None + def __init__(self, *args): + super().__init__(*args); self.brushColours, self.originPoint, self.toolState = None, None, self.TS_NONE; + # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120