libroar/RoarClient.py, libgui/GuiFrame.py: split from libgui/GuiFrame.py.

libroar/RoarCanvasWindow.py, libgui/GuiWindow.py: split from libgui/GuiCanvasPanel.py.
roar.py: updated.
This commit is contained in:
Lucio Andrés Illanes Albornoz 2019-09-10 10:14:12 +02:00
parent 548f63b4e7
commit 0aceb126f3
7 changed files with 228 additions and 175 deletions

View File

@ -212,7 +212,7 @@ class GuiCanvasInterface():
def canvasImportEmpty(pathName): def canvasImportEmpty(pathName):
nonlocal newCanvasSize nonlocal newCanvasSize
if newCanvasSize == None: if newCanvasSize == None:
newCanvasSize = list(self.parentCanvas.defaultCanvasSize) newCanvasSize = list(self.parentCanvas.canvas.size)
newMap = [[[1, 1, 0, " "] for x in range(newCanvasSize[0])] for y in range(newCanvasSize[1])] newMap = [[[1, 1, 0, " "] for x in range(newCanvasSize[0])] for y in range(newCanvasSize[1])]
return (True, "", newMap, None, newCanvasSize) return (True, "", newMap, None, newCanvasSize)
if self._promptSaveChanges(): if self._promptSaveChanges():
@ -335,7 +335,7 @@ class GuiCanvasInterface():
self.update(toolName=self.currentTool.name) self.update(toolName=self.currentTool.name)
viewRect = self.parentCanvas.GetViewStart() viewRect = self.parentCanvas.GetViewStart()
eventDc = self.parentCanvas.backend.getDeviceContext(self.parentCanvas, viewRect) eventDc = self.parentCanvas.backend.getDeviceContext(self.parentCanvas, viewRect)
self.parentCanvas.applyTool(eventDc, wx.wxEVT_MOTION, None, None, self.parentCanvas.brushPos, False, False, False, self.currentTool, viewRect) self.parentCanvas.applyTool(eventDc, True, None, None, self.parentCanvas.brushPos, False, False, False, self.currentTool, viewRect)
setattr(canvasTool_, "attrDict", f.attrList[idx]) setattr(canvasTool_, "attrDict", f.attrList[idx])
setattr(canvasTool_, "isSelect", True) setattr(canvasTool_, "isSelect", True)
return canvasTool_ return canvasTool_

View File

@ -104,8 +104,8 @@ class GuiCanvasWxBackend():
self._lastBrushBg, self._lastBrushFg, self._lastPen = None, None, None self._lastBrushBg, self._lastBrushFg, self._lastPen = None, None, None
return eventDc return eventDc
# }}} # }}}
# {{{ onPanelPaintEvent(self, canvasSize, cellSize, clientSize, panelWindow, viewRect) # {{{ onPaintEvent(self, canvasSize, cellSize, clientSize, panelWindow, viewRect)
def onPanelPaintEvent(self, canvasSize, cellSize, clientSize, panelWindow, viewRect): def onPaintEvent(self, canvasSize, cellSize, clientSize, panelWindow, viewRect):
if self.canvasBitmap != None: if self.canvasBitmap != None:
if viewRect == (0, 0): if viewRect == (0, 0):
eventDc = wx.BufferedPaintDC(panelWindow, self.canvasBitmap) eventDc = wx.BufferedPaintDC(panelWindow, self.canvasBitmap)

View File

@ -4,13 +4,7 @@
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de> # Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
# #
from Canvas import Canvas import os, sys, wx
from GuiCanvasColours import Colours
from GuiCanvasPanel import GuiCanvasPanel
from GuiCanvasWxBackend import GuiCanvasWxBackend
from glob import glob
import os, random, sys, wx
# #
# Non-items (0xf000-0xffff) # Non-items (0xf000-0xffff)
@ -18,8 +12,15 @@ NID_MENU_SEP = 0xf000
NID_TOOLBAR_HSEP = 0xf001 NID_TOOLBAR_HSEP = 0xf001
class GuiFrame(wx.Frame): class GuiFrame(wx.Frame):
# {{{ _initAccelTable(self, accels) # {{{ _initIcon(self, iconPathName)
def _initAccelTable(self, accels): def _initIcon(self, iconPathName):
icon = wx.Icon()
icon.CopyFromBitmap(wx.Bitmap(iconPathName, wx.BITMAP_TYPE_ANY))
self.SetIcon(icon)
# }}}
# {{{ loadAccels(self, accels)
def loadAccels(self, accels):
accelTableEntries = [] accelTableEntries = []
for accel in accels: for accel in accels:
if accel.attrDict["accel"] != None: if accel.attrDict["accel"] != None:
@ -29,17 +30,25 @@ class GuiFrame(wx.Frame):
accelTableEntries[-1].Set(*accel.attrDict["accel"], accel.attrDict["id"]) accelTableEntries[-1].Set(*accel.attrDict["accel"], accel.attrDict["id"])
accel.attrDict["accelEntry"] = accelTableEntries[-1] accel.attrDict["accelEntry"] = accelTableEntries[-1]
self.itemsById[accel.attrDict["id"]] = accel self.itemsById[accel.attrDict["id"]] = accel
self.Bind(wx.EVT_MENU, self.onInput, id=accel.attrDict["id"]) self.Bind(wx.EVT_MENU, self.onMenu, id=accel.attrDict["id"])
self.SetAcceleratorTable(wx.AcceleratorTable(accelTableEntries)) self.SetAcceleratorTable(wx.AcceleratorTable(accelTableEntries))
# }}} # }}}
# {{{ _initIcon(self) # {{{ loadBitmap(self, basePathName, descr, size=(16, 16))
def _initIcon(self): def loadBitmap(self, basePathName, descr, size=(16, 16)):
iconPathNames = glob(os.path.join("assets", "images", "logo*.bmp")) if descr == None:
iconPathName = iconPathNames[random.randint(0, len(iconPathNames) - 1)] descr = ["", None, wx.ArtProvider.GetBitmap(wx.ART_HELP, wx.ART_TOOLBAR, size)]
icon = wx.Icon(); icon.CopyFromBitmap(wx.Bitmap(iconPathName, wx.BITMAP_TYPE_ANY)); self.SetIcon(icon); elif (descr[0] == "") and (descr[1] != None):
descr = ["", None, wx.ArtProvider.GetBitmap(descr[1], wx.ART_TOOLBAR, size)]
elif descr[0] != "":
bitmap, bitmapPathName = wx.Bitmap((16, 16)), os.path.join(basePathName, descr[0])
bitmap.LoadFile(bitmapPathName, wx.BITMAP_TYPE_ANY)
descr = ["", None, bitmap]
elif len(descr) == 3:
descr = ("", None, descr[2])
return descr
# }}} # }}}
# {{{ _initMenus(self, menus) # {{{ loadMenus(self, menus)
def _initMenus(self, menus): def loadMenus(self, menus):
menuBar = wx.MenuBar() menuBar = wx.MenuBar()
for menu in menus: for menu in menus:
menuWindow = wx.Menu() menuWindow = wx.Menu()
@ -57,7 +66,7 @@ class GuiFrame(wx.Frame):
if menuItem.attrDict["accel"] != None: if menuItem.attrDict["accel"] != None:
menuItemWindow.SetAccel(menuItem.attrDict["accelEntry"]) menuItemWindow.SetAccel(menuItem.attrDict["accelEntry"])
self.menuItemsById[menuItem.attrDict["id"]] = menuItemWindow self.menuItemsById[menuItem.attrDict["id"]] = menuItemWindow
self.Bind(wx.EVT_MENU, self.onInput, menuItemWindow) self.Bind(wx.EVT_MENU, self.onMenu, menuItemWindow)
if menuItem.attrDict["initialState"] != None: if menuItem.attrDict["initialState"] != None:
if hasattr(menuItem, "isSelect"): if hasattr(menuItem, "isSelect"):
menuItemWindow.Check(menuItem.attrDict["initialState"]) menuItemWindow.Check(menuItem.attrDict["initialState"])
@ -66,14 +75,10 @@ class GuiFrame(wx.Frame):
menuBar.Append(menuWindow, menu[0]) menuBar.Append(menuWindow, menu[0])
self.SetMenuBar(menuBar) self.SetMenuBar(menuBar)
# }}} # }}}
# {{{ _initStatusBar(self) # {{{ loadToolBars(self, toolBars)
def _initStatusBar(self): def loadToolBars(self, toolBars):
self.statusBar = self.CreateStatusBar()
# }}}
# {{{ _initToolBars(self, toolBars, panelSkin)
def _initToolBars(self, toolBars, panelSkin):
for toolBar in toolBars: for toolBar in toolBars:
self.toolBars.append(wx.ToolBar(panelSkin, -1, style=wx.TB_FLAT | wx.HORIZONTAL | wx.TB_NODIVIDER)) self.toolBars.append(wx.ToolBar(self.panelSkin, -1, style=wx.TB_FLAT | wx.HORIZONTAL | wx.TB_NODIVIDER))
self.toolBars[-1].SetToolBitmapSize((16, 16)) self.toolBars[-1].SetToolBitmapSize((16, 16))
for toolBarItem in toolBar: for toolBarItem in toolBar:
if toolBarItem == NID_TOOLBAR_HSEP: if toolBarItem == NID_TOOLBAR_HSEP:
@ -87,8 +92,8 @@ class GuiFrame(wx.Frame):
else: else:
toolBarItemWindow = self.toolBars[-1].AddTool(toolBarItem.attrDict["id"], toolBarItem.attrDict["caption"], toolBarItem.attrDict["icon"][2], shortHelp=toolBarItem.attrDict["label"]) toolBarItemWindow = self.toolBars[-1].AddTool(toolBarItem.attrDict["id"], toolBarItem.attrDict["caption"], toolBarItem.attrDict["icon"][2], shortHelp=toolBarItem.attrDict["label"])
self.toolBarItemsById[toolBarItem.attrDict["id"]] = toolBarItemWindow self.toolBarItemsById[toolBarItem.attrDict["id"]] = toolBarItemWindow
self.Bind(wx.EVT_TOOL, self.onInput, toolBarItemWindow) self.Bind(wx.EVT_TOOL, self.onMenu, toolBarItemWindow)
self.Bind(wx.EVT_TOOL_RCLICKED, self.onInput, toolBarItemWindow) self.Bind(wx.EVT_TOOL_RCLICKED, self.onMenu, toolBarItemWindow)
if toolBarItem.attrDict["initialState"] != None: if toolBarItem.attrDict["initialState"] != None:
if hasattr(toolBarItem, "isSelect"): if hasattr(toolBarItem, "isSelect"):
toolBarItemWindow.Toggle(toolBarItem.attrDict["initialState"]) toolBarItemWindow.Toggle(toolBarItem.attrDict["initialState"])
@ -98,77 +103,34 @@ class GuiFrame(wx.Frame):
self.sizerSkin.Add(toolBar, 0, wx.ALIGN_LEFT | wx.ALL, 3) self.sizerSkin.Add(toolBar, 0, wx.ALIGN_LEFT | wx.ALL, 3)
toolBar.Realize(); toolBar.Fit(); toolBar.Realize(); toolBar.Fit();
# }}} # }}}
# {{{ _initToolBitmaps(self, toolBars) # {{{ addWindow(self, window, border=14, expand=False)
def _initToolBitmaps(self, toolBars): def addWindow(self, window, border=14, expand=False):
for toolBar in toolBars: flags = wx.ALL; flags = flags | wx.EXPAND if expand else flags;
for toolBarItem in toolBar: self.sizerSkin.Add(window, 0, flags, border); self.sizerSkin.Fit(self.panelSkin);
if toolBarItem == NID_TOOLBAR_HSEP:
continue
elif toolBarItem.attrDict["icon"] == None:
toolBarItem.attrDict["icon"] = ["", None, wx.ArtProvider.GetBitmap(wx.ART_HELP, wx.ART_TOOLBAR, (16, 16))]
elif (toolBarItem.attrDict["icon"][0] == "") \
and (toolBarItem.attrDict["icon"][1] != None):
toolBarItem.attrDict["icon"] = ["", None, wx.ArtProvider.GetBitmap(toolBarItem.attrDict["icon"][1], wx.ART_TOOLBAR, (16, 16))]
elif (toolBarItem.attrDict["icon"][0] == "") \
and (toolBarItem.attrDict["icon"][1] == None):
toolBarItem.attrDict["icon"] = ["", None, toolBarItem.attrDict["icon"][2]]
elif toolBarItem.attrDict["icon"][0] != "":
toolBitmapPathName = os.path.dirname(sys.argv[0])
toolBitmapPathName = os.path.join(toolBitmapPathName, "assets", "images", toolBarItem.attrDict["icon"][0])
toolBitmap = wx.Bitmap((16, 16))
toolBitmap.LoadFile(toolBitmapPathName, wx.BITMAP_TYPE_ANY)
toolBarItem.attrDict["icon"] = ["", None, toolBitmap]
# }}} # }}}
# {{{ onChar(self, event) # {{{ onChar(self, event)
def onChar(self, event): def onChar(self, event):
self.canvasPanel.onPanelInput(event) event.Skip()
# }}} # }}}
# {{{ onInput(self, event) # {{{ onMenu(self, event)
def onInput(self, event): def onMenu(self, event):
eventId = event.GetId(); self.itemsById[eventId](self.canvasPanel.interface, event); event.Skip()
# }}} # }}}
# {{{ onMouseWheel(self, event) # {{{ onMouseWheel(self, event)
def onMouseWheel(self, event): def onMouseWheel(self, event):
self.canvasPanel.GetEventHandler().ProcessEvent(event) event.Skip()
# }}} # }}}
# #
# __init__(self, canvasInterface, parent, appSize=(840, 630), defaultCanvasPos=(0, 75), defaultCanvasSize=(100, 30), defaultCellSize=(7, 14)): initialisation method # __init__(self, iconPathName, size, parent=None, title=""): initialisation method
def __init__(self, canvasInterface, parent, appSize=(840, 630), defaultCanvasPos=(0, 75), defaultCanvasSize=(100, 30), defaultCellSize=(7, 14)): def __init__(self, iconPathName, size, parent=None, title=""):
super().__init__(parent, wx.ID_ANY, "", size=appSize) super().__init__(parent, wx.ID_ANY, title, size=size)
self.itemsById, self.menuItemsById, self.toolBarItemsById = {}, {}, {}; self.lastId = 0; self.itemsById, self.lastId, self.menuItemsById, self.toolBarItemsById = {}, 0, {}, {}
self.panelSkin, self.sizerSkin, self.toolBars = wx.Panel(self, wx.ID_ANY), wx.BoxSizer(wx.VERTICAL), [] self.panelSkin, self.sizerSkin, self.toolBars = wx.Panel(self, wx.ID_ANY), wx.BoxSizer(wx.VERTICAL), []
self.sizerSkin.AddSpacer(5); self.panelSkin.SetSizer(self.sizerSkin); self.panelSkin.SetAutoLayout(1);
self.canvas, self.canvasPanel = Canvas(defaultCanvasSize), None self._initIcon(iconPathName); self.statusBar = self.CreateStatusBar();
self.canvasPanel = GuiCanvasPanel(self.panelSkin, self, GuiCanvasWxBackend, self.canvas, defaultCanvasPos, defaultCanvasSize, defaultCellSize, canvasInterface) self.sizerSkin.Fit(self.panelSkin); self.SetFocus(); self.Show(True);
for event, f in ((wx.EVT_CHAR, self.onChar), (wx.EVT_MENU, self.onMenu), (wx.EVT_MOUSEWHEEL, self.onMouseWheel)):
# Initialise accelerators (hotkeys) self.Bind(event, f)
# Initialise icon
# Initialise menu bar, menus & menu items
# Initialise status bar
# Initialise toolbar & toolbar items
self._initAccelTable(self.canvasPanel.interface.accels)
self._initIcon()
self._initMenus(self.canvasPanel.interface.menus)
self._initStatusBar()
self._initToolBitmaps(self.canvasPanel.interface.toolBars)
self._initToolBars(self.canvasPanel.interface.toolBars, self.panelSkin)
self.sizerSkin.AddSpacer(5)
self.sizerSkin.Add(self.canvasPanel, 0, wx.ALL | wx.EXPAND, 14)
self.panelSkin.SetSizer(self.sizerSkin)
self.panelSkin.SetAutoLayout(1)
self.sizerSkin.Fit(self.panelSkin)
self.canvasPanel.interface.canvasNew(None)
self.canvasPanel.interface.canvasTool(self.canvasPanel.interface.canvasTool, 5)(self.canvasPanel.interface, None)
self.canvasPanel.interface.update(brushSize=self.canvasPanel.brushSize, colours=self.canvasPanel.brushColours)
self.Bind(wx.EVT_CHAR, self.onChar)
self.Bind(wx.EVT_MOUSEWHEEL, self.onMouseWheel)
# Set focus on & show window
self.SetFocus(); self.Show(True);
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120

70
libgui/GuiWindow.py Normal file
View File

@ -0,0 +1,70 @@
#!/usr/bin/env python3
#
# GuiWindow.py
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
#
import wx
class GuiWindow(wx.ScrolledWindow):
# {{{ _updateScrollBars(self)
def _updateScrollBars(self):
clientSize = self.GetClientSize()
if (self.size[0] > clientSize[0]) or (self.size[1] > clientSize[1]):
self.scrollFlag = True; super().SetVirtualSize(self.size);
elif self.scrollFlag \
and ((self.size[0] <= clientSize[0]) or (self.size[1] <= clientSize[1])):
self.scrollFlag = False; super().SetVirtualSize((0, 0));
# }}}
# {{{ onClose(self, event)
def onClose(self, event):
self.Destroy()
# }}}
# {{{ onKeyboardInput(self, event)
def onKeyboardInput(self, event):
return False
# }}}
# {{{ onLeaveWindow(self, event)
def onLeaveWindow(self, event):
event.Skip()
# }}}
# {{{ onMouseInput(self, event)
def onMouseInput(self, event):
return False
# }}}
# {{{ onPaint(self, event)
def onPaint(self, event):
event.Skip()
# }}}
# {{{ onScroll(self, event)
def onScroll(self, event):
event.Skip()
# }}}
# {{{ onSize(self, event)
def onSize(self, event):
self._updateScrollBars(); event.Skip();
# }}}
# {{{ resize(self, newSize)
def resize(self, newSize):
self.size = newSize; self._updateScrollBars();
self.SetMinSize(self.size); self.SetSize(wx.DefaultCoord, wx.DefaultCoord, *self.size);
curWindow = self
while curWindow != None:
curWindow.Layout(); curWindow = curWindow.GetParent();
# }}}
#
# __init__(self, parent, pos, scrollStep, size): initialisation method
def __init__(self, parent, pos, scrollStep, size):
super().__init__(parent, pos=pos, size=size)
self.pos, self.scrollFlag, self.scrollStep, self.size = pos, False, scrollStep, size
for eventType, f in (
(wx.EVT_CHAR, self.onKeyboardInput), (wx.EVT_CLOSE, self.onClose), (wx.EVT_LEAVE_WINDOW, self.onLeaveWindow),
(wx.EVT_LEFT_DOWN, self.onMouseInput), (wx.EVT_MOTION, self.onMouseInput), (wx.EVT_PAINT, self.onPaint),
(wx.EVT_RIGHT_DOWN, self.onMouseInput), (wx.EVT_SCROLLWIN_LINEDOWN, self.onScroll), (wx.EVT_SCROLLWIN_LINEUP, self.onScroll),
(wx.EVT_SIZE, self.onSize)):
self.Bind(eventType, f)
self.SetScrollRate(*self.scrollStep); self._updateScrollBars();
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120

View File

@ -1,12 +1,12 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# #
# GuiCanvasPanel.py # RoarCanvasWindow.py
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de> # Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
# #
import wx from GuiWindow import GuiWindow
class GuiCanvasPanel(wx.ScrolledWindow): class RoarCanvasWindow(GuiWindow):
# {{{ _drawPatch(self, eventDc, isCursor, patch, viewRect) # {{{ _drawPatch(self, eventDc, isCursor, patch, viewRect)
def _drawPatch(self, eventDc, isCursor, patch, viewRect): def _drawPatch(self, eventDc, isCursor, patch, viewRect):
if not self.canvas.dirtyCursor: if not self.canvas.dirtyCursor:
@ -16,35 +16,24 @@ class GuiCanvasPanel(wx.ScrolledWindow):
patchDeltaCell = self.canvas.map[patch[1]][patch[0]]; patchDelta = [*patch[0:2], *patchDeltaCell]; patchDeltaCell = self.canvas.map[patch[1]][patch[0]]; patchDelta = [*patch[0:2], *patchDeltaCell];
self.canvas.journal.pushCursor(patchDelta) self.canvas.journal.pushCursor(patchDelta)
# }}} # }}}
# {{{ _updateScrollBars(self)
def _updateScrollBars(self):
clientSize = self.GetClientSize()
if (self.winSize[0] > clientSize[0]) \
or (self.winSize[1] > clientSize[1]):
self.scrollFlag = True; super().SetVirtualSize(self.winSize);
elif self.scrollFlag \
and ((self.winSize[0] <= clientSize[0]) \
or (self.winSize[1] <= clientSize[1])):
self.scrollFlag = False; super().SetVirtualSize((0, 0));
# }}}
# {{{ applyTool(self, eventDc, eventType, keyChar, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, tool, viewRect) # {{{ applyTool(self, eventDc, eventMouse, keyChar, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, tool, viewRect)
def applyTool(self, eventDc, eventType, keyChar, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, tool, viewRect): def applyTool(self, eventDc, eventMouse, keyChar, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, tool, viewRect):
dirty, self.canvas.dirtyCursor, rc = False, False, False dirty, self.canvas.dirtyCursor, rc = False, False, False
self.canvas.journal.begin() self.canvas.journal.begin()
if eventType == wx.wxEVT_CHAR: if eventMouse:
rc, dirty = tool.onKeyboardEvent(self.brushColours, self.brushSize, self.dispatchPatchSingle, eventDc, keyChar, keyModifiers, self.brushPos, viewRect)
else:
if (mapPoint[0] < self.canvas.size[0]) \ if (mapPoint[0] < self.canvas.size[0]) \
and (mapPoint[1] < self.canvas.size[1]): and (mapPoint[1] < self.canvas.size[1]):
self.brushPos = mapPoint self.brushPos = mapPoint
rc, dirty = tool.onMouseEvent(self.brushColours, self.brushSize, self.dispatchPatchSingle, eventDc, self.brushPos, mouseDragging, mouseLeftDown, mouseRightDown, viewRect) rc, dirty = tool.onMouseEvent(self.brushColours, self.brushSize, self.dispatchPatchSingle, eventDc, self.brushPos, mouseDragging, mouseLeftDown, mouseRightDown, viewRect)
else:
rc, dirty = tool.onKeyboardEvent(self.brushColours, self.brushSize, self.dispatchPatchSingle, eventDc, keyChar, keyModifiers, self.brushPos, viewRect)
if dirty: if dirty:
self.dirty = True self.dirty = True
self.interface.update(dirty=self.dirty, cellPos=self.brushPos, undoLevel=self.canvas.journal.patchesUndoLevel) self.interface.update(dirty=self.dirty, cellPos=self.brushPos, undoLevel=self.canvas.journal.patchesUndoLevel)
else:
self.interface.update(cellPos=mapPoint if mapPoint else self.brushPos)
self.canvas.journal.end() self.canvas.journal.end()
if eventType == wx.wxEVT_MOTION:
self.interface.update(cellPos=mapPoint)
return rc return rc
# }}} # }}}
# {{{ dispatchDeltaPatches(self, deltaPatches) # {{{ dispatchDeltaPatches(self, deltaPatches)
@ -73,11 +62,7 @@ class GuiCanvasPanel(wx.ScrolledWindow):
oldSize = [0, 0] if self.canvas.map == None else self.canvas.size oldSize = [0, 0] if self.canvas.map == None else self.canvas.size
deltaSize = [b - a for a, b in zip(oldSize, newSize)] deltaSize = [b - a for a, b in zip(oldSize, newSize)]
if self.canvas.resize(newSize, commitUndo): if self.canvas.resize(newSize, commitUndo):
self.winSize = [a * b for a, b in zip(newSize, self.backend.cellSize)]; self._updateScrollBars(); super().resize([a * b for a, b in zip(newSize, self.backend.cellSize)])
self.SetMinSize(self.winSize); self.SetSize(wx.DefaultCoord, wx.DefaultCoord, *self.winSize);
curWindow = self
while curWindow != None:
curWindow.Layout(); curWindow = curWindow.GetParent();
self.backend.resize(newSize, self.backend.cellSize) self.backend.resize(newSize, self.backend.cellSize)
viewRect = self.GetViewStart(); eventDc = self.backend.getDeviceContext(self, viewRect); viewRect = self.GetViewStart(); eventDc = self.backend.getDeviceContext(self, viewRect);
if deltaSize[0] > 0: if deltaSize[0] > 0:
@ -100,39 +85,32 @@ class GuiCanvasPanel(wx.ScrolledWindow):
self.backend.drawPatch(eventDc, [numCol, numRow, *self.canvas.map[numRow][numCol]], self.GetViewStart()) self.backend.drawPatch(eventDc, [numCol, numRow, *self.canvas.map[numRow][numCol]], self.GetViewStart())
# }}} # }}}
# {{{ onPanelClose(self, event) # {{{ onKeyboardInput(self, event)
def onPanelClose(self, event): def onKeyboardInput(self, event):
self.Destroy() viewRect = self.GetViewStart(); eventDc = self.backend.getDeviceContext(self, viewRect);
# }}}
# {{{ onPanelInput(self, event)
def onPanelInput(self, event):
eventType, viewRect = event.GetEventType(), self.GetViewStart()
eventDc = self.backend.getDeviceContext(self, viewRect)
if eventType == wx.wxEVT_CHAR:
keyChar, keyModifiers = chr(event.GetUnicodeKey()), event.GetModifiers() keyChar, keyModifiers = chr(event.GetUnicodeKey()), event.GetModifiers()
mapPoint, mouseDragging, mouseLeftDown, mouseRightDown = None, None, None, None if not self.applyTool(eventDc, False, keyChar, keyModifiers, None, None, None, None, self.interface.currentTool, viewRect):
else:
keyChar, keyModifiers = None, None
mouseDragging, mouseLeftDown, mouseRightDown = event.Dragging(), event.LeftIsDown(), event.RightIsDown()
mapPoint = self.backend.xlateEventPoint(event, eventDc, viewRect)
if not self.applyTool(eventDc, eventType, keyChar, keyModifiers, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, self.interface.currentTool, self.GetViewStart()):
event.Skip() event.Skip()
# }}} # }}}
# {{{ onPanelLeaveWindow(self, event) # {{{ onLeaveWindow(self, event)
def onPanelLeaveWindow(self, event): def onLeaveWindow(self, event):
eventDc = self.backend.getDeviceContext(self, self.GetViewStart()) eventDc = self.backend.getDeviceContext(self, self.GetViewStart())
self.backend.drawCursorMaskWithJournal(self.canvas.journal, eventDc, self.GetViewStart()) self.backend.drawCursorMaskWithJournal(self.canvas.journal, eventDc, self.GetViewStart())
# }}} # }}}
# {{{ onPanelPaint(self, event) # {{{ onMouseInput(self, event)
def onPanelPaint(self, event): def onMouseInput(self, event):
self.backend.onPanelPaintEvent(self.canvas.size, self.defaultCellSize, self.GetClientSize(), self, self.GetViewStart()) viewRect = self.GetViewStart(); eventDc = self.backend.getDeviceContext(self, viewRect);
mouseDragging, mouseLeftDown, mouseRightDown = event.Dragging(), event.LeftIsDown(), event.RightIsDown()
mapPoint = self.backend.xlateEventPoint(event, eventDc, viewRect)
if not self.applyTool(eventDc, True, None, None, mapPoint, mouseDragging, mouseLeftDown, mouseRightDown, self.interface.currentTool, viewRect):
event.Skip()
# }}} # }}}
# {{{ onPanelSize(self, event) # {{{ onPaint(self, event)
def onPanelSize(self, event): def onPaint(self, event):
self._updateScrollBars(); event.Skip(); self.backend.onPaintEvent(self.canvas.size, self.cellSize, self.GetClientSize(), self, self.GetViewStart())
# }}} # }}}
# {{{ onPanelScroll(self, event) # {{{ onScroll(self, event)
def onPanelScroll(self, event): def onScroll(self, event):
if self.canvas.dirtyCursor: if self.canvas.dirtyCursor:
viewRect = self.GetViewStart() viewRect = self.GetViewStart()
eventDc = self.backend.getDeviceContext(self, viewRect) eventDc = self.backend.getDeviceContext(self, viewRect)
@ -142,24 +120,10 @@ class GuiCanvasPanel(wx.ScrolledWindow):
# }}} # }}}
# #
# __init__(self, parent, parentFrame, backend, canvas, defaultCanvasPos, defaultCanvasSize, defaultCellSize, interface): initialisation method # __init__(self, backend, canvas, cellSize, interface, parent, parentFrame, pos, scrollStep, size): initialisation method
def __init__(self, parent, parentFrame, backend, canvas, defaultCanvasPos, defaultCanvasSize, defaultCellSize, interface): def __init__(self, backend, canvas, cellSize, interface, parent, parentFrame, pos, scrollStep, size):
self.winSize = [w * h for w, h in zip(defaultCanvasSize, defaultCellSize)] super().__init__(parent, pos, scrollStep, [w * h for w, h in zip(cellSize, size)])
super().__init__(parent, pos=defaultCanvasPos, size=self.winSize) self.backend, self.canvas, self.cellSize, self.interface, self.parentFrame = backend(self.size, cellSize), canvas, cellSize, interface(self, parentFrame), parentFrame
self.backend, self.interface = backend(defaultCanvasSize, defaultCellSize), interface(self, parentFrame) self.brushColours, self.brushPos, self.brushSize, self.dirty = [4, 1], [0, 0], [1, 1], False
self.brushColours, self.brushPos, self.brushSize = [4, 1], [0, 0], [1, 1]
self.canvas, self.canvasPos, self.defaultCanvasPos, self.defaultCanvasSize, self.defaultCellSize = canvas, defaultCanvasPos, defaultCanvasPos, defaultCanvasSize, defaultCellSize
self.dirty, self.parentFrame, self.scrollFlag = False, parentFrame, False
self.SetScrollRate(*defaultCellSize); self._updateScrollBars();
self.Bind(wx.EVT_CLOSE, self.onPanelClose)
self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelLeaveWindow)
self.Bind(wx.EVT_CHAR, self.onPanelInput)
for eventType in (wx.EVT_LEFT_DOWN, wx.EVT_MOTION, wx.EVT_RIGHT_DOWN):
self.Bind(eventType, self.onPanelInput)
self.Bind(wx.EVT_PAINT, self.onPanelPaint)
self.Bind(wx.EVT_SCROLLWIN_LINEDOWN, self.onPanelScroll)
self.Bind(wx.EVT_SCROLLWIN_LINEUP, self.onPanelScroll)
self.Bind(wx.EVT_SIZE, self.onPanelSize)
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120 # vim:expandtab foldmethod=marker sw=4 ts=4 tw=120

59
libroar/RoarClient.py Normal file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env python3
#
# RoarClient.py
# Copyright (c) 2018, 2019 Lucio Andrés Illanes Albornoz <lucio@lucioillanes.de>
#
from Canvas import Canvas
from GuiCanvasInterface import GuiCanvasInterface
from GuiCanvasWxBackend import GuiCanvasWxBackend
from GuiFrame import GuiFrame, NID_TOOLBAR_HSEP
from RoarCanvasWindow import RoarCanvasWindow
from glob import glob
import os, random, sys
class RoarClient(GuiFrame):
# {{{ _getIconPathName(self)
def _getIconPathName(self):
iconPathNames = glob(os.path.join("assets", "images", "logo*.bmp"))
return iconPathNames[random.randint(0, len(iconPathNames) - 1)]
# }}}
# {{{ _initToolBitmaps(self, toolBars)
def _initToolBitmaps(self, toolBars):
basePathName = os.path.join(os.path.dirname(sys.argv[0]), "assets", "images")
for toolBar in toolBars:
for toolBarItem in [i for i in toolBar if i != NID_TOOLBAR_HSEP]:
toolBarItem.attrDict["icon"] = self.loadBitmap(basePathName, toolBarItem.attrDict["icon"])
# }}}
# {{{ onChar(self, event)
def onChar(self, event):
self.canvasPanel.onKeyboardInput(event)
# }}}
# {{{ onMenu(self, event)
def onMenu(self, event):
eventId = event.GetId(); self.itemsById[eventId](self.canvasPanel.interface, event);
# }}}
# {{{ onMouseWheel(self, event)
def onMouseWheel(self, event):
self.canvasPanel.GetEventHandler().ProcessEvent(event)
# }}}
#
# __init__(self, parent, defaultCanvasPos=(0, 75), defaultCanvasSize=(100, 30), defaultCellSize=(7, 14), size=(840, 630), title=""): initialisation method
def __init__(self, parent, defaultCanvasPos=(0, 75), defaultCanvasSize=(100, 30), defaultCellSize=(7, 14), size=(840, 630), title=""):
super().__init__(self._getIconPathName(), size, parent, title)
self.canvas = Canvas(defaultCanvasSize)
self.canvasPanel = RoarCanvasWindow(GuiCanvasWxBackend, self.canvas, defaultCellSize, GuiCanvasInterface, self.panelSkin, self, defaultCanvasPos, defaultCellSize, defaultCanvasSize)
self.loadAccels(self.canvasPanel.interface.accels)
self.loadMenus(self.canvasPanel.interface.menus)
self._initToolBitmaps(self.canvasPanel.interface.toolBars)
self.loadToolBars(self.canvasPanel.interface.toolBars)
self.canvasPanel.interface.canvasNew(None)
self.canvasPanel.interface.canvasTool(self.canvasPanel.interface.canvasTool, 5)(self.canvasPanel.interface, None)
self.canvasPanel.interface.update(brushSize=self.canvasPanel.brushSize, colours=self.canvasPanel.brushColours)
self.addWindow(self.canvasPanel, expand=True)
# vim:expandtab foldmethod=marker sw=4 ts=4 tw=120

18
roar.py
View File

@ -6,23 +6,21 @@
import os, sys import os, sys
[sys.path.append(os.path.join(os.getcwd(), path)) for path in \ [sys.path.append(os.path.join(os.getcwd(), path)) for path in \
["libcanvas", "libgui", "librtl", "libtools"]] ["libcanvas", "libgui", "libroar", "librtl", "libtools"]]
from GuiCanvasInterface import GuiCanvasInterface from RoarClient import RoarClient
from GuiFrame import GuiFrame
import wx import wx
# #
# Entry point # Entry point
def main(*argv): def main(*argv):
wxApp, appFrame = wx.App(False), GuiFrame(GuiCanvasInterface, None) wxApp, roarClient = wx.App(False), RoarClient(None)
if len(argv) > 1 \ if (len(argv) > 1) and (len(argv[1]) > 0):
and len(argv[1]) > 0: roarClient.canvasPanel.interface.canvasPathName = argv[1]
appFrame.canvasPanel.interface.canvasPathName = argv[1] rc, error = roarClient.canvasPanel.canvas.importStore.importTextFile(argv[1])
rc, error = appFrame.canvasPanel.canvas.importStore.importTextFile(argv[1])
if rc: if rc:
appFrame.canvasPanel.update(appFrame.canvasPanel.canvas.importStore.inSize, False, appFrame.canvasPanel.canvas.importStore.outMap) roarClient.canvasPanel.update(roarClient.canvasPanel.canvas.importStore.inSize, False, roarClient.canvasPanel.canvas.importStore.outMap)
appFrame.canvasPanel.interface.update(pathName=argv[1], undoLevel=-1) roarClient.canvasPanel.interface.update(pathName=argv[1], undoLevel=-1)
else: else:
print("error: {}".format(error), file=sys.stderr) print("error: {}".format(error), file=sys.stderr)
wxApp.MainLoop() wxApp.MainLoop()