MiRCART{Canvas{,Backend},Frame}.py: replace MemoryDC() w/ BufferedDC(canvasBitmap).

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-09 22:23:27 +01:00
parent 2e014cd7ec
commit ed8ccab26f
3 changed files with 55 additions and 58 deletions

View File

@ -39,15 +39,15 @@ class MiRCARTCanvas(wx.Panel):
def _commitPatch(self, patch):
self.canvasMap[patch[0][1]][patch[0][0]] = patch[1:]
# }}}
# {{{ _dispatchInput(self, eventDc, patches, tmpDc): XXX
def _dispatchInput(self, eventDc, patches, tmpDc):
# {{{ _dispatchInput(self, eventDc, patches): XXX
def _dispatchInput(self, eventDc, patches):
self.canvasBackend.drawCursorMaskWithJournal( \
self.canvasJournal, eventDc, tmpDc)
self.canvasJournal, eventDc)
cursorPatches = []; undoPatches = []; newPatches = [];
for patchDescr in patches:
patchIsCursor = patchDescr[0]
for patch in patchDescr[1]:
if self.canvasBackend.drawPatch(eventDc, patch, tmpDc) == False:
if self.canvasBackend.drawPatch(eventDc, patch) == False:
continue
else:
patchDeltaCell = self.canvasMap[patch[0][1]][patch[0][0]]
@ -69,7 +69,7 @@ class MiRCARTCanvas(wx.Panel):
# }}}
# {{{ onPanelKeyboardInput(self, event): XXX
def onPanelKeyboardInput(self, event):
eventDc, tmpDc = self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
tool = self.canvasCurTool; mapPoint = self.brushPos;
keyModifiers = event.GetModifiers()
if keyModifiers != wx.MOD_NONE \
@ -80,12 +80,12 @@ class MiRCARTCanvas(wx.Panel):
event, mapPoint, self.brushColours, self.brushSize, \
chr(event.GetUnicodeKey()))
if len(patches):
self._dispatchInput(eventDc, patches, tmpDc)
self._dispatchInput(eventDc, patches)
self.parentFrame.onCanvasUpdate()
# }}}
# {{{ onPanelMouseInput(self, event): XXX
def onPanelMouseInput(self, event):
eventDc, tmpDc = self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
tool = self.canvasCurTool
self.brushPos = mapPoint = \
self.canvasBackend.xlateEventPoint(event, eventDc)
@ -93,23 +93,21 @@ class MiRCARTCanvas(wx.Panel):
event, mapPoint, self.brushColours, self.brushSize, \
event.Dragging(), event.LeftIsDown(), event.RightIsDown())
if len(patches):
self._dispatchInput(eventDc, patches, tmpDc)
self._dispatchInput(eventDc, patches)
self.parentFrame.onCanvasUpdate()
self.parentFrame.onCanvasMotion(event, mapPoint)
# }}}
# {{{ onPanelFocus(self, event): XXX
def onPanelFocus(self, event):
if event.GetEventType() == wx.wxEVT_LEAVE_WINDOW:
eventDc, tmpDc = \
self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
self.canvasBackend.drawCursorMaskWithJournal( \
self.canvasJournal, eventDc, tmpDc)
self.canvasJournal, eventDc)
self.parentFrame.onCanvasMotion(event)
# }}}
# {{{ onPanelPaint(self, event): XXX
def onPanelPaint(self, event):
if self.canvasBackend.canvasBitmap != None:
eventDc = wx.BufferedPaintDC(self, self.canvasBackend.canvasBitmap)
self.canvasBackend.onPanelPaintEvent(self, event)
# }}}
# {{{ onStoreUpdate(self, newCanvasSize, newCanvas=None):
def onStoreUpdate(self, newCanvasSize, newCanvas=None):
@ -119,7 +117,7 @@ class MiRCARTCanvas(wx.Panel):
self.canvasMap = [[[(1, 1), 0, " "] \
for x in range(self.canvasSize[0])] \
for y in range(self.canvasSize[1])]
eventDc, tmpDc = self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
for numRow in range(self.canvasSize[1]):
for numCol in range(self.canvasSize[0]):
if newCanvas != None \
@ -129,15 +127,15 @@ class MiRCARTCanvas(wx.Panel):
[numCol, numRow], *newCanvas[numRow][numCol]])
self.canvasBackend.drawPatch(eventDc, \
([numCol, numRow], \
*self.canvasMap[numRow][numCol]), tmpDc)
*self.canvasMap[numRow][numCol]))
wx.SafeYield()
# }}}
# {{{ popRedo(self):
def popRedo(self):
eventDc, tmpDc = self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
patches = self.canvasJournal.popRedo()
for patch in patches:
if self.canvasBackend.drawPatch(eventDc, patch, tmpDc) == False:
if self.canvasBackend.drawPatch(eventDc, patch) == False:
continue
else:
self._commitPatch(patch)
@ -145,10 +143,10 @@ class MiRCARTCanvas(wx.Panel):
# }}}
# {{{ popUndo(self):
def popUndo(self):
eventDc, tmpDc = self.canvasBackend.getDeviceContexts(self)
eventDc = self.canvasBackend.getDeviceContext(self)
patches = self.canvasJournal.popUndo()
for patch in patches:
if self.canvasBackend.drawPatch(eventDc, patch, tmpDc) == False:
if self.canvasBackend.drawPatch(eventDc, patch) == False:
continue
else:
self._commitPatch(patch)
@ -178,7 +176,6 @@ class MiRCARTCanvas(wx.Panel):
def __init__(self, parent, parentFrame, canvasPos, canvasSize, cellSize):
super().__init__(parent, pos=canvasPos, \
size=[w*h for w,h in zip(canvasSize, cellSize)])
self.SetDoubleBuffered(True)
self.parentFrame = parentFrame
self.canvasMap = None; self.canvasPos = canvasPos; self.canvasSize = canvasSize;

