allow color options for CycleHotkeyLabel

and use the option to render `On` in green for ToggleHotkeyLabel
develop
Myk Taylor 2023-01-06 11:34:15 -08:00
parent 18e17e4e36
commit e111a73763
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
3 changed files with 22 additions and 13 deletions

@ -54,6 +54,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
- ``gui.View``: ``visible`` and ``active`` can now be functions that return a boolean - ``gui.View``: ``visible`` and ``active`` can now be functions that return a boolean
- ``widgets.Panel``: new attributes to control window dragging and resizing with mouse or keyboard - ``widgets.Panel``: new attributes to control window dragging and resizing with mouse or keyboard
- ``widgets.Window``: Panel subclass with attributes preset for top-level windows - ``widgets.Window``: Panel subclass with attributes preset for top-level windows
- ``widgets.CycleHotkeyLabel``: now supports rendering option labels in the color of your choice
- ``widgets.ToggleHotkeyLabel``: now renders the ``On`` option in green text
- `overlay`: ``OverlayWidget`` now inherits from ``Panel`` instead of ``Widget`` to get all the frame and mouse integration goodies - `overlay`: ``OverlayWidget`` now inherits from ``Panel`` instead of ``Widget`` to get all the frame and mouse integration goodies
- ``dfhack.gui.getDFViewscreen()``: returns the topmost underlying DF viewscreen - ``dfhack.gui.getDFViewscreen()``: returns the topmost underlying DF viewscreen

@ -4608,8 +4608,10 @@ It has the following attributes:
hotkey. hotkey.
:label_width: The number of spaces to allocate to the ``label`` (for use in :label_width: The number of spaces to allocate to the ``label`` (for use in
aligning a column of ``CycleHotkeyLabel`` labels). aligning a column of ``CycleHotkeyLabel`` labels).
:options: A list of strings or tables of ``{label=string, value=string}``. :options: A list of strings or tables of
String options use the same string for the label and value. ``{label=string, value=string[, pen=pen]}``. String options use the same
string for the label and value and the default pen. The optional ``pen``
element could be a color like ``COLOR_RED``.
:initial_option: The value or numeric index of the initial option. :initial_option: The value or numeric index of the initial option.
:on_change: The callback to call when the selected option changes. It is called :on_change: The callback to call when the selected option changes. It is called
as ``on_change(new_option_value, old_option_value)``. as ``on_change(new_option_value, old_option_value)``.
@ -4637,7 +4639,8 @@ ToggleHotkeyLabel
----------------- -----------------
This is a specialized subclass of CycleHotkeyLabel that has two options: This is a specialized subclass of CycleHotkeyLabel that has two options:
``On`` (with a value of ``true``) and ``Off`` (with a value of ``false``). ``On`` (with a value of ``true``) and ``Off`` (with a value of ``false``). The
``On`` option is rendered in green.
List class List class
---------- ----------

@ -1416,7 +1416,8 @@ function CycleHotkeyLabel:init()
{key=self.key, key_sep=': ', text=self.label, width=self.label_width, {key=self.key, key_sep=': ', text=self.label, width=self.label_width,
on_activate=self:callback('cycle')}, on_activate=self:callback('cycle')},
' ', ' ',
{text=self:callback('getOptionLabel')}, {text=self:callback('getOptionLabel'),
pen=self:callback('getOptionPen')},
} }
end end
@ -1433,22 +1434,25 @@ function CycleHotkeyLabel:cycle()
end end
end end
function CycleHotkeyLabel:getOptionLabel(option_idx) local function cyclehotkeylabel_getOptionElem(self, option_idx, key)
option_idx = option_idx or self.option_idx option_idx = option_idx or self.option_idx
local option = self.options[option_idx] local option = self.options[option_idx]
if type(option) == 'table' then if type(option) == 'table' then
return option.label return option[key]
end end
return option return option
end end
function CycleHotkeyLabel:getOptionLabel(option_idx)
return cyclehotkeylabel_getOptionElem(self, option_idx, 'label')
end
function CycleHotkeyLabel:getOptionValue(option_idx) function CycleHotkeyLabel:getOptionValue(option_idx)
option_idx = option_idx or self.option_idx return cyclehotkeylabel_getOptionElem(self, option_idx, 'value')
local option = self.options[option_idx]
if type(option) == 'table' then
return option.value
end end
return option
function CycleHotkeyLabel:getOptionPen(option_idx)
return cyclehotkeylabel_getOptionElem(self, option_idx, 'pen')
end end
function CycleHotkeyLabel:onInput(keys) function CycleHotkeyLabel:onInput(keys)
@ -1466,7 +1470,7 @@ end
ToggleHotkeyLabel = defclass(ToggleHotkeyLabel, CycleHotkeyLabel) ToggleHotkeyLabel = defclass(ToggleHotkeyLabel, CycleHotkeyLabel)
ToggleHotkeyLabel.ATTRS{ ToggleHotkeyLabel.ATTRS{
options={{label='On', value=true}, options={{label='On', value=true, pen=COLOR_GREEN},
{label='Off', value=false}}, {label='Off', value=false}},
} }