Reduce memory usage by folding nested patch {coordinate,colour} list(s).

This commit is contained in:
Lucio Andrés Illanes Albornoz 2018-01-30 11:17:35 +01:00
parent 1a503979d1
commit 6d5e081dfb
11 changed files with 55 additions and 55 deletions

View File

@ -41,7 +41,7 @@ class MiRCARTCanvas(wx.Panel):
# {{{ _commitPatch(self, patch): XXX # {{{ _commitPatch(self, patch): XXX
def _commitPatch(self, patch): def _commitPatch(self, patch):
self.canvasMap[patch[0][1]][patch[0][0]] = patch[1:] self.canvasMap[patch[1]][patch[0]] = patch[2:]
# }}} # }}}
# {{{ _dispatchDeltaPatches(self, deltaPatches): XXX # {{{ _dispatchDeltaPatches(self, deltaPatches): XXX
def _dispatchDeltaPatches(self, deltaPatches): def _dispatchDeltaPatches(self, deltaPatches):
@ -58,8 +58,8 @@ class MiRCARTCanvas(wx.Panel):
self.canvasJournal, eventDc) self.canvasJournal, eventDc)
self._canvasDirtyCursor = True self._canvasDirtyCursor = True
if self.canvasBackend.drawPatch(eventDc, patch): if self.canvasBackend.drawPatch(eventDc, patch):
patchDeltaCell = self.canvasMap[patch[0][1]][patch[0][0]] patchDeltaCell = self.canvasMap[patch[1]][patch[0]]
patchDelta = [list(patch[0]), *patchDeltaCell.copy()] patchDelta = [*patch[0:2], *patchDeltaCell]
if isCursor: if isCursor:
self.canvasJournal.pushCursor(patchDelta) self.canvasJournal.pushCursor(patchDelta)
else: else:
@ -127,10 +127,10 @@ class MiRCARTCanvas(wx.Panel):
and numRow < len(newCanvas) \ and numRow < len(newCanvas) \
and numCol < len(newCanvas[numRow]): and numCol < len(newCanvas[numRow]):
self._commitPatch([ \ self._commitPatch([ \
[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])) *self.canvasMap[numRow][numCol]])
wx.SafeYield() wx.SafeYield()
# }}} # }}}
# {{{ resize(self, newCanvasSize): XXX # {{{ resize(self, newCanvasSize): XXX
@ -160,20 +160,20 @@ class MiRCARTCanvas(wx.Panel):
else: else:
for numRow in range(oldCanvasSize[1]): for numRow in range(oldCanvasSize[1]):
self.canvasMap[numRow].extend( \ self.canvasMap[numRow].extend( \
[[[1, 1], 0, " "]] * deltaCanvasSize[0]) [[1, 1, 0, " "]] * deltaCanvasSize[0])
for numNewCol in range(oldCanvasSize[0], newCanvasSize[0]): for numNewCol in range(oldCanvasSize[0], newCanvasSize[0]):
self.canvasBackend.drawPatch( \ self.canvasBackend.drawPatch( \
eventDc, [[numNewCol, numRow], \ eventDc, [numNewCol, numRow, \
*self.canvasMap[numRow][-1]]) *self.canvasMap[numRow][-1]])
if deltaCanvasSize[1] < 0: if deltaCanvasSize[1] < 0:
del self.canvasMap[-1:(deltaCanvasSize[1]-1):-1] del self.canvasMap[-1:(deltaCanvasSize[1]-1):-1]
else: else:
for numNewRow in range(oldCanvasSize[1], newCanvasSize[1]): for numNewRow in range(oldCanvasSize[1], newCanvasSize[1]):
self.canvasMap.extend( \ self.canvasMap.extend( \
[[[[1, 1], 0, " "]] * newCanvasSize[0]]) [[[1, 1, 0, " "]] * newCanvasSize[0]])
for numNewCol in range(newCanvasSize[0]): for numNewCol in range(newCanvasSize[0]):
self.canvasBackend.drawPatch( \ self.canvasBackend.drawPatch( \
eventDc, [[numNewCol, numNewRow], \ eventDc, [numNewCol, numNewRow, \
*self.canvasMap[-1][-1]]) *self.canvasMap[-1][-1]])
self.canvasSize = newCanvasSize self.canvasSize = newCanvasSize