View File

@ -31,32 +31,30 @@ class MiRCARTCanvasBackend():
_lastBrush = _lastPen = None
canvasBitmap = cellSize = None
# {{{ _drawBrushPatch(self, eventDc, patch, tmpDc): XXX
def _drawBrushPatch(self, eventDc, patch, tmpDc):
# {{{ _drawBrushPatch(self, eventDc, patch): XXX
def _drawBrushPatch(self, eventDc, patch):
absPoint = self._xlatePoint(patch[0])
brushFg = self._brushes[patch[1][1]]
brushBg = self._brushes[patch[1][0]]
pen = self._pens[patch[1][1]]
self._setBrushDc(brushBg, brushFg, (eventDc, tmpDc), pen)
self._setBrushDc(brushBg, brushFg, eventDc, pen)
eventDc.DrawRectangle(*absPoint, *self.cellSize)
tmpDc.DrawRectangle(*absPoint, *self.cellSize)
# }}}
# {{{ _drawCharPatch(self, eventDc, patch, tmpDc): XXX
def _drawCharPatch(self, eventDc, patch, tmpDc):
# {{{ _drawCharPatch(self, eventDc, patch): XXX
def _drawCharPatch(self, eventDc, patch):
absPoint = self._xlatePoint(patch[0])
brushFg = self._brushes[patch[1][0]]
brushBg = self._brushes[patch[1][1]]
pen = self._pens[patch[1][1]]
for dc in (eventDc, tmpDc):
fontBitmap = wx.Bitmap(*self.cellSize)
fontDc = wx.MemoryDC(); fontDc.SelectObject(fontBitmap);
fontDc.SetTextForeground(wx.Colour(MiRCARTColours[patch[1][0]][0:4]))
fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[1][1]][0:4]))
fontDc.SetBrush(brushBg); fontDc.SetBackground(brushBg); fontDc.SetPen(pen);
fontDc.SetFont(self._font)
fontDc.DrawRectangle(0, 0, *self.cellSize)
fontDc.DrawText(patch[3], 0, 0)
dc.Blit(*absPoint, *self.cellSize, fontDc, 0, 0)
fontBitmap = wx.Bitmap(*self.cellSize)
fontDc = wx.MemoryDC(); fontDc.SelectObject(fontBitmap);
fontDc.SetTextForeground(wx.Colour(MiRCARTColours[patch[1][0]][0:4]))
fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[1][1]][0:4]))
fontDc.SetBrush(brushBg); fontDc.SetBackground(brushBg); fontDc.SetPen(pen);
fontDc.SetFont(self._font)
fontDc.DrawRectangle(0, 0, *self.cellSize)
fontDc.DrawText(patch[3], 0, 0)
eventDc.Blit(*absPoint, *self.cellSize, fontDc, 0, 0)
# }}}
# {{{ _finiBrushesAndPens(self): XXX
def _finiBrushesAndPens(self):
@ -79,19 +77,16 @@ class MiRCARTCanvasBackend():
wx.Colour(MiRCARTColours[mircColour][0:4]), 1)
self._lastBrushBg = self._lastBrushFg = self._lastPen = None;
# }}}
# {{{ _setBrushDc(self, brushBg, brushFg, dcList, pen): XXX
def _setBrushDc(self, brushBg, brushFg, dcList, pen):
# {{{ _setBrushDc(self, brushBg, brushFg, dc, pen): XXX
def _setBrushDc(self, brushBg, brushFg, dc, pen):
if self._lastBrushBg != brushBg:
for dc in dcList:
dc.SetBackground(brushBg)
dc.SetBackground(brushBg)
self._lastBrushBg = brushBg
if self._lastBrushFg != brushFg:
for dc in dcList:
dc.SetBrush(brushFg)
dc.SetBrush(brushFg)
self._lastBrushFg = brushFg
if self._lastPen != pen:
for dc in dcList:
dc.SetPen(pen)
dc.SetPen(pen)
self._lastPen = pen
# }}}
# {{{ _xlatePoint(self, relMapPoint): XXX
@ -99,29 +94,34 @@ class MiRCARTCanvasBackend():
return [a*b for a,b in zip(relMapPoint, self.cellSize)]
# }}}
# {{{ drawPatch(self, eventDc, patch, tmpDc): XXX
def drawPatch(self, eventDc, patch, tmpDc):
# {{{ drawPatch(self, eventDc, patch): XXX
def drawPatch(self, eventDc, patch):
if patch[0][0] < self.canvasSize[0] \
and patch[0][1] < self.canvasSize[1]:
if patch[3] == " ":
self._drawBrushPatch(eventDc, patch, tmpDc)
self._drawBrushPatch(eventDc, patch)
else:
self._drawCharPatch(eventDc, patch, tmpDc)
self._drawCharPatch(eventDc, patch)
return True
else:
return False
# }}}
# {{{ drawCursorMaskWithJournal(self, canvasJournal, eventDc, tmpDc): XXX
def drawCursorMaskWithJournal(self, canvasJournal, eventDc, tmpDc):
# {{{ drawCursorMaskWithJournal(self, canvasJournal, eventDc): XXX
def drawCursorMaskWithJournal(self, canvasJournal, eventDc):
for patch in canvasJournal.popCursor():
self.drawPatch(eventDc, patch, tmpDc)
self.drawPatch(eventDc, patch)
# }}}
# {{{ getDeviceContexts(self, parentWindow): XXX
def getDeviceContexts(self, parentWindow):
eventDc = wx.ClientDC(parentWindow); tmpDc = wx.MemoryDC();
tmpDc.SelectObject(self.canvasBitmap)
# {{{ getDeviceContext(self, parentWindow): XXX
def getDeviceContext(self, parentWindow):
eventDc = wx.BufferedDC( \
wx.ClientDC(parentWindow), self.canvasBitmap)
self._lastBrushBg = self._lastBrushFg = self._lastPen = None;
return (eventDc, tmpDc)
return eventDc
# }}}
# {{{ onPanelPaintEvent(self, panelWindow, panelEvent): XXX
def onPanelPaintEvent(self, panelWindow, panelEvent):
if self.canvasBitmap != None:
eventDc = wx.BufferedPaintDC(panelWindow, self.canvasBitmap)
# }}}
# {{{ reset(self, canvasSize, cellSize):
def reset(self, canvasSize, cellSize):

View File

@ -358,7 +358,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
pass
elif cid == self.CID_INCR_CANVAS[0] \
or cid == self.CID_DECR_CANVAS[0]:
eventDc, tmpDc = self.panelCanvas.canvasBackend.getDeviceContexts(self)
eventDc = self.panelCanvas.canvasBackend.getDeviceContext(self)
if cid == self.CID_INCR_CANVAS[0]:
newCanvasSize = [a+1 for a in self.panelCanvas.canvasSize]
else:
@ -373,7 +373,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
for numCol in range(self.panelCanvas.canvasSize[0]):
self.panelCanvas.canvasMap[-1].append([[1, 1], 0, " "])
self.panelCanvas.canvasBackend.drawPatch(eventDc, \
([numCol, self.panelCanvas.canvasSize[1] - 1], *[[1, 1], 0, " "]), tmpDc)
([numCol, self.panelCanvas.canvasSize[1] - 1], *[[1, 1], 0, " "]))
wx.SafeYield()
elif cid == self.CID_INCR_BRUSH[0]:
self.panelCanvas.brushSize = \