Skip to content

Commit

Permalink
Merge pull request #4955 from wiktor-obrebski/refactor/widgets
Browse files Browse the repository at this point in the history
Refactor/widgets
  • Loading branch information
myk002 authored Sep 28, 2024
2 parents d60b196 + 0badb26 commit 7c417a4
Show file tree
Hide file tree
Showing 25 changed files with 3,524 additions and 3,333 deletions.
3,365 changes: 32 additions & 3,333 deletions library/lua/gui/widgets.lua

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions library/lua/gui/widgets/buttons/button_group.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
local CycleHotkeyLabel = require('gui.widgets.labels.cycle_hotkey_label')
local Label = require('gui.widgets.labels.label')

-----------------
-- ButtonGroup --
-----------------

---@class widgets.ButtonGroup.attrs: widgets.CycleHotkeyLabel.attrs

---@class widgets.ButtonGroup.initTable: widgets.ButtonGroup.attrs
---@field button_specs widgets.ButtonLabelSpec[]
---@field button_specs_selected widgets.ButtonLabelSpec[]

---@class widgets.ButtonGroup: widgets.ButtonGroup.attrs, widgets.CycleHotkeyLabel
---@field super widgets.CycleHotkeyLabel
---@overload fun(init_table: widgets.ButtonGroup.initTable): self
ButtonGroup = defclass(ButtonGroup, CycleHotkeyLabel)

ButtonGroup.ATTRS{
auto_height=false,
}

function ButtonGroup:init(info)
ensure_key(self, 'frame').h = #info.button_specs[1].chars + 2

local start = 0
for idx in ipairs(self.options) do
local opt_val = self:getOptionValue(idx)
local width = #info.button_specs[idx].chars[1]
self:addviews{
Label{
frame={t=2, l=start, w=width},
text=Label.makeButtonLabelText(info.button_specs[idx]),
on_click=function() self:setOption(opt_val, true) end,
visible=function() return self:getOptionValue() ~= opt_val end,
},
Label{
frame={t=2, l=start, w=width},
text=Label.makeButtonLabelText(info.button_specs_selected[idx]),
on_click=function() self:setOption(opt_val, false) end,
visible=function() return self:getOptionValue() == opt_val end,
},
}
start = start + width
end
end

return ButtonGroup
53 changes: 53 additions & 0 deletions library/lua/gui/widgets/buttons/configure_button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local textures = require('gui.textures')
local Panel = require('gui.widgets.containers.panel')
local Label = require('gui.widgets.labels.label')

local to_pen = dfhack.pen.parse

local button_pen_left = to_pen{fg=COLOR_CYAN,
tile=curry(textures.tp_control_panel, 7) or nil, ch=string.byte('[')}
local button_pen_right = to_pen{fg=COLOR_CYAN,
tile=curry(textures.tp_control_panel, 8) or nil, ch=string.byte(']')}
local configure_pen_center = to_pen{
tile=curry(textures.tp_control_panel, 10) or nil, ch=15} -- gear/masterwork symbol

---------------------
-- ConfigureButton --
---------------------

---@class widgets.ConfigureButton.attrs: widgets.Panel.attrs
---@field on_click? function

---@class widgets.ConfigureButton.attrs.partial: widgets.ConfigureButton.attrs

---@class widgets.ConfigureButton: widgets.Panel, widgets.ConfigureButton.attrs
---@field super widgets.Panel
---@field ATTRS widgets.ConfigureButton.attrs|fun(attributes: widgets.ConfigureButton.attrs.partial)
---@overload fun(init_table: widgets.ConfigureButton.attrs.partial): self
ConfigureButton = defclass(ConfigureButton, Panel)

ConfigureButton.ATTRS{
on_click=DEFAULT_NIL,
}

function ConfigureButton:preinit(init_table)
init_table.frame = init_table.frame or {}
init_table.frame.h = init_table.frame.h or 1
init_table.frame.w = init_table.frame.w or 3
end

function ConfigureButton:init()
self:addviews{
Label{
frame={t=0, l=0, w=3, h=1},
text={
{tile=button_pen_left},
{tile=configure_pen_center},
{tile=button_pen_right},
},
on_click=self.on_click,
},
}
end

return ConfigureButton
53 changes: 53 additions & 0 deletions library/lua/gui/widgets/buttons/help_button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
local textures = require('gui.textures')
local Panel = require('gui.widgets.containers.panel')
local Label = require('gui.widgets.labels.label')

local to_pen = dfhack.pen.parse

----------------
-- HelpButton --
----------------

