Skip to content

Commit

Permalink
Merge pull request #3653 from myk002/myk_panel
Browse files Browse the repository at this point in the history
add functions to Panel in addition to attributes
  • Loading branch information
myk002 authored Aug 8, 2023
2 parents 3b67785 + 8bff139 commit 7b1bbec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 17 additions & 3 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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``)
Expand Down Expand Up @@ -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
Expand Down
25 changes: 20 additions & 5 deletions library/lua/gui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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 --
------------
Expand Down

0 comments on commit 7b1bbec

Please sign in to comment.