roar/roar-python/libtools/ToolRect.py
2020-01-14 21:21:49 +01:00

48 lines
2.2 KiB
Python

#!/usr/bin/env python3
#
# ToolRect.py
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
#
from Tool import Tool
import wx
class ToolRect(Tool):
name = "Rectangle"
TS_NONE = 0
TS_ORIGIN = 1
def _drawRect(self, brushColours, canvas, rect):
patches = []
for brushRow in range((rect[3] - rect[1]) + 1):
for brushCol in range((rect[2] - rect[0]) + 1):
if (brushCol in [0, (rect[2] - rect[0])]) or (brushRow in [0, (rect[3] - rect[1])]):
patchColours = [brushColours[0]] * 2
else:
patchColours = [brushColours[1]] * 2
patches += [[rect[0] + brushCol, rect[1] + brushRow, *patchColours, 0, " "]]
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)] if brushSize[0] > 1 else mapPoint)]
elif self.toolState == self.TS_ORIGIN:
rect = [*self.originPoint, *mapPoint]
if not (mouseLeftDown or mouseRightDown):
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