renamed Slider to RangeSlider, and added draft of Lua API.rst entry

develop
Taxi Service 2023-04-20 13:24:47 +02:00
parent a0b259bb67
commit 8371aa0b8b
4 changed files with 26 additions and 15 deletions

@ -49,7 +49,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## API ## API
## Lua ## Lua
- ``widgets``: "Slider" class has been moved from ``filterselection.lua`` into ``widgets.lua`` - ``widgets``: "Slider" class has been moved from ``filterselection.lua`` into ``widgets.lua`` and renamed to "RangeSlider"
## Removed ## Removed

@ -5130,6 +5130,17 @@ widget does not require direct usage of ``Tab``.
usage of ``Tab`` in ``TabBar:init()`` for an example. See the default value of ``active_tab_pens`` or ``inactive_tab_pens`` usage of ``Tab`` in ``TabBar:init()`` for an example. See the default value of ``active_tab_pens`` or ``inactive_tab_pens``
in ``TabBar`` for an example of how to construct pens. in ``TabBar`` for an example of how to construct pens.
RangeSlider class
-----------------
This widget implements a mouse-interactable range-slider. The user can move its two handles to set minimum and maximum values to define a range.
:num_stops: Specifies the amount of "notches" in the range slider, the places where handles can stop.
:get_left_idx_fn: The function used by the RangeSlider to determine what value to display on its left handle.
:get_right_idx_fn: The function used by the RangeSlider to determine what value to display on its right handle.
:on_left_change: Callback executed when a handle is moved leftwards.
:on_right_change: Callback executed when a handle is moved rightwards.
.. _lua-plugins: .. _lua-plugins:
======= =======

@ -2294,11 +2294,11 @@ function TabBar:onInput(keys)
end end
-------------------------------- --------------------------------
-- Slider -- RangeSlider
-- --
Slider = defclass(Slider, Widget) RangeSlider = defclass(RangeSlider, Widget)
Slider.ATTRS{ RangeSlider.ATTRS{
num_stops=DEFAULT_NIL, num_stops=DEFAULT_NIL,
get_left_idx_fn=DEFAULT_NIL, get_left_idx_fn=DEFAULT_NIL,
get_right_idx_fn=DEFAULT_NIL, get_right_idx_fn=DEFAULT_NIL,
@ -2306,27 +2306,27 @@ Slider.ATTRS{
on_right_change=DEFAULT_NIL, on_right_change=DEFAULT_NIL,
} }
function Slider:preinit(init_table) function RangeSlider:preinit(init_table)
init_table.frame = init_table.frame or {} init_table.frame = init_table.frame or {}
init_table.frame.h = init_table.frame.h or 1 init_table.frame.h = init_table.frame.h or 1
end end
function Slider:init() function RangeSlider:init()
if self.num_stops < 2 then error('too few Slider stops') end if self.num_stops < 2 then error('too few RangeSlider stops') end
self.is_dragging_target = nil -- 'left', 'right', or 'both' self.is_dragging_target = nil -- 'left', 'right', or 'both'
self.is_dragging_idx = nil -- offset from leftmost dragged tile self.is_dragging_idx = nil -- offset from leftmost dragged tile
end end
local function slider_get_width_per_idx(self) local function rangeslider_get_width_per_idx(self)
return math.max(5, (self.frame_body.width-7) // (self.num_stops-1)) return math.max(5, (self.frame_body.width-7) // (self.num_stops-1))
end end
function Slider:onInput(keys) function RangeSlider:onInput(keys)
if not keys._MOUSE_L_DOWN then return false end if not keys._MOUSE_L_DOWN then return false end
local x = self:getMousePos() local x = self:getMousePos()
if not x then return false end if not x then return false end
local left_idx, right_idx = self.get_left_idx_fn(), self.get_right_idx_fn() local left_idx, right_idx = self.get_left_idx_fn(), self.get_right_idx_fn()
local width_per_idx = slider_get_width_per_idx(self) local width_per_idx = rangeslider_get_width_per_idx(self)
local left_pos = width_per_idx*(left_idx-1) local left_pos = width_per_idx*(left_idx-1)
local right_pos = width_per_idx*(right_idx-1) + 4 local right_pos = width_per_idx*(right_idx-1) + 4
if x < left_pos then if x < left_pos then
@ -2346,7 +2346,7 @@ function Slider:onInput(keys)
return true return true
end end
local function slider_do_drag(self, width_per_idx) local function rangeslider_do_drag(self, width_per_idx)
local x = self.frame_body:localXY(dfhack.screen.getMousePos()) local x = self.frame_body:localXY(dfhack.screen.getMousePos())
local cur_pos = x - self.is_dragging_idx local cur_pos = x - self.is_dragging_idx
cur_pos = math.max(0, cur_pos) cur_pos = math.max(0, cur_pos)
@ -2383,9 +2383,9 @@ local SLIDER_TAB_LEFT = to_pen{ch=60, fg=COLOR_BLACK, bg=COLOR_YELLOW}
local SLIDER_TAB_CENTER = to_pen{ch=9, fg=COLOR_BLACK, bg=COLOR_YELLOW} local SLIDER_TAB_CENTER = to_pen{ch=9, fg=COLOR_BLACK, bg=COLOR_YELLOW}
local SLIDER_TAB_RIGHT = to_pen{ch=62, fg=COLOR_BLACK, bg=COLOR_YELLOW} local SLIDER_TAB_RIGHT = to_pen{ch=62, fg=COLOR_BLACK, bg=COLOR_YELLOW}
function Slider:onRenderBody(dc, rect) function RangeSlider:onRenderBody(dc, rect)
local left_idx, right_idx = self.get_left_idx_fn(), self.get_right_idx_fn() local left_idx, right_idx = self.get_left_idx_fn(), self.get_right_idx_fn()
local width_per_idx = slider_get_width_per_idx(self) local width_per_idx = rangeslider_get_width_per_idx(self)
-- draw track -- draw track
dc:seek(1,0) dc:seek(1,0)
dc:char(nil, SLIDER_LEFT_END) dc:char(nil, SLIDER_LEFT_END)
@ -2422,7 +2422,7 @@ function Slider:onRenderBody(dc, rect)
dc:char(nil, SLIDER_TAB_RIGHT) dc:char(nil, SLIDER_TAB_RIGHT)
-- manage dragging -- manage dragging
if self.is_dragging_target then if self.is_dragging_target then
slider_do_drag(self, width_per_idx) rangeslider_do_drag(self, width_per_idx)
end end
if df.global.enabler.mouse_lbut == 0 then if df.global.enabler.mouse_lbut == 0 then
self.is_dragging_target = nil self.is_dragging_target = nil

@ -191,7 +191,7 @@ function QualityAndMaterialsPage:init()
enabled=enable_item_quality, enabled=enable_item_quality,
on_change=function(val) self:set_max_quality(val+1) end, on_change=function(val) self:set_max_quality(val+1) end,
}, },
widgets.Slider{ widgets.RangeSlider{
frame={l=0, t=6}, frame={l=0, t=6},
num_stops=7, num_stops=7,
get_left_idx_fn=function() get_left_idx_fn=function()