Console.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "Input.h"
  31. #include "InputEvents.h"
  32. #include "IOEvents.h"
  33. #include "LineEdit.h"
  34. #include "ListView.h"
  35. #include "Log.h"
  36. #include "ResourceCache.h"
  37. #include "ScrollBar.h"
  38. #include "Text.h"
  39. #include "UI.h"
  40. #include "UIEvents.h"
  41. #include "DebugNew.h"
  42. namespace Urho3D
  43. {
  44. static const int DEFAULT_CONSOLE_ROWS = 16;
  45. static const int DEFAULT_HISTORY_SIZE = 16;
  46. Console::Console(Context* context) :
  47. Object(context),
  48. autoVisibleOnError_(false),
  49. historyRows_(DEFAULT_HISTORY_SIZE),
  50. historyPosition_(0),
  51. printing_(false)
  52. {
  53. UI* ui = GetSubsystem<UI>();
  54. UIElement* uiRoot = ui->GetRoot();
  55. // By default prevent the automatic showing of the screen keyboard
  56. focusOnShow_ = !ui->GetUseScreenKeyboard();
  57. background_ = new BorderImage(context_);
  58. background_->SetBringToBack(false);
  59. background_->SetClipChildren(true);
  60. background_->SetEnabled(true);
  61. background_->SetVisible(false); // Hide by default
  62. background_->SetPriority(200); // Show on top of the debug HUD
  63. background_->SetLayout(LM_VERTICAL);
  64. rowContainer_ = new ListView(context_);
  65. background_->AddChild(rowContainer_);
  66. lineEdit_ = new LineEdit(context_);
  67. lineEdit_->SetFocusMode(FM_FOCUSABLE); // Do not allow defocus with ESC
  68. background_->AddChild(lineEdit_);
  69. uiRoot->AddChild(background_);
  70. SetNumRows(DEFAULT_CONSOLE_ROWS);
  71. SubscribeToEvent(lineEdit_, E_TEXTFINISHED, HANDLER(Console, HandleTextFinished));
  72. SubscribeToEvent(lineEdit_, E_UNHANDLEDKEY, HANDLER(Console, HandleLineEditKey));
  73. SubscribeToEvent(E_SCREENMODE, HANDLER(Console, HandleScreenMode));
  74. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
  75. SubscribeToEvent(E_POSTUPDATE, HANDLER(Console, HandlePostUpdate));
  76. }
  77. Console::~Console()
  78. {
  79. background_->Remove();
  80. }
  81. void Console::SetDefaultStyle(XMLFile* style)
  82. {
  83. if (!style)
  84. return;
  85. background_->SetDefaultStyle(style);
  86. background_->SetStyle("ConsoleBackground");
  87. rowContainer_->SetStyle("ListView");
  88. for (unsigned i = 0; i < rowContainer_->GetNumItems(); ++i)
  89. rowContainer_->GetItem(i)->SetStyle("ConsoleText");
  90. lineEdit_->SetStyle("ConsoleLineEdit");
  91. UpdateElements();
  92. }
  93. void Console::SetVisible(bool enable)
  94. {
  95. Input* input = GetSubsystem<Input>();
  96. background_->SetVisible(enable);
  97. if (enable)
  98. {
  99. // Check if we have handler for E_CONSOLECOMMAND every time here in case the handler is being added later dynamically
  100. bool hasConsoleCommandEventHandler = context_->GetEventReceivers(this, E_CONSOLECOMMAND) != 0 ||
  101. context_->GetEventReceivers(E_CONSOLECOMMAND) != 0;
  102. lineEdit_->SetVisible(hasConsoleCommandEventHandler);
  103. if (hasConsoleCommandEventHandler && focusOnShow_)
  104. GetSubsystem<UI>()->SetFocusElement(lineEdit_);
  105. // Ensure the background has no empty space when shown without the lineedit
  106. background_->SetHeight(background_->GetMinHeight());
  107. // Show OS mouse
  108. savedMouseVisibility_ = input->IsMouseVisible();
  109. input->SetMouseVisible(true);
  110. }
  111. else
  112. {
  113. lineEdit_->SetFocus(false);
  114. // Restore OS mouse visibility
  115. input->SetMouseVisible(savedMouseVisibility_);
  116. }
  117. }
  118. void Console::Toggle()
  119. {
  120. SetVisible(!IsVisible());
  121. }
  122. void Console::SetNumBufferedRows(unsigned rows)
  123. {
  124. if (rows < displayedRows_)
  125. return;
  126. rowContainer_->DisableLayoutUpdate();
  127. int delta = rowContainer_->GetNumItems() - rows;
  128. if (delta > 0)
  129. {
  130. // We have more, remove oldest rows first
  131. for (int i = 0; i < delta; ++i)
  132. rowContainer_->RemoveItem((unsigned)0);
  133. }
  134. else
  135. {
  136. // We have less, add more rows at the top
  137. for (int i = 0; i > delta; --i)
  138. {
  139. Text* text = new Text(context_);
  140. // If style is already set, apply here to ensure proper height of the console when
  141. // amount of rows is changed
  142. if (background_->GetDefaultStyle())
  143. text->SetStyle("ConsoleText");
  144. rowContainer_->InsertItem(0, text);
  145. }
  146. }
  147. rowContainer_->EnsureItemVisibility(rowContainer_->GetItem(rowContainer_->GetNumItems() - 1));
  148. rowContainer_->EnableLayoutUpdate();
  149. rowContainer_->UpdateLayout();
  150. UpdateElements();
  151. }
  152. void Console::SetNumRows(unsigned rows)
  153. {
  154. if (!rows)
  155. return;
  156. displayedRows_ = rows;
  157. if (GetNumBufferedRows() < rows)
  158. SetNumBufferedRows(rows);
  159. UpdateElements();
  160. }
  161. void Console::SetNumHistoryRows(unsigned rows)
  162. {
  163. historyRows_ = rows;
  164. if (history_.Size() > rows)
  165. history_.Resize(rows);
  166. if (historyPosition_ > rows)
  167. historyPosition_ = rows;
  168. }
  169. void Console::SetFocusOnShow(bool enable)
  170. {
  171. focusOnShow_ = enable;
  172. }
  173. void Console::UpdateElements()
  174. {
  175. int width = GetSubsystem<Graphics>()->GetWidth();
  176. const IntRect& border = background_->GetLayoutBorder();
  177. const IntRect& panelBorder = rowContainer_->GetScrollPanel()->GetClipBorder();
  178. background_->SetFixedWidth(width);
  179. background_->SetHeight(background_->GetMinHeight());
  180. rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
  181. rowContainer_->SetFixedHeight(displayedRows_ * rowContainer_->GetItem((unsigned)0)->GetHeight() + panelBorder.top_ + panelBorder.bottom_ +
  182. (rowContainer_->GetHorizontalScrollBar()->IsVisible() ? rowContainer_->GetHorizontalScrollBar()->GetHeight() : 0));
  183. }
  184. XMLFile* Console::GetDefaultStyle() const
  185. {
  186. return background_->GetDefaultStyle(false);
  187. }
  188. bool Console::IsVisible() const
  189. {
  190. return background_ && background_->IsVisible();
  191. }
  192. unsigned Console::GetNumBufferedRows() const
  193. {
  194. return rowContainer_->GetNumItems();
  195. }
  196. const String& Console::GetHistoryRow(unsigned index) const
  197. {
  198. return index < history_.Size() ? history_[index] : String::EMPTY;
  199. }
  200. void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
  201. {
  202. using namespace TextFinished;
  203. String line = lineEdit_->GetText();
  204. if (!line.Empty())
  205. {
  206. // Send the command as an event for script subsystem
  207. using namespace ConsoleCommand;
  208. VariantMap& eventData = GetEventDataMap();
  209. eventData[P_COMMAND] = line;
  210. SendEvent(E_CONSOLECOMMAND, eventData);
  211. // Store to history, then clear the lineedit
  212. history_.Push(line);
  213. if (history_.Size() > historyRows_)
  214. history_.Erase(history_.Begin());
  215. historyPosition_ = history_.Size();
  216. currentRow_.Clear();
  217. lineEdit_->SetText(currentRow_);
  218. }
  219. }
  220. void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
  221. {
  222. if (!historyRows_)
  223. return;
  224. using namespace UnhandledKey;
  225. bool changed = false;
  226. switch (eventData[P_KEY].GetInt())
  227. {
  228. case KEY_UP:
  229. if (historyPosition_ > 0)
  230. {
  231. if (historyPosition_ == history_.Size())
  232. currentRow_ = lineEdit_->GetText();
  233. --historyPosition_;
  234. changed = true;
  235. }
  236. break;
  237. case KEY_DOWN:
  238. if (historyPosition_ < history_.Size())
  239. {
  240. ++historyPosition_;
  241. changed = true;
  242. }
  243. break;
  244. }
  245. if (changed)
  246. {
  247. if (historyPosition_ < history_.Size())
  248. lineEdit_->SetText(history_[historyPosition_]);
  249. else
  250. lineEdit_->SetText(currentRow_);
  251. }
  252. }
  253. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  254. {
  255. UpdateElements();
  256. }
  257. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  258. {
  259. // If printing a log message causes more messages to be logged (error accessing font), disregard them
  260. if (printing_)
  261. return;
  262. using namespace LogMessage;
  263. int level = eventData[P_LEVEL].GetInt();
  264. // The message may be multi-line, so split to rows in that case
  265. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  266. for (unsigned i = 0; i < rows.Size(); ++i)
  267. pendingRows_.Push(MakePair(level, rows[i]));
  268. if (autoVisibleOnError_ && level == LOG_ERROR && !IsVisible())
  269. SetVisible(true);
  270. }
  271. void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  272. {
  273. if (!rowContainer_->GetNumItems() || pendingRows_.Empty())
  274. return;
  275. printing_ = true;
  276. rowContainer_->DisableLayoutUpdate();
  277. Text* text;
  278. for (unsigned i = 0; i < pendingRows_.Size(); ++i)
  279. {
  280. rowContainer_->RemoveItem((unsigned)0);
  281. text = new Text(context_);
  282. text->SetText(pendingRows_[i].second_);
  283. // Make error message highlight
  284. text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
  285. rowContainer_->AddItem(text);
  286. }
  287. pendingRows_.Clear();
  288. rowContainer_->EnsureItemVisibility(text);
  289. rowContainer_->EnableLayoutUpdate();
  290. rowContainer_->UpdateLayout();
  291. UpdateElements(); // May need to readjust the height due to scrollbar visibility changes
  292. printing_ = false;
  293. }
  294. }