More widgets stuff, cleaning code

This commit is contained in:
Insality
2024-11-19 23:06:57 +02:00
parent 37190684c4
commit c35dfc7066
36 changed files with 1494 additions and 508 deletions

View File

@@ -31,10 +31,26 @@ nodes {
w: 8.0
}
}
nodes {
position {
y: 70.0
}
size {
x: 200.0
y: 40.0
}
type: TYPE_BOX
id: "header"
pivot: PIVOT_N
parent: "root"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
visible: false
}
nodes {
position {
x: -92.0
y: 63.0
y: -8.0
}
scale {
x: 0.5
@@ -64,26 +80,26 @@ nodes {
y: 1.0
z: 1.0
}
parent: "root"
parent: "header"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
nodes {
position {
x: 92.0
y: 67.0
x: 96.0
y: -4.0
}
color {
x: 0.129
y: 0.141
z: 0.157
x: 0.306
y: 0.31
z: 0.314
}
type: TYPE_BOX
texture: "druid/ui_circle_32"
texture: "druid/icon_drag"
id: "icon_drag"
pivot: PIVOT_NE
parent: "root"
parent: "header"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
}
@@ -102,7 +118,7 @@ nodes {
}
type: TYPE_BOX
texture: "druid/ui_circle_16"
id: "panel_diagram"
id: "content"
pivot: PIVOT_S
parent: "root"
inherit_alpha: true
@@ -113,6 +129,7 @@ nodes {
w: 8.0
}
clipping_mode: CLIPPING_MODE_STENCIL
material: "gui_stencil"
}
nodes {
size {
@@ -128,44 +145,32 @@ nodes {
texture: "druid/pixel"
id: "prefab_line"
pivot: PIVOT_S
parent: "panel_diagram"
parent: "content"
inherit_alpha: true
}
nodes {
position {
y: 12.0
}
scale {
x: 0.7
y: 0.7
x: -10.0
y: 4.0
}
size {
x: 260.0
y: 40.0
x: 8.0
y: 8.0
}
color {
x: 0.463
y: 0.475
z: 0.49
x: 0.557
y: 0.835
z: 0.62
}
type: TYPE_TEXT
text: "120.23 KB"
font: "text_bold"
id: "text_value"
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "root"
type: TYPE_BOX
texture: "druid/pixel"
id: "color_low"
parent: "content"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
materials {
name: "gui_stencil"
material: "/druid/materials/stencil/gui_stencil.material"
}

View File

@@ -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