RigsofRods
Soft-body Physics Simulation
GUI_MessageBox.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  Copyright 2013-2020 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
26 
27 #include "GUI_MessageBox.h"
28 
29 #include "Application.h"
30 #include "Language.h"
31 #include "GameContext.h"
32 #include "GUIManager.h"
33 #include "ScriptEngine.h"
34 
35 #include <imgui.h>
36 
37 using namespace RoR;
38 using namespace GUI;
39 
40  // Setup functions
41 
43 {
44  m_is_visible = true;
45  m_cfg = cfg;
46 
48  {
50  }
51  else if (m_cfg.mbc_allow_close)
52  {
54  }
55  else
56  {
57  m_close_handle = nullptr;
58  }
59 
60  std::stringstream ss;
61  ss << "[RoR|MessageBox] Showing a message box\n<MessageBox title> " << cfg.mbc_title << "\n<MessageBox text> " << cfg.mbc_text << "'";
62  for (MessageBoxButton const& button: cfg.mbc_buttons)
63  {
64  ss << "\n<MessageBox button> '" << button.mbb_caption << "' (MsgType: " << MsgTypeToString(button.mbb_mq_message) << ")";
65  }
66  LOG(ss.str());
67 }
68 
69 void MessageBoxDialog::Show(const char* title, const char* text, bool allow_close, const char* button1_text, const char* button2_text)
70 {
71  MessageBoxConfig conf;
72  conf.mbc_title = title;
73  conf.mbc_text = text;
74  conf.mbc_allow_close = allow_close;
75 
76  if (button1_text)
77  {
78  MessageBoxButton button;
79  button.mbb_caption = button1_text;
80  button.mbb_script_number = 1; // Standard
81  conf.mbc_buttons.push_back(button);
82  }
83 
84  if (button2_text)
85  {
86  MessageBoxButton button;
87  button.mbb_caption = button2_text;
88  button.mbb_script_number = 2; // Standard
89  conf.mbc_buttons.push_back(button);
90  }
91 
92  this->Show(conf);
93 }
94 
95  // Draw funcions
96 
98 {
99  const bool was_visible = m_is_visible;
100 
101  // Draw window
102  ImGui::SetNextWindowContentWidth(m_cfg.mbc_content_width); // Hard limit, actually
103  ImGui::SetNextWindowPosCenter(ImGuiCond_Appearing); // Initial pos. only
104  ImGui::Begin(m_cfg.mbc_title.c_str(), m_close_handle);
105  ImGui::TextWrapped("%s", m_cfg.mbc_text.c_str());
106 
107  // Draw buttons
108  for (MessageBoxButton const& button: m_cfg.mbc_buttons)
109  {
110  this->DrawButton(button);
111  }
112 
113  // Draw "always ask"
115  {
116  ImGui::Separator();
117  bool ask = m_cfg.mbc_always_ask_conf->getBool();
118  if (ImGui::Checkbox(_LC("MessageBox", "Always ask"), &ask))
119  {
121  }
122  }
123 
124  // Finalize
125  App::GetGuiManager()->RequestGuiCaptureKeyboard(ImGui::IsWindowHovered());
126  ImGui::End();
127  if (m_is_visible != was_visible)
128  {
130  }
131 }
132 
134 {
135  if (ImGui::Button(button.mbb_caption.c_str()))
136  {
137  ROR_ASSERT(button.mbb_script_number != 0); // Reserved value (close button)
138 
139  if (button.mbb_script_number > 0)
140  {
142  }
143 
144  if (button.mbb_mq_message != MsgType::MSG_INVALID)
145  {
146  Message m(button.mbb_mq_message);
147  m.description = button.mbb_mq_description;
148  m.payload = button.mbb_mq_payload;
150  }
151 
152  m_is_visible = false;
153  }
154 }
155 
RoR::GUI::MessageBoxConfig::mbc_buttons
std::vector< MessageBoxButton > mbc_buttons
Definition: GUI_MessageBox.h:59
ROR_ASSERT
#define ROR_ASSERT(_EXPR)
Definition: Application.h:40
GameContext.h
Game state manager and message-queue provider.
RoR::GUI::MessageBoxDialog::DrawButton
void DrawButton(MessageBoxButton const &button)
Definition: GUI_MessageBox.cpp:133
GUI_MessageBox.h
Generic UI dialog (not modal). Invocable from scripting. Any number of buttons. Configurable to fire ...
RoR::App::GetGuiManager
GUIManager * GetGuiManager()
Definition: Application.cpp:269
RoR::SE_GENERIC_MESSAGEBOX_CLICK
@ SE_GENERIC_MESSAGEBOX_CLICK
triggered when the user clicks on a message box button, the argument refers to the button pressed
Definition: ScriptEvents.h:60
RoR::GUI::MessageBoxDialog::m_close_handle
bool * m_close_handle
If nullptr, close button is hidden. Otherwise visible.
Definition: GUI_MessageBox.h:77
RoR::CVar::getBool
bool getBool() const
Definition: CVar.h:98
RoR::TRIGGER_EVENT_ASYNC
void TRIGGER_EVENT_ASYNC(scriptEvents type, int arg1, int arg2ex=0, int arg3ex=0, int arg4ex=0, std::string arg5ex="", std::string arg6ex="", std::string arg7ex="", std::string arg8ex="")
Asynchronously (via MSG_SIM_SCRIPT_EVENT_TRIGGERED) invoke script function eventCallbackEx(),...
Definition: ScriptEngine.h:51
RoR::MsgTypeToString
const char * MsgTypeToString(MsgType type)
Definition: Application.cpp:557
RoR::GUI::MessageBoxButton::mbb_mq_payload
void * mbb_mq_payload
Message argument to queue on click.
Definition: GUI_MessageBox.h:46
RoR::GUI::MessageBoxButton::mbb_caption
std::string mbb_caption
Definition: GUI_MessageBox.h:43
Language.h
GUIManager.h
RoR::GUI::MessageBoxDialog::m_is_visible
bool m_is_visible
Definition: GUI_MessageBox.h:78
RoR::GUI::MessageBoxButton
Definition: GUI_MessageBox.h:41
ScriptEngine.h
RoR::GameContext::PushMessage
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
Definition: GameContext.cpp:65
RoR::GUI::MessageBoxConfig::mbc_title
std::string mbc_title
Definition: GUI_MessageBox.h:52
Application.h
Central state/object manager and communications hub.
RoR::Message::payload
void * payload
Definition: GameContext.h:59
RoR::App::GetGameContext
GameContext * GetGameContext()
Definition: Application.cpp:280
RoR::GUI::MessageBoxConfig::mbc_text
std::string mbc_text
Definition: GUI_MessageBox.h:53
RoR::GUI::MessageBoxDialog::Show
void Show(MessageBoxConfig const &cfg)
Definition: GUI_MessageBox.cpp:42
RoR::Message::description
std::string description
Definition: GameContext.h:58
_LC
#define _LC(ctx, str)
Definition: Language.h:42
RoR::MSG_INVALID
@ MSG_INVALID
Definition: Application.h:83
RoR::GUI::MessageBoxButton::mbb_mq_message
MsgType mbb_mq_message
Message to queue on click.
Definition: GUI_MessageBox.h:44
RoR::GUI::MessageBoxButton::mbb_script_number
int mbb_script_number
Valid values are >= 1. -1 means not defined.
Definition: GUI_MessageBox.h:47
RoR::GUI::MessageBoxConfig::mbc_close_handle
bool * mbc_close_handle
External close handle - not required for mbc_allow_close.
Definition: GUI_MessageBox.h:55
RoR::GUI::MessageBoxConfig::mbc_always_ask_conf
CVar * mbc_always_ask_conf
If set, displays classic checkbox "[x] Always ask".
Definition: GUI_MessageBox.h:54
RoR::GUI::MessageBoxDialog::Draw
void Draw()
Definition: GUI_MessageBox.cpp:97
RoR::GUI::MessageBoxDialog::m_cfg
MessageBoxConfig m_cfg
Definition: GUI_MessageBox.h:76
RoR::GUI::MessageBoxConfig
Definition: GUI_MessageBox.h:50
RoR::Message
Unified game event system - all requests and state changes are reported using a message.
Definition: GameContext.h:51
RoR::CVar::setVal
void setVal(T val)
Definition: CVar.h:72
RoR::GUI::MessageBoxConfig::mbc_content_width
float mbc_content_width
Parameter to ImGui::SetContentWidth() - hard limit on content size.
Definition: GUI_MessageBox.h:57
RoR::GUI::MessageBoxConfig::mbc_allow_close
bool mbc_allow_close
Show close handle even if dbc_close_handle isn't set.
Definition: GUI_MessageBox.h:56
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::MessageBoxButton::mbb_mq_description
std::string mbb_mq_description
Message argument to queue on click.
Definition: GUI_MessageBox.h:45