From 8c166935d58d21ac00bc37066b63e51f6305ed0d Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Mon, 7 Aug 2023 12:40:48 -0700 Subject: [PATCH] factor banner out into reusable panel class --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 16 ++++++++++++---- library/lua/gui/widgets.lua | 34 ++++++++++++++++++---------------- plugins/lua/zone.lua | 4 ++-- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 8d5e18467..db25afecd 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -69,6 +69,7 @@ Template for new versions: ## Lua - ``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 ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index dbd837e25..970aa06a9 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -5077,13 +5077,21 @@ This is a specialized subclass of CycleHotkeyLabel that has two options: ``On`` (with a value of ``true``) and ``Off`` (with a value of ``false``). The ``On`` option is rendered in green. +BannerPanel class +----------------- + +This is a Panel subclass that prints a distinctive banner along the far left +and right columns of the widget frame. Note that this is not a "proper" frame +since it doesn't have top or bottom borders. Subviews of this panel should +inset their frames one tile from the left and right edges. + TextButton class ---------------- -This is a Panel subclass that wraps a HotkeyLabel with some decorators on the -sides to make it look more like a button, suitable for both graphics and ASCII -mdoes. All HotkeyLabel parameters passed to the constructor are passed through -to the wrapped HotkeyLabel. +This is a BannerPanel subclass that wraps a HotkeyLabel with some decorators on +the sides to make it look more like a button, suitable for both graphics and +ASCII modes. All HotkeyLabel parameters passed to the constructor are passed +through to the wrapped HotkeyLabel. List class ---------- diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index e1b71d186..6623211e4 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1489,13 +1489,27 @@ function HotkeyLabel:onInput(keys) end end +----------------- +-- BannerPanel -- +----------------- + +BannerPanel = defclass(BannerPanel, Panel) + +local BANNER_PEN = dfhack.pen.parse{fg=COLOR_YELLOW, bg=COLOR_RED} + +function BannerPanel:onRenderBody(dc) + dc:pen(BANNER_PEN) + for y=0,self.frame_rect.height-1 do + dc:seek(0, y):char(string.char(221)) -- half-width stripe on left + dc:seek(self.frame_rect.width-1):char(string.char(222)) -- half-width stripe on right + end +end + ---------------- -- TextButton -- ---------------- -TextButton = defclass(TextButton, Panel) - -local BUTTON_PEN = dfhack.pen.parse{fg=COLOR_YELLOW, bg=COLOR_RED} +TextButton = defclass(TextButton, BannerPanel) function TextButton:init(info) self.label = HotkeyLabel{ @@ -1516,19 +1530,7 @@ function TextButton:init(info) scroll_keys=info.scroll_keys, } - self:addviews{ - Label{ - frame={t=0, l=0, w=1}, - text=string.char(221), -- half-width stripe on left - text_pen=BUTTON_PEN, - }, - self.label, - Label{ - frame={t=0, r=0, w=1}, - text=string.char(222), -- half-width stripe on right - text_pen=BUTTON_PEN, - } - } + self:addviews{self.label} end function TextButton:setLabel(label) diff --git a/plugins/lua/zone.lua b/plugins/lua/zone.lua index 3c70c1e63..46cae3d24 100644 --- a/plugins/lua/zone.lua +++ b/plugins/lua/zone.lua @@ -694,13 +694,13 @@ PastureOverlay.ATTRS{ function PastureOverlay:init() self:addviews{ widgets.TextButton{ - frame={t=0, l=0}, + frame={t=0, l=0, r=0, h=1}, label='DFHack manage pasture', key='CUSTOM_CTRL_T', on_activate=function() view = view and view:raise() or PastureScreen{}:show() end, }, widgets.TextButton{ - frame={t=2, l=0}, + frame={t=2, l=0, r=0, h=1}, label='DFHack autobutcher', key='CUSTOM_CTRL_B', on_activate=function() dfhack.run_script('gui/autobutcher') end,