roar/libtools/ToolText.py
Lucio Andrés Illanes Albornoz 0c66f94797 Cleanup, bugfixes & C++ backend implementation.
1) {About,Melp?} window: switch to green on black.
2) Assets window: scroll assets list on selected item update w/ <Cursor> or on deletion.
3) Canvas window: change default brush colours to [3, -1].
4) Canvas window: copy canvas cells given transparent cells from tools.
5) Canvas window: don't disable {re,un}do during object tool usage.
6) Canvas window: don't hide cursor during {re,un}do.
7) Canvas window: draw new cells using current brush background colour on resize.
8) Canvas window: fix memory leak on cell size updating.
9) Text tool: process [\r\n] in text pasted from clipboard.

assets/audio/roar{vap0r[1-8],viking[1-5]}.wav: added.
assets/text/README.txt: updated.
assets/tools/AnsiToMiRCART.py: added (for spoke.)
assets/tools/deploy-python.sh: updated.
2019-10-24 21:14:00 +02:00

132 lines
6.0 KiB
Python

#!/usr/bin/env python3
#
# ToolText.py
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
#
from Tool import Tool
import re, string, wx
class ToolText(Tool):
name = "Text"
arabicCombiningRegEx = r'^[\u064B-\u065F\uFE70-\uFE72\uFE74\uFE76-\uFE7F]+$'
arabicRegEx = r'^[\u0621-\u063A\u0640-\u064A]+$'
rtlRegEx = r'^[\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC]+$'
def _checkRtl(self, canvas, brushPos, keyChar):
rtlFlag = False
if (keyChar != None) and re.match(self.rtlRegEx, keyChar):
rtlFlag = True
else:
lastX, lastY = brushPos[0], brushPos[1]
while True:
if canvas.map[lastY][lastX][3] == " ":
if (lastX + 1) >= canvas.size[0]:
if lastY == 0:
break
else:
lastX, lastY = 0, lastY - 1
else:
lastX += 1
elif re.match(self.arabicRegEx, canvas.map[lastY][lastX][3]):
rtlFlag = True
if (lastX + 1) >= canvas.size[0]:
if lastY == 0:
break
else:
lastX, lastY = 0, lastY - 1
else:
lastX += 1
else:
break
return rtlFlag
def _processKeyChar(self, brushColours, brushPos, canvas, keyChar, keyModifiers):
patches, rc = [], False
if (ord(keyChar) != wx.WXK_NONE) \
and (not keyChar in set("\t\n\v\f\r")) \
and ((ord(keyChar) >= 32) if ord(keyChar) < 127 else True):
patches += [[*brushPos, *brushColours, 0, keyChar]]
if not self._checkRtl(canvas, brushPos, keyChar):
if brushPos[0] < (canvas.size[0] - 1):
brushPos[0] += 1
elif brushPos[1] < (canvas.size[1] - 1):
brushPos[0], brushPos[1] = 0, brushPos[1] + 1
else:
brushPos[0], brushPos[1] = 0, 0
else:
if brushPos[0] > 0:
brushPos[0] -= 1
elif brushPos[1] > 0:
brushPos[0], brushPos[1] = canvas.size[0] - 1, brushPos[1] - 1
else:
brushPos[0], brushPos[1] = canvas.size[0] - 1, canvas.size[1] - 1
rc = True
return rc, patches
def onKeyboardEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyChar, keyCode, keyModifiers, mapPoint):
patches, patchesCursor, rc = [], [], False
if re.match(self.arabicCombiningRegEx, keyChar):
rc = True
elif keyCode == wx.WXK_CONTROL_V:
rc = True
if wx.TheClipboard.IsSupported(wx.DataFormat(wx.DF_TEXT)) and wx.TheClipboard.Open():
inBuffer = wx.TextDataObject()
if wx.TheClipboard.GetData(inBuffer):
brushPosOriginX = brushPos[0]
for inBufferChar in list(inBuffer.GetText()):
if inBufferChar in set("\r\n"):
if brushPos[1] < (canvas.size[1] - 1):
brushPos[0], brushPos[1] = brushPosOriginX, brushPos[1] + 1
else:
brushPos[0], brushPos[1] = brushPosOriginX, 0
elif not re.match(self.arabicCombiningRegEx, inBufferChar):
rc_, patches_ = self._processKeyChar(brushColours, brushPos, canvas, inBufferChar, 0)
patches += patches_
rc = True if rc_ else rc
if rc:
patchesCursor += [[*brushPos, *brushColours, 0, "_"]]
wx.TheClipboard.Close()
else:
rc, error = False, "Clipboard does not contain text data and/or cannot be opened"
elif keyCode == wx.WXK_BACK:
if ((brushPos[0] + 1) >= canvas.size[0]):
lastBrushPos = [0, brushPos[1] - 1] if brushPos[1] > 0 else [0, 0]
else:
lastBrushPos = [brushPos[0] + 1, brushPos[1]]
if not self._checkRtl(canvas, lastBrushPos, None):
patches += [[*brushPos, *brushColours, 0, " "]]
if brushPos[0] > 0:
brushPos[0] -= 1
elif brushPos[1] > 0:
brushPos[0], brushPos[1] = canvas.size[0] - 1, brushPos[1] - 1
else:
brushPos[0], brushPos[1] = canvas.size[0] - 1, canvas.size[1] - 1
else:
if brushPos[0] < (canvas.size[0] - 1):
brushPos[0] += 1
elif brushPos[1] > 0:
brushPos[0], brushPos[1] = 0, brushPos[1] - 1
else:
brushPos[0], brushPos[1] = canvas.size[0] - 1, 0
rc = True; patchesCursor += [[*brushPos, *brushColours, 0, "_"]];
elif keyCode == wx.WXK_RETURN:
if brushPos[1] < (canvas.size[1] - 1):
brushPos[0], brushPos[1] = 0, brushPos[1] + 1
else:
brushPos[0], brushPos[1] = 0, 0
rc = True; patchesCursor += [[*brushPos, *brushColours, 0, "_"]];
elif not (keyModifiers in (wx.MOD_ALT, wx.MOD_ALTGR, wx.MOD_CONTROL)):
rc, patches_ = self._processKeyChar(brushColours, brushPos, canvas, keyChar, keyModifiers)
patches += patches_
if rc:
patchesCursor += [[*brushPos, *brushColours, 0, "_"]]
return rc, patches, patchesCursor
def onMouseEvent(self, atPoint, brushColours, brushPos, brushSize, canvas, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown):
if mouseLeftDown or mouseRightDown:
brushPos[0], brushPos[1] = atPoint[0], atPoint[1]
return True, None, [[*brushPos, *brushColours, 0, "_"]]
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120