diff --git a/libtools/MiRCARTToolSelect.py b/libtools/MiRCARTToolSelect.py index 0733d2b..85d6f36 100644 --- a/libtools/MiRCARTToolSelect.py +++ b/libtools/MiRCARTToolSelect.py @@ -8,104 +8,118 @@ from MiRCARTTool import MiRCARTTool class MiRCARTToolSelect(MiRCARTTool): """XXX""" - toolColours = toolRect = toolState = None - toolLastAtPoint = None - toolSelectMap = None - srcRect = None - TS_NONE = 0 TS_ORIGIN = 1 - TS_TARGET = 2 + TS_SELECT = 2 + TS_TARGET = 3 + # {{{ _dispatchSelectEvent(self, atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, selectRect): XXX + def _dispatchSelectEvent(self, atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, selectRect): + if isLeftDown: + disp, isCursor = [atPoint[m] - self.lastAtPoint[m] for m in [0, 1]], True + newTargetRect = [[selectRect[n][m] + disp[m] for m in [0, 1]] for n in [0, 1]] + self.lastAtPoint = list(atPoint) + elif isRightDown: + disp, isCursor, newTargetRect = [0, 0], False, selectRect.copy() + else: + disp, isCursor, newTargetRect = [0, 0], True, selectRect.copy() + self.onSelectEvent(disp, dispatchFn, eventDc, isCursor, newTargetRect, selectRect) + self._drawSelectRect(newTargetRect, dispatchFn, eventDc) + self.targetRect = newTargetRect + # }}} # {{{ _drawSelectRect(self, rect, dispatchFn, eventDc): XXX def _drawSelectRect(self, rect, dispatchFn, eventDc): - rectFrame = [ \ - [rect[0][0]-1, rect[0][1]-1], \ - [rect[1][0]+1, rect[1][1]+1]] + rectFrame = [[rect[m[0]][n] + m[1] for n in [0, 1]] for m in [[0, -1], [1, +1]]] if rectFrame[0][0] > rectFrame[1][0]: - rectFrame[0][0], rectFrame[1][0] = \ - rectFrame[1][0], rectFrame[0][0] + rectFrame[0][0], rectFrame[1][0] = rectFrame[1][0], rectFrame[0][0] if rectFrame[0][1] > rectFrame[1][1]: - rectFrame[0][1], rectFrame[1][1] = \ - rectFrame[1][1], rectFrame[0][1] + rectFrame[0][1], rectFrame[1][1] = rectFrame[1][1], rectFrame[0][1] curColours = [0, 0] for rectX in range(rectFrame[0][0], rectFrame[1][0]+1): - if curColours == [0, 0]: - curColours = [1, 1] - else: - curColours = [0, 0] - dispatchFn(eventDc, True, \ - [rectX, rectFrame[0][1], *curColours, 0, " "]) - dispatchFn(eventDc, True, \ - [rectX, rectFrame[1][1], *curColours, 0, " "]) + curColours = [1, 1] if curColours == [0, 0] else [0, 0] + dispatchFn(eventDc, True, [rectX, rectFrame[0][1], *curColours, 0, " "]) + dispatchFn(eventDc, True, [rectX, rectFrame[1][1], *curColours, 0, " "]) for rectY in range(rectFrame[0][1], rectFrame[1][1]+1): - if curColours == [0, 0]: - curColours = [1, 1] - else: - curColours = [0, 0] - dispatchFn(eventDc, True, \ - [rectFrame[0][0], rectY, *curColours, 0, " "]) - dispatchFn(eventDc, True, \ - [rectFrame[1][0], rectY, *curColours, 0, " "]) + curColours = [1, 1] if curColours == [0, 0] else [0, 0] + dispatchFn(eventDc, True, [rectFrame[0][0], rectY, *curColours, 0, " "]) + dispatchFn(eventDc, True, [rectFrame[1][0], rectY, *curColours, 0, " "]) + # }}} + # {{{ _mouseEventTsNone(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): XXX + def _mouseEventTsNone(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): + if isLeftDown: + self.targetRect, self.toolState = [list(atPoint), []], self.TS_ORIGIN + else: + dispatchFn(eventDc, True, [*atPoint, *brushColours, 0, " "]) + # }}} + # {{{ _mouseEventTsOrigin(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): XXX + def _mouseEventTsOrigin(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): + if isLeftDown: + self.targetRect[1] = list(atPoint) + if self.targetRect[0][0] > self.targetRect[1][0]: + self.targetRect[0][0], self.targetRect[1][0] = self.targetRect[1][0], self.targetRect[0][0] + if self.targetRect[0][1] > self.targetRect[1][1]: + self.targetRect[0][1], self.targetRect[1][1] = self.targetRect[1][1], self.targetRect[0][1] + self.srcRect, self.lastAtPoint, self.toolSelectMap, self.toolState = self.targetRect[0], list(atPoint), [], self.TS_SELECT + for numRow in range((self.targetRect[1][1] - self.targetRect[0][1]) + 1): + self.toolSelectMap.append([]) + for numCol in range((self.targetRect[1][0] - self.targetRect[0][0]) + 1): + rectX, rectY = self.targetRect[0][0] + numCol, self.targetRect[0][1] + numRow + self.toolSelectMap[numRow].append(self.parentCanvas.canvasMap[rectY][rectX]) + self._drawSelectRect(self.targetRect, dispatchFn, eventDc) + elif isRightDown: + self.targetRect, self.toolState = None, self.TS_NONE + else: + self.targetRect[1] = list(atPoint) + self._drawSelectRect(self.targetRect, dispatchFn, eventDc) + # }}} + # {{{ _mouseEventTsSelect(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): XXX + def _mouseEventTsSelect(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): + if isLeftDown \ + and (atPoint[0] >= (self.targetRect[0][0] - 1)) \ + and (atPoint[0] <= (self.targetRect[1][0] + 1)) \ + and (atPoint[1] >= (self.targetRect[0][1] - 1)) \ + and (atPoint[1] <= (self.targetRect[1][1] + 1)): + self.lastAtPoint, self.toolState = list(atPoint), self.TS_TARGET + elif isRightDown: + self._dispatchSelectEvent(atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, self.targetRect) + self.targetRect, self.toolState = None, self.TS_NONE + else: + self._dispatchSelectEvent(atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, self.targetRect) + # }}} + # {{{ _mouseEventTsTarget(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): XXX + def _mouseEventTsTarget(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown): + if isLeftDown: + self.toolState = self.TS_TARGET + self._dispatchSelectEvent(atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, self.targetRect) + elif isRightDown: + self._dispatchSelectEvent(atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, self.targetRect) + self.targetRect, self.toolState = None, self.TS_NONE + else: + self.toolState = self.TS_SELECT # }}} - - # - # onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): XXX - def onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): - pass # # onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): XXX def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): if self.toolState == self.TS_NONE: - if isLeftDown or isRightDown: - self.toolColours = [0, 1] - self.toolRect = [list(atPoint), []] - self.toolState = self.TS_ORIGIN - else: - dispatchFn(eventDc, True, \ - [*atPoint, *brushColours, 0, " "]) + self._mouseEventTsNone(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown) elif self.toolState == self.TS_ORIGIN: - self.toolRect[1] = list(atPoint) - if isLeftDown or isRightDown: - if self.toolRect[0][0] > self.toolRect[1][0]: - self.toolRect[0][0], self.toolRect[1][0] = \ - self.toolRect[1][0], self.toolRect[0][0] - if self.toolRect[0][1] > self.toolRect[1][1]: - self.toolRect[0][1], self.toolRect[1][1] = \ - self.toolRect[1][1], self.toolRect[0][1] - self.srcRect = self.toolRect[0] - self.toolLastAtPoint = list(atPoint) - self.toolState = self.TS_TARGET - self.toolSelectMap = [] - for numRow in range((self.toolRect[1][1] - self.toolRect[0][1]) + 1): - self.toolSelectMap.append([]) - for numCol in range((self.toolRect[1][0] - self.toolRect[0][0]) + 1): - rectY = self.toolRect[0][1] + numRow - rectX = self.toolRect[0][0] + numCol - self.toolSelectMap[numRow].append( \ - self.parentCanvas.canvasMap[rectY][rectX]) - self._drawSelectRect(self.toolRect, dispatchFn, eventDc) + self._mouseEventTsOrigin(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown) + elif self.toolState == self.TS_SELECT: + self._mouseEventTsSelect(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown) elif self.toolState == self.TS_TARGET: - if isRightDown: - self.onSelectEvent(event, atPoint, self.toolRect, \ - brushColours, brushSize, isLeftDown, isRightDown, \ - dispatchFn, eventDc) - self.toolColours = None - self.toolRect = None - self.toolState = self.TS_NONE - else: - self.onSelectEvent(event, atPoint, self.toolRect, \ - brushColours, brushSize, isLeftDown, isRightDown, \ - dispatchFn, eventDc) + self._mouseEventTsTarget(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown) + + # + # onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newTargetRect, selectRect): XXX + def onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newTargetRect, selectRect): + pass # __init__(self, *args): initialisation method def __init__(self, *args): super().__init__(*args) - self.toolColours = None - self.toolRect = None - self.toolState = self.TS_NONE - self.toolLastAtPoint = None - self.toolSelectMap = None + self.lastAtPoint, self.srcRect, self.targetRect, \ + self.toolSelectMap, self.toolState = \ + None, None, None, None, self.TS_NONE # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/libtools/MiRCARTToolSelectClone.py b/libtools/MiRCARTToolSelectClone.py index 08550b1..f82ce49 100644 --- a/libtools/MiRCARTToolSelectClone.py +++ b/libtools/MiRCARTToolSelectClone.py @@ -11,32 +11,12 @@ class MiRCARTToolSelectClone(MiRCARTToolSelect): name = "Clone selection" # - # onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): XXX - def onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): - if isLeftDown: - atPoint = list(atPoint) - disp = [atPoint[0]-self.toolLastAtPoint[0], \ - atPoint[1]-self.toolLastAtPoint[1]] - self.toolLastAtPoint = atPoint - newToolRect = [ \ - [selectRect[0][0]+disp[0], selectRect[0][1]+disp[1]], \ - [selectRect[1][0]+disp[0], selectRect[1][1]+disp[1]]] - isCursor = True - elif isRightDown: - disp = [0, 0] - newToolRect = selectRect.copy() - isCursor = False - else: - disp = [0, 0] - newToolRect = selectRect.copy() - isCursor = True + # onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newToolRect, selectRect): XXX + def onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newToolRect, selectRect): for numRow in range(len(self.toolSelectMap)): for numCol in range(len(self.toolSelectMap[numRow])): cellOld = self.toolSelectMap[numRow][numCol] - rectY = selectRect[0][1] + numRow - rectX = selectRect[0][0] + numCol - dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld]) - self._drawSelectRect(newToolRect, dispatchFn, eventDc) - self.toolRect = newToolRect + rectX, rectY = selectRect[0][0] + numCol, selectRect[0][1] + numRow + dispatchFn(eventDc, isCursor, [rectX + disp[0], rectY + disp[1], *cellOld]) # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 diff --git a/libtools/MiRCARTToolSelectMove.py b/libtools/MiRCARTToolSelectMove.py index ed0513b..34786a7 100644 --- a/libtools/MiRCARTToolSelectMove.py +++ b/libtools/MiRCARTToolSelectMove.py @@ -11,36 +11,15 @@ class MiRCARTToolSelectMove(MiRCARTToolSelect): name = "Move selection" # - # onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): XXX - def onSelectEvent(self, event, atPoint, selectRect, brushColours, brushSize, isLeftDown, isRightDown, dispatchFn, eventDc): - if isLeftDown: - atPoint = list(atPoint) - disp = [atPoint[0]-self.toolLastAtPoint[0], \ - atPoint[1]-self.toolLastAtPoint[1]] - self.toolLastAtPoint = atPoint - newToolRect = [ \ - [selectRect[0][0]+disp[0], selectRect[0][1]+disp[1]], \ - [selectRect[1][0]+disp[0], selectRect[1][1]+disp[1]]] - isCursor = True - elif isRightDown: - disp = [0, 0] - newToolRect = selectRect.copy() - isCursor = False - else: - disp = [0, 0] - newToolRect = selectRect.copy() - isCursor = True + # onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newToolRect, selectRect): XXX + def onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newToolRect, selectRect): for numRow in range(len(self.toolSelectMap)): for numCol in range(len(self.toolSelectMap[numRow])): - dispatchFn(eventDc, isCursor, [self.srcRect[0] + numCol, \ - self.srcRect[1] + numRow, 1, 1, 0, " "]) + dispatchFn(eventDc, isCursor, [self.srcRect[0] + numCol, self.srcRect[1] + numRow, 1, 1, 0, " "]) for numRow in range(len(self.toolSelectMap)): for numCol in range(len(self.toolSelectMap[numRow])): cellOld = self.toolSelectMap[numRow][numCol] - rectY = selectRect[0][1] + numRow - rectX = selectRect[0][0] + numCol - dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld]) - self._drawSelectRect(newToolRect, dispatchFn, eventDc) - self.toolRect = newToolRect + rectX, rectY = selectRect[0][0] + numCol, selectRect[0][1] + numRow + dispatchFn(eventDc, isCursor, [rectX + disp[0], rectY + disp[1], *cellOld]) # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120