Console.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. {
  68. UnsubscribeFromEvent(E_SYSTEMUIFRAME);
  69. ImGui::SetWindowFocus(0);
  70. }
  71. }
  72. void Console::Toggle()
  73. {
  74. SetVisible(!IsVisible());
  75. }
  76. void Console::SetNumHistoryRows(unsigned rows)
  77. {
  78. historyRows_ = rows;
  79. if (history_.Size() > rows)
  80. history_.Resize(rows);
  81. }
  82. bool Console::IsVisible() const
  83. {
  84. return isOpen_;
  85. }
  86. bool Console::PopulateInterpreter()
  87. {
  88. EventReceiverGroup* group = context_->GetEventReceivers(E_CONSOLECOMMAND);
  89. if (!group || group->receivers_.Empty())
  90. return false;
  91. String currentInterpreterName;
  92. if (currentInterpreter_ < interpreters_.Size())
  93. currentInterpreterName = interpreters_[currentInterpreter_];
  94. interpreters_.Clear();
  95. interpretersPointers_.Clear();
  96. for (unsigned i = 0; i < group->receivers_.Size(); ++i)
  97. {
  98. Object* receiver = group->receivers_[i];
  99. if (receiver)
  100. {
  101. interpreters_.Push(receiver->GetTypeName());
  102. interpretersPointers_.Push(interpreters_.Back().CString());
  103. }
  104. }
  105. Sort(interpreters_.Begin(), interpreters_.End());
  106. currentInterpreter_ = interpreters_.IndexOf(currentInterpreterName);
  107. if (currentInterpreter_ == interpreters_.Size())
  108. currentInterpreter_ = 0;
  109. return true;
  110. }
  111. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  112. {
  113. using namespace LogMessage;
  114. int level = eventData[P_LEVEL].GetInt();
  115. String levelText;
  116. switch (level)
  117. {
  118. case LOG_DEBUG:
  119. levelText = "[Debug] ";
  120. break;
  121. case LOG_INFO:
  122. levelText = "[Info] ";
  123. break;
  124. case LOG_WARNING:
  125. levelText = "[Warning] ";
  126. break;
  127. case LOG_ERROR:
  128. levelText = "[Error] ";
  129. break;
  130. case LOG_NONE:
  131. case LOG_RAW:
  132. default:
  133. break;
  134. }
  135. // The message may be multi-line, so split to rows in that case
  136. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  137. for (unsigned i = 0; i < rows.Size(); ++i)
  138. history_.Push(levelText + rows[i]);
  139. scrollToEnd_ = true;
  140. if (autoVisibleOnError_ && level == LOG_ERROR && !IsVisible())
  141. SetVisible(true);
  142. }
  143. void Console::RenderUi(StringHash eventType, VariantMap& eventData)
  144. {
  145. Graphics* graphics = GetSubsystem<Graphics>();
  146. ImGui::SetNextWindowPos(ImVec2(0, 0));
  147. bool wasOpen = isOpen_;
  148. ImVec2 size(graphics->GetWidth(), windowSize_.y_);
  149. ImGui::SetNextWindowSize(size);
  150. if (ImGui::Begin("Debug Console", &isOpen_, ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoMove|
  151. ImGuiWindowFlags_NoSavedSettings))
  152. {
  153. auto region = ImGui::GetContentRegionAvail();
  154. ImGui::BeginChild("scrolling", ImVec2(region.x, region.y - 30), false, ImGuiWindowFlags_HorizontalScrollbar);
  155. for (const auto& row : history_)
  156. ImGui::TextUnformatted(row.CString());
  157. if (scrollToEnd_)
  158. {
  159. ImGui::SetScrollHere();
  160. scrollToEnd_ = false;
  161. }
  162. ImGui::EndChild();
  163. ImGui::PushItemWidth(100);
  164. if (ImGui::Combo("", &currentInterpreter_, &interpretersPointers_.Front(), interpretersPointers_.Size()))
  165. {
  166. }
  167. ImGui::PopItemWidth();
  168. ImGui::SameLine();
  169. ImGui::PushItemWidth(region.x - 110);
  170. if (focusInput_)
  171. {
  172. ImGui::SetKeyboardFocusHere();
  173. focusInput_ = false;
  174. }
  175. if (ImGui::InputText("", inputBuffer_, sizeof(inputBuffer_), ImGuiInputTextFlags_EnterReturnsTrue))
  176. {
  177. focusInput_ = true;
  178. String line(inputBuffer_);
  179. if (line.Length())
  180. {
  181. // Store to history, then clear the lineedit
  182. history_.Push(line);
  183. if (history_.Size() > historyRows_)
  184. history_.Erase(history_.Begin());
  185. scrollToEnd_ = true;
  186. inputBuffer_[0] = 0;
  187. // Send the command as an event for script subsystem
  188. using namespace ConsoleCommand;
  189. VariantMap& newEventData = GetEventDataMap();
  190. newEventData[P_COMMAND] = line;
  191. newEventData[P_ID] = interpreters_[currentInterpreter_];
  192. SendEvent(E_CONSOLECOMMAND, newEventData);
  193. }
  194. }
  195. ImGui::PopItemWidth();
  196. }
  197. else if (wasOpen)
  198. {
  199. SetVisible(false);
  200. ImGui::SetWindowFocus(0);
  201. SendEvent(E_CONSOLECLOSED);
  202. }
  203. windowSize_.y_ = ImGui::GetWindowHeight();
  204. ImGui::End();
  205. }
  206. void Console::Clear()
  207. {
  208. history_.Clear();
  209. }
  210. void Console::SetCommandInterpreter(const String& interpreter)
  211. {
  212. auto index = interpreters_.IndexOf(interpreter);
  213. if (index == interpreters_.Size())
  214. index = 0;
  215. currentInterpreter_ = index;
  216. }
  217. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  218. {
  219. Graphics* graphics = GetSubsystem<Graphics>();
  220. windowSize_.x_ = Clamp(windowSize_.x_, 0, graphics->GetWidth());
  221. windowSize_.y_ = Clamp(windowSize_.y_, 0, graphics->GetHeight());
  222. }
  223. }