mirror of
https://github.com/lalbornoz/roar.git
synced 2024-11-22 15:26:37 +00:00
MiRCART.py:MiRCARTCanvas._eventPointToMap[XY](): added for _onMouseEvent().
MiRCART.py:MiRCARTCanvas._onMouseEvent(): split into _processMapPatches() and _drawPatch(). MiRCART.py:MiRCARTCanvas.getCell{Height,Width}(): added for _drawPatch() and _eventPointToMap[XY](). MiRCART.py:MiRCARTCanvas.onPaletteEvent(): only set self.mirc[FB]g. MiRCART.py:MiRCARTCanvas.__init__(): cache canvas window size. MiRCART.py:MiRCARTCanvas.__init__(): initialise & append canvasTools. MiRCART.py:MiRCARTCanvas.__init__(): pre-create 16 mIRC colour brushes & pens. MiRCART.py:MiRCARTCanvas.__init__(): initialise temporary & {un,re}do patches. MiRCART.py:MiRCARTTool: added abstract base class for all canvas tools. MiRCART.py:MiRCARTToolRect: initial implementation w/ cursor, limited to 1x1 rectangles. MiRCART.py:MiRCARTFrame.__init__(): initialise MiRCARTCanvas() w/ canvasTools=[MiRCARTToolRect].
This commit is contained in:
parent
91e33f8a4a
commit
325a454db7
190
MiRCART.py
190
MiRCART.py
@ -48,47 +48,79 @@ mircColours = [
|
|||||||
|
|
||||||
class MiRCARTCanvas(wx.Panel):
|
class MiRCARTCanvas(wx.Panel):
|
||||||
"""XXX"""
|
"""XXX"""
|
||||||
canvasPos = canvasSize = None
|
canvasPos = canvasSize = canvasWinSize = cellPos = cellSize = None
|
||||||
canvasBitmap = canvasMap = None
|
canvasBitmap = canvasMap = canvasTools = None
|
||||||
cellPos = cellSize = None
|
mircBg = mircFg = mircBrushes = mircPens = None
|
||||||
brushBg = brushFg = penBg = penFg = None
|
patchesTmp = patchesUndo = None
|
||||||
mircBg = mircFg = None
|
|
||||||
|
|
||||||
|
# {{{ _drawPatch(): XXX
|
||||||
|
def _drawPatch(self, patch, eventDc, tmpDc, atX, atY):
|
||||||
|
patchXabs = (atX + patch[0]) * self.getCellWidth()
|
||||||
|
patchYabs = (atY + patch[1]) * self.getCellHeight()
|
||||||
|
brushFg = self.mircBrushes[patch[2]]
|
||||||
|
brushBg = self.mircBrushes[patch[3]]
|
||||||
|
pen = self.mircPens[patch[2]]
|
||||||
|
for dc in (eventDc, tmpDc):
|
||||||
|
dc.SetBrush(brushFg); dc.SetBackground(brushBg); dc.SetPen(pen);
|
||||||
|
dc.DrawRectangle(patchXabs, patchYabs, \
|
||||||
|
self.getCellWidth(), self.getCellHeight())
|
||||||
|
# }}}
|
||||||
|
# {{{ _eventPointToMapX(): XXX
|
||||||
|
def _eventPointToMapX(self, eventPoint):
|
||||||
|
rectX = eventPoint.x - (eventPoint.x % self.getCellWidth())
|
||||||
|
return int(rectX / self.getCellWidth() if rectX else 0)
|
||||||
|
# }}}
|
||||||
|
# {{{ _eventPointToMapY(): XXX
|
||||||
|
def _eventPointToMapY(self, eventPoint):
|
||||||
|
rectY = eventPoint.y - (eventPoint.y % self.getCellHeight())
|
||||||
|
return int(rectY / self.getCellHeight() if rectY else 0)
|
||||||
|
# }}}
|
||||||
# {{{ _onMouseEvent(): XXX
|
# {{{ _onMouseEvent(): XXX
|
||||||
def _onMouseEvent(self, event):
|
def _onMouseEvent(self, event):
|
||||||
eventObject = event.GetEventObject()
|
eventObject = event.GetEventObject()
|
||||||
if event.Dragging():
|
eventDc = wx.ClientDC(self); tmpDc = wx.MemoryDC();
|
||||||
eventDc = wx.ClientDC(self)
|
|
||||||
tmpDc = wx.MemoryDC()
|
|
||||||
tmpDc.SelectObject(self.canvasBitmap)
|
tmpDc.SelectObject(self.canvasBitmap)
|
||||||
eventPoint = event.GetLogicalPosition(eventDc)
|
eventPoint = event.GetLogicalPosition(eventDc)
|
||||||
rectX = eventPoint.x - (eventPoint.x % self.cellSize[0])
|
mapX = self._eventPointToMapX(eventPoint)
|
||||||
rectY = eventPoint.y - (eventPoint.y % self.cellSize[1])
|
mapY = self._eventPointToMapY(eventPoint)
|
||||||
mapX = int(rectX / 7 if rectX else 0)
|
for tool in self.canvasTools:
|
||||||
mapY = int(rectY / 14 if rectY else 0)
|
if event.Dragging():
|
||||||
eventDc.SetBackground(self.brushBg);
|
mapPatches = tool.onMouseMotion(event, mapX, mapY, event.LeftIsDown(), event.RightIsDown())
|
||||||
tmpDc.SetBackground(self.brushBg);
|
else:
|
||||||
if event.LeftIsDown():
|
mapPatches = tool.onMouseDown(event, mapX, mapY, event.LeftIsDown(), event.RightIsDown())
|
||||||
eventDc.SetBrush(self.brushFg);
|
self._processMapPatches(mapPatches, eventDc, tmpDc, mapX, mapY)
|
||||||
eventDc.SetPen(self.penFg)
|
# }}}
|
||||||
tmpDc.SetBrush(self.brushFg);
|
# {{{ _processMapPatches(): XXX
|
||||||
tmpDc.SetPen(self.penFg)
|
def _processMapPatches(self, mapPatches, eventDc, tmpDc, atX, atY):
|
||||||
self.canvasMap[mapY][mapX] = [self.mircFg, self.mircFg, " "]
|
for mapPatch in mapPatches:
|
||||||
elif event.RightIsDown():
|
mapPatchTmp = mapPatch[0]; mapPatchW = mapPatch[1]; mapPatchH = mapPatch[2];
|
||||||
eventDc.SetBrush(self.brushBg);
|
if mapPatchTmp and self.patchesTmp:
|
||||||
eventDc.SetPen(self.penBg)
|
for patch in self.patchesTmp:
|
||||||
tmpDc.SetBrush(self.brushBg);
|
self._drawPatch(patch, eventDc, tmpDc, 0, 0)
|
||||||
tmpDc.SetPen(self.penBg)
|
self.patchesTmp = []
|
||||||
self.canvasMap[mapY][mapX] = [self.mircBg, self.mircBg, " "]
|
for patch in mapPatch[3]:
|
||||||
eventDc.DrawRectangle(rectX, rectY, \
|
if mapPatchTmp:
|
||||||
self.cellSize[0], self.cellSize[1])
|
mapItem = self.canvasMap[atY + patch[1]][atX + patch[0]]
|
||||||
tmpDc.DrawRectangle(rectX, rectY, \
|
self.patchesTmp.append((atX + patch[0], \
|
||||||
self.cellSize[0], self.cellSize[1])
|
atY + patch[1], mapItem[0], mapItem[1], mapItem[2]))
|
||||||
|
self._drawPatch(patch, eventDc, tmpDc, atX, atY)
|
||||||
|
else:
|
||||||
|
self._drawPatch(patch, eventDc, tmpDc, atX, atY)
|
||||||
|
self.canvasMap[atY + patch[1]][atX + patch[0]] =\
|
||||||
|
[patch[2], patch[3], " "];
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ getBackgroundColour(): XXX
|
# {{{ getBackgroundColour(): XXX
|
||||||
def getBackgroundColour(self):
|
def getBackgroundColour(self):
|
||||||
return self.mircBg
|
return self.mircBg
|
||||||
# }}}
|
# }}}
|
||||||
|
# {{{ getCellHeight(): XXX
|
||||||
|
def getCellHeight(self):
|
||||||
|
return self.cellSize[1]
|
||||||
|
# }}}
|
||||||
|
# {{{ getCellWidth(): XXX
|
||||||
|
def getCellWidth(self):
|
||||||
|
return self.cellSize[0]
|
||||||
|
# }}}
|
||||||
# {{{ getForegroundColour(): XXX
|
# {{{ getForegroundColour(): XXX
|
||||||
def getForegroundColour(self):
|
def getForegroundColour(self):
|
||||||
return self.mircFg
|
return self.mircFg
|
||||||
@ -121,33 +153,36 @@ class MiRCARTCanvas(wx.Panel):
|
|||||||
def onPaletteEvent(self, leftDown, rightDown, numColour):
|
def onPaletteEvent(self, leftDown, rightDown, numColour):
|
||||||
if leftDown:
|
if leftDown:
|
||||||
self.mircFg = numColour
|
self.mircFg = numColour
|
||||||
self.brushFg = wx.Brush(wx.Colour(mircColours[self.mircFg]), wx.BRUSHSTYLE_SOLID)
|
|
||||||
self.penFg = wx.Pen(wx.Colour(mircColours[self.mircFg]), 1)
|
|
||||||
elif rightDown:
|
elif rightDown:
|
||||||
self.mircBg = numColour
|
self.mircBg = numColour
|
||||||
self.brushBg = wx.Brush(wx.Colour(mircColours[self.mircBg]), wx.BRUSHSTYLE_SOLID)
|
|
||||||
self.penBg = wx.Pen(wx.Colour(mircColours[self.mircBg]), 1)
|
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ onRightDown(): XXX
|
# {{{ onRightDown(): XXX
|
||||||
def onRightDown(self, event):
|
def onRightDown(self, event):
|
||||||
self._onMouseEvent(event)
|
self._onMouseEvent(event)
|
||||||
# }}}
|
# }}}
|
||||||
# {{{ Initialisation method
|
# {{{ Initialisation method
|
||||||
def __init__(self, parent, canvasPos, cellSize, canvasSize):
|
def __init__(self, parent, canvasPos, cellSize, canvasSize, canvasTools):
|
||||||
winSizeW = cellSize[0] * canvasSize[0]
|
canvasWinSize = (cellSize[0] * canvasSize[0], cellSize[1] * canvasSize[1])
|
||||||
winSizeH = cellSize[1] * canvasSize[1]
|
super().__init__(parent, pos=canvasPos, size=canvasWinSize)
|
||||||
super().__init__(parent, pos=canvasPos, size=(winSizeW, winSizeH))
|
self.canvasPos = canvasPos; self.canvasSize = canvasSize; self.canvasWinSize = canvasWinSize;
|
||||||
|
|
||||||
self.canvasPos = canvasPos; self.canvasSize = canvasSize;
|
|
||||||
self.canvasBitmap = wx.Bitmap(winSizeW, winSizeH)
|
|
||||||
self.canvasMap = [[[1, 1, " "] for x in range(canvasSize[0])] for y in range(canvasSize[1])]
|
|
||||||
self.cellPos = (0, 0); self.cellSize = cellSize;
|
self.cellPos = (0, 0); self.cellSize = cellSize;
|
||||||
self.brushBg = wx.Brush(wx.Colour(mircColours[1]), wx.BRUSHSTYLE_SOLID)
|
|
||||||
self.brushFg = wx.Brush(wx.Colour(mircColours[4]), wx.BRUSHSTYLE_SOLID)
|
self.canvasBitmap = wx.Bitmap(canvasWinSize)
|
||||||
self.penBg = wx.Pen(wx.Colour(mircColours[1]), 1)
|
self.canvasMap = [[[1, 1, " "] for x in range(canvasSize[0])] for y in range(canvasSize[1])]
|
||||||
self.penFg = wx.Pen(wx.Colour(mircColours[4]), 1)
|
self.canvasTools = []
|
||||||
|
for canvasTool in canvasTools:
|
||||||
|
self.canvasTools.append(canvasTool(self))
|
||||||
|
|
||||||
self.mircBg = 1; self.mircFg = 4;
|
self.mircBg = 1; self.mircFg = 4;
|
||||||
self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
|
self.mircBrushes = [None for x in range(len(mircColours))]
|
||||||
|
self.mircPens = [None for x in range(len(mircColours))]
|
||||||
|
for mircColour in range(0, len(mircColours)):
|
||||||
|
self.mircBrushes[mircColour] = wx.Brush( \
|
||||||
|
wx.Colour(mircColours[mircColour]), wx.BRUSHSTYLE_SOLID)
|
||||||
|
self.mircPens[mircColour] = wx.Pen( \
|
||||||
|
wx.Colour(mircColours[mircColour]), 1)
|
||||||
|
|
||||||
|
self.patchesTmp = []; self.patchesUndo = [];
|
||||||
|
|
||||||
self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
|
self.Bind(wx.EVT_LEFT_DOWN, self.onLeftDown)
|
||||||
self.Bind(wx.EVT_MOTION, self.onMotion)
|
self.Bind(wx.EVT_MOTION, self.onMotion)
|
||||||
@ -155,6 +190,64 @@ class MiRCARTCanvas(wx.Panel):
|
|||||||
self.Bind(wx.EVT_RIGHT_DOWN, self.onRightDown)
|
self.Bind(wx.EVT_RIGHT_DOWN, self.onRightDown)
|
||||||
# }}}
|
# }}}
|
||||||
|
|
||||||
|
class MiRCARTTool():
|
||||||
|
"""XXX"""
|
||||||
|
parentCanvas = None
|
||||||
|
|
||||||
|
# {{{ onMouseDown(): XXX
|
||||||
|
def onMouseDown(self, event, mapX, mapY, isLeftDown, isRightDown):
|
||||||
|
pass
|
||||||
|
# }}}
|
||||||
|
# {{{ onMouseMotion(): XXX
|
||||||
|
def onMouseMotion(self, event, mapX, mapY, isLeftDown, isRightDown):
|
||||||
|
pass
|
||||||
|
# }}}
|
||||||
|
# {{{ Initialisation method
|
||||||
|
def __init__(self, parentCanvas):
|
||||||
|
self.parentCanvas = parentCanvas
|
||||||
|
# }}}
|
||||||
|
|
||||||
|
class MiRCARTToolRect(MiRCARTTool):
|
||||||
|
"""XXX"""
|
||||||
|
|
||||||
|
# {{{ _draw(): XXX
|
||||||
|
def _draw(self, event, mapX, mapY, isLeftDown, isRightDown):
|
||||||
|
if isLeftDown:
|
||||||
|
return [[False, 1, 1, [[0, 0, \
|
||||||
|
self.parentCanvas.getForegroundColour(), \
|
||||||
|
self.parentCanvas.getForegroundColour(), " "]]],
|
||||||
|
[True, 1, 1, [[0, 0, \
|
||||||
|
self.parentCanvas.getForegroundColour(), \
|
||||||
|
self.parentCanvas.getForegroundColour(), " "]]]]
|
||||||
|
elif isRightDown:
|
||||||
|
return [[False, 1, 1, [[0, 0, \
|
||||||
|
self.parentCanvas.getBackgroundColour(), \
|
||||||
|
self.parentCanvas.getBackgroundColour(), " "]]], \
|
||||||
|
[True, 1, 1, [[0, 0, \
|
||||||
|
self.parentCanvas.getForegroundColour(), \
|
||||||
|
self.parentCanvas.getForegroundColour(), " "]]]]
|
||||||
|
else:
|
||||||
|
return [[True, 1, 1, [[0, 0, \
|
||||||
|
self.parentCanvas.getForegroundColour(), \
|
||||||
|
self.parentCanvas.getForegroundColour(), " "]]]]
|
||||||
|
# }}}
|
||||||
|
# {{{ onMouseDown(): XXX
|
||||||
|
def onMouseDown(self, event, mapX, mapY, isLeftDown, isRightDown):
|
||||||
|
print("mouse down")
|
||||||
|
return self._draw(event, mapX, mapY, isLeftDown, isRightDown)
|
||||||
|
# }}}
|
||||||
|
# {{{ onMouseMotion(): XXX
|
||||||
|
def onMouseMotion(self, event, mapX, mapY, isLeftDown, isRightDown):
|
||||||
|
print("mouse moving")
|
||||||
|
if isLeftDown or isRightDown:
|
||||||
|
print("mouse depressed")
|
||||||
|
return self._draw(event, mapX, mapY, isLeftDown, isRightDown)
|
||||||
|
# }}}
|
||||||
|
# {{{ Initialisation method
|
||||||
|
def __init__(self, parentCanvas):
|
||||||
|
super().__init__(parentCanvas)
|
||||||
|
# }}}
|
||||||
|
|
||||||
class MiRCARTPalette(wx.Panel):
|
class MiRCARTPalette(wx.Panel):
|
||||||
"""XXX"""
|
"""XXX"""
|
||||||
panelsByColour = onPaletteEvent = None
|
panelsByColour = onPaletteEvent = None
|
||||||
@ -250,7 +343,8 @@ class MiRCARTFrame(wx.Frame):
|
|||||||
|
|
||||||
self.panelSkin = wx.Panel(self, wx.ID_ANY)
|
self.panelSkin = wx.Panel(self, wx.ID_ANY)
|
||||||
self.panelCanvas = MiRCARTCanvas(self.panelSkin, \
|
self.panelCanvas = MiRCARTCanvas(self.panelSkin, \
|
||||||
canvasPos=canvasPos, cellSize=cellSize, canvasSize=canvasSize)
|
canvasPos=canvasPos, cellSize=cellSize, \
|
||||||
|
canvasSize=canvasSize, canvasTools=[MiRCARTToolRect])
|
||||||
self.panelPalette = MiRCARTPalette(self.panelSkin, \
|
self.panelPalette = MiRCARTPalette(self.panelSkin, \
|
||||||
(25, (canvasSize[1] + 3) * cellSize[1]), cellSize, self.onPaletteEvent)
|
(25, (canvasSize[1] + 3) * cellSize[1]), cellSize, self.onPaletteEvent)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user