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

@@ -0,0 +1,222 @@
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
y: 100.0
}
type: TYPE_BOX
id: "root"
inherit_alpha: true
size_mode: SIZE_MODE_AUTO
visible: false
}
nodes {
type: TYPE_TEMPLATE
id: "mini_graph"
parent: "root"
inherit_alpha: true
template: "/druid/widget/mini_graph/mini_graph.gui"
}
nodes {
color {
x: 0.173
y: 0.184
z: 0.204
}
type: TYPE_BOX
id: "mini_graph/root"
parent: "mini_graph"
overridden_fields: 5
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "mini_graph/header"
parent: "mini_graph/root"
template_node_child: true
}
nodes {
type: TYPE_TEXT
text: "FPS"
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/header"
template_node_child: true
}
nodes {
type: TYPE_BOX
id: "mini_graph/content"
parent: "mini_graph/root"
template_node_child: true
}
nodes {
color {
x: 0.525
y: 0.525
z: 0.525
}
type: TYPE_BOX
id: "mini_graph/prefab_line"
parent: "mini_graph/content"
overridden_fields: 5
template_node_child: true
}
nodes {
color {
x: 0.957
y: 0.608
z: 0.608
}
type: TYPE_BOX
id: "mini_graph/color_low"
parent: "mini_graph/content"
overridden_fields: 5
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: 260.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "12 FPS"
font: "text_regular"
id: "text_min_fps"
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 {
y: 12.0
}
scale {
x: 0.3
y: 0.3
}
size {
x: 260.0
y: 40.0
}
color {
x: 0.463
y: 0.475
z: 0.49
}
type: TYPE_TEXT
text: "60 FPS"
font: "text_bold"
id: "text_fps"
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
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT

View File

@@ -0,0 +1,84 @@
local mini_graph = require("druid.widget.mini_graph.mini_graph")
---@class widget.fps_panel: druid.widget
---@field root node
local M = {}
local TARGET_FPS = sys.get_config_int("display.update_frequency", 60)
if TARGET_FPS == 0 then
TARGET_FPS = 60
end
function M:init()
self.delta_time = 0.1 -- in seconds
self.collect_time = 3 -- in seconds
self.collect_time_counter = 0
self.graph_samples = self.collect_time / self.delta_time
-- Store frame time in seconds last collect_time seconds
self.fps_samples = {}
self.mini_graph = self.druid:new_widget(mini_graph, "mini_graph")
self.mini_graph:set_samples(self.graph_samples) -- show last 30 seconds
self.mini_graph:set_max_value(TARGET_FPS)
gui.set_parent(self:get_node("content"), self.mini_graph.content, true)
self.text_min_fps = self.druid:new_text("text_min_fps")
self.text_fps = self.druid:new_text("text_fps")
self.timer_id = timer.delay(self.delta_time, true, function()
self:push_fps_value()
end)
end
function M:update(dt)
if not self.previous_time then
self.previous_time = socket.gettime()
return
end
local current_time = socket.gettime()
local delta_time = current_time - self.previous_time
self.previous_time = current_time
self.collect_time_counter = self.collect_time_counter + delta_time
table.insert(self.fps_samples, 1, delta_time)
while self.collect_time_counter > self.collect_time do
-- Remove last
local removed_value = table.remove(self.fps_samples)
self.collect_time_counter = self.collect_time_counter - removed_value
end
end
function M:push_fps_value()
if #self.fps_samples == 0 then
return
end
local max_frame_time = 0
local average_frame_time = 0
local average_samples_count = self.delta_time
local average_collected = 0
for index = 1, #self.fps_samples do
if average_frame_time < average_samples_count then
average_frame_time = average_frame_time + self.fps_samples[index]
average_collected = average_collected + 1
end
max_frame_time = math.max(max_frame_time, self.fps_samples[index])
end
average_frame_time = average_frame_time / average_collected
self.mini_graph:push_line_value(1 / average_frame_time)
self.text_fps:set_to(tostring(math.ceil(1 / average_frame_time) .. " FPS"))
local lowest_value = math.ceil(self.mini_graph:get_lowest_value())
self.text_min_fps:set_to(lowest_value .. " lowest")
end
return M