View File

@ -33,27 +33,27 @@ class MiRCARTCanvasBackend():
# {{{ _drawBrushPatch(self, eventDc, patch): XXX # {{{ _drawBrushPatch(self, eventDc, patch): XXX
def _drawBrushPatch(self, eventDc, patch): def _drawBrushPatch(self, eventDc, patch):
absPoint = self._xlatePoint(patch[0]) absPoint = self._xlatePoint(patch)
brushFg = self._brushes[patch[1][1]] brushFg = self._brushes[patch[3]]
brushBg = self._brushes[patch[1][0]] brushBg = self._brushes[patch[2]]
pen = self._pens[patch[1][1]] pen = self._pens[patch[3]]
self._setBrushDc(brushBg, brushFg, eventDc, pen) self._setBrushDc(brushBg, brushFg, eventDc, pen)
eventDc.DrawRectangle(*absPoint, *self.cellSize) eventDc.DrawRectangle(*absPoint, *self.cellSize)
# }}} # }}}
# {{{ _drawCharPatch(self, eventDc, patch): XXX # {{{ _drawCharPatch(self, eventDc, patch): XXX
def _drawCharPatch(self, eventDc, patch): def _drawCharPatch(self, eventDc, patch):
absPoint = self._xlatePoint(patch[0]) absPoint = self._xlatePoint(patch)
brushFg = self._brushes[patch[1][0]] brushFg = self._brushes[patch[2]]
brushBg = self._brushes[patch[1][1]] brushBg = self._brushes[patch[3]]
pen = self._pens[patch[1][1]] pen = self._pens[patch[3]]
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[2]][0:4]))
fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[1][1]][0:4])) fontDc.SetTextBackground(wx.Colour(MiRCARTColours[patch[3]][0:4]))
fontDc.SetBrush(brushBg); fontDc.SetBackground(brushBg); fontDc.SetPen(pen); fontDc.SetBrush(brushBg); fontDc.SetBackground(brushBg); fontDc.SetPen(pen);
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[5], 0, 0)
eventDc.Blit(*absPoint, *self.cellSize, fontDc, 0, 0) eventDc.Blit(*absPoint, *self.cellSize, fontDc, 0, 0)
# }}} # }}}
# {{{ _finiBrushesAndPens(self): XXX # {{{ _finiBrushesAndPens(self): XXX
@ -89,18 +89,18 @@ class MiRCARTCanvasBackend():
dc.SetPen(pen) dc.SetPen(pen)
self._lastPen = pen self._lastPen = pen
# }}} # }}}
# {{{ _xlatePoint(self, relMapPoint): XXX # {{{ _xlatePoint(self, patch): XXX
def _xlatePoint(self, relMapPoint): def _xlatePoint(self, patch):
return [a*b for a,b in zip(relMapPoint, self.cellSize)] return [a*b for a,b in zip(patch[0:2], self.cellSize)]
# }}} # }}}
# {{{ drawPatch(self, eventDc, patch): XXX # {{{ drawPatch(self, eventDc, patch): XXX
def drawPatch(self, eventDc, patch): def drawPatch(self, eventDc, patch):
if patch[0][0] < self.canvasSize[0] \ if patch[0] < self.canvasSize[0] \
and patch[0][0] >= 0 \ and patch[0] >= 0 \
and patch[0][1] < self.canvasSize[1] \ and patch[1] < self.canvasSize[1] \
and patch[0][1] >= 0: and patch[1] >= 0:
if patch[3] == " ": if patch[5] == " ":
self._drawBrushPatch(eventDc, patch) self._drawBrushPatch(eventDc, patch)
else: else:
self._drawCharPatch(eventDc, patch) self._drawCharPatch(eventDc, patch)

View File

