From c43fe19f1d82eee4b39cb64ef7ce3522da0041f4 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Fri, 12 Jul 2019 12:47:51 -0600 Subject: [PATCH] sliders in forms + form component setValue (hopefully didnt break anything) --- sys/modules/opus/ui/components/Form.lua | 9 ++--- sys/modules/opus/ui/components/Slider.lua | 35 ++++++++++++++++++-- sys/modules/opus/ui/components/TextEntry.lua | 4 +-- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/sys/modules/opus/ui/components/Form.lua b/sys/modules/opus/ui/components/Form.lua index f1eb6f3..68b59d3 100644 --- a/sys/modules/opus/ui/components/Form.lua +++ b/sys/modules/opus/ui/components/Form.lua @@ -29,10 +29,11 @@ function UI.Form:setValues(values) self.values = values for _,child in pairs(self.children) do if child.formKey then - -- this should be child:setValue(self.values[child.formKey]) - -- so chooser can set default choice if null - -- null should be valid as well - child.value = self.values[child.formKey] or '' + if child.setValue then + child:setValue(self.values[child.formKey]) + else + child.value = self.values[child.formKey] or '' + end end end end diff --git a/sys/modules/opus/ui/components/Slider.lua b/sys/modules/opus/ui/components/Slider.lua index 5966160..9dee765 100644 --- a/sys/modules/opus/ui/components/Slider.lua +++ b/sys/modules/opus/ui/components/Slider.lua @@ -12,13 +12,31 @@ UI.Slider.defaults = { barColor = colors.gray, sliderChar = UI.extChars and '\143' or '\124', sliderColor = colors.blue, - leftBorder = '\141', - rightBorder = '\142', + sliderFocusColor = colors.lightBlue, + leftBorder = UI.extChars and '\141' or '\124', + rightBorder = UI.extChars and '\142' or '\124', value = 0, min = 0, max = 100, event = 'slider_update', + accelerators = { + right = 'slide_right', + left = 'slide_left', + } } +function UI.Slider:setValue(value) + self.value = tonumber(value) or self.min +end + +function UI.Slider:reset() -- form support + self.value = self.min + self:draw() +end + +function UI.Slider:focus() + self:draw() +end + function UI.Slider:draw() local range = self.max - self.min local perc = (self.value - self.min) / range @@ -34,7 +52,7 @@ function UI.Slider:draw() table.insert(bar, filler) end self:write(1, 1, table.concat(bar), nil, self.barColor) - self:write(progress, 1, self.sliderChar, nil, self.sliderColor) + self:write(progress, 1, self.sliderChar, nil, self.focused and self.sliderFocusColor or self.sliderColor) end function UI.Slider:eventHandler(event) @@ -44,5 +62,16 @@ function UI.Slider:eventHandler(event) self.value = self.min + (i * range) self:emit({ type = self.event, value = self.value, element = self }) self:draw() + + elseif event.type == 'slide_left' or event.type == 'slide_right' then + local range = self.max - self.min + local step = range / self.width + if event.type == 'slide_left' then + self.value = Util.clamp(self.value - step, self.min, self.max) + else + self.value = Util.clamp(self.value + step, self.min, self.max) + end + self:emit({ type = self.event, value = self.value, element = self }) + self:draw() end end diff --git a/sys/modules/opus/ui/components/TextEntry.lua b/sys/modules/opus/ui/components/TextEntry.lua index 09631ef..151dd2b 100644 --- a/sys/modules/opus/ui/components/TextEntry.lua +++ b/sys/modules/opus/ui/components/TextEntry.lua @@ -25,7 +25,7 @@ UI.TextEntry.defaults = { } } function UI.TextEntry:postInit() - self.value = tostring(self.value) + self.value = tostring(self.value) -- is this right ? shouldnt raw numbers be allowed self.entry = entry({ limit = self.limit, offset = 2 }) end @@ -35,7 +35,7 @@ function UI.TextEntry:layout() end function UI.TextEntry:setValue(value) - self.value = value + self.value = value or '' self.entry:unmark() self.entry.value = tostring(value) self.entry:updateScroll()