From 2d2ecf6f0dc0d737ff22a9c3d4b411324ad85e0d Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Sun, 16 Jul 2023 20:24:39 -0700 Subject: [PATCH] add TextButton --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 12 ++++++++-- library/lua/gui/widgets.lua | 46 +++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index 163306650..e45273a25 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -65,6 +65,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - ``dfhack.items.markForTrade``: mark items for trade - ``dfhack.items.isRequestedTradeGood``: discover whether an item is named in a trade agreement with an active caravan - ``dfhack.items.getValue``: gained optional ``caravan`` and ``caravan_buying`` parameters for prices that take trader races and agreements into account +- ``widgets.TextButton``: wraps a ``HotkeyLabel`` and decorates it to look more like a button ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 954ea952d..b52bf3c76 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -5063,13 +5063,21 @@ The CycleHotkeyLabel widget implements the following methods: selected option if no index is given. If an option was defined as just a string, then this function will return ``nil`` for that option. -ToggleHotkeyLabel ------------------ +ToggleHotkeyLabel class +----------------------- 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. +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. + List class ---------- diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index b034ed1e8..74d193025 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1489,6 +1489,52 @@ function HotkeyLabel:onInput(keys) end end +---------------- +-- TextButton -- +---------------- + +TextButton = defclass(TextButton, Panel) + +local BUTTON_PEN = dfhack.pen.parse{fg=COLOR_YELLOW, bg=COLOR_RED} + +function TextButton:init(info) + self.label = HotkeyLabel{ + frame={t=0, l=1, r=1}, + key=info.key, + key_sep=info.key_sep, + label=info.label, + on_activate=info.on_activate, + text_pen=info.text_pen, + text_dpen=info.text_dpen, + text_hpen=info.text_hpen, + disabled=info.disabled, + enabled=info.enabled, + auto_height=info.auto_height, + auto_width=info.auto_width, + on_click=info.on_click, + on_rclick=info.on_rclick, + 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, + } + } +end + +function TextButton:setLabel(label) + self.label:setLabel(label) +end + ---------------------- -- CycleHotkeyLabel -- ----------------------