diff options
| author | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
|---|---|---|
| committer | untodesu <kirill@untode.su> | 2025-07-01 03:08:39 +0500 |
| commit | 458e0005690ea9d579588a0a12368fc2c2c9a93a (patch) | |
| tree | 588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /game/client/gui/progress_bar.cc | |
| parent | c7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff) | |
| download | voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2 voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip | |
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight
up grinded the entire repository to a better state until 03:09 AM. I
guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'game/client/gui/progress_bar.cc')
| -rw-r--r-- | game/client/gui/progress_bar.cc | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/game/client/gui/progress_bar.cc b/game/client/gui/progress_bar.cc new file mode 100644 index 0000000..2bfb69e --- /dev/null +++ b/game/client/gui/progress_bar.cc @@ -0,0 +1,111 @@ +#include "client/pch.hh" + +#include "client/gui/progress_bar.hh" + +#include "core/math/constexpr.hh" + +#include "client/gui/language.hh" + +#include "client/globals.hh" + +constexpr static ImGuiWindowFlags WINDOW_FLAGS = ImGuiWindowFlags_NoBackground | ImGuiWindowFlags_NoDecoration; + +static std::string str_title; +static std::string str_button; +static gui::progress_bar_action button_action; + +void gui::progress_bar::init(void) +{ + str_title = "Loading"; + str_button = std::string(); + button_action = nullptr; +} + +void gui::progress_bar::layout(void) +{ + const auto viewport = ImGui::GetMainViewport(); + const auto window_start = ImVec2(0.0f, viewport->Size.y * 0.30f); + const auto window_size = ImVec2(viewport->Size.x, viewport->Size.y * 0.70f); + + ImGui::SetNextWindowPos(window_start); + ImGui::SetNextWindowSize(window_size); + + if(ImGui::Begin("###UIProgress", nullptr, WINDOW_FLAGS)) { + ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0.0f, 1.0f * globals::gui_scale)); + + const float title_width = ImGui::CalcTextSize(str_title.c_str()).x; + ImGui::SetCursorPosX(0.5f * (window_size.x - title_width)); + ImGui::TextUnformatted(str_title.c_str()); + + ImGui::Dummy(ImVec2(0.0f, 8.0f * globals::gui_scale)); + + const ImVec2 cursor = ImGui::GetCursorPos(); + + const std::size_t num_bars = 32; + const float spinner_width = 0.8f * ImGui::CalcItemWidth(); + const float bar_width = spinner_width / static_cast<float>(num_bars); + const float bar_height = 0.5f * ImGui::GetFrameHeight(); + + const float base_xpos = window_start.x + 0.5f * (window_size.x - spinner_width) + 0.5f; + const float base_ypos = window_start.y + cursor.y; + const float phase = 2.0f * ImGui::GetTime(); + + const ImVec4& background = ImGui::GetStyleColorVec4(ImGuiCol_Button); + const ImVec4& foreground = ImGui::GetStyleColorVec4(ImGuiCol_PlotHistogram); + + for(std::size_t i = 0; i < num_bars; ++i) { + const float sinval = std::sin(M_PI * static_cast<float>(i) / static_cast<float>(num_bars) - phase); + const float modifier = std::exp(-8.0f * (0.5f + 0.5f * sinval)); + + ImVec4 color = {}; + color.x = math::lerp(background.x, foreground.x, modifier); + color.y = math::lerp(background.y, foreground.y, modifier); + color.z = math::lerp(background.z, foreground.z, modifier); + color.w = math::lerp(background.w, foreground.w, modifier); + + const ImVec2 start = ImVec2(base_xpos + bar_width * i, base_ypos); + const ImVec2 end = ImVec2(start.x + bar_width, start.y + bar_height); + ImGui::GetWindowDrawList()->AddRectFilled(start, end, ImGui::GetColorU32(color)); + } + + // The NewLine call tricks ImGui into correctly padding the + // next widget that comes after the progress_bar spinner; this + // is needed to ensure the button is located in the correct place + ImGui::NewLine(); + + if(!str_button.empty()) { + ImGui::Dummy(ImVec2(0.0f, 32.0f * globals::gui_scale)); + + const float button_width = 0.8f * ImGui::CalcItemWidth(); + ImGui::SetCursorPosX(0.5f * (window_size.x - button_width)); + + if(ImGui::Button(str_button.c_str(), ImVec2(button_width, 0.0f))) { + if(button_action) { + button_action(); + } + } + } + + ImGui::PopStyleVar(); + } + + ImGui::End(); +} + +void gui::progress_bar::reset(void) +{ + str_title.clear(); + str_button.clear(); + button_action = nullptr; +} + +void gui::progress_bar::set_title(const char* title) +{ + str_title = gui::language::resolve(title); +} + +void gui::progress_bar::set_button(const char* text, const progress_bar_action& action) +{ + str_button = std::format("{}###ProgressBar_Button", gui::language::resolve(text)); + button_action = action; +} |
