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_GameChatBox.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-2019 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
22#include "GUI_GameChatBox.h"
23
24#include "Actor.h"
25#include "Application.h"
26#include "ChatSystem.h"
27#include "Console.h"
28#include "GUIManager.h"
29#include "GUIUtils.h"
30#include "Language.h"
31#include "InputEngine.h"
32
33#include <cstring> // strtok, strncmp
34#include <fmt/format.h>
35#include <imgui.h>
36
37using namespace RoR;
38using namespace GUI;
39
41{
42 m_console_view.cvw_filter_area_actor = false; // Disable vehicle spawn warnings/errors
43 m_console_view.cvw_filter_type_error = true; // Enable errors
44 m_console_view.cvw_filter_type_cmd = false; // Disable commands
47}
48
50{
52 ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0,0,0,0)); // Fully transparent background!
53 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0,0));
54
55 // Begin drawing the messages pane (no input)
56 ImGuiWindowFlags msg_flags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
57 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar;
58 const float width = ImGui::GetIO().DisplaySize.x - (2 * theme.screen_edge_padding.x);
59 ImVec2 msg_size(width, (ImGui::GetIO().DisplaySize.y / 3.f) + (2*ImGui::GetStyle().WindowPadding.y));
60 ImVec2 chat_size(width, ImGui::GetTextLineHeightWithSpacing() + 20);
61 ImVec2 msg_pos(theme.screen_edge_padding.x, ImGui::GetIO().DisplaySize.y - (msg_size.y + theme.screen_edge_padding.y));
62 if (m_is_visible)
63 {
64 msg_pos.y -= chat_size.y;
65 msg_size.y -= 6.f; // prevents partially bottom chat messages
66 }
67 else
68 {
69 msg_flags |= ImGuiWindowFlags_NoInputs;
70 }
71 ImGui::SetNextWindowPos(msg_pos);
72 ImGui::SetNextWindowSize(msg_size);
73
74 ImGui::Begin("ChatMessages", nullptr, msg_flags);
75
76 if (initialized)
77 {
79 fmt::format(_LC("ChatBox", "Press {} to spawn a vehicle"),
80 App::GetInputEngine()->getEventCommandTrimmed(EV_COMMON_GET_NEW_VEHICLE)), "lightbulb.png");
81 initialized = false;
82 }
83
84 if (m_is_visible)
85 {
88 m_console_view.cvw_msg_duration_ms = 3600000; // 1hour
93 if (init_scroll == false) // Initialize auto scrolling
94 {
96 ImGui::SetScrollFromPosY(9999); // Force to bottom once
97 init_scroll = true;
98 }
99 }
100 else
101 {
108 m_console_view.cvw_msg_duration_ms = 10000; // 10sec
109 if (init_scroll == true)
110 {
112 init_scroll = false;
113 }
114 }
115
116 if (!App::mp_chat_auto_hide->getBool())
117 {
118 m_console_view.cvw_msg_duration_ms = 2629800000; // 1month, should be enough
119 }
120
122
123 ImGui::End();
124
125 // Draw chat box
126 ImGuiWindowFlags chat_flags = ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoResize |
127 ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoScrollWithMouse;
128 if (!m_is_visible)
129 {
130 chat_flags |= ImGuiWindowFlags_NoInputs;
131 }
132
133 ImGui::SetNextWindowSize(chat_size);
134 ImGui::SetNextWindowPos(ImVec2(theme.screen_edge_padding.x, ImGui::GetIO().DisplaySize.y - (chat_size.y + theme.screen_edge_padding.y)));
135 ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4(0,0.0,0,0)); // Fully transparent background!
136 ImGui::Begin("ChatBottomBar", nullptr, chat_flags);
137
138 if (ImGui::IsKeyPressed(ImGui::GetKeyIndex(ImGuiKey_Escape)))
139 {
140 this->SetVisible(false);
141 }
142
143 if (m_is_visible) // Full display?
144 {
145 ImGui::Text("%s", _LC("ChatBox", "Chat history (use mouse wheel to scroll)"));
146 ImGui::Text("%s", _LC("ChatBox", "Message"));
147 ImGui::SameLine();
148 if (!m_kb_focused)
149 {
150 ImGui::SetKeyboardFocusHere();
151 m_kb_focused = true;
152 }
154 {
156 }
157 const ImGuiInputTextFlags cmd_flags = ImGuiInputTextFlags_EnterReturnsTrue;
158 if (ImGui::InputText("##chatbox", m_msg_buffer.GetBuffer(), m_msg_buffer.GetCapacity(), cmd_flags))
159 {
160 if (App::mp_state->getEnum<MpState>() == MpState::CONNECTED)
161 {
162 this->SubmitMessage();
163 }
165 this->SetVisible(false);
166 }
167 }
168
169 ImGui::End();
170
171 ImGui::PopStyleColor(2); // 2*WindowBg
172 ImGui::PopStyleVar(); // WindowPadding
173}
174
176{
177#ifdef USE_SOCKETW
178 if (m_msg_buffer.IsEmpty())
179 {
180 return;
181 }
182 if (std::strncmp(m_msg_buffer.GetBuffer(), "/whisper", 8) == 0)
183 {
184 std::strtok(m_msg_buffer.GetBuffer(), " ");
185 const char* username = std::strtok(nullptr, " ");
186 const char* message = std::strtok(nullptr, " ");
187 if (username != nullptr && message != nullptr)
188 {
189 RoRnet::UserInfo user;
190 if (App::GetNetwork()->FindUserInfo(username, user))
191 {
192 App::GetNetwork()->WhisperChatMsg(user, message);
193 }
194 else
195 {
198 fmt::format(_LC("ChatBox", "Whisper message not sent, unknown username: {}"), username));
199 }
200 }
201 else
202 {
205 _LC("ChatBox", "usage: /whisper username message"));
206 }
207 }
208 else
209 {
211 }
212#endif // USE_SOCKETW
213}
214
Central state/object manager and communications hub.
Handles controller inputs from player.
#define _LC(ctx, str)
Definition Language.h:38
@ CONSOLE_MSGTYPE_INFO
Generic message.
Definition Console.h:60
void putMessage(MessageArea area, MessageType type, std::string const &msg, std::string icon="")
Definition Console.cpp:103
@ CONSOLE_SYSTEM_NOTICE
Definition Console.h:51
@ CONSOLE_SYSTEM_WARNING
Definition Console.h:53
void SubmitMessage()
Flush the user input box.
bool m_is_visible
Special: false means 'display only messages'.
GuiTheme & GetTheme()
Definition GUIManager.h:168
void BroadcastChatMsg(const char *msg)
Definition Network.cpp:838
void WhisperChatMsg(RoRnet::UserInfo const &user, const char *msg)
Definition Network.cpp:843
Str & Clear()
Definition Str.h:54
char * GetBuffer()
Definition Str.h:48
bool IsEmpty() const
Definition Str.h:47
size_t GetCapacity() const
Definition Str.h:49
@ EV_COMMON_GET_NEW_VEHICLE
get new vehicle
InputEngine * GetInputEngine()
GUIManager * GetGuiManager()
CVar * mp_chat_auto_hide
CVar * mp_state
Console * GetConsole()
Network * GetNetwork()
bool ImMoveTextInputCursorToEnd(const char *label)
Definition GUIUtils.cpp:556
size_t cvw_msg_duration_ms
Message expiration; 0 means unlimited.