Console.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 ||
  96. context_->GetEventReceivers(E_CONSOLECOMMAND) != 0;
  97. lineEdit_->SetVisible(hasConsoleCommandEventHandler);
  98. if (hasConsoleCommandEventHandler)
  99. GetSubsystem<UI>()->SetFocusElement(lineEdit_);
  100. // Ensure the background has no empty space when shown without the lineedit
  101. background_->SetHeight(background_->GetMinHeight());
  102. }
  103. else
  104. lineEdit_->SetFocus(false);
  105. }
  106. void Console::Toggle()
  107. {
  108. SetVisible(!IsVisible());
  109. }
  110. void Console::SetNumRows(unsigned rows)
  111. {
  112. if (!rows)
  113. return;
  114. rowContainer_->DisableLayoutUpdate();
  115. int delta = rowContainer_->GetNumChildren() - rows;
  116. if (delta > 0)
  117. {
  118. // We have more, remove oldest rows first
  119. for (int i = 0; i < delta; ++i)
  120. rowContainer_->RemoveChildAtIndex(0);
  121. }
  122. else
  123. {
  124. // We have less, add more rows at the bottom
  125. for (int i = 0; i > delta; --i)
  126. {
  127. Text* text = rowContainer_->CreateChild<Text>();
  128. // If style is already set, apply here to ensure proper height of the console when
  129. // amount of rows is changed
  130. if (background_->GetDefaultStyle())
  131. text->SetStyle("ConsoleText");
  132. }
  133. }
  134. rowContainer_->EnableLayoutUpdate();
  135. rowContainer_->UpdateLayout();
  136. UpdateElements();
  137. }
  138. void Console::SetNumHistoryRows(unsigned rows)
  139. {
  140. historyRows_ = rows;
  141. if (history_.Size() > rows)
  142. history_.Resize(rows);
  143. if (historyPosition_ > rows)
  144. historyPosition_ = rows;
  145. }
  146. void Console::UpdateElements()
  147. {
  148. int width = GetSubsystem<Graphics>()->GetWidth();
  149. const IntRect& border = background_->GetLayoutBorder();
  150. background_->SetFixedWidth(width);
  151. background_->SetHeight(background_->GetMinHeight());
  152. rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
  153. }
  154. XMLFile* Console::GetDefaultStyle() const
  155. {
  156. return background_->GetDefaultStyle(false);
  157. }
  158. bool Console::IsVisible() const
  159. {
  160. return background_ ? background_->IsVisible() : false;
  161. }
  162. unsigned Console::GetNumRows() const
  163. {
  164. return rowContainer_->GetNumChildren();
  165. }
  166. const String& Console::GetHistoryRow(unsigned index) const
  167. {
  168. return index < history_.Size() ? history_[index] : String::EMPTY;
  169. }
  170. void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
  171. {
  172. using namespace TextFinished;
  173. String line = lineEdit_->GetText();
  174. if (!line.Empty())
  175. {
  176. // Send the command as an event for script subsystem
  177. using namespace ConsoleCommand;
  178. VariantMap& eventData = GetEventDataMap();
  179. eventData[P_COMMAND] = line;
  180. SendEvent(E_CONSOLECOMMAND, eventData);
  181. // Store to history, then clear the lineedit
  182. history_.Push(line);
  183. if (history_.Size() > historyRows_)
  184. history_.Erase(history_.Begin());
  185. historyPosition_ = history_.Size();
  186. currentRow_.Clear();
  187. lineEdit_->SetText(currentRow_);
  188. }
  189. }
  190. void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
  191. {
  192. if (!historyRows_)
  193. return;
  194. using namespace UnhandledKey;
  195. bool changed = false;
  196. switch (eventData[P_KEY].GetInt())
  197. {
  198. case KEY_UP:
  199. if (historyPosition_ > 0)
  200. {
  201. if (historyPosition_ == history_.Size())
  202. currentRow_ = lineEdit_->GetText();
  203. --historyPosition_;
  204. changed = true;
  205. }
  206. break;
  207. case KEY_DOWN:
  208. if (historyPosition_ < history_.Size())
  209. {
  210. ++historyPosition_;
  211. changed = true;
  212. }
  213. break;
  214. }
  215. if (changed)
  216. {
  217. if (historyPosition_ < history_.Size())
  218. lineEdit_->SetText(history_[historyPosition_]);
  219. else
  220. lineEdit_->SetText(currentRow_);
  221. }
  222. }
  223. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  224. {
  225. UpdateElements();
  226. }
  227. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  228. {
  229. // If printing a log message causes more messages to be logged (error accessing font), disregard them
  230. if (printing_)
  231. return;
  232. using namespace LogMessage;
  233. int level = eventData[P_LEVEL].GetInt();
  234. // The message may be multi-line, so split to rows in that case
  235. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  236. for (unsigned i = 0; i < rows.Size(); ++i)
  237. pendingRows_.Push(MakePair(level, rows[i]));
  238. }
  239. void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  240. {
  241. if (!rowContainer_->GetNumChildren())
  242. return;
  243. printing_ = true;
  244. rowContainer_->DisableLayoutUpdate();
  245. for (unsigned i = 0; i < pendingRows_.Size(); ++i)
  246. {
  247. rowContainer_->RemoveChildAtIndex(0);
  248. Text* text = rowContainer_->CreateChild<Text>();
  249. text->SetText(pendingRows_[i].second_);
  250. // Make error message highlight
  251. text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
  252. }
  253. pendingRows_.Clear();
  254. rowContainer_->EnableLayoutUpdate();
  255. rowContainer_->UpdateLayout();
  256. printing_ = false;
  257. }
  258. }