2019-09-01 14:34:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
2019-09-03 16:58:50 +00:00
|
|
|
# Canvas.py -- XXX
|
2019-09-04 14:23:59 +00:00
|
|
|
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
|
2019-09-01 14:34:00 +00:00
|
|
|
#
|
|
|
|
|
2019-09-07 08:39:26 +00:00
|
|
|
from CanvasExportStore import CanvasExportStore
|
2019-09-03 16:58:50 +00:00
|
|
|
from CanvasImportStore import CanvasImportStore
|
2019-09-07 08:39:26 +00:00
|
|
|
from CanvasJournal import CanvasJournal
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-07 08:39:26 +00:00
|
|
|
class Canvas():
|
2019-09-01 14:34:00 +00:00
|
|
|
"""XXX"""
|
|
|
|
|
|
|
|
# {{{ _commitPatch(self, patch): XXX
|
|
|
|
def _commitPatch(self, patch):
|
2019-09-07 08:39:26 +00:00
|
|
|
self.map[patch[1]][patch[0]] = patch[2:]
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
|
|
|
|
2019-09-07 08:39:26 +00:00
|
|
|
# {{{ dispatchPatch(self, isCursor, patch, commitUndo=True): XXX
|
|
|
|
def dispatchPatch(self, isCursor, patch, commitUndo=True):
|
|
|
|
patchDeltaCell = self.map[patch[1]][patch[0]]; patchDelta = [*patch[0:2], *patchDeltaCell];
|
|
|
|
if isCursor:
|
|
|
|
self.journal.pushCursor(patchDelta)
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
if commitUndo:
|
|
|
|
if not self.dirty:
|
|
|
|
self.journal.pushDeltas([], []); self.dirty = True;
|
|
|
|
self.journal.updateCurrentDeltas(patch, patchDelta)
|
|
|
|
self._commitPatch(patch)
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-07 08:39:26 +00:00
|
|
|
# {{{ resize(self, newSize, commitUndo=True): XXX
|
|
|
|
def resize(self, newSize, commitUndo=True):
|
|
|
|
if newSize != self.size:
|
|
|
|
self.dirty = False
|
|
|
|
if self.map == None:
|
|
|
|
self.map, oldSize = [], [0, 0]
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
oldSize = self.size
|
|
|
|
deltaSize = [b - a for a, b in zip(oldSize, newSize)]
|
|
|
|
self.journal.resetCursor()
|
2019-09-04 13:52:54 +00:00
|
|
|
if commitUndo:
|
2019-09-07 08:39:26 +00:00
|
|
|
undoPatches, redoPatches = ["resize", *oldSize], ["resize", *newSize]
|
|
|
|
if not self.dirty:
|
|
|
|
self.journal.pushDeltas([], []); self.dirty = True;
|
|
|
|
self.journal.updateCurrentDeltas(redoPatches, undoPatches)
|
|
|
|
if deltaSize[0] < 0:
|
|
|
|
for numRow in range(oldSize[1]):
|
2019-09-04 13:52:54 +00:00
|
|
|
if commitUndo:
|
2019-09-07 08:39:26 +00:00
|
|
|
for numCol in range((oldSize[0] + deltaSize[0]), oldSize[0]):
|
|
|
|
if not self.dirty:
|
|
|
|
self.journal.pushDeltas([], []); self.dirty = True;
|
|
|
|
self.journal.updateCurrentDeltas(None, [numCol, numRow, *self.map[numRow][numCol]])
|
|
|
|
del self.map[numRow][-1:(deltaSize[0]-1):-1]
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
for numRow in range(oldSize[1]):
|
|
|
|
self.map[numRow].extend([[1, 1, 0, " "]] * deltaSize[0])
|
|
|
|
for numNewCol in range(oldSize[0], newSize[0]):
|
|
|
|
self.dispatchPatch(False, [numNewCol, numRow, 1, 1, 0, " "], commitUndo)
|
|
|
|
if deltaSize[1] < 0:
|
2019-09-04 13:52:54 +00:00
|
|
|
if commitUndo:
|
2019-09-07 08:39:26 +00:00
|
|
|
for numRow in range((oldSize[1] + deltaSize[1]), oldSize[1]):
|
|
|
|
for numCol in range(oldSize[0] + deltaSize[0]):
|
|
|
|
if not self.dirty:
|
|
|
|
self.journal.pushDeltas([], []); self.dirty = True;
|
|
|
|
self.journal.updateCurrentDeltas(None, [numCol, numRow, *self.map[numRow][numCol]])
|
|
|
|
del self.map[-1:(deltaSize[1]-1):-1]
|
2019-09-01 14:34:00 +00:00
|
|
|
else:
|
2019-09-07 08:39:26 +00:00
|
|
|
for numNewRow in range(oldSize[1], newSize[1]):
|
|
|
|
self.map.extend([[[1, 1, 0, " "]] * newSize[0]])
|
|
|
|
for numNewCol in range(newSize[0]):
|
|
|
|
self.dispatchPatch(False, [numNewCol, numNewRow, 1, 1, 0, " "], commitUndo)
|
|
|
|
self.size = newSize
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
2019-09-07 08:39:26 +00:00
|
|
|
# {{{ update(self, newSize, newCanvas=None): XXX
|
|
|
|
def update(self, newSize, newCanvas=None):
|
|
|
|
for numRow in range(self.size[1]):
|
|
|
|
for numCol in range(self.size[0]):
|
|
|
|
if (newCanvas != None) \
|
|
|
|
and (numRow < len(newCanvas)) \
|
|
|
|
and (numCol < len(newCanvas[numRow])):
|
|
|
|
self._commitPatch([numCol, numRow, *newCanvas[numRow][numCol]])
|
2019-09-01 14:34:00 +00:00
|
|
|
# }}}
|
|
|
|
|
|
|
|
#
|
2019-09-07 08:39:26 +00:00
|
|
|
# __init__(self, size): initialisation method
|
|
|
|
def __init__(self, size):
|
|
|
|
self.dirty, self.dirtyCursor, self.map, self.size = False, False, None, size
|
|
|
|
self.exportStore, self.importStore, self.journal = CanvasExportStore(), CanvasImportStore(), CanvasJournal()
|
2019-09-01 14:34:00 +00:00
|
|
|
|
2019-09-04 13:52:54 +00:00
|
|
|
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=0
|