Console.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // Copyright (c) 2017 the Atomic project.
  3. // Copyright (c) 2008-2015 the Urho3D project.
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "../../Core/Context.h"
  24. #include "../../Core/CoreEvents.h"
  25. #include "../../Engine/EngineEvents.h"
  26. #include "../../Graphics/Graphics.h"
  27. #include "../../Graphics/GraphicsEvents.h"
  28. #include "../../Input/Input.h"
  29. #include "../../IO/IOEvents.h"
  30. #include "../../IO/Log.h"
  31. #include "../../Resource/ResourceCache.h"
  32. #include "SystemUI.h"
  33. #include "SystemUIEvents.h"
  34. #include "Console.h"
  35. #include "../../DebugNew.h"
  36. namespace Atomic
  37. {
  38. static const int DEFAULT_HISTORY_SIZE = 512;
  39. Console::Console(Context* context) :
  40. Object(context),
  41. autoVisibleOnError_(false),
  42. historyRows_(DEFAULT_HISTORY_SIZE),
  43. isOpen_(false),
  44. windowSize_(M_MAX_INT, 200) // Width gets clamped by HandleScreenMode()
  45. {
  46. inputBuffer_[0] = 0;
  47. SetNumHistoryRows(DEFAULT_HISTORY_SIZE);
  48. VariantMap dummy;
  49. HandleScreenMode(0, dummy);
  50. PopulateInterpreter();
  51. SubscribeToEvent(E_SCREENMODE, ATOMIC_HANDLER(Console, HandleScreenMode));
  52. SubscribeToEvent(E_LOGMESSAGE, ATOMIC_HANDLER(Console, HandleLogMessage));
  53. }
  54. Console::~Console()
  55. {
  56. UnsubscribeFromAllEvents();
  57. }
  58. void Console::SetVisible(bool enable)
  59. {
  60. isOpen_ = enable;
  61. if (isOpen_)
  62. {
  63. focusInput_ = true;
  64. SubscribeToEvent(E_SYSTEMUIFRAME, ATOMIC_HANDLER(Console, RenderUi));
  65. }
  66. else
  67. UnsubscribeFromEvent(E_SYSTEMUIFRAME);
  68. }
  69. void Console::Toggle()
  70. {
  71. SetVisible(!IsVisible());
  72. }
  73. void Console::SetNumHistoryRows(unsigned rows)
  74. {
  75. historyRows_ = rows;
  76. if (history_.Size() > rows)
  77. history_.Resize(rows);
  78. }
  79. bool Console::IsVisible() const
  80. {
  81. return isOpen_;
  82. }
  83. bool Console::PopulateInterpreter()
  84. {
  85. EventReceiverGroup* group = context_->GetEventReceivers(E_CONSOLECOMMAND);
  86. if (!group || group->receivers_.Empty())
  87. return false;
  88. String currentInterpreterName;
  89. if (currentInterpreter_ < interpreters_.Size())
  90. currentInterpreterName = interpreters_[currentInterpreter_];
  91. interpreters_.Clear();
  92. interpretersPointers_.Clear();
  93. for (unsigned i = 0; i < group->receivers_.Size(); ++i)
  94. {
  95. Object* receiver = group->receivers_[i];
  96. if (receiver)
  97. {
  98. interpreters_.Push(receiver->GetTypeName());
  99. interpretersPointers_.Push(interpreters_.Back().CString());
  100. }
  101. }
  102. Sort(interpreters_.Begin(), interpreters_.End());
  103. currentInterpreter_ = interpreters_.IndexOf(currentInterpreterName);
  104. if (currentInterpreter_ == interpreters_.Size())
  105. currentInterpreter_ = 0;
  106. return true;
  107. }
  108. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  109. {
  110. using namespace LogMessage;
  111. int level = eventData[P_LEVEL].GetInt();
  112. String levelText;
  113. switch (level)
  114. {
  115. case LOG_DEBUG:
  116. levelText = "[Debug] ";
  117. break;
  118. case LOG_INFO:
  119. levelText = "[Info] ";
  120. break;
  121. case LOG_WARNING:
  122. levelText = "[Warning] ";
  123. break;
  124. case LOG_ERROR:
  125. levelText = "[Error] ";
  126. break;
  127. case LOG_NONE:
  128. case LOG_RAW:
  129. default:
  130. break;
  131. }
  132. // The message may be multi-line, so split to rows in that case
  133. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  134. for (unsigned i = 0; i < rows.Size(); ++i)
  135. history_.Push(levelText + rows[i]);
  136. scrollToEnd_ = true;
  137. if (autoVisibleOnError_ && level == LOG_ERROR && !IsVisible())
  138. SetVisible(true);
  139. }
  140. void Console::RenderUi(StringHash eventType, VariantMap& eventData)
  141. {
  142. Graphics* graphics = GetSubsystem<Graphics>();
  143. ImGui::SetNextWindowPos(ImVec2(0, 0));
  144. bool wasOpen = isOpen_;
  145. ImVec2 size(graphics->GetWidth(), windowSize_.y_);
  146. ImGui::SetNextWindowSize(size);
  147. if (ImGui::Begin("Debug Console", &isOpen_, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|
  148. ImGuiWindowFlags_NoSavedSettings))
  149. {
  150. auto region = ImGui::GetContentRegionAvail();
  151. ImGui::BeginChild("scrolling", ImVec2(region.x, region.y - 30), false, ImGuiWindowFlags_HorizontalScrollbar);
  152. for (const auto& row : history_)
  153. ImGui::TextUnformatted(row.CString());
  154. if (scrollToEnd_)
  155. {
  156. ImGui::SetScrollHere();
  157. scrollToEnd_ = false;
  158. }
  159. ImGui::EndChild();
  160. ImGui::PushItemWidth(100);
  161. if (ImGui::Combo("", &currentInterpreter_, &interpretersPointers_.Front(), interpretersPointers_.Size()))
  162. {
  163. }
  164. ImGui::PopItemWidth();
  165. ImGui::SameLine();
  166. ImGui::PushItemWidth(region.x - 110);
  167. if (focusInput_)
  168. {
  169. ImGui::SetKeyboardFocusHere();
  170. focusInput_ = false;
  171. }
  172. if (ImGui::InputText("", inputBuffer_, sizeof(inputBuffer_), ImGuiInputTextFlags_EnterReturnsTrue))
  173. {
  174. focusInput_ = true;
  175. String line(inputBuffer_);
  176. if (line.Length())
  177. {
  178. // Store to history, then clear the lineedit
  179. history_.Push(line);
  180. if (history_.Size() > historyRows_)
  181. history_.Erase(history_.Begin());
  182. scrollToEnd_ = true;
  183. inputBuffer_[0] = 0;
  184. // Send the command as an event for script subsystem
  185. using namespace ConsoleCommand;
  186. VariantMap& newEventData = GetEventDataMap();
  187. newEventData[P_COMMAND] = line;
  188. newEventData[P_ID] = interpreters_[currentInterpreter_];
  189. SendEvent(E_CONSOLECOMMAND, newEventData);
  190. }
  191. }
  192. ImGui::PopItemWidth();
  193. }
  194. else if (wasOpen)
  195. {
  196. SetVisible(false);
  197. ImGui::SetWindowFocus(0);
  198. SendEvent(E_CONSOLECLOSED);
  199. }
  200. windowSize_.y_ = ImGui::GetWindowHeight();
  201. if (ImGui::IsWindowFocused() && ImGui::IsKeyPressed(SCANCODE_ESCAPE))
  202. {
  203. SetVisible(false);
  204. ImGui::SetWindowFocus(0);
  205. }
  206. ImGui::End();
  207. }
  208. void Console::Clear()
  209. {
  210. history_.Clear();
  211. }
  212. void Console::SetCommandInterpreter(const String& interpreter)
  213. {
  214. auto index = interpreters_.IndexOf(interpreter);
  215. if (index == interpreters_.Size())
  216. index = 0;
  217. currentInterpreter_ = index;
  218. }
  219. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  220. {
  221. Graphics* graphics = GetSubsystem<Graphics>();
  222. windowSize_.x_ = Clamp(windowSize_.x_, 0, graphics->GetWidth());
  223. windowSize_.y_ = Clamp(windowSize_.y_, 0, graphics->GetHeight());
  224. }
  225. }