2019-09-01 14:34:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-08 16:08:04 +00:00
|
|
|
# ToolSelect.py
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2019-09-01 14:34:00 +00:00
|
|
|
#
|
|
|
|
|
2019-09-03 16:58:50 +00:00
|
|
|
from Tool import Tool
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-03 16:58:50 +00:00
|
|
|
class ToolSelect(Tool):
|
2019-09-01 14:34:00 +00:00
|
|
|
TS_NONE = 0
|
|
|
|
TS_ORIGIN = 1
|
2019-09-03 13:48:48 +00:00
|
|
|
TS_SELECT = 2
|
|
|
|
TS_TARGET = 3
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _dispatchSelectEvent(self, atPoint, dispatchFn, eventDc, isLeftDown, isRightDown, selectRect)
|
2019-09-03 13:48:48 +00:00
|
|
|
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
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _drawSelectRect(self, rect, dispatchFn, eventDc)
|
2019-09-01 14:34:00 +00:00
|
|
|
def _drawSelectRect(self, rect, dispatchFn, eventDc):
|
2019-09-03 13:48:48 +00:00
|
|
|
rectFrame = [[rect[m[0]][n] + m[1] for n in [0, 1]] for m in [[0, -1], [1, +1]]]
|
2019-09-01 14:34:00 +00:00
|
|
|
if rectFrame[0][0] > rectFrame[1][0]:
|
2019-09-03 13:48:48 +00:00
|
|
|
rectFrame[0][0], rectFrame[1][0] = rectFrame[1][0], rectFrame[0][0]
|
2019-09-01 14:34:00 +00:00
|
|
|
if rectFrame[0][1] > rectFrame[1][1]:
|
2019-09-03 13:48:48 +00:00
|
|
|
rectFrame[0][1], rectFrame[1][1] = rectFrame[1][1], rectFrame[0][1]
|
2019-09-01 14:34:00 +00:00
|
|
|
curColours = [0, 0]
|
2019-09-03 16:58:50 +00:00
|
|
|
for rectX in range(rectFrame[0][0], rectFrame[1][0] + 1):
|
2019-09-03 13:48:48 +00:00
|
|
|
curColours = [1, 1] if curColours == [0, 0] else [0, 0]
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
dispatchFn(eventDc, True, [rectX, rectFrame[0][1], *curColours, 0, " "], viewRect)
|
|
|
|
dispatchFn(eventDc, True, [rectX, rectFrame[1][1], *curColours, 0, " "], viewRect)
|
2019-09-03 16:58:50 +00:00
|
|
|
for rectY in range(rectFrame[0][1], rectFrame[1][1] + 1):
|
2019-09-03 13:48:48 +00:00
|
|
|
curColours = [1, 1] if curColours == [0, 0] else [0, 0]
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
dispatchFn(eventDc, True, [rectFrame[0][0], rectY, *curColours, 0, " "], viewRect)
|
|
|
|
dispatchFn(eventDc, True, [rectFrame[1][0], rectY, *curColours, 0, " "], viewRect)
|
2019-09-03 13:48:48 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _mouseEventTsNone(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-03 13:48:48 +00:00
|
|
|
def _mouseEventTsNone(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown):
|
|
|
|
if isLeftDown:
|
|
|
|
self.targetRect, self.toolState = [list(atPoint), []], self.TS_ORIGIN
|
|
|
|
else:
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
dispatchFn(eventDc, True, [*atPoint, *brushColours, 0, " "], viewRect)
|
2019-09-03 13:48:48 +00:00
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _mouseEventTsOrigin(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-03 13:48:48 +00:00
|
|
|
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
|
2019-09-07 08:39:26 +00:00
|
|
|
self.toolSelectMap[numRow].append(self.parentCanvas.canvas.map[rectY][rectX])
|
2019-09-03 13:48:48 +00:00
|
|
|
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)
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _mouseEventTsSelect(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-03 13:48:48 +00:00
|
|
|
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)
|
|
|
|
# }}}
|
2019-09-08 16:08:04 +00:00
|
|
|
# {{{ _mouseEventTsTarget(self, atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-03 13:48:48 +00:00
|
|
|
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
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
|
|
|
|
|
|
|
#
|
Initial canvas panel scrollbar implementation.
assets/text/TODO: updated.
libgui/GuiCanvasPanel.py:__init__(): provide self.winSize & call SetScrollRate().
libgui/GuiCanvasPanel.py:{_drawPatch,dispatch{DeltaPatches,Patch},onPanel{Input,LeaveWindow},resize,update}(): receive and/or pass viewRect.
libgui/GuiCanvasPanel.py:onPanelPaint(): updated.
libgui/GuiCanvasPanel.py:resize(): call SetVirtualSize().
libgui/GuiCanvasWxBackend.py:_{draw{Brush,Char}Patch,_get{Brush,Char}PatchColours,xlatePoint}(): updated.
libgui/GuiCanvasWxBackend.py:{draw{CursorMaskWithJournal,Patch},getDeviceContext,xlateEventPoint}(): receive and/or pass viewRect.
libgui/GuiCanvasWxBackend.py:getDeviceContext(): return ClientDC() if viewRect > (0, 0)
libgui/GuiCanvasWxBackend.py:onPanelPaintEvent(): blit subset of canvasBitmap into BufferedPaintDC() if viewRect > (0, 0).
libtools/Tool{,Circle,Fill,Line,Rect,Select{,Clone,Move},Text}.py: receive & pass viewRect to dispatchFn().
2019-09-09 10:30:25 +00:00
|
|
|
# onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc, viewRect)
|
|
|
|
def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc, viewRect):
|
2019-09-01 14:34:00 +00:00
|
|
|
if self.toolState == self.TS_NONE:
|
2019-09-03 13:48:48 +00:00
|
|
|
self._mouseEventTsNone(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-01 14:34:00 +00:00
|
|
|
elif self.toolState == self.TS_ORIGIN:
|
2019-09-03 13:48:48 +00:00
|
|
|
self._mouseEventTsOrigin(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
|
|
|
elif self.toolState == self.TS_SELECT:
|
|
|
|
self._mouseEventTsSelect(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
2019-09-01 14:34:00 +00:00
|
|
|
elif self.toolState == self.TS_TARGET:
|
2019-09-03 13:48:48 +00:00
|
|
|
self._mouseEventTsTarget(atPoint, brushColours, dispatchFn, eventDc, isDragging, isLeftDown, isRightDown)
|
|
|
|
|
|
|
|
#
|
2019-09-08 16:08:04 +00:00
|
|
|
# onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newTargetRect, selectRect)
|
2019-09-03 13:48:48 +00:00
|
|
|
def onSelectEvent(self, disp, dispatchFn, eventDc, isCursor, newTargetRect, selectRect):
|
|
|
|
pass
|
2019-09-01 14:34:00 +00:00
|
|
|
|
|
|
|
# __init__(self, *args): initialisation method
|
|
|
|
def __init__(self, *args):
|
|
|
|
super().__init__(*args)
|
2019-09-03 13:48:48 +00:00
|
|
|
self.lastAtPoint, self.srcRect, self.targetRect, \
|
|
|
|
self.toolSelectMap, self.toolState = \
|
|
|
|
None, None, None, None, self.TS_NONE
|
2019-09-01 14:34:00 +00:00
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|