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

@@ -1,3 +1,15 @@
fonts {
name: "text_regular"
font: "/druid/fonts/text_regular.font"
}
fonts {
name: "text_bold"
font: "/druid/fonts/text_bold.font"
}
textures {
name: "druid"
texture: "/druid/druid.atlas"
}
nodes {
size {
x: 200.0
@@ -23,36 +35,208 @@ nodes {
template_node_child: true
}
nodes {
type: TYPE_TEXT
text: "Memory Panel"
id: "mini_graph/text_header"
type: TYPE_BOX
id: "mini_graph/header"
parent: "mini_graph/root"
template_node_child: true
}
nodes {
type: TYPE_TEXT
text: "Memory"
id: "mini_graph/text_header"
parent: "mini_graph/header"
overridden_fields: 8
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "mini_graph/icon_drag"
parent: "mini_graph/root"
parent: "mini_graph/header"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "mini_graph/panel_diagram"
id: "mini_graph/content"
parent: "mini_graph/root"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "mini_graph/prefab_line"
parent: "mini_graph/panel_diagram"
parent: "mini_graph/content"
template_node_child: true
}
nodes {
type: TYPE_TEXT
id: "mini_graph/text_value"
parent: "mini_graph/root"
type: TYPE_BOX
id: "mini_graph/color_low"
parent: "mini_graph/content"
template_node_child: true
}
nodes {
size {
x: 200.0
y: 100.0
}
type: TYPE_BOX
id: "content"
parent: "root"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
visible: false
}
nodes {
position {
x: -96.0
y: 12.0
}
scale {
x: 0.3
y: 0.3
}
size {
x: 200.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "120.23 KB"
font: "text_regular"
id: "text_max_value"
pivot: PIVOT_W
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "content"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
nodes {
position {
x: 96.0
y: 12.0
}
scale {
x: 0.3
y: 0.3
}
size {
x: 200.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "120 KB/s"
font: "text_regular"
id: "text_per_second"
pivot: PIVOT_E
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "content"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
nodes {
position {
x: -33.4
y: 30.0
}
size {
x: 3.0
y: 8.0
}
color {
x: 0.173
y: 0.184
z: 0.204
}
type: TYPE_BOX
texture: "druid/pixel"
id: "line_second_1"
pivot: PIVOT_N
parent: "content"
inherit_alpha: true
}
nodes {
position {
x: 33.2
y: 30.0
}
size {
x: 3.0
y: 8.0
}
color {
x: 0.173
y: 0.184
z: 0.204
}
type: TYPE_BOX
texture: "druid/pixel"
id: "line_second_2"
pivot: PIVOT_N
parent: "content"
inherit_alpha: true
}
nodes {
position {
y: 12.0
}
scale {
x: 0.3
y: 0.3
}
size {
x: 200.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "120 KB"
font: "text_bold"
id: "text_memory"
outline {
x: 1.0
y: 1.0
z: 1.0
}
shadow {
x: 1.0
y: 1.0
z: 1.0
}
parent: "content"
inherit_alpha: true
outline_alpha: 0.0
shadow_alpha: 0.0
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT

View File

@@ -4,19 +4,73 @@ local mini_graph = require("druid.widget.mini_graph.mini_graph")
---@field root node
local M = {}
function M:init()
self.druid = self:get_druid()
self.delta_time = 0.1
self.samples_count = 30
self.memory_limit = 100
self.mini_graph = self.druid:new_widget(mini_graph, "mini_graph")
self.mini_graph:set_samples(self.samples_count)
gui.set_parent(self:get_node("content"), self.mini_graph.content, true)
--for index = 1, 32 do
-- self.mini_graph:set_line_value(index, 0)
--end
self.max_value = self.druid:new_text("text_max_value")
self.text_per_second = self.druid:new_text("text_per_second")
self.text_memory = self.druid:new_text("text_memory")
timer.delay(0.1, true, function()
self.mini_graph:push_line_value(math.random())
self.memory = collectgarbage("count")
self.memory_samples = {}
self:update_text_memory()
self.timer_id = timer.delay(self.delta_time, true, function()
self:push_next_value()
end)
end
function M:set_low_memory_limit(limit)
self.memory_limit = limit
end
function M:push_next_value()
local memory = collectgarbage("count")
local diff = math.max(0, memory - self.memory)
self.memory = memory
self:update_text_memory()
table.insert(self.memory_samples, diff)
if #self.memory_samples > self.samples_count then
table.remove(self.memory_samples, 1)
end
self.mini_graph:push_line_value(diff)
local max_value = math.max(unpack(self.memory_samples))
max_value = math.max(max_value, self.memory_limit) -- low limit to display
self.mini_graph:set_max_value(max_value)
local max_memory = math.ceil(self.mini_graph:get_highest_value())
self.max_value:set_to(max_memory .. " KB")
local last_second = 0
local last_second_samples = math.ceil(1 / self.delta_time)
for index = #self.memory_samples - last_second_samples + 1, #self.memory_samples do
last_second = last_second + (self.memory_samples[index] or 0)
end
self.text_per_second:set_to(math.ceil(last_second) .. " KB/s")
end
function M:update_text_memory()
local memory = math.ceil(collectgarbage("count")) -- in KB
if memory > 1024 then
memory = memory / 1024
self.text_memory:set_to(string.format("%.2f", memory) .. " MB")
else
self.text_memory:set_to(memory .. " KB")
end
end
return M