2018-01-07 01:08:35 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# MiRCARTCanvas.py -- XXX
|
|
|
|
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
#
|
|
|
|
|
2018-01-08 23:21:43 +00:00
|
|
|
from MiRCARTCanvasBackend import MiRCARTCanvasBackend
|
2018-01-07 01:08:35 +00:00
|
|
|
from MiRCARTCanvasJournal import MiRCARTCanvasJournal
|
2018-01-07 15:43:41 +00:00
|
|
|
from MiRCARTCanvasStore import MiRCARTCanvasStore, haveMiRCARTToPngFile, haveUrllib
|
2018-01-07 01:08:35 +00:00
|
|
|
from MiRCARTColours import MiRCARTColours
|
|
|
|
import wx
|
|
|
|
|
|
|
|
class MiRCARTCanvas(wx.Panel):
|
|
|
|
"""XXX"""
|
|
|
|
parentFrame = None
|
2018-01-08 23:21:43 +00:00
|
|
|
canvasMap = canvasPos = canvasSize = None
|
2018-01-07 15:43:41 +00:00
|
|
|
brushColours = brushPos = brushSize = None
|
2018-01-08 23:21:43 +00:00
|
|
|
canvasBackend = canvasCurTool = canvasJournal = canvasStore = None
|
2018-01-07 01:08:35 +00:00
|
|
|
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ _commitPatch(self, patch): XXX
|
2018-01-08 23:21:43 +00:00
|
|
|
def _commitPatch(self, patch):
|
|
|
|
self.canvasMap[patch[0][1]][patch[0][0]] = patch[1:]
|
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ _dispatchDeltaPatches(self, deltaPatches): XXX
|
|
|
|
def _dispatchDeltaPatches(self, deltaPatches):
|
|
|
|
eventDc = self.canvasBackend.getDeviceContext(self)
|
|
|
|
for patch in deltaPatches:
|
|
|
|
if self.canvasBackend.drawPatch(eventDc, patch):
|
|
|
|
self._commitPatch(patch)
|
|
|
|
self.parentFrame.onCanvasUpdate()
|
|
|
|
# }}}
|
|
|
|
# {{{ _dispatchPatch(self, eventDc, isCursor, patch): XXX
|
|
|
|
def _dispatchPatch(self, eventDc, isCursor, patch):
|
|
|
|
if not self._canvasDirtyCursor:
|
|
|
|
self.canvasBackend.drawCursorMaskWithJournal( \
|
|
|
|
self.canvasJournal, eventDc)
|
|
|
|
self._canvasDirtyCursor = True
|
|
|
|
if self.canvasBackend.drawPatch(eventDc, patch):
|
|
|
|
patchDeltaCell = self.canvasMap[patch[0][1]][patch[0][0]]
|
|
|
|
patchDelta = [list(patch[0]), *patchDeltaCell.copy()]
|
|
|
|
if isCursor:
|
|
|
|
self.canvasJournal.pushCursor(patchDelta)
|
|
|
|
else:
|
|
|
|
if not self._canvasDirty:
|
|
|
|
self.canvasJournal.pushDeltas([], [])
|
|
|
|
self._canvasDirty = True
|
|
|
|
self.canvasJournal.updateCurrentDeltas(patchDelta, patch)
|
|
|
|
self._commitPatch(patch)
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
2018-01-07 03:19:38 +00:00
|
|
|
|
2018-01-08 23:21:43 +00:00
|
|
|
# {{{ onPanelClose(self, event): XXX
|
|
|
|
def onPanelClose(self, event):
|
|
|
|
self.Destroy()
|
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ onPanelInput(self, event): XXX
|
|
|
|
def onPanelInput(self, event):
|
2018-01-09 21:23:27 +00:00
|
|
|
eventDc = self.canvasBackend.getDeviceContext(self)
|
2018-01-09 23:25:42 +00:00
|
|
|
eventType = event.GetEventType()
|
|
|
|
self._canvasDirty = self._canvasDirtyCursor = False
|
2018-01-08 15:12:11 +00:00
|
|
|
tool = self.canvasCurTool
|
2018-01-09 23:25:42 +00:00
|
|
|
if eventType == wx.wxEVT_CHAR:
|
|
|
|
mapPoint = self.brushPos
|
|
|
|
doSkip = tool.onKeyboardEvent( \
|
|
|
|
event, mapPoint, self.brushColours, self.brushSize, \
|
|
|
|
chr(event.GetUnicodeKey()), self._dispatchPatch, eventDc)
|
|
|
|
if doSkip:
|
|
|
|
event.Skip(); return;
|
|
|
|
else:
|
|
|
|
mapPoint = self.canvasBackend.xlateEventPoint(event, eventDc)
|
|
|
|
self.brushPos = mapPoint
|
|
|
|
tool.onMouseEvent( \
|
|
|
|
event, mapPoint, self.brushColours, self.brushSize, \
|
|
|
|
event.Dragging(), event.LeftIsDown(), event.RightIsDown(), \
|
|
|
|
self._dispatchPatch, eventDc)
|
|
|
|
if self._canvasDirty:
|
2018-01-08 23:21:43 +00:00
|
|
|
self.parentFrame.onCanvasUpdate()
|
2018-01-07 15:43:41 +00:00
|
|
|
self.parentFrame.onCanvasMotion(event, mapPoint)
|
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ onPanelLeaveWindow(self, event): XXX
|
|
|
|
def onPanelLeaveWindow(self, event):
|
|
|
|
eventDc = self.canvasBackend.getDeviceContext(self)
|
|
|
|
self.canvasBackend.drawCursorMaskWithJournal( \
|
|
|
|
self.canvasJournal, eventDc)
|
2018-01-07 15:43:41 +00:00
|
|
|
self.parentFrame.onCanvasMotion(event)
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
2018-01-08 23:21:43 +00:00
|
|
|
# {{{ onPanelPaint(self, event): XXX
|
|
|
|
def onPanelPaint(self, event):
|
2018-01-09 21:23:27 +00:00
|
|
|
self.canvasBackend.onPanelPaintEvent(self, event)
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ onStoreUpdate(self, newCanvasSize, newCanvas=None): XXX
|
2018-01-07 15:43:41 +00:00
|
|
|
def onStoreUpdate(self, newCanvasSize, newCanvas=None):
|
2018-01-08 23:21:43 +00:00
|
|
|
self.resize(newCanvasSize=newCanvasSize)
|
2018-01-09 21:23:27 +00:00
|
|
|
eventDc = self.canvasBackend.getDeviceContext(self)
|
2018-01-07 15:43:41 +00:00
|
|
|
for numRow in range(self.canvasSize[1]):
|
|
|
|
for numCol in range(self.canvasSize[0]):
|
2018-01-08 23:21:43 +00:00
|
|
|
if newCanvas != None \
|
|
|
|
and numRow < len(newCanvas) \
|
|
|
|
and numCol < len(newCanvas[numRow]):
|
|
|
|
self._commitPatch([ \
|
|
|
|
[numCol, numRow], *newCanvas[numRow][numCol]])
|
|
|
|
self.canvasBackend.drawPatch(eventDc, \
|
|
|
|
([numCol, numRow], \
|
2018-01-09 21:23:27 +00:00
|
|
|
*self.canvasMap[numRow][numCol]))
|
2018-01-07 15:43:41 +00:00
|
|
|
wx.SafeYield()
|
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ popRedo(self): XXX
|
2018-01-08 23:21:43 +00:00
|
|
|
def popRedo(self):
|
2018-01-09 23:25:42 +00:00
|
|
|
self._dispatchDeltaPatches(self.canvasJournal.popRedo())
|
2018-01-08 23:21:43 +00:00
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ popUndo(self): XXX
|
2018-01-08 23:21:43 +00:00
|
|
|
def popUndo(self):
|
2018-01-09 23:25:42 +00:00
|
|
|
self._dispatchDeltaPatches(self.canvasJournal.popUndo())
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
2018-01-09 23:25:42 +00:00
|
|
|
# {{{ resize(self, newCanvasSize): XXX
|
2018-01-07 03:19:38 +00:00
|
|
|
def resize(self, newCanvasSize):
|
|
|
|
if newCanvasSize != self.canvasSize:
|
2018-01-09 23:25:42 +00:00
|
|
|
if self.canvasMap == None:
|
|
|
|
self.canvasMap = [[[(1, 1), 0, " "] \
|
|
|
|
for x in range(self.canvasSize[0])] \
|
|
|
|
for y in range(self.canvasSize[1])]
|
|
|
|
else:
|
|
|
|
for numRow in range(self.canvasSize[1]):
|
|
|
|
for numNewCol in range(self.canvasSize[0], newCanvasSize[0]):
|
|
|
|
self.canvasMap[numRow].append([[1, 1], 0, " "])
|
|
|
|
for numNewRow in range(self.canvasSize[1], newCanvasSize[1]):
|
|
|
|
self.canvasMap.append([])
|
|
|
|
for numNewCol in range(newCanvasSize[0]):
|
|
|
|
self.canvasMap[numNewRow].append([[1, 1], 0, " "])
|
2018-01-07 03:19:38 +00:00
|
|
|
self.canvasSize = newCanvasSize
|
2018-01-09 23:25:42 +00:00
|
|
|
self.SetSize(*self.canvasPos, \
|
|
|
|
*[a*b for a,b in zip(self.canvasSize, \
|
|
|
|
self.canvasBackend.cellSize)])
|
|
|
|
self.canvasBackend.reset(self.canvasSize, self.canvasBackend.cellSize)
|
|
|
|
self.canvasJournal.resetCursor(); self.canvasJournal.resetUndo();
|
|
|
|
self.parentFrame.onCanvasUpdate()
|
2018-01-07 01:08:35 +00:00
|
|
|
# }}}
|
|
|
|
|
|
|
|
#
|
2018-01-08 15:12:11 +00:00
|
|
|
# _init__(self, parent, parentFrame, canvasPos, canvasSize, cellSize): initialisation method
|
|
|
|
def __init__(self, parent, parentFrame, canvasPos, canvasSize, cellSize):
|
2018-01-08 23:21:43 +00:00
|
|
|
super().__init__(parent, pos=canvasPos, \
|
|
|
|
size=[w*h for w,h in zip(canvasSize, cellSize)])
|
2018-01-07 01:08:35 +00:00
|
|
|
|
2018-01-08 23:21:43 +00:00
|
|
|
self.parentFrame = parentFrame
|
|
|
|
self.canvasMap = None; self.canvasPos = canvasPos; self.canvasSize = canvasSize;
|
|
|
|
self.brushColours = [4, 1]; self.brushPos = [0, 0]; self.brushSize = [1, 1];
|
|
|
|
self.canvasBackend = MiRCARTCanvasBackend(canvasSize, cellSize)
|
2018-01-08 15:12:11 +00:00
|
|
|
self.canvasCurTool = None
|
2018-01-08 23:21:43 +00:00
|
|
|
self.canvasJournal = MiRCARTCanvasJournal()
|
|
|
|
self.canvasStore = MiRCARTCanvasStore(parentCanvas=self)
|
2018-01-07 01:08:35 +00:00
|
|
|
|
2018-01-08 23:21:43 +00:00
|
|
|
# Bind event handlers
|
|
|
|
self.Bind(wx.EVT_CLOSE, self.onPanelClose)
|
2018-01-09 23:25:42 +00:00
|
|
|
self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelLeaveWindow)
|
|
|
|
self.parentFrame.Bind(wx.EVT_CHAR, self.onPanelInput)
|
2018-01-08 23:21:43 +00:00
|
|
|
for eventType in( \
|
|
|
|
wx.EVT_LEFT_DOWN, wx.EVT_MOTION, wx.EVT_RIGHT_DOWN):
|
2018-01-09 23:25:42 +00:00
|
|
|
self.Bind(eventType, self.onPanelInput)
|
2018-01-08 23:21:43 +00:00
|
|
|
self.Bind(wx.EVT_PAINT, self.onPanelPaint)
|
2018-01-07 01:08:35 +00:00
|
|
|
|
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120
|