add simple slider component

This commit is contained in:
Insality 2019-09-26 00:27:27 +03:00
parent 7917a08f5c
commit 6cebc9d0c9
4 changed files with 158 additions and 4 deletions

82
druid/base/slider.lua Normal file
View File

@ -0,0 +1,82 @@
--- Druid slider component
-- @module base.slider
local helper = require("druid.helper")
local const = require("druid.const")
local M = {}
M.interest = {
const.ON_SWIPE
}
local function on_change_value(self)
if self.callback then
self.callback(self.parent.parent, self.value)
end
end
function M.init(self, node, end_pos, callback)
self.node = helper.get_node(node)
self.start_pos = gui.get_position(self.node)
self.pos = gui.get_position(self.node)
self.target_pos = self.pos
self.end_pos = end_pos
self.dist = self.end_pos - self.start_pos
self.is_drag = false
self.value = 0
self.callback = callback
assert(self.dist.x == 0 or self.dist.y == 0, "Slider for now can be only vertical or horizontal")
end
function M.on_input(self, action_id, action)
if action_id ~= const.ACTION_TOUCH then
return false
end
if gui.pick_node(self.node, action.x, action.y) then
if action.pressed then
self.pos = gui.get_position(self.node)
self.is_drag = true
end
end
if self.is_drag and not action.pressed then
-- move
self.pos.x = self.pos.x + action.dx
self.pos.y = self.pos.y + action.dy
local prev_x = self.target_pos.x
local prev_y = self.target_pos.y
self.target_pos.x = helper.clamp(self.pos.x, self.start_pos.x, self.end_pos.x)
self.target_pos.y = helper.clamp(self.pos.y, self.start_pos.y, self.end_pos.y)
gui.set_position(self.node, self.target_pos)
if prev_x ~= self.target_pos.x or prev_y ~= self.target_pos.y then
if self.dist.x > 0 then
self.value = (self.target_pos.x - self.start_pos.x) / self.dist.x
end
if self.dist.y > 0 then
self.value = (self.target_pos.y - self.start_pos.y) / self.dist.y
end
on_change_value(self)
end
end
if action.released then
self.is_drag = false
end
end
return M

View File

@ -22,6 +22,7 @@ M.comps = {
progress = require("druid.base.progress"),
grid = require("druid.base.grid"),
scroll = require("druid.base.scroll"),
slider = require("druid.base.slider"),
checkbox = require("druid.base.checkbox"),
progress_rich = require("druid.rich.progress_rich"),

View File

@ -1570,6 +1570,69 @@ nodes {
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
nodes {
position {
x: 0.0
y: 20.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 0.7
y: 0.7
z: 1.0
w: 1.0
}
size {
x: 100.0
y: 60.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_TEXT
blend_mode: BLEND_MODE_ALPHA
text: "50%"
font: "game"
id: "text_progress_slider"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
outline {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
adjust_mode: ADJUST_MODE_FIT
line_break: false
parent: "slider_back"
layer: ""
inherit_alpha: true
alpha: 1.0
outline_alpha: 1.0
shadow_alpha: 0.0
template_node_child: false
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 0.0

View File

@ -11,7 +11,7 @@ end
local function random_progress(progress, text)
local rnd = math.random()
gui.set_text(text, math.ceil(rnd * 100))
gui.set_text(text, math.ceil(rnd * 100) .. "%")
progress:to(rnd)
end
@ -38,10 +38,10 @@ end
local function setup_progress(self)
local progress = self.druid:new_progress("progress_fill", "x", 0.4)
random_progress(progress, gui.get_node("text_progress"))
self.progress = self.druid:new_progress("progress_fill", "x", 0.4)
random_progress(self.progress, gui.get_node("text_progress"))
timer.delay(2, true, function()
random_progress(progress, gui.get_node("text_progress_amount"))
random_progress(self.progress, gui.get_node("text_progress_amount"))
end)
end
@ -62,6 +62,13 @@ local function setup_grid(self)
end
local function setup_slider(self)
self.druid:new_slider("slider_pin", vmath.vector3(95, 0, 0), function(_, value)
gui.set_text(gui.get_node("text_progress_slider"), math.ceil(value * 100) .. "%")
end)
end
local function setup_checkbox(self)
self.druid:new_checkbox("radio1/check", nil, "radio1/back")
self.druid:new_checkbox("radio2/check", nil, "radio2/back")
@ -97,6 +104,7 @@ function M.setup_page(self)
setup_timer(self)
setup_checkbox(self)
setup_scroll(self)
setup_slider(self)
setup_back_handler(self)
end