mirror of
https://github.com/Insality/druid
synced 2025-09-27 18:12:21 +02:00
More widgets stuff, cleaning code
This commit is contained in:
@@ -7,25 +7,46 @@ local SIZE_Y = hash("size.y")
|
||||
|
||||
|
||||
function M:init()
|
||||
self.druid = self:get_druid()
|
||||
self.root = self:get_node("root")
|
||||
self.container = self.druid:new_container(self.root)
|
||||
self.text_header = self.druid:new_text("text_header")
|
||||
self.text_value = self.druid:new_text("text_value")
|
||||
self.drag_corner = self.druid:new_drag("icon_drag", self.on_drag_corner)
|
||||
self.layout = self.druid:new_layout("panel_diagram", "horizontal")
|
||||
|
||||
self.druid:new_drag("header", self.on_drag_widget)
|
||||
self.druid:new_button("icon_drag", self.toggle_hide)
|
||||
:set_style(nil)
|
||||
|
||||
self.content = self:get_node("content")
|
||||
self.layout = self.druid:new_layout(self.content, "horizontal")
|
||||
:set_margin(0, 0)
|
||||
:set_padding(0, 0, 0, 0)
|
||||
|
||||
self.prefab_line = self:get_node("prefab_line")
|
||||
gui.set_enabled(self.prefab_line, false)
|
||||
|
||||
self.color_zero = color.hex2vector4("#8ED59E")
|
||||
self.color_one = color.hex2vector4("#F49B9B")
|
||||
local node_color_low = self:get_node("color_low")
|
||||
self.color_zero = gui.get_color(node_color_low)
|
||||
self.color_one = gui.get_color(self.prefab_line)
|
||||
gui.set_enabled(node_color_low, false)
|
||||
|
||||
self.is_hidden = false
|
||||
self.max_value = 1 -- in this value line will be at max height
|
||||
self.lines = {}
|
||||
self.values = {}
|
||||
self.samples = 64
|
||||
|
||||
self.container = self.druid:new_container(self.root)
|
||||
self.container:add_container("header")
|
||||
self.default_size = self.container:get_size()
|
||||
end
|
||||
|
||||
|
||||
function M:set_samples(samples)
|
||||
self.samples = samples
|
||||
self.layout:clear_layout()
|
||||
for index = 1, #self.lines do
|
||||
gui.delete_node(self.lines[index])
|
||||
end
|
||||
self.lines = {}
|
||||
|
||||
local line_width = self.layout:get_size().x / self.samples
|
||||
for index = 1, self.samples do
|
||||
local line = gui.clone(self.prefab_line)
|
||||
@@ -34,11 +55,6 @@ function M:init()
|
||||
self.layout:add(line)
|
||||
table.insert(self.lines, line)
|
||||
end
|
||||
|
||||
for index = 1, self.samples do
|
||||
local outsine = index/self.samples
|
||||
self:set_line_value(index, outsine)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -50,39 +66,78 @@ function M:set_line_value(index, value)
|
||||
return
|
||||
end
|
||||
|
||||
local target_color = color.lerp(value * value, self.color_zero, self.color_one)
|
||||
gui.set(line, SIZE_Y, value * 70)
|
||||
gui.set_color(line, target_color)
|
||||
|
||||
self.values[index] = value
|
||||
|
||||
local normalized = vmath.clamp(value/self.max_value, 0, 1)
|
||||
local target_color = color.lerp(normalized, self.color_zero, self.color_one)
|
||||
gui.set_color(line, target_color)
|
||||
self:set_line_height(index)
|
||||
|
||||
end
|
||||
|
||||
|
||||
---@return number
|
||||
function M:get_line_value(index)
|
||||
return self.values[index]
|
||||
return self.values[index] or 0
|
||||
end
|
||||
|
||||
|
||||
function M:push_line_value(value)
|
||||
for index = 1, self.samples - 1 do
|
||||
self:set_line_value(index, self:get_line_value(index + 1), true)
|
||||
self:set_line_value(index, self:get_line_value(index + 1))
|
||||
end
|
||||
|
||||
self:set_line_value(self.samples, value, true)
|
||||
self:set_line_value(self.samples, value)
|
||||
end
|
||||
|
||||
|
||||
---@param text string
|
||||
function M:set_text(text)
|
||||
self.text_value:set_to(text)
|
||||
function M:set_max_value(max_value)
|
||||
if self.max_value == max_value then
|
||||
return
|
||||
end
|
||||
|
||||
self.max_value = max_value
|
||||
for index = 1, self.samples do
|
||||
self:set_line_height(index)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function M:on_drag_corner(dx, dy)
|
||||
function M:set_line_height(index)
|
||||
local value = self.values[index] or 0
|
||||
local normalized = vmath.clamp(value / self.max_value, 0, 1)
|
||||
local size_y = normalized * 70
|
||||
gui.set(self.lines[index], SIZE_Y, size_y)
|
||||
end
|
||||
|
||||
|
||||
function M:get_lowest_value()
|
||||
return math.min(unpack(self.values))
|
||||
end
|
||||
|
||||
|
||||
function M:get_highest_value()
|
||||
return math.max(unpack(self.values))
|
||||
end
|
||||
|
||||
|
||||
function M:on_drag_widget(dx, dy)
|
||||
local position = self.container:get_position()
|
||||
self.container:set_position(position.x + dx, position.y + dy)
|
||||
end
|
||||
|
||||
|
||||
function M:toggle_hide()
|
||||
self.is_hidden = not self.is_hidden
|
||||
local hidden_size = gui.get_size(self:get_node("header"))
|
||||
|
||||
local new_size = self.is_hidden and hidden_size or self.default_size
|
||||
self.container:set_size(new_size.x, new_size.y, gui.PIVOT_N)
|
||||
|
||||
gui.set_enabled(self.content, not self.is_hidden)
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
return M
|
Reference in New Issue
Block a user