---@class widgets.HelpButton.attrs: widgets.Panel.attrs
---@field command? string

---@class widgets.HelpButton.attrs.partial: widgets.HelpButton.attrs

---@class widgets.HelpButton: widgets.Panel, widgets.HelpButton.attrs
---@field super widgets.Panel
---@field ATTRS widgets.HelpButton.attrs|fun(attributes: widgets.HelpButton.attrs.partial)
---@overload fun(init_table: widgets.HelpButton.attrs.partial): self
HelpButton = defclass(HelpButton, Panel)

HelpButton.ATTRS{
frame={t=0, r=1, w=3, h=1},
command=DEFAULT_NIL,
}

local button_pen_left = to_pen{fg=COLOR_CYAN,
tile=curry(textures.tp_control_panel, 7) or nil, ch=string.byte('[')}
local button_pen_right = to_pen{fg=COLOR_CYAN,
tile=curry(textures.tp_control_panel, 8) or nil, ch=string.byte(']')}
local help_pen_center = to_pen{
tile=curry(textures.tp_control_panel, 9) or nil, ch=string.byte('?')}

function HelpButton:init()
self.frame.w = self.frame.w or 3
self.frame.h = self.frame.h or 1

local command = self.command .. ' '

self:addviews{
Label{
frame={t=0, l=0, w=3, h=1},
text={
{tile=button_pen_left},
{tile=help_pen_center},
{tile=button_pen_right},
},
on_click=function() dfhack.run_command('gui/launcher', command) end,
},
}
end

return HelpButton
42 changes: 42 additions & 0 deletions library/lua/gui/widgets/buttons/text_button.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local BannerPanel = require('gui.widgets.containers.banner_panel')
local HotkeyLabel = require('gui.widgets.labels.hotkey_label')

----------------
-- TextButton --
----------------

---@class widgets.TextButton.initTable: widgets.Panel.attrs.partial, widgets.HotkeyLabel.attrs.partial

---@class widgets.TextButton: widgets.BannerPanel
---@field super widgets.BannerPanel
---@overload fun(init_table: widgets.TextButton.initTable): self
TextButton = defclass(TextButton, BannerPanel)

---@param info widgets.TextButton.initTable
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{self.label}
end

function TextButton:setLabel(label)
self.label:setLabel(label)
end

return TextButton
20 changes: 20 additions & 0 deletions library/lua/gui/widgets/containers/banner_panel.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
local Panel = require('gui.widgets.containers.panel')

-----------------
-- BannerPanel --
-----------------

---@class widgets.BannerPanel: widgets.Panel
---@field super widgets.Panel
BannerPanel = defclass(BannerPanel, Panel)

---@param dc gui.Painter
function BannerPanel:onRenderBody(dc)
dc:pen(COLOR_RED)
for y=0,self.frame_rect.height-1 do
dc:seek(0, y):char('[')
dc:seek(self.frame_rect.width-1):char(']')
end
end

return BannerPanel
63 changes: 63 additions & 0 deletions library/lua/gui/widgets/containers/pages.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local gui = require('gui')
local utils = require('utils')
local Panel = require('gui.widgets.containers.panel')

---@param view gui.View
---@param vis boolean
local function show_view(view,vis)
if view then
view.visible = vis
end
end

-----------
-- Pages --
-----------

---@class widgets.Pages.initTable: widgets.Panel.attrs
---@field selected? integer|string

---@class widgets.Pages: widgets.Panel
---@field super widgets.Panel
---@overload fun(attributes: widgets.Pages.initTable): self
Pages = defclass(Pages, Panel)

---@param self widgets.Pages
---@param args widgets.Pages.initTable
function Pages:init(args)
for _,v in ipairs(self.subviews) do
v.visible = false
end
self:setSelected(args.selected or 1)
end

---@param idx integer|string
function Pages:setSelected(idx)
if type(idx) ~= 'number' then
local key = idx
if type(idx) == 'string' then
key = self.subviews[key]
end
idx = utils.linear_index(self.subviews, key)
if not idx then
error('Unknown page: '..tostring(key))
end
end

show_view(self.subviews[self.selected], false)
self.selected = math.min(math.max(1, idx), #self.subviews)
show_view(self.subviews[self.selected], true)
end

---@return integer index
---@return gui.View child
function Pages:getSelected()
return self.selected, self.subviews[self.selected]
end

---@return gui.View child
function Pages:getSelectedPage()
return self.subviews[self.selected]
end

return Pages
Loading

0 comments on commit 7c417a4

Please sign in to comment.