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

View File

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

View File

@ -358,7 +358,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
pass pass
elif cid == self.CID_INCR_CANVAS[0] \ elif cid == self.CID_INCR_CANVAS[0] \
or cid == self.CID_DECR_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]: if cid == self.CID_INCR_CANVAS[0]:
newCanvasSize = [a+1 for a in self.panelCanvas.canvasSize] newCanvasSize = [a+1 for a in self.panelCanvas.canvasSize]
else: else:
@ -373,7 +373,7 @@ class MiRCARTFrame(MiRCARTGeneralFrame):
for numCol in range(self.panelCanvas.canvasSize[0]): for numCol in range(self.panelCanvas.canvasSize[0]):
self.panelCanvas.canvasMap[-1].append([[1, 1], 0, " "]) self.panelCanvas.canvasMap[-1].append([[1, 1], 0, " "])
self.panelCanvas.canvasBackend.drawPatch(eventDc, \ 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() wx.SafeYield()
elif cid == self.CID_INCR_BRUSH[0]: elif cid == self.CID_INCR_BRUSH[0]:
self.panelCanvas.brushSize = \ self.panelCanvas.brushSize = \