@ -105,7 +105,7 @@ class MiRCARTCanvasImportStore():
inCellState, self._CellState.CS_UNDERLINE) inCellState, self._CellState.CS_UNDERLINE)
else: else:
inRowCols += 1 inRowCols += 1
outMap[inCurRow].append((inCurColours, inCellState, inChar)) outMap[inCurRow].append([*inCurColours, inCellState, inChar])
elif inParseState == self._ParseState.PS_COLOUR_DIGIT0 \ elif inParseState == self._ParseState.PS_COLOUR_DIGIT0 \
or inParseState == self._ParseState.PS_COLOUR_DIGIT1: or inParseState == self._ParseState.PS_COLOUR_DIGIT1:
if inChar == "," \ if inChar == "," \
@ -146,8 +146,8 @@ class MiRCARTCanvasImportStore():
# }}} # }}}
# {{{ importNew(self, newCanvasSize=None): XXX # {{{ importNew(self, newCanvasSize=None): XXX
def importNew(self, newCanvasSize=None): def importNew(self, newCanvasSize=None):
newMap = [[[[1, 1], 0, " "] \ newMap = [[[1, 1, 0, " "] \
for x in range(newCanvasSize[0])] \ for x in range(newCanvasSize[0])] \
for y in range(newCanvasSize[1])] for y in range(newCanvasSize[1])]
self.parentCanvas.onStoreUpdate(newCanvasSize, newMap) self.parentCanvas.onStoreUpdate(newCanvasSize, newMap)
# }}} # }}}

View File

@ -45,9 +45,9 @@ class MiRCARTToolCircle(MiRCARTTool):
for brushX in range(-radius, radius + 1): for brushX in range(-radius, radius + 1):
if ((brushX**2)+(brushY**2) < (((radius**2)+radius)*0.8)): if ((brushX**2)+(brushY**2) < (((radius**2)+radius)*0.8)):
patch = [ \ patch = [ \
[atPoint[0] + int(originPoint[0]+brushX), \ atPoint[0] + int(originPoint[0]+brushX), \
atPoint[1] + int(originPoint[1]+brushY)], \ atPoint[1] + int(originPoint[1]+brushY), \
brushColours, 0, " "] *brushColours, 0, " "]
if isLeftDown or isRightDown: if isLeftDown or isRightDown:
dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch);
else: else:

View File

@ -32,17 +32,17 @@ class MiRCARTToolFill(MiRCARTTool):
# onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): XXX # onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): XXX
def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc):
pointStack = [list(atPoint)]; pointsDone = []; pointStack = [list(atPoint)]; pointsDone = [];
testColour = self.parentCanvas.canvasMap[atPoint[1]][atPoint[0]][0][1] testColour = self.parentCanvas.canvasMap[atPoint[1]][atPoint[0]][0:2]
if isLeftDown or isRightDown: if isLeftDown or isRightDown:
if isRightDown: if isRightDown:
brushColours = [brushColours[1], brushColours[0]] brushColours = [brushColours[1], brushColours[0]]
while len(pointStack) > 0: while len(pointStack) > 0:
point = pointStack.pop() point = pointStack.pop()
pointCell = self.parentCanvas.canvasMap[point[1]][point[0]] pointCell = self.parentCanvas.canvasMap[point[1]][point[0]]
if pointCell[0][1] == testColour: if pointCell[0:2] == testColour:
if not point in pointsDone: if not point in pointsDone:
dispatchFn(eventDc, False, [point.copy(), \ dispatchFn(eventDc, False, [*point, \
[brushColours[0], brushColours[0]], 0, " "]) brushColours[0], brushColours[0], 0, " "])
if point[0] > 0: if point[0] > 0:
pointStack.append([point[0] - 1, point[1]]) pointStack.append([point[0] - 1, point[1]])
if point[0] < (self.parentCanvas.canvasSize[0] - 1): if point[0] < (self.parentCanvas.canvasSize[0] - 1):

View File

@ -55,10 +55,10 @@ class MiRCARTToolLine(MiRCARTTool):
lineD = 2 * pointDelta[1] - pointDelta[0]; lineY = 0; lineD = 2 * pointDelta[1] - pointDelta[0]; lineY = 0;
for lineX in range(pointDelta[0] + 1): for lineX in range(pointDelta[0] + 1):
for brushStep in range(brushSize[0]): for brushStep in range(brushSize[0]):
patch = [[ \ patch = [ \
originPoint[0] + lineX*lineXX + lineY*lineYX + brushStep, \ originPoint[0] + lineX*lineXX + lineY*lineYX + brushStep, \
originPoint[1] + lineX*lineXY + lineY*lineYY], \ originPoint[1] + lineX*lineXY + lineY*lineYY, \
brushColours, 0, " "] *brushColours, 0, " "]
if isCursor: if isCursor:
dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch);
else: else:
@ -83,7 +83,7 @@ class MiRCARTToolLine(MiRCARTTool):
self.toolColours = brushColours self.toolColours = brushColours
self.toolOriginPoint = list(atPoint) self.toolOriginPoint = list(atPoint)
self.toolState = self.TS_ORIGIN self.toolState = self.TS_ORIGIN
dispatchFn(eventDc, True, [atPoint, brushColours, 0, " "]) dispatchFn(eventDc, True, [*atPoint, *brushColours, 0, " "])
elif self.toolState == self.TS_ORIGIN: elif self.toolState == self.TS_ORIGIN:
targetPoint = list(atPoint) targetPoint = list(atPoint)
originPoint = self.toolOriginPoint originPoint = self.toolOriginPoint

View File

@ -43,7 +43,7 @@ class MiRCARTToolRect(MiRCARTTool):
brushSize[0] *= 2 brushSize[0] *= 2
for brushRow in range(brushSize[1]): for brushRow in range(brushSize[1]):
for brushCol in range(brushSize[0]): for brushCol in range(brushSize[0]):
patch = [[atPoint[0] + brushCol, atPoint[1] + brushRow],brushColours, 0, " "] patch = [atPoint[0] + brushCol, atPoint[1] + brushRow, *brushColours, 0, " "]
if isLeftDown or isRightDown: if isLeftDown or isRightDown:
dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch); dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch);
else: else:

View File

@ -53,18 +53,18 @@ class MiRCARTToolSelect(MiRCARTTool):
else: else:
curColours = [0, 0] curColours = [0, 0]
dispatchFn(eventDc, True, \ dispatchFn(eventDc, True, \
[[rectX, rectFrame[0][1]], curColours, 0, " "]) [rectX, rectFrame[0][1], *curColours, 0, " "])
dispatchFn(eventDc, True, \ dispatchFn(eventDc, True, \
[[rectX, rectFrame[1][1]], curColours, 0, " "]) [rectX, rectFrame[1][1], *curColours, 0, " "])
for rectY in range(rectFrame[0][1], rectFrame[1][1]+1): for rectY in range(rectFrame[0][1], rectFrame[1][1]+1):
if curColours == [0, 0]: if curColours == [0, 0]:
curColours = [1, 1] curColours = [1, 1]
else: else:
curColours = [0, 0] curColours = [0, 0]
dispatchFn(eventDc, True, \ dispatchFn(eventDc, True, \
[[rectFrame[0][0], rectY], curColours, 0, " "]) [rectFrame[0][0], rectY, *curColours, 0, " "])
dispatchFn(eventDc, True, \ dispatchFn(eventDc, True, \
[[rectFrame[1][0], rectY], curColours, 0, " "]) [rectFrame[1][0], rectY, *curColours, 0, " "])
# }}} # }}}
# #
@ -82,7 +82,7 @@ class MiRCARTToolSelect(MiRCARTTool):
self.toolState = self.TS_ORIGIN self.toolState = self.TS_ORIGIN
else: else:
dispatchFn(eventDc, True, \ dispatchFn(eventDc, True, \
[list(atPoint), brushColours.copy(), 0, " "]) [*atPoint, *brushColours, 0, " "])
elif self.toolState == self.TS_ORIGIN: elif self.toolState == self.TS_ORIGIN:
self.toolRect[1] = list(atPoint) self.toolRect[1] = list(atPoint)
if isLeftDown or isRightDown: if isLeftDown or isRightDown:

View File

@ -53,7 +53,7 @@ class MiRCARTToolSelectClone(MiRCARTToolSelect):
cellOld = self.toolSelectMap[numRow][numCol] cellOld = self.toolSelectMap[numRow][numCol]
rectY = selectRect[0][1] + numRow rectY = selectRect[0][1] + numRow
rectX = selectRect[0][0] + numCol rectX = selectRect[0][0] + numCol
dispatchFn(eventDc, isCursor, [[rectX+disp[0], rectY+disp[1]], *cellOld]) dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld])
self._drawSelectRect(newToolRect, dispatchFn, eventDc) self._drawSelectRect(newToolRect, dispatchFn, eventDc)
self.toolRect = newToolRect self.toolRect = newToolRect

View File

@ -50,14 +50,14 @@ class MiRCARTToolSelectMove(MiRCARTToolSelect):
isCursor = True isCursor = True
for numRow in range(len(self.toolSelectMap)): for numRow in range(len(self.toolSelectMap)):
for numCol in range(len(self.toolSelectMap[numRow])): for numCol in range(len(self.toolSelectMap[numRow])):
dispatchFn(eventDc, isCursor, [[self.srcRect[0] + numCol, \ dispatchFn(eventDc, isCursor, [self.srcRect[0] + numCol, \
self.srcRect[1] + numRow], [1, 1], 0, " "]) self.srcRect[1] + numRow, 1, 1, 0, " "])
for numRow in range(len(self.toolSelectMap)): for numRow in range(len(self.toolSelectMap)):
for numCol in range(len(self.toolSelectMap[numRow])): for numCol in range(len(self.toolSelectMap[numRow])):
cellOld = self.toolSelectMap[numRow][numCol] cellOld = self.toolSelectMap[numRow][numCol]
rectY = selectRect[0][1] + numRow rectY = selectRect[0][1] + numRow
rectX = selectRect[0][0] + numCol rectX = selectRect[0][0] + numCol
dispatchFn(eventDc, isCursor, [[rectX+disp[0], rectY+disp[1]], *cellOld]) dispatchFn(eventDc, isCursor, [rectX+disp[0], rectY+disp[1], *cellOld])
self._drawSelectRect(newToolRect, dispatchFn, eventDc) self._drawSelectRect(newToolRect, dispatchFn, eventDc)
self.toolRect = newToolRect self.toolRect = newToolRect

View File

@ -42,7 +42,7 @@ class MiRCARTToolText(MiRCARTTool):
self.textColours = brushColours.copy() self.textColours = brushColours.copy()
if self.textPos == None: if self.textPos == None:
self.textPos = list(atPoint) self.textPos = list(atPoint)
dispatchFn(eventDc, False, [self.textPos, self.textColours, 0, keyChar]) dispatchFn(eventDc, False, [*self.textPos, *self.textColours, 0, keyChar])
if self.textPos[0] < (self.parentCanvas.canvasSize[0] - 1): if self.textPos[0] < (self.parentCanvas.canvasSize[0] - 1):
self.textPos[0] += 1 self.textPos[0] += 1
elif self.textPos[1] < (self.parentCanvas.canvasSize[1] - 1): elif self.textPos[1] < (self.parentCanvas.canvasSize[1] - 1):
@ -65,6 +65,6 @@ class MiRCARTToolText(MiRCARTTool):
if self.textColours == None: if self.textColours == None:
self.textColours = brushColours.copy() self.textColours = brushColours.copy()
self.textPos = list(atPoint) self.textPos = list(atPoint)
dispatchFn(eventDc, True, [self.textPos, self.textColours, 0, "_"]) dispatchFn(eventDc, True, [*self.textPos, *self.textColours, 0, "_"])
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120