Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Widget: Slider #5208

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Template for new versions:
- ``dfhack.units.setAutomaticProfessions``: sets unit labors according to current work detail settings
- ``dfhack.military.removeFromSquad``: Lua API for ``Military::removeFromSquad``
- ``gui.dwarfmode``: adventure mode cursor now supported in ``getCursorPos``, ``setCursorPos``, and ``clearCursorPos`` funcitons
- ``widgets.Slider``: new mouse-controlled single-headed slider widget

## Removed

Expand Down
16 changes: 15 additions & 1 deletion docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6452,10 +6452,24 @@ change, the ``RangeSlider`` appearance will adjust automatically.
:get_left_idx_fn: The function used by the RangeSlider to get the notch index on which
to display the left handle.
:get_right_idx_fn: The function used by the RangeSlider to get the notch index on which
to display the right handle.
to display the right handle.
:on_left_change: Callback executed when moving the left handle.
:on_right_change: Callback executed when moving the right handle.

Slider class
-----------------

This widget implements a mouse-interactable slider. The player can move the handle to
set the value of the slider. The parent widget owns the slider value, and can control
it independently (e.g., with ``CycleHotkeyLabels``). If the value changes, the ``Slider``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
it independently (e.g., with ``CycleHotkeyLabels``). If the value changes, the ``Slider``
it independently (e.g., with a ``CycleHotkeyLabel``). If the value changes, the ``Slider``

appearance will adjust automatically.

:num_stops: Used to specify the number of "notches" in the slider, the places
where the handle can stop. (This should match the parents' number of options.)
:get_idx_fn: The function used by the Slider to get the notch index on which
to display the handle.
:on_change: Callback executed when moving the handle.

DimensionsTooltip class
-----------------------

Expand Down
1 change: 1 addition & 0 deletions library/lua/gui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ List = require('gui.widgets.list')
FilteredList = require('gui.widgets.filtered_list')
TabBar = require('gui.widgets.tab_bar')
RangeSlider = require('gui.widgets.range_slider')
Slider = require('gui.widgets.slider')
DimensionsTooltip = require('gui.widgets.dimensions_tooltip')
TextArea = require('gui.widgets.text_area')

Expand Down
35 changes: 24 additions & 11 deletions library/lua/gui/widgets/range_slider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ RangeSlider.ATTRS{
get_right_idx_fn=DEFAULT_NIL,
on_left_change=DEFAULT_NIL,
on_right_change=DEFAULT_NIL,
is_single=DEFAULT_NIL
}

function RangeSlider:preinit(init_table)
Expand All @@ -43,7 +44,8 @@ function RangeSlider:init()
end

local function rangeslider_get_width_per_idx(self)
return math.max(5, (self.frame_body.width-7) // (self.num_stops-1))
local min_value = (self.is_single) and 3 or 5 -- Single slider = 3, else 5
return math.max(min_value, (self.frame_body.width-7) // (self.num_stops-1))
end

function RangeSlider:onInput(keys)
Expand Down Expand Up @@ -142,16 +144,27 @@ function RangeSlider:onRenderBody(dc, rect)
end
dc:char(nil, SLIDER_TRACK)
dc:char(nil, SLIDER_RIGHT_END)
-- draw tabs
dc:seek(width_per_idx*(left_idx-1))
dc:char(nil, SLIDER_TAB_LEFT)
dc:char(nil, SLIDER_TAB_CENTER)
dc:char(nil, SLIDER_TAB_RIGHT)
dc:seek(width_per_idx*(right_idx-1)+4)
dc:char(nil, SLIDER_TAB_LEFT)
dc:char(nil, SLIDER_TAB_CENTER)
dc:char(nil, SLIDER_TAB_RIGHT)
-- manage dragging

-- Draw tab(s)
if self.is_single then
-- Single slider: Draw one centered tab
dc:seek(width_per_idx * (left_idx-1) + 2) -- Center the tab
dc:char(nil, SLIDER_TAB_LEFT)
dc:char(nil, SLIDER_TAB_CENTER)
dc:char(nil, SLIDER_TAB_RIGHT)
else
-- Dual slider: Draw left and right tabs separately
dc:seek(width_per_idx * (left_idx-1))
dc:char(nil, SLIDER_TAB_LEFT)
dc:char(nil, SLIDER_TAB_CENTER)
dc:char(nil, SLIDER_TAB_RIGHT)
dc:seek(width_per_idx*(right_idx-1)+4)
dc:char(nil, SLIDER_TAB_LEFT)
dc:char(nil, SLIDER_TAB_CENTER)
dc:char(nil, SLIDER_TAB_RIGHT)
end

-- Manage dragging
if self.is_dragging_target then
rangeslider_do_drag(self, width_per_idx)
end
Expand Down
22 changes: 22 additions & 0 deletions library/lua/gui/widgets/slider.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
-- slider.lua
local RangeSlider = require('gui.widgets.range_slider')

local Slider = defclass(Slider, RangeSlider)
Slider.ATTRS {
get_idx_fn = DEFAULT_NIL, -- Function to get the current index
on_change = DEFAULT_NIL, -- Function to handle index change
is_single = true
}

function Slider:init()
self.get_left_idx_fn = self.get_idx_fn
self.get_right_idx_fn = self.get_idx_fn
self.on_left_change = function(index)
if self.on_change then self.on_change(index) end
end
self.on_right_change = function(index)
if self.on_change then self.on_change(index) end
end
end

return Slider