RigsofRods
Soft-body Physics Simulation
GUI_LoadingWindow.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5 
6  For more information, see http://www.rigsofrods.org/
7 
8  Rigs of Rods is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License version 3, as
10  published by the Free Software Foundation.
11 
12  Rigs of Rods is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "GUI_LoadingWindow.h"
22 #include <fmt/format.h>
23 
24 #include "Actor.h"
25 #include "GUIManager.h"
26 #include "GUIUtils.h"
27 #include "OgreImGui.h"
28 #include "Language.h"
29 
30 using namespace RoR;
31 using namespace GUI;
32 
33 void LoadingWindow::SetProgress(int percent, std::string const& text, bool render_frame/*=true*/)
34 {
35  if (render_frame && m_timer.getMilliseconds() > 10)
36  {
37  App::GetGuiManager()->NewImGuiFrame(m_timer.getMilliseconds() * 0.001);
38  m_timer.reset();
39  }
40  else
41  {
42  render_frame = false;
43  }
44 
45  this->SetVisible(true); // Traditional behavior
46  m_percent = percent;
47  m_text = text;
48 
49  // Count lines
50  Ogre::StringUtil::trim(m_text); // Remove leading/trailing whitespace, incl. newlines
51  m_text_num_lines = 1; // First or single line
52  size_t pos = 0;
53  while ((pos = m_text.find("\n", pos+1)) != std::string::npos) // Count newlines
54  {
55  ++m_text_num_lines; // New line
56  }
57 
58  if (render_frame)
59  {
60  this->Draw();
61  Ogre::Root::getSingleton().renderOneFrame();
62  }
63 
64  // Echo status to log
65  Str<200> msg;
66  msg << "[RoR|PleaseWaitUI] ";
67  if (percent == PERC_SHOW_SPINNER) { msg << "<spinner>"; }
68  else if (percent != PERC_HIDE_PROGRESSBAR) { msg << "<" << percent << "%>"; }
69  msg << " " << text;
70  RoR::Log(msg);
71 }
72 
73 void LoadingWindow::SetProgressNetConnect(const std::string& net_status)
74 {
76  fmt::format( "{} [{}:{}]\n{}", _LC("LoadingWindow", "Joining"),
77  App::mp_server_host->getStr(), App::mp_server_port->getInt(), net_status));
78 }
79 
81 {
83 
84  // Height calc
85  float text_h = ImGui::CalcTextSize("A").y;
86  float statusbar_h = text_h + (ImGui::GetStyle().FramePadding.y * 2);
87  float titlebar_h = text_h + (ImGui::GetStyle().FramePadding.y * 2);
88 
89  float height = titlebar_h +
90  ImGui::GetStyle().WindowPadding.y +
91  (text_h * (m_text_num_lines + 1)) + // + One blank line
92  (ImGui::GetStyle().ItemSpacing.y * 2) + // Blank line, statusbar
93  statusbar_h +
94  ImGui::GetStyle().WindowPadding.y;
95 
96  ImGui::SetNextWindowSize(ImVec2(500.f, height));
97  ImGui::SetNextWindowPosCenter();
98  ImGuiWindowFlags flags = ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoMove;
99  ImGui::Begin(_LC("LoadingWindow", "Please wait"), nullptr, flags);
100  ImGui::Text("%s", m_text.c_str());
101 
103  {
104  float spinner_size = 8.f;
105  LoadingIndicatorCircle("spinner", spinner_size, theme.value_blue_text_color, theme.value_blue_text_color, 10, 10);
106  }
107  else if (m_percent == PERC_HIDE_PROGRESSBAR)
108  {
109  ImGui::NewLine();
110  }
111  else
112  {
113  ImGui::NewLine();
114  ImGui::ProgressBar(static_cast<float>(m_percent)/100.f);
115  }
116  ImGui::End();
117 }
RoR::GUI::LoadingWindow::Draw
void Draw()
Definition: GUI_LoadingWindow.cpp:80
RoR::GUIManager::GuiTheme
Definition: GUIManager.h:70
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
RoR::GUIManager::GuiTheme::value_blue_text_color
ImVec4 value_blue_text_color
Definition: GUIManager.h:77
RoR::GUI::LoadingWindow::SetProgressNetConnect
void SetProgressNetConnect(const std::string &net_status)
Definition: GUI_LoadingWindow.cpp:73
format
Truck file format(technical spec)
GUIUtils.h
RoR::GUI::LoadingWindow::m_percent
int m_percent
Definition: GUI_LoadingWindow.h:45
Language.h
OgreImGui.h
GUIManager.h
RoR::GUI::LoadingWindow::m_timer
Ogre::Timer m_timer
Definition: GUI_LoadingWindow.h:49
Actor.h
RoR::GUI::LoadingWindow::m_text
std::string m_text
Definition: GUI_LoadingWindow.h:47
RoR::GUIManager::NewImGuiFrame
void NewImGuiFrame(float dt)
Definition: GUIManager.cpp:304
RoR::GUIManager::GetTheme
GuiTheme & GetTheme()
Definition: GUIManager.h:158
RoR::Str< 200 >
GUI_LoadingWindow.h
RoR::GUI::LoadingWindow::SetVisible
void SetVisible(bool v)
Definition: GUI_LoadingWindow.h:41
RoR::GUI::LoadingWindow::SetProgress
void SetProgress(int _percent, const std::string &_text="", bool render_frame=true)
Definition: GUI_LoadingWindow.cpp:33
RoR::GUI::LoadingWindow::PERC_HIDE_PROGRESSBAR
const int PERC_HIDE_PROGRESSBAR
Definition: GUI_LoadingWindow.h:34
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::App::mp_server_port
CVar * mp_server_port
Definition: Application.cpp:122
RoR::App::mp_server_host
CVar * mp_server_host
Definition: Application.cpp:121
RoR::GUI::LoadingWindow::PERC_SHOW_SPINNER
const int PERC_SHOW_SPINNER
Definition: GUI_LoadingWindow.h:35
RoR::LoadingIndicatorCircle
void LoadingIndicatorCircle(const char *label, const float indicator_radius, const ImVec4 &main_color, const ImVec4 &backdrop_color, const int circle_count, const float speed)
Draws animated loading spinner.
Definition: GUIUtils.cpp:131
RoR
Definition: AppContext.h:36
RoR::Log
void Log(const char *msg)
The ultimate, application-wide logging function. Adds a line (any length) in 'RoR....
Definition: Application.cpp:419
RoR::GUI::LoadingWindow::m_text_num_lines
int m_text_num_lines
Definition: GUI_LoadingWindow.h:48