Console.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Console.h"
  24. #include "Context.h"
  25. #include "CoreEvents.h"
  26. #include "EngineEvents.h"
  27. #include "Font.h"
  28. #include "Graphics.h"
  29. #include "GraphicsEvents.h"
  30. #include "InputEvents.h"
  31. #include "IOEvents.h"
  32. #include "LineEdit.h"
  33. #include "Log.h"
  34. #include "ResourceCache.h"
  35. #include "Text.h"
  36. #include "UI.h"
  37. #include "UIEvents.h"
  38. #include "DebugNew.h"
  39. namespace Urho3D
  40. {
  41. static const int DEFAULT_CONSOLE_ROWS = 16;
  42. static const int DEFAULT_HISTORY_SIZE = 16;
  43. Console::Console(Context* context) :
  44. Object(context),
  45. historyRows_(DEFAULT_HISTORY_SIZE),
  46. historyPosition_(0),
  47. printing_(false)
  48. {
  49. UI* ui = GetSubsystem<UI>();
  50. UIElement* uiRoot = ui->GetRoot();
  51. background_ = new BorderImage(context_);
  52. background_->SetBringToBack(false);
  53. background_->SetClipChildren(true);
  54. background_->SetEnabled(true);
  55. background_->SetVisible(false); // Hide by default
  56. background_->SetPriority(200); // Show on top of the debug HUD
  57. background_->SetLayout(LM_VERTICAL);
  58. rowContainer_ = new UIElement(context_);
  59. rowContainer_->SetClipChildren(true);
  60. rowContainer_->SetLayout(LM_VERTICAL);
  61. background_->AddChild(rowContainer_);
  62. lineEdit_ = new LineEdit(context_);
  63. lineEdit_->SetFocusMode(FM_FOCUSABLE); // Do not allow defocus with ESC
  64. background_->AddChild(lineEdit_);
  65. uiRoot->AddChild(background_);
  66. SetNumRows(DEFAULT_CONSOLE_ROWS);
  67. SubscribeToEvent(lineEdit_, E_TEXTFINISHED, HANDLER(Console, HandleTextFinished));
  68. SubscribeToEvent(lineEdit_, E_UNHANDLEDKEY, HANDLER(Console, HandleLineEditKey));
  69. SubscribeToEvent(E_SCREENMODE, HANDLER(Console, HandleScreenMode));
  70. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
  71. SubscribeToEvent(E_POSTUPDATE, HANDLER(Console, HandlePostUpdate));
  72. }
  73. Console::~Console()
  74. {
  75. background_->Remove();
  76. }
  77. void Console::SetDefaultStyle(XMLFile* style)
  78. {
  79. if (!style)
  80. return;
  81. background_->SetDefaultStyle(style);
  82. background_->SetStyle("ConsoleBackground");
  83. const Vector<SharedPtr<UIElement> >& children = rowContainer_->GetChildren();
  84. for (unsigned i = 0; i < children.Size(); ++i)
  85. children[i]->SetStyle("ConsoleText");
  86. lineEdit_->SetStyle("ConsoleLineEdit");
  87. UpdateElements();
  88. }
  89. void Console::SetVisible(bool enable)
  90. {
  91. background_->SetVisible(enable);
  92. if (enable)
  93. {
  94. // Check if we have handler for E_CONSOLECOMMAND every time here in case the handler is being added later dynamically
  95. bool hasConsoleCommandEventHandler = context_->GetEventReceivers(this, E_CONSOLECOMMAND) != 0 || context_->GetEventReceivers(E_CONSOLECOMMAND) != 0;
  96. lineEdit_->SetVisible(hasConsoleCommandEventHandler);
  97. if (hasConsoleCommandEventHandler)
  98. GetSubsystem<UI>()->SetFocusElement(lineEdit_);
  99. // Ensure the background has no empty space when shown without the lineedit
  100. background_->SetHeight(background_->GetMinHeight());
  101. }
  102. else
  103. lineEdit_->SetFocus(false);
  104. }
  105. void Console::Toggle()
  106. {
  107. SetVisible(!IsVisible());
  108. }
  109. void Console::SetNumRows(unsigned rows)
  110. {
  111. if (!rows)
  112. return;
  113. rowContainer_->DisableLayoutUpdate();
  114. int delta = rowContainer_->GetNumChildren() - rows;
  115. if (delta > 0)
  116. {
  117. // We have more, remove oldest rows first
  118. for (int i = 0; i < delta; ++i)
  119. rowContainer_->RemoveChildAtIndex(0);
  120. }
  121. else
  122. {
  123. // We have less, add more rows at the bottom (text element style will be set initially in SetDefaultStyle() when the stylesheet is available and subsequently in HandlePostUpdate())
  124. for (int i = 0; i > delta; --i)
  125. rowContainer_->CreateChild<Text>();
  126. }
  127. rowContainer_->EnableLayoutUpdate();
  128. rowContainer_->UpdateLayout();
  129. UpdateElements();
  130. }
  131. void Console::SetNumHistoryRows(unsigned rows)
  132. {
  133. historyRows_ = rows;
  134. if (history_.Size() > rows)
  135. history_.Resize(rows);
  136. if (historyPosition_ > rows)
  137. historyPosition_ = rows;
  138. }
  139. void Console::UpdateElements()
  140. {
  141. int width = GetSubsystem<Graphics>()->GetWidth();
  142. const IntRect& border = background_->GetLayoutBorder();
  143. background_->SetFixedWidth(width);
  144. background_->SetHeight(background_->GetMinHeight());
  145. rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
  146. }
  147. XMLFile* Console::GetDefaultStyle() const
  148. {
  149. return background_->GetDefaultStyle(false);
  150. }
  151. bool Console::IsVisible() const
  152. {
  153. return background_ ? background_->IsVisible() : false;
  154. }
  155. unsigned Console::GetNumRows() const
  156. {
  157. return rowContainer_->GetNumChildren();
  158. }
  159. const String& Console::GetHistoryRow(unsigned index) const
  160. {
  161. return index < history_.Size() ? history_[index] : String::EMPTY;
  162. }
  163. void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
  164. {
  165. using namespace TextFinished;
  166. String line = lineEdit_->GetText();
  167. if (!line.Empty())
  168. {
  169. // Send the command as an event for script subsystem
  170. using namespace ConsoleCommand;
  171. VariantMap& eventData = GetEventDataMap();
  172. eventData[P_COMMAND] = line;
  173. SendEvent(E_CONSOLECOMMAND, eventData);
  174. // Store to history, then clear the lineedit
  175. history_.Push(line);
  176. if (history_.Size() > historyRows_)
  177. history_.Erase(history_.Begin());
  178. historyPosition_ = history_.Size();
  179. currentRow_.Clear();
  180. lineEdit_->SetText(currentRow_);
  181. }
  182. }
  183. void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
  184. {
  185. if (!historyRows_)
  186. return;
  187. using namespace UnhandledKey;
  188. bool changed = false;
  189. switch (eventData[P_KEY].GetInt())
  190. {
  191. case KEY_UP:
  192. if (historyPosition_ > 0)
  193. {
  194. if (historyPosition_ == history_.Size())
  195. currentRow_ = lineEdit_->GetText();
  196. --historyPosition_;
  197. changed = true;
  198. }
  199. break;
  200. case KEY_DOWN:
  201. if (historyPosition_ < history_.Size())
  202. {
  203. ++historyPosition_;
  204. changed = true;
  205. }
  206. break;
  207. }
  208. if (changed)
  209. {
  210. if (historyPosition_ < history_.Size())
  211. lineEdit_->SetText(history_[historyPosition_]);
  212. else
  213. lineEdit_->SetText(currentRow_);
  214. }
  215. }
  216. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  217. {
  218. UpdateElements();
  219. }
  220. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  221. {
  222. // If printing a log message causes more messages to be logged (error accessing font), disregard them
  223. if (printing_)
  224. return;
  225. using namespace LogMessage;
  226. int level = eventData[P_LEVEL].GetInt();
  227. // The message may be multi-line, so split to rows in that case
  228. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  229. for (unsigned i = 0; i < rows.Size(); ++i)
  230. pendingRows_.Push(MakePair(level, rows[i]));
  231. }
  232. void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  233. {
  234. if (!rowContainer_->GetNumChildren())
  235. return;
  236. printing_ = true;
  237. rowContainer_->DisableLayoutUpdate();
  238. for (unsigned i = 0; i < pendingRows_.Size(); ++i)
  239. {
  240. rowContainer_->RemoveChildAtIndex(0);
  241. Text* text = rowContainer_->CreateChild<Text>();
  242. text->SetText(pendingRows_[i].second_);
  243. // Make error message highlight
  244. text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
  245. }
  246. pendingRows_.Clear();
  247. rowContainer_->EnableLayoutUpdate();
  248. rowContainer_->UpdateLayout();
  249. printing_ = false;
  250. }
  251. }