Unravel Engine C++ Reference
Loading...
Searching...
No Matches
statistics_utils.cpp
Go to the documentation of this file.
1#include "statistics_utils.h"
2
3#include <algorithm>
4#include <numeric>
5
7{
8
9// Constants
10namespace
11{
12 constexpr float HOVER_COLOR_MULTIPLIER = 0.1f;
13 constexpr int32_t SMART_INIT_SAMPLES = 20;
14}
15
17{
18 reset(0.0f);
19}
20
21auto sample_data::reset(float value) -> void
22{
23 offset_ = 0;
24 std::fill(values_.begin(), values_.end(), value);
25
26 min_ = value;
27 max_ = value;
28 average_ = value;
29
30 smart_init_samples_ = SMART_INIT_SAMPLES;
31}
32
33auto sample_data::push_sample(float value) -> void
34{
35 if(smart_init_samples_ > 0 && offset_ > smart_init_samples_)
36 {
37 reset(value);
38 smart_init_samples_ = -1;
39 }
40
41 values_[offset_] = value;
42 offset_ = (offset_ + 1) % NUM_SAMPLES;
43
44 float min = std::numeric_limits<float>::max();
45 float max = std::numeric_limits<float>::lowest();
46 float sum = 0.0f;
47
48 for(const auto& val : values_)
49 {
50 min = std::min(min, val);
51 max = std::max(max, val);
52 sum += val;
53 }
54
55 min_ = min;
56 max_ = max;
57 average_ = sum / NUM_SAMPLES;
58}
59
60auto draw_progress_bar(float width, float max_width, float height, const ImVec4& color) -> bool
61{
62 const ImGuiStyle& style = ImGui::GetStyle();
63
64 ImVec4 hovered_color(
65 color.x + color.x * HOVER_COLOR_MULTIPLIER,
66 color.y + color.y * HOVER_COLOR_MULTIPLIER,
67 color.z + color.z * HOVER_COLOR_MULTIPLIER,
68 color.w + color.w * HOVER_COLOR_MULTIPLIER
69 );
70
71 ImGui::PushStyleColor(ImGuiCol_Button, color);
72 ImGui::PushStyleColor(ImGuiCol_ButtonHovered, hovered_color);
73 ImGui::PushStyleColor(ImGuiCol_ButtonActive, color);
74 ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);
75 ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0.0f, style.ItemSpacing.y));
76
77 bool item_hovered = false;
78
79 ImGui::Button("##bar_button", ImVec2(width, height));
80 item_hovered |= ImGui::IsItemHovered();
81
82 ImGui::SameLine();
83 ImGui::InvisibleButton("##bar_invisible", ImVec2(max_width - width + 1, height));
84 item_hovered |= ImGui::IsItemHovered();
85
86 ImGui::PopStyleVar(2);
87 ImGui::PopStyleColor(3);
88
89 return item_hovered;
90}
91
92auto draw_resource_bar(const char* name,
93 const char* tooltip,
94 uint32_t current_value,
95 uint32_t max_value,
96 float max_width,
97 float height) -> void
98{
99 bool item_hovered = false;
100 ImGui::PushID(name);
101
102 ImGui::AlignTextToFramePadding();
103 ImGui::Text("%s: %6d / %6d", name, current_value, max_value);
104 item_hovered |= ImGui::IsItemHovered();
105 ImGui::SameLine();
106
107 const float percentage = static_cast<float>(current_value) / static_cast<float>(max_value);
108 static const ImVec4 color(0.5f, 0.5f, 0.5f, 1.0f);
109 item_hovered |= draw_progress_bar(std::max(1.0f, percentage * max_width), max_width, height, color);
110 ImGui::SameLine();
111
112 ImGui::Text("%5.2f%%", static_cast<double>(percentage * 100.0f));
113
114 if(item_hovered)
115 {
116 ImGui::SetNextWindowViewportToCurrent();
117 ImGui::BeginTooltip();
118 ImGui::Text("%s %5.2f%%", tooltip, static_cast<double>(percentage * 100.0f));
119 ImGui::EndTooltip();
120 }
121
122 ImGui::PopID();
123}
124
125} // namespace unravel::statistics_utils
sample_data()
Constructor that initializes all samples to zero.
auto push_sample(float value) -> void
Add a new sample to the rolling buffer. Automatically updates min, max, and average statistics.
auto reset(float value) -> void
Reset all samples to a specific value.
std::string name
Definition hub.cpp:27
const char * tooltip
auto draw_resource_bar(const char *name, const char *tooltip, uint32_t current_value, uint32_t max_value, float max_width, float height) -> void
Draw a resource usage bar with label and percentage.
auto draw_progress_bar(float width, float max_width, float height, const ImVec4 &color) -> bool
Draw a colored progress bar with hover effects.