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

View File

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

View File

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

View File

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

View File

@ -32,17 +32,17 @@ class MiRCARTToolFill(MiRCARTTool):
# onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc): XXX
def onMouseEvent(self, event, atPoint, brushColours, brushSize, isDragging, isLeftDown, isRightDown, dispatchFn, eventDc):
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 isRightDown:
brushColours = [brushColours[1], brushColours[0]]
while len(pointStack) > 0:
point = pointStack.pop()
pointCell = self.parentCanvas.canvasMap[point[1]][point[0]]
if pointCell[0][1] == testColour:
if pointCell[0:2] == testColour:
if not point in pointsDone:
dispatchFn(eventDc, False, [point.copy(), \
[brushColours[0], brushColours[0]], 0, " "])
dispatchFn(eventDc, False, [*point, \
brushColours[0], brushColours[0], 0, " "])
if point[0] > 0:
pointStack.append([point[0] - 1, point[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;
for lineX in range(pointDelta[0] + 1):
for brushStep in range(brushSize[0]):
patch = [[ \
patch = [ \
originPoint[0] + lineX*lineXX + lineY*lineYX + brushStep, \
originPoint[1] + lineX*lineXY + lineY*lineYY], \
brushColours, 0, " "]
originPoint[1] + lineX*lineXY + lineY*lineYY, \
*brushColours, 0, " "]
if isCursor:
dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch);
else:
@ -83,7 +83,7 @@ class MiRCARTToolLine(MiRCARTTool):
self.toolColours = brushColours
self.toolOriginPoint = list(atPoint)
self.toolState = self.TS_ORIGIN
dispatchFn(eventDc, True, [atPoint, brushColours, 0, " "])
dispatchFn(eventDc, True, [*atPoint, *brushColours, 0, " "])
elif self.toolState == self.TS_ORIGIN:
targetPoint = list(atPoint)
originPoint = self.toolOriginPoint

View File

@ -43,7 +43,7 @@ class MiRCARTToolRect(MiRCARTTool):
brushSize[0] *= 2
for brushRow in range(brushSize[1]):
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:
dispatchFn(eventDc, False, patch); dispatchFn(eventDc, True, patch);
else:

View File

@ -53,18 +53,18 @@ class MiRCARTToolSelect(MiRCARTTool):
else:
curColours = [0, 0]
dispatchFn(eventDc, True, \
[[rectX, rectFrame[0][1]], curColours, 0, " "])
[rectX, rectFrame[0][1], *curColours, 0, " "])
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):
if curColours == [0, 0]:
curColours = [1, 1]
else:
curColours = [0, 0]
dispatchFn(eventDc, True, \
[[rectFrame[0][0], rectY], curColours, 0, " "])
[rectFrame[0][0], rectY, *curColours, 0, " "])
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
else:
dispatchFn(eventDc, True, \
[list(atPoint), brushColours.copy(), 0, " "])
[*atPoint, *brushColours, 0, " "])
elif self.toolState == self.TS_ORIGIN:
self.toolRect[1] = list(atPoint)
if isLeftDown or isRightDown:

View File

@ -53,7 +53,7 @@ class MiRCARTToolSelectClone(MiRCARTToolSelect):
cellOld = self.toolSelectMap[numRow][numCol]
rectY = selectRect[0][1] + numRow
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.toolRect = newToolRect

View File

@ -50,14 +50,14 @@ class MiRCARTToolSelectMove(MiRCARTToolSelect):
isCursor = True
for numRow in range(len(self.toolSelectMap)):
for numCol in range(len(self.toolSelectMap[numRow])):
dispatchFn(eventDc, isCursor, [[self.srcRect[0] + numCol, \
self.srcRect[1] + numRow], [1, 1], 0, " "])
dispatchFn(eventDc, isCursor, [self.srcRect[0] + numCol, \
self.srcRect[1] + numRow, 1, 1, 0, " "])
for numRow in range(len(self.toolSelectMap)):
for numCol in range(len(self.toolSelectMap[numRow])):
cellOld = self.toolSelectMap[numRow][numCol]
rectY = selectRect[0][1] + numRow
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.toolRect = newToolRect

View File

@ -42,7 +42,7 @@ class MiRCARTToolText(MiRCARTTool):
self.textColours = brushColours.copy()
if self.textPos == None:
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):
self.textPos[0] += 1
elif self.textPos[1] < (self.parentCanvas.canvasSize[1] - 1):
@ -65,6 +65,6 @@ class MiRCARTToolText(MiRCARTTool):
if self.textColours == None:
self.textColours = brushColours.copy()
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