Symphony Of Empires
interface.cpp
Go to the documentation of this file.
1 // Eng3D - General purpouse game engine
2 // Copyright (C) 2021, Eng3D contributors
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <https://www.gnu.org/licenses/>.
16 //
17 // ----------------------------------------------------------------------------
18 // Name:
19 // interface.cpp
20 //
21 // Abstract:
22 // Provides common UI elements from Eng3D to debug/profile or do other
23 // things in-game.
24 // ----------------------------------------------------------------------------
25 
26 #include "eng3d/interface.hpp"
27 #include "eng3d/profiler.hpp"
28 #include "eng3d/color.hpp"
29 #include "eng3d/ui/widget.hpp"
30 #include "eng3d/ui/components.hpp"
31 #include "eng3d/state.hpp"
32 #include "eng3d/string.hpp"
33 
35  : UI::Window(0, 0, 240, _s.width, nullptr),
36  s{ _s },
37  profiler{ _profiler }
38 {
39  this->padding.x = 0;
40  this->padding.y = 48;
41  this->set_text(translate("Performance profiler"));
42  this->is_scroll = false;
43  this->current_texture.reset();
44 
45  this->set_close_btn_function([this](UI::Widget&) {
46  this->kill();
47  });
48 
49  auto& fps_lab = this->make_widget<UI::Label>(10, 0, "?");
50  fps_lab.on_update = ([this](UI::Widget& w) {
51  w.set_text(translate_format("FPS: %.2f", this->profiler.get_fps()));
52  });
53  fps_lab.on_update(fps_lab);
54 
55  auto& task_chart = this->make_widget<UI::BarChart>(20, 20, this->width - 40, 20);
56  task_chart.on_update = ([this](UI::Widget& w) {
57  auto& chart = static_cast<UI::BarChart&>(w);
58  std::vector<UI::ChartData> data;
59  const auto& tasks = this->profiler.get_tasks();
60  std::transform(tasks.cbegin(), tasks.cend(), std::back_inserter(data), [](const auto& e) {
61  return UI::ChartData(e->get_average_time_ms(), e->name, Eng3D::Color::get_random(e->color).get_value());
62  });
63  chart.set_data(data);
64  });
65  task_chart.on_update(task_chart);
66 
67  this->on_update = ([this](UI::Widget&) {
68  auto current_tasks = this->profiler.get_tasks();
69  auto& current_views = this->task_views;
70  if(current_views.size() < current_tasks.size()) {
71  for(size_t i = current_views.size(); i < current_tasks.size(); i++)
72  current_views.push_back(new ProfilerTaskView(this, 10, 50 + i * 25));
73  } else if(current_views.size() > current_tasks.size()) {
74  for(size_t i = current_tasks.size(); i < current_views.size(); i++)
75  current_views[i]->kill();
76  current_views.erase(current_views.begin() + current_tasks.size(), current_views.end());
77  }
78  for(size_t i = 0; i < current_views.size(); i++)
79  current_views[i]->set_task(*current_tasks[i]);
80  });
81 }
82 
84  UI::Group(_x, _y, 300, 25, profiler_view)
85 {
86  this->color_box = &this->make_widget<UI::Div>(0, 0, 20, 20);
87  this->label = &this->make_widget<UI::Label>(30, 0, " ");
88 }
89 
91  this->color_box->background_color = Eng3D::Color::rgb32(profiler_view.color);
92  float time = profiler_view.get_average_time_ms();
93  auto format_time = std::to_string((int)time);
94  format_time = std::string(3 - glm::min<size_t>(3, format_time.length()), '0') + format_time;
95  this->label->set_text(format_time + " ms " + profiler_view.name);
96 }
float get_average_time_ms()
Definition: profiler.cpp:52
const uint32_t color
Definition: profiler.hpp:54
const std::string name
Definition: profiler.hpp:53
ProfilerTaskView(ProfilerView *profiler_view, int x, int y)
Definition: interface.cpp:83
void set_task(Eng3D::BenchmarkTask &profiler_view)
Definition: interface.cpp:90
ProfilerView(Eng3D::State &s, Eng3D::Profiler &profiler)
Definition: interface.cpp:34
Bar chart that draws horizontal bar chart.
Definition: barchart.hpp:32
The master widget all the other widgets inherit from, do not use directly instead use one of the many...
Definition: widget.hpp:176
std::function< void(UI::Widget &)> on_update
Definition: widget.hpp:349
bool is_scroll
Definition: widget.hpp:306
size_t width
Definition: widget.hpp:325
std::shared_ptr< Eng3D::Texture > current_texture
Definition: widget.hpp:327
void kill()
Kills the current widget, setting it up for deletion when dead widgets are cleared by the UI context.
Definition: widget.hpp:283
glm::ivec2 padding
Definition: widget.hpp:323
virtual void set_text(const std::string &text)
Generates text for the widget and overrides the current text texture.
Definition: widget.cpp:445
void set_close_btn_function(std::function< void(Widget &)> on_click)
Definition: window.cpp:65
std::string translate(const std::string_view str)
Definition: string.cpp:76
std::string translate_format(const std::string_view format, Args &&... args)
String formatter, with translation.
Definition: string.hpp:128
constexpr static Color rgb32(uint32_t argb)
Create a color from RGB32 components.
Definition: color.hpp:89
float get_fps()
Definition: profiler.cpp:108
const std::vector< BenchmarkTask * > get_tasks()
Definition: profiler.cpp:128