MiRCART.py:MiRCARTCanvas.canvasBitmap: added to optimise onPaint().

MiRCART.py:MiRCARTCanvas._onMouseEvent(): additionally draw to canvasBitmap via temporary wx.MemoryDC().
MiRCART.py:MiRCARTCanvas.onPaint(): reimplement using (double-buffered) wx.BufferedPaintDC() from canvasBitmap.
MiRCART.py:MiRCARTCanvas.__init__(): initialise canvasBitmap from canvas window size.
This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-04 20:22:15 +01:00
parent dcb4ca83bb
commit 91e33f8a4a

View File

@ -49,7 +49,7 @@ mircColours = [
class MiRCARTCanvas(wx.Panel): class MiRCARTCanvas(wx.Panel):
"""XXX""" """XXX"""
canvasPos = canvasSize = None canvasPos = canvasSize = None
canvasMap = None canvasBitmap = canvasMap = None
cellPos = cellSize = None cellPos = cellSize = None
brushBg = brushFg = penBg = penFg = None brushBg = brushFg = penBg = penFg = None
mircBg = mircFg = None mircBg = mircFg = None
@ -59,22 +59,31 @@ class MiRCARTCanvas(wx.Panel):
eventObject = event.GetEventObject() eventObject = event.GetEventObject()
if event.Dragging(): if event.Dragging():
eventDc = wx.ClientDC(self) eventDc = wx.ClientDC(self)
tmpDc = wx.MemoryDC()
tmpDc.SelectObject(self.canvasBitmap)
eventPoint = event.GetLogicalPosition(eventDc) eventPoint = event.GetLogicalPosition(eventDc)
rectX = eventPoint.x - (eventPoint.x % self.cellSize[0]) rectX = eventPoint.x - (eventPoint.x % self.cellSize[0])
rectY = eventPoint.y - (eventPoint.y % self.cellSize[1]) rectY = eventPoint.y - (eventPoint.y % self.cellSize[1])
mapX = int(rectX / 7 if rectX else 0) mapX = int(rectX / 7 if rectX else 0)
mapY = int(rectY / 14 if rectY else 0) mapY = int(rectY / 14 if rectY else 0)
eventDc.SetBackground(self.brushBg); eventDc.SetBackground(self.brushBg);
tmpDc.SetBackground(self.brushBg);
if event.LeftIsDown(): if event.LeftIsDown():
eventDc.SetBrush(self.brushFg); eventDc.SetBrush(self.brushFg);
eventDc.SetPen(self.penFg) eventDc.SetPen(self.penFg)
tmpDc.SetBrush(self.brushFg);
tmpDc.SetPen(self.penFg)
self.canvasMap[mapY][mapX] = [self.mircFg, self.mircFg, " "] self.canvasMap[mapY][mapX] = [self.mircFg, self.mircFg, " "]
elif event.RightIsDown(): elif event.RightIsDown():
eventDc.SetBrush(self.brushBg); eventDc.SetBrush(self.brushBg);
eventDc.SetPen(self.penBg) eventDc.SetPen(self.penBg)
tmpDc.SetBrush(self.brushBg);
tmpDc.SetPen(self.penBg)
self.canvasMap[mapY][mapX] = [self.mircBg, self.mircBg, " "] self.canvasMap[mapY][mapX] = [self.mircBg, self.mircBg, " "]
eventDc.DrawRectangle(rectX, rectY, \ eventDc.DrawRectangle(rectX, rectY, \
self.cellSize[0], self.cellSize[1]) self.cellSize[0], self.cellSize[1])
tmpDc.DrawRectangle(rectX, rectY, \
self.cellSize[0], self.cellSize[1])
# }}} # }}}
# {{{ getBackgroundColour(): XXX # {{{ getBackgroundColour(): XXX
def getBackgroundColour(self): def getBackgroundColour(self):
@ -106,17 +115,7 @@ class MiRCARTCanvas(wx.Panel):
# }}} # }}}
# {{{ onPaint(): XXX # {{{ onPaint(): XXX
def onPaint(self, event): def onPaint(self, event):
eventDc = wx.BufferedPaintDC(self) eventDc = wx.BufferedPaintDC(self, self.canvasBitmap)
eventDc.SetBackground(wx.Brush(wx.BLACK))
eventDc.Clear()
for cellX in range(0, self.canvasSize[0]):
for cellY in range(0, self.canvasSize[1]):
eventDc.SetBackground(wx.Brush(wx.Colour(mircColours[self.canvasMap[cellY][cellX][0]]), wx.BRUSHSTYLE_SOLID))
eventDc.SetBrush(wx.Brush(wx.Colour(mircColours[self.canvasMap[cellY][cellX][1]]), wx.BRUSHSTYLE_SOLID))
eventDc.SetPen(wx.Pen(wx.Colour(mircColours[self.canvasMap[cellY][cellX][1]]), 1))
rectX = cellX * self.cellSize[0]; rectY = cellY * self.cellSize[1];
eventDc.DrawRectangle(rectX, rectY, \
self.cellSize[0], self.cellSize[1])
# }}} # }}}
# {{{ onPaletteEvent(): XXX # {{{ onPaletteEvent(): XXX
def onPaletteEvent(self, leftDown, rightDown, numColour): def onPaletteEvent(self, leftDown, rightDown, numColour):
@ -135,11 +134,12 @@ class MiRCARTCanvas(wx.Panel):
# }}} # }}}
# {{{ Initialisation method # {{{ Initialisation method
def __init__(self, parent, canvasPos, cellSize, canvasSize): def __init__(self, parent, canvasPos, cellSize, canvasSize):
super().__init__(parent, pos=canvasPos, size=( \ winSizeW = cellSize[0] * canvasSize[0]
cellSize[0] * canvasSize[0], winSizeH = cellSize[1] * canvasSize[1]
cellSize[1] * canvasSize[1])) super().__init__(parent, pos=canvasPos, size=(winSizeW, winSizeH))
self.canvasPos = canvasPos; self.canvasSize = canvasSize; 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.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.brushBg = wx.Brush(wx.Colour(mircColours[1]), wx.BRUSHSTYLE_SOLID)