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