Rigs of Rods 2023.09
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Loading...
Searching...
No Matches
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
37using namespace RoR;
38using 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
69void MessageBoxDialog::Show(const char* title, const char* text, bool allow_close, const char* button1_text, const char* button2_text)
70{
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
145 {
146 Message m(button.mbb_mq_message);
148 m.payload = button.mbb_mq_payload;
150 }
151
152 m_is_visible = false;
153 }
154}
155
Central state/object manager and communications hub.
#define ROR_ASSERT(_EXPR)
Definition Application.h:40
void LOG(const char *msg)
Legacy alias - formerly a macro.
Game state manager and message-queue provider.
#define _LC(ctx, str)
Definition Language.h:38
bool getBool() const
Definition CVar.h:98
void setVal(T val)
Definition CVar.h:72
bool * m_close_handle
If nullptr, close button is hidden. Otherwise visible.
void DrawButton(MessageBoxButton const &button)
void Show(MessageBoxConfig const &cfg)
void RequestGuiCaptureKeyboard(bool val)
Pass true during frame to prevent input passing to application.
void PushMessage(Message m)
Doesn't guarantee order! Use ChainMessage() if order matters.
const char * MsgTypeToString(MsgType type)
@ MSG_INVALID
Definition Application.h:84
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(),...
GUIManager * GetGuiManager()
GameContext * GetGameContext()
@ SE_GENERIC_MESSAGEBOX_CLICK
triggered when the user clicks on a message box button, the argument refers to the button pressed
void * mbb_mq_payload
Message argument to queue on click.
int mbb_script_number
Valid values are >= 1. -1 means not defined.
MsgType mbb_mq_message
Message to queue on click.
std::string mbb_mq_description
Message argument to queue on click.
float mbc_content_width
Parameter to ImGui::SetContentWidth() - hard limit on content size.
bool * mbc_close_handle
External close handle - not required for mbc_allow_close.
std::vector< MessageBoxButton > mbc_buttons
bool mbc_allow_close
Show close handle even if dbc_close_handle isn't set.
CVar * mbc_always_ask_conf
If set, displays classic checkbox "[x] Always ask".
Unified game event system - all requests and state changes are reported using a message.
Definition GameContext.h:52
std::string description
Definition GameContext.h:58
void * payload
Definition GameContext.h:59