From 8bff1399d43a1abae98533a2667dec20c340e91e Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 7 Aug 2023 17:02:55 -0700 Subject: [PATCH] add functions to Panel in addition to attributes --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 20 +++++++++++++++++--- library/lua/gui/widgets.lua | 25 ++++++++++++++++++++----- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 1f9574ea8..d48be6be6 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -71,6 +71,7 @@ Template for new versions: - ``new()``: improved error handling so that certain errors that were previously uncatchable (creating objects with members with unknown vtables) are now catchable with ``pcall()`` - ``dfhack.items.getValue()``: remove ``caravan_buying`` param as per C++ API change - ``widgets.BannerPanel``: panel with distinctive border for marking DFHack UI elements on otherwise vanilla screens +- ``widgets.Panel``: new functions to override instead of setting corresponding properties (useful when subclassing instead of just setting attributes): ``onDragBegin``, ``onDragEnd``, ``onResizeBegin``, ``onResizeEnd`` ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 970aa06a9..d8383ac00 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4559,7 +4559,7 @@ Has attributes: * ``drag_anchors = {}`` (default: ``{title=true, frame=false/true, body=true}``) * ``drag_bound = 'frame' or 'body'`` (default: ``'frame'``) * ``on_drag_begin = function()`` (default: ``nil``) -* ``on_drag_end = function(bool)`` (default: ``nil``) +* ``on_drag_end = function(success, new_frame)`` (default: ``nil``) If ``draggable`` is set to ``true``, then the above attributes come into play when the panel is dragged around the screen, either with the mouse or the @@ -4573,13 +4573,15 @@ Has attributes: otherwise. Dragging can be canceled by right clicking while dragging with the mouse, hitting :kbd:`Esc` (while dragging with the mouse or keyboard), or by calling ``Panel:setKeyboaredDragEnabled(false)`` (while dragging with the - keyboard). + keyboard). If it is more convenient to do so, you can choose to override the + ``panel:onDragBegin`` and/or the ``panel:onDragEnd`` methods instead of + setting the ``on_drag_begin`` and/or ``on_drag_end`` attributes. * ``resizable = bool`` (default: ``false``) * ``resize_anchors = {}`` (default: ``{t=false, l=true, r=true, b=true}`` * ``resize_min = {}`` (default: w and h from the ``frame``, or ``{w=5, h=5}``) * ``on_resize_begin = function()`` (default: ``nil``) -* ``on_resize_end = function(bool)`` (default: ``nil``) +* ``on_resize_end = function(success, new_frame)`` (default: ``nil``) If ``resizable`` is set to ``true``, then the player can click the mouse on any edge specified in ``resize_anchors`` and drag the border to resize the @@ -4593,6 +4595,9 @@ Has attributes: Dragging can be canceled by right clicking while resizing with the mouse, hitting :kbd:`Esc` (while resizing with the mouse or keyboard), or by calling ``Panel:setKeyboardResizeEnabled(false)`` (while resizing with the keyboard). + If it is more convenient to do so, you can choose to override the + ``panel:onResizeBegin`` and/or the ``panel:onResizeEnd`` methods instead of + setting the ``on_resize_begin`` and/or ``on_resize_end`` attributes. * ``autoarrange_subviews = bool`` (default: ``false``) * ``autoarrange_gap = int`` (default: ``0``) @@ -4637,6 +4642,15 @@ Has functions: commit the new window size or :kbd:`Esc` to cancel. If resizing is canceled, then the window size from before the resize operation is restored. +* ``panel:onDragBegin()`` +* ``panel:onDragEnd(success, new_frame)`` +* ``panel:onResizeBegin()`` +* ``panel:onResizeEnd(success, new_frame)`` + +The default implementations of these methods call the associated attribute (if +set). You can override them in a subclass if that is more convenient than +setting the attributes. + Double clicking: If the panel is resizable and the user double-clicks on the top edge (the frame diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index b742f6dff..52a3a6352 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -6,7 +6,6 @@ local gui = require('gui') local guidm = require('gui.dwarfmode') local utils = require('utils') -local dscreen = dfhack.screen local getval = utils.getval local to_pen = dfhack.pen.parse @@ -223,9 +222,9 @@ local function Panel_begin_drag(self, drag_offset, resize_edge) self.prev_focus_owner = self.focus_group.cur self:setFocus(true) if self.resize_edge then - if self.on_resize_begin then self.on_resize_begin(success) end + self:onResizeBegin() else - if self.on_drag_begin then self.on_drag_begin(success) end + self:onDragBegin() end end @@ -239,9 +238,9 @@ local function Panel_end_drag(self, frame, success) local resize_edge = self.resize_edge Panel_update_frame(self, frame, true) if resize_edge then - if self.on_resize_end then self.on_resize_end(success) end + self:onResizeEnd(success, self.frame) else - if self.on_drag_end then self.on_drag_end(success) end + self:onDragEnd(success, self.frame) end end @@ -495,6 +494,22 @@ function Panel:onRenderFrame(dc, rect) end end +function Panel:onDragBegin() + if self.on_drag_begin then self.on_drag_begin() end +end + +function Panel:onDragEnd(success, new_frame) + if self.on_drag_end then self.on_drag_end(success, new_frame) end +end + +function Panel:onResizeBegin() + if self.on_resize_begin then self.on_resize_begin() end +end + +function Panel:onResizeEnd(success, new_frame) + if self.on_resize_end then self.on_resize_end(success, new_frame) end +end + ------------ -- Window -- ------------