roar/libcanvas/Canvas.py
Lucio Andrés Illanes Albornoz c8429b3db2 libgui/GuiCanvasInterface.py: merged from libcanvas/CanvasInterface.py.
libgui/GuiCanvasInterfaceAbout.py: split from libgui/GuiCanvasInterface.py.
libcanvas/Canvas.py, libgui/GuiFrame.py: updated.
assets/text/TODO: updated.
2019-09-04 11:47:09 +02:00

193 lines
8.8 KiB
Python

#!/usr/bin/env python3
#
# Canvas.py -- XXX
# Copyright (c) 2018 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
#
from CanvasBackend import CanvasBackend
from CanvasJournal import CanvasJournal
from CanvasExportStore import CanvasExportStore, haveToPngFile, haveUrllib
from CanvasImportStore import CanvasImportStore
from ImgurApiKey import ImgurApiKey
import wx
class Canvas(wx.Panel):
"""XXX"""
imgurApiKey = ImgurApiKey.imgurApiKey
# {{{ _commitPatch(self, patch): XXX
def _commitPatch(self, patch):
self.canvasMap[patch[1]][patch[0]] = patch[2:]
# }}}
# {{{ _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(undoLevel=self.canvasJournal.patchesUndoLevel)
# }}}
# {{{ _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[1]][patch[0]]
patchDelta = [*patch[0:2], *patchDeltaCell]
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)
# }}}
# {{{ onPanelClose(self, event): XXX
def onPanelClose(self, event):
self.Destroy()
# }}}
# {{{ onPanelEnterWindow(self, event): XXX
def onPanelEnterWindow(self, event):
self.parentFrame.SetFocus()
# }}}
# {{{ onPanelInput(self, event): XXX
def onPanelInput(self, event):
eventDc = self.canvasBackend.getDeviceContext(self)
eventType = event.GetEventType()
self._canvasDirty = self._canvasDirtyCursor = False
tool = self.canvasInterface.canvasTool
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)
if mapPoint[0] >= self.canvasSize[0] \
or mapPoint[1] >= self.canvasSize[1]:
return
self.brushPos = mapPoint
tool.onMouseEvent( \
event, mapPoint, self.brushColours, self.brushSize, \
event.Dragging(), event.LeftIsDown(), event.RightIsDown(), \
self._dispatchPatch, eventDc)
if self._canvasDirty:
self.parentFrame.onCanvasUpdate(cellPos=self.brushPos, \
undoLevel=self.canvasJournal.patchesUndoLevel)
if eventType == wx.wxEVT_MOTION:
self.parentFrame.onCanvasUpdate(cellPos=mapPoint)
# }}}
# {{{ onPanelLeaveWindow(self, event): XXX
def onPanelLeaveWindow(self, event):
eventDc = self.canvasBackend.getDeviceContext(self)
self.canvasBackend.drawCursorMaskWithJournal( \
self.canvasJournal, eventDc)
# }}}
# {{{ onPanelPaint(self, event): XXX
def onPanelPaint(self, event):
self.canvasBackend.onPanelPaintEvent(self, event)
# }}}
# {{{ onStoreUpdate(self, newCanvasSize, newCanvas=None): XXX
def onStoreUpdate(self, newCanvasSize, newCanvas=None):
self.resize(newCanvasSize=newCanvasSize)
eventDc = self.canvasBackend.getDeviceContext(self)
for numRow in range(self.canvasSize[1]):
for numCol in range(self.canvasSize[0]):
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, \
*self.canvasMap[numRow][numCol]])
wx.SafeYield()
# }}}
# {{{ resize(self, newCanvasSize): XXX
def resize(self, newCanvasSize):
if newCanvasSize != self.canvasSize:
if self.canvasMap == None:
self.canvasMap = []; oldCanvasSize = [0, 0];
else:
oldCanvasSize = self.canvasSize
deltaCanvasSize = [b - a for a, b in zip(oldCanvasSize, newCanvasSize)]
newWinSize = [a * b for a, b in zip(newCanvasSize, self.canvasBackend.cellSize)]
self.SetMinSize(newWinSize)
self.SetSize(wx.DefaultCoord, wx.DefaultCoord, *newWinSize)
curWindow = self
while curWindow != None:
curWindow.Layout()
curWindow = curWindow.GetParent()
self.canvasBackend.resize(newCanvasSize, self.canvasBackend.cellSize)
eventDc = self.canvasBackend.getDeviceContext(self)
self.canvasJournal.resetCursor(); self.canvasJournal.resetUndo();
if deltaCanvasSize[0] < 0:
for numRow in range(oldCanvasSize[1]):
del self.canvasMap[numRow][-1:(deltaCanvasSize[0]-1):-1]
else:
for numRow in range(oldCanvasSize[1]):
self.canvasMap[numRow].extend( \
[[1, 1, 0, " "]] * deltaCanvasSize[0])
for numNewCol in range(oldCanvasSize[0], newCanvasSize[0]):
self.canvasBackend.drawPatch( \
eventDc, [numNewCol, numRow, \
*self.canvasMap[numRow][-1]])
if deltaCanvasSize[1] < 0:
del self.canvasMap[-1:(deltaCanvasSize[1]-1):-1]
else:
for numNewRow in range(oldCanvasSize[1], newCanvasSize[1]):
self.canvasMap.extend( \
[[[1, 1, 0, " "]] * newCanvasSize[0]])
for numNewCol in range(newCanvasSize[0]):
self.canvasBackend.drawPatch( \
eventDc, [numNewCol, numNewRow, \
*self.canvasMap[-1][-1]])
self.canvasSize = newCanvasSize
wx.SafeYield()
self.parentFrame.onCanvasUpdate(size=newCanvasSize, undoLevel=-1)
# }}}
# {{{ __del__(self): destructor method
def __del__(self):
if self.canvasMap != None:
self.canvasMap.clear(); self.canvasMap = None;
# }}}
#
# __init__(self, parent, parentFrame, canvasInterface, defaultCanvasPos, defaultCanvasSize, defaultCellSize): initialisation method
def __init__(self, parent, parentFrame, canvasInterface, defaultCanvasPos, defaultCanvasSize, defaultCellSize):
super().__init__(parent, pos=defaultCanvasPos, size=[w * h for w, h in zip(defaultCanvasSize, defaultCellSize)])
self.brushColours, self.brushPos, self.brushSize = [4, 1], [0, 0], [1, 1]
self.canvasMap, self.canvasPos, self.canvasSize, = None, defaultCanvasPos, defaultCanvasSize
self.defaultCanvasPos, self.defaultCanvasSize, self.defaultCellSize = defaultCanvasPos, defaultCanvasSize, defaultCellSize
self.parentFrame = parentFrame
self.parentFrame.onCanvasUpdate(brushSize=self.brushSize, colours=self.brushColours)
self.canvasBackend = CanvasBackend(defaultCanvasSize, defaultCellSize)
self.canvasJournal = CanvasJournal()
self.canvasExportStore = CanvasExportStore(parentCanvas=self)
self.canvasImportStore = CanvasImportStore(parentCanvas=self)
self.canvasInterface = canvasInterface(self, parentFrame)
# Bind event handlers
self.Bind(wx.EVT_CLOSE, self.onPanelClose)
self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelEnterWindow)
self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelLeaveWindow)
self.parentFrame.Bind(wx.EVT_CHAR, self.onPanelInput)
for eventType in (wx.EVT_LEFT_DOWN, wx.EVT_MOTION, wx.EVT_RIGHT_DOWN):
self.Bind(eventType, self.onPanelInput)
self.Bind(wx.EVT_PAINT, self.onPanelPaint)
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120