RigsofRods
Soft-body Physics Simulation
GUI_NodeBeamUtils.cpp
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2016-2020 Petr Ohlidal
4 
5  For more information, see http://www.rigsofrods.org/
6 
7  Rigs of Rods is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License version 3, as
9  published by the Free Software Foundation.
10 
11  Rigs of Rods is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "GUI_NodeBeamUtils.h"
22 
23 #include "Application.h"
24 #include "Actor.h"
25 #include "GameContext.h"
26 #include "GUIManager.h"
27 #include "Language.h"
28 
29 using namespace RoR;
30 using namespace GUI;
31 
33 {
35  if (!actor)
36  {
37  this->SetVisible(false);
38  return;
39  }
40  ImGui::SetNextWindowPosCenter(ImGuiCond_FirstUseEver);
41  ImGui::SetNextWindowSize(ImVec2(600.f, 675.f), ImGuiCond_FirstUseEver);
42  const int flags = ImGuiWindowFlags_NoCollapse;
43  bool keep_open = true;
44  ImGui::Begin(_LC("NodeBeamUtils", "Node/Beam Utils"), &keep_open, flags);
45 
46  ImGui::PushItemWidth(500.f); // Width includes [+/-] buttons
47  float ref_mass = actor->ar_initial_total_mass;
48  float cur_mass = actor->getTotalMass(false);
49  if (ImGui::SliderFloat(_LC("NodeBeamUtils", "Total mass"), &cur_mass, ref_mass * 0.5f, ref_mass * 2.0f, "%.2f kg"))
50  {
51  actor->ar_nb_mass_scale = cur_mass / ref_mass;
52  actor->ar_nb_initialized = false;
53  actor->applyNodeBeamScales();
54  }
55  ImGui::Separator();
56  ImGui::TextColored(GRAY_HINT_TEXT, _LC("NodeBeamUtils", "Beams:"));
57  if (ImGui::SliderFloat("Spring##Beams", &actor->ar_nb_beams_scale.first, 0.1f, 10.0f, "%.5f"))
58  {
59  actor->applyNodeBeamScales();
60  }
61  if (ImGui::SliderFloat("Damping##Beams", &actor->ar_nb_beams_scale.second, 0.1f, 10.0f, "%.5f"))
62  {
63  actor->applyNodeBeamScales();
64  }
65  ImGui::Separator();
66  ImGui::TextColored(GRAY_HINT_TEXT, _LC("NodeBeamUtils", "Shocks:"));
67  if (ImGui::SliderFloat("Spring##Shocks", &actor->ar_nb_shocks_scale.first, 0.1f, 10.0f, "%.5f"))
68  {
69  actor->applyNodeBeamScales();
70  }
71  if (ImGui::SliderFloat("Damping##Shocks", &actor->ar_nb_shocks_scale.second, 0.1f, 10.0f, "%.5f"))
72  {
73  actor->applyNodeBeamScales();
74  }
75  ImGui::Separator();
76  ImGui::TextColored(GRAY_HINT_TEXT, _LC("NodeBeamUtils", "Wheels:"));
77  if (ImGui::SliderFloat("Spring##Wheels", &actor->ar_nb_wheels_scale.first, 0.1f, 10.0f, "%.5f"))
78  {
79  actor->applyNodeBeamScales();
80  }
81  if (ImGui::SliderFloat("Damping##Wheels", &actor->ar_nb_wheels_scale.second, 0.1f, 10.0f, "%.5f"))
82  {
83  actor->applyNodeBeamScales();
84  }
85  ImGui::Separator();
86  ImGui::Spacing();
87  if (ImGui::Button(_LC("NodeBeamUtils", "Reset to default settings"), ImVec2(280.f, 25.f)))
88  {
89  actor->ar_nb_mass_scale = 1.0f;
90  actor->ar_nb_beams_scale = {1.0f, 1.0f};
91  actor->ar_nb_shocks_scale = {1.0f, 1.0f};
92  actor->ar_nb_wheels_scale = {1.0f, 1.0f};
93  actor->SyncReset(true);
94  }
95  ImGui::SameLine();
96  if (ImGui::Button(_LC("NodeBeamUtils", "Update initial node positions"), ImVec2(280.f, 25.f)))
97  {
98  actor->updateInitPosition();
99  }
100  ImGui::PopItemWidth();
101 
102  ImGui::PushItemWidth(235.f); // Width includes [+/-] buttons
103  ImGui::TextColored(GRAY_HINT_TEXT,"%s", _LC("NodeBeamUtils", "Physics steps:"));
104  ImGui::SliderInt("Skip##BeamsInt", &actor->ar_nb_skip_steps, 0, 2000);
105  ImGui::SameLine();
106  ImGui::SliderInt("Measure##BeamsInt", &actor->ar_nb_measure_steps, 2, 6000);
107  ImGui::PopItemWidth();
108  ImGui::PushItemWidth(138.f); // Width includes [+/-] buttons
109  ImGui::Separator();
110  ImGui::TextColored(GRAY_HINT_TEXT,"%s", _LC("NodeBeamUtils", "Beams (spring & damping search interval):"));
111  ImGui::SliderFloat("##BSL", &actor->ar_nb_beams_k_interval.first, 0.1f, actor->ar_nb_beams_k_interval.second);
112  ImGui::SameLine();
113  ImGui::SliderFloat("##BSU", &actor->ar_nb_beams_k_interval.second, actor->ar_nb_beams_k_interval.first, 10.0f);
114  ImGui::SameLine();
115  ImGui::SliderFloat("##BDL", &actor->ar_nb_beams_d_interval.first, 0.1f, actor->ar_nb_beams_d_interval.second);
116  ImGui::SameLine();
117  ImGui::SliderFloat("##BDU", &actor->ar_nb_beams_d_interval.second, actor->ar_nb_beams_d_interval.first, 10.0f);
118  ImGui::TextColored(GRAY_HINT_TEXT,"%s", _LC("NodeBeamUtils", "Shocks (spring & damping search interval):"));
119  ImGui::SliderFloat("##SSL", &actor->ar_nb_shocks_k_interval.first, 0.1f, actor->ar_nb_shocks_k_interval.second);
120  ImGui::SameLine();
121  ImGui::SliderFloat("##SSU", &actor->ar_nb_shocks_k_interval.second, actor->ar_nb_shocks_k_interval.first, 10.0f);
122  ImGui::SameLine();
123  ImGui::SliderFloat("##SDL", &actor->ar_nb_shocks_d_interval.first, 0.1f, actor->ar_nb_shocks_d_interval.second);
124  ImGui::SameLine();
125  ImGui::SliderFloat("##SDU", &actor->ar_nb_shocks_d_interval.second, actor->ar_nb_shocks_d_interval.first, 10.0f);
126  ImGui::TextColored(GRAY_HINT_TEXT,"%s", _LC("NodeBeamUtils", "Wheels (spring & damping search interval):"));
127  ImGui::SliderFloat("##WSL", &actor->ar_nb_wheels_k_interval.first, 0.1f, actor->ar_nb_wheels_k_interval.second);
128  ImGui::SameLine();
129  ImGui::SliderFloat("##WSU", &actor->ar_nb_wheels_k_interval.second, actor->ar_nb_wheels_k_interval.first, 10.0f);
130  ImGui::SameLine();
131  ImGui::SliderFloat("##WDL", &actor->ar_nb_wheels_d_interval.first, 0.1f, actor->ar_nb_wheels_d_interval.second);
132  ImGui::SameLine();
133  ImGui::SliderFloat("##WDU", &actor->ar_nb_wheels_d_interval.second, actor->ar_nb_wheels_d_interval.first, 10.0f);
134  ImGui::PopItemWidth();
135  ImGui::Separator();
136  ImGui::Spacing();
137  if (ImGui::Button(m_is_searching ? _LC("NodeBeamUtils", "Stop searching") : actor->ar_nb_initialized ? _LC("NodeBeamUtils", "Continue searching") : _LC("NodeBeamUtils", "Start searching"),
138  ImVec2(280.f, 25.f)))
139  {
141  if (!m_is_searching)
142  {
143  actor->SyncReset(true);
144  }
145  }
146  ImGui::SameLine();
147  if (ImGui::Button(_LC("NodeBeamUtils", "Reset search"), ImVec2(280.f, 25.f)))
148  {
149  actor->ar_nb_initialized = false;
150  m_is_searching = false;
151  }
152  ImGui::Separator();
153  ImGui::Spacing();
154  if (actor->ar_nb_initialized)
155  {
156  ImGui::Columns(2, _LC("NodeBeamUtils", "Search results"));
157  ImGui::SetColumnOffset(1, 290.f);
158  ImGui::Text("%s", _LC("NodeBeamUtils", "Reference"));
159  ImGui::NextColumn();
160  ImGui::Text("%s", _LC("NodeBeamUtils", "Optimum"));
161  ImGui::NextColumn();
162  ImGui::Separator();
163  ImGui::Text("%s: %f (%f)", _LC("NodeBeamUtils", "Movement"), actor->ar_nb_reference[5] / actor->ar_num_nodes, actor->ar_nb_reference[4]);
164  ImGui::Text("%s: %.2f (%.2f)", _LC("NodeBeamUtils", "Stress"), actor->ar_nb_reference[1] / actor->ar_num_beams, actor->ar_nb_reference[0]);
165  ImGui::Text("%s: %f (%f)", _LC("NodeBeamUtils", "Yitter"), actor->ar_nb_reference[3] / actor->ar_num_beams, actor->ar_nb_reference[2]);
166  ImGui::NextColumn();
167  ImGui::Text("%s: %f (%f)", _LC("NodeBeamUtils", "Movement"), actor->ar_nb_optimum[5] / actor->ar_num_nodes, actor->ar_nb_optimum[4]);
168  ImGui::Text("%s: %.2f (%.2f)", _LC("NodeBeamUtils", "Stress"), actor->ar_nb_optimum[1] / actor->ar_num_beams, actor->ar_nb_optimum[0]);
169  ImGui::Text("%s: %f (%f)", _LC("NodeBeamUtils", "Yitter"), actor->ar_nb_optimum[3] / actor->ar_num_beams, actor->ar_nb_optimum[2]);
170  ImGui::Columns(1);
171  }
172 
173  if (m_is_searching)
174  {
175  actor->searchBeamDefaults();
176  }
177 
178  m_is_hovered = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
180 
181  ImGui::End();
182  if (!keep_open)
183  {
184  this->SetVisible(false);
185  }
186 }
187 
189 {
190  m_is_visible = v;
191  m_is_hovered = false;
192  if (!v)
193  {
194  m_is_searching = false;
195  }
196 }
GameContext.h
Game state manager and message-queue provider.
RoR::Actor::searchBeamDefaults
void searchBeamDefaults()
Searches for more stable beam defaults.
Definition: Actor.cpp:1808
RoR::Actor::ar_nb_optimum
std::vector< float > ar_nb_optimum
Temporary storage of the optimum search result.
Definition: Actor.h:457
RoR::Actor::ar_nb_reference
std::vector< float > ar_nb_reference
Temporary storage of the reference search result.
Definition: Actor.h:458
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
RoR::Actor::ar_num_nodes
int ar_num_nodes
Definition: Actor.h:283
RoR::GUI::NodeBeamUtils::GRAY_HINT_TEXT
const ImVec4 GRAY_HINT_TEXT
Definition: GUI_NodeBeamUtils.h:42
RoR::GUI::NodeBeamUtils::m_is_visible
bool m_is_visible
Definition: GUI_NodeBeamUtils.h:38
RoR::Actor::ar_nb_shocks_k_interval
std::pair< float, float > ar_nb_shocks_k_interval
Search interval for springiness & damping of shock beams.
Definition: Actor.h:468
RoR::GUI::NodeBeamUtils::m_is_searching
bool m_is_searching
Definition: GUI_NodeBeamUtils.h:40
RoR::Actor::ar_nb_shocks_scale
std::pair< float, float > ar_nb_shocks_scale
Scales for springiness & damping of shock beams.
Definition: Actor.h:463
RoR::Actor::ar_nb_measure_steps
int ar_nb_measure_steps
Amount of physics steps to be measured.
Definition: Actor.h:460
Language.h
RefCountingObjectPtr< Actor >
RoR::Actor::ar_nb_wheels_scale
std::pair< float, float > ar_nb_wheels_scale
Scales for springiness & damping of wheel / rim beams.
Definition: Actor.h:464
GUIManager.h
Actor.h
RoR::Actor::ar_nb_beams_d_interval
std::pair< float, float > ar_nb_beams_d_interval
Search interval for springiness & damping of regular beams.
Definition: Actor.h:465
RoR::Actor::ar_nb_shocks_d_interval
std::pair< float, float > ar_nb_shocks_d_interval
Search interval for springiness & damping of shock beams.
Definition: Actor.h:467
RoR::Actor::ar_nb_beams_k_interval
std::pair< float, float > ar_nb_beams_k_interval
Search interval for springiness & damping of regular beams.
Definition: Actor.h:466
RoR::Actor::SyncReset
void SyncReset(bool reset_position)
this one should be called only synchronously (without physics running in background)
Definition: Actor.cpp:1600
RoR::GUI::NodeBeamUtils::m_is_hovered
bool m_is_hovered
Definition: GUI_NodeBeamUtils.h:39
Application.h
Central state/object manager and communications hub.
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::Actor::ar_initial_total_mass
float ar_initial_total_mass
Definition: Actor.h:309
RoR::Actor::ar_num_beams
int ar_num_beams
Definition: Actor.h:288
RoR::Actor::getTotalMass
float getTotalMass(bool withLocked=true)
Definition: Actor.cpp:810
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::Actor::ar_nb_wheels_k_interval
std::pair< float, float > ar_nb_wheels_k_interval
Search interval for springiness & damping of wheel / rim beams.
Definition: Actor.h:470
RoR::Actor::ar_nb_mass_scale
float ar_nb_mass_scale
Global mass scale (affects all nodes the same way)
Definition: Actor.h:461
GUI_NodeBeamUtils.h
RoR::Actor::ar_nb_initialized
bool ar_nb_initialized
Definition: Actor.h:456
RoR::Actor::ar_nb_wheels_d_interval
std::pair< float, float > ar_nb_wheels_d_interval
Search interval for springiness & damping of wheel / rim beams.
Definition: Actor.h:469
RoR::Actor::ar_nb_beams_scale
std::pair< float, float > ar_nb_beams_scale
Scales for springiness & damping of regular beams.
Definition: Actor.h:462
RoR::GUI::NodeBeamUtils::SetVisible
void SetVisible(bool visible)
Definition: GUI_NodeBeamUtils.cpp:188
RoR::Actor::ar_nb_skip_steps
int ar_nb_skip_steps
Amount of physics steps to be skipped before measuring.
Definition: Actor.h:459
RoR::GameContext::GetPlayerActor
const ActorPtr & GetPlayerActor()
Definition: GameContext.h:134
RoR::Actor::updateInitPosition
void updateInitPosition()
Definition: Actor.cpp:1252
RoR::GUIManager::RequestGuiCaptureKeyboard
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
Definition: GUIManager.cpp:439
RoR
Definition: AppContext.h:36
RoR::GUI::NodeBeamUtils::Draw
void Draw()
Definition: GUI_NodeBeamUtils.cpp:32
RoR::Actor::applyNodeBeamScales
void applyNodeBeamScales()
For GUI::NodeBeamUtils.
Definition: Actor.cpp:1757