Console.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. #ifdef __disabled
  2. //
  3. // Copyright (c) 2008-2014 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 "Precompiled.h"
  24. #include "../Engine/Console.h"
  25. #include "../Core/Context.h"
  26. #include "../Core/CoreEvents.h"
  27. #include "../Engine/EngineEvents.h"
  28. #include "../Graphics/Graphics.h"
  29. #include "../Graphics/GraphicsEvents.h"
  30. #include "../Input/Input.h"
  31. #include "../Input/InputEvents.h"
  32. #include "../IO/IOEvents.h"
  33. #include "../IO/Log.h"
  34. #include "../Resource/ResourceCache.h"
  35. #include "../UI/UI.h"
  36. #include "../UI/UIEvents.h"
  37. #include "../DebugNew.h"
  38. namespace Atomic
  39. {
  40. static const int DEFAULT_CONSOLE_ROWS = 16;
  41. static const int DEFAULT_HISTORY_SIZE = 16;
  42. Console::Console(Context* context) :
  43. Object(context),
  44. autoVisibleOnError_(false),
  45. historyRows_(DEFAULT_HISTORY_SIZE),
  46. historyPosition_(0),
  47. printing_(false)
  48. {
  49. UI* ui = GetSubsystem<UI>();
  50. UIElement* uiRoot = ui->GetRoot();
  51. // By default prevent the automatic showing of the screen keyboard
  52. focusOnShow_ = !ui->GetUseScreenKeyboard();
  53. background_ = uiRoot->CreateChild<BorderImage>();
  54. background_->SetBringToBack(false);
  55. background_->SetClipChildren(true);
  56. background_->SetEnabled(true);
  57. background_->SetVisible(false); // Hide by default
  58. background_->SetPriority(200); // Show on top of the debug HUD
  59. background_->SetBringToBack(false);
  60. background_->SetLayout(LM_VERTICAL);
  61. rowContainer_ = background_->CreateChild<ListView>();
  62. rowContainer_->SetHighlightMode(HM_ALWAYS);
  63. rowContainer_->SetMultiselect(true);
  64. commandLine_ = background_->CreateChild<UIElement>();
  65. commandLine_->SetLayoutMode(LM_HORIZONTAL);
  66. commandLine_->SetLayoutSpacing(1);
  67. interpreters_ = commandLine_->CreateChild<DropDownList>();
  68. lineEdit_ = commandLine_->CreateChild<LineEdit>();
  69. lineEdit_->SetFocusMode(FM_FOCUSABLE); // Do not allow defocus with ESC
  70. closeButton_ = uiRoot->CreateChild<Button>();
  71. closeButton_->SetVisible(false);
  72. closeButton_->SetPriority(background_->GetPriority() + 1); // Show on top of console's background
  73. closeButton_->SetBringToBack(false);
  74. SetNumRows(DEFAULT_CONSOLE_ROWS);
  75. SubscribeToEvent(interpreters_, E_ITEMSELECTED, HANDLER(Console, HandleInterpreterSelected));
  76. SubscribeToEvent(lineEdit_, E_TEXTFINISHED, HANDLER(Console, HandleTextFinished));
  77. SubscribeToEvent(lineEdit_, E_UNHANDLEDKEY, HANDLER(Console, HandleLineEditKey));
  78. SubscribeToEvent(closeButton_, E_RELEASED, HANDLER(Console, HandleCloseButtonPressed));
  79. SubscribeToEvent(E_SCREENMODE, HANDLER(Console, HandleScreenMode));
  80. SubscribeToEvent(E_LOGMESSAGE, HANDLER(Console, HandleLogMessage));
  81. SubscribeToEvent(E_POSTUPDATE, HANDLER(Console, HandlePostUpdate));
  82. }
  83. Console::~Console()
  84. {
  85. background_->Remove();
  86. closeButton_->Remove();
  87. }
  88. void Console::SetDefaultStyle(XMLFile* style)
  89. {
  90. if (!style)
  91. return;
  92. background_->SetDefaultStyle(style);
  93. background_->SetStyle("ConsoleBackground");
  94. rowContainer_->SetStyleAuto();
  95. for (unsigned i = 0; i < rowContainer_->GetNumItems(); ++i)
  96. rowContainer_->GetItem(i)->SetStyle("ConsoleText");
  97. interpreters_->SetStyleAuto();
  98. for (unsigned i = 0; i < interpreters_->GetNumItems(); ++i)
  99. interpreters_->GetItem(i)->SetStyle("ConsoleText");
  100. lineEdit_->SetStyle("ConsoleLineEdit");
  101. closeButton_->SetDefaultStyle(style);
  102. closeButton_->SetStyle("CloseButton");
  103. UpdateElements();
  104. }
  105. void Console::SetVisible(bool enable)
  106. {
  107. Input* input = GetSubsystem<Input>();
  108. background_->SetVisible(enable);
  109. closeButton_->SetVisible(enable);
  110. if (enable)
  111. {
  112. // Check if we have receivers for E_CONSOLECOMMAND every time here in case the handler is being added later dynamically
  113. bool hasInterpreter = PopulateInterpreter();
  114. commandLine_->SetVisible(hasInterpreter);
  115. if (hasInterpreter && focusOnShow_)
  116. GetSubsystem<UI>()->SetFocusElement(lineEdit_);
  117. // Ensure the background has no empty space when shown without the lineedit
  118. background_->SetHeight(background_->GetMinHeight());
  119. // Show OS mouse
  120. input->SetMouseVisible(true, true);
  121. }
  122. else
  123. {
  124. rowContainer_->SetFocus(false);
  125. interpreters_->SetFocus(false);
  126. lineEdit_->SetFocus(false);
  127. // Restore OS mouse visibility
  128. input->ResetMouseVisible();
  129. }
  130. }
  131. void Console::Toggle()
  132. {
  133. SetVisible(!IsVisible());
  134. }
  135. void Console::SetNumBufferedRows(unsigned rows)
  136. {
  137. if (rows < displayedRows_)
  138. return;
  139. rowContainer_->DisableLayoutUpdate();
  140. int delta = rowContainer_->GetNumItems() - rows;
  141. if (delta > 0)
  142. {
  143. // We have more, remove oldest rows first
  144. for (int i = 0; i < delta; ++i)
  145. rowContainer_->RemoveItem((unsigned)0);
  146. }
  147. else
  148. {
  149. // We have less, add more rows at the top
  150. for (int i = 0; i > delta; --i)
  151. {
  152. Text* text = new Text(context_);
  153. // If style is already set, apply here to ensure proper height of the console when
  154. // amount of rows is changed
  155. if (background_->GetDefaultStyle())
  156. text->SetStyle("ConsoleText");
  157. rowContainer_->InsertItem(0, text);
  158. }
  159. }
  160. rowContainer_->EnsureItemVisibility(rowContainer_->GetItem(rowContainer_->GetNumItems() - 1));
  161. rowContainer_->EnableLayoutUpdate();
  162. rowContainer_->UpdateLayout();
  163. UpdateElements();
  164. }
  165. void Console::SetNumRows(unsigned rows)
  166. {
  167. if (!rows)
  168. return;
  169. displayedRows_ = rows;
  170. if (GetNumBufferedRows() < rows)
  171. SetNumBufferedRows(rows);
  172. UpdateElements();
  173. }
  174. void Console::SetNumHistoryRows(unsigned rows)
  175. {
  176. historyRows_ = rows;
  177. if (history_.Size() > rows)
  178. history_.Resize(rows);
  179. if (historyPosition_ > rows)
  180. historyPosition_ = rows;
  181. }
  182. void Console::SetFocusOnShow(bool enable)
  183. {
  184. focusOnShow_ = enable;
  185. }
  186. void Console::UpdateElements()
  187. {
  188. int width = GetSubsystem<Graphics>()->GetWidth();
  189. const IntRect& border = background_->GetLayoutBorder();
  190. const IntRect& panelBorder = rowContainer_->GetScrollPanel()->GetClipBorder();
  191. rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
  192. rowContainer_->SetFixedHeight(displayedRows_ * rowContainer_->GetItem((unsigned)0)->GetHeight() + panelBorder.top_ + panelBorder.bottom_ +
  193. (rowContainer_->GetHorizontalScrollBar()->IsVisible() ? rowContainer_->GetHorizontalScrollBar()->GetHeight() : 0));
  194. background_->SetFixedWidth(width);
  195. background_->SetHeight(background_->GetMinHeight());
  196. }
  197. XMLFile* Console::GetDefaultStyle() const
  198. {
  199. return background_->GetDefaultStyle(false);
  200. }
  201. bool Console::IsVisible() const
  202. {
  203. return background_ && background_->IsVisible();
  204. }
  205. unsigned Console::GetNumBufferedRows() const
  206. {
  207. return rowContainer_->GetNumItems();
  208. }
  209. void Console::CopySelectedRows() const
  210. {
  211. rowContainer_->CopySelectedItemsToClipboard();
  212. }
  213. const String& Console::GetHistoryRow(unsigned index) const
  214. {
  215. return index < history_.Size() ? history_[index] : String::EMPTY;
  216. }
  217. bool Console::PopulateInterpreter()
  218. {
  219. interpreters_->RemoveAllItems();
  220. HashSet<Object*>* receivers = context_->GetEventReceivers(E_CONSOLECOMMAND);
  221. if (!receivers || receivers->Empty())
  222. return false;
  223. Vector<String> names;
  224. for (HashSet<Object*>::ConstIterator iter = receivers->Begin(); iter != receivers->End(); ++iter)
  225. names.Push((*iter)->GetTypeName());
  226. Sort(names.Begin(), names.End());
  227. unsigned selection = M_MAX_UNSIGNED;
  228. for (unsigned i = 0; i < names.Size(); ++i)
  229. {
  230. const String& name = names[i];
  231. if (name == commandInterpreter_)
  232. selection = i;
  233. Text* text = new Text(context_);
  234. text->SetStyle("ConsoleText");
  235. text->SetText(name);
  236. interpreters_->AddItem(text);
  237. }
  238. const IntRect& border = interpreters_->GetPopup()->GetLayoutBorder();
  239. interpreters_->SetMaxWidth(interpreters_->GetListView()->GetContentElement()->GetWidth() + border.left_ + border.right_);
  240. bool enabled = interpreters_->GetNumItems() > 1;
  241. interpreters_->SetEnabled(enabled);
  242. interpreters_->SetFocusMode(enabled ? FM_FOCUSABLE_DEFOCUSABLE : FM_NOTFOCUSABLE);
  243. if (selection == M_MAX_UNSIGNED)
  244. {
  245. selection = 0;
  246. commandInterpreter_ = names[selection];
  247. }
  248. interpreters_->SetSelection(selection);
  249. return true;
  250. }
  251. void Console::HandleInterpreterSelected(StringHash eventType, VariantMap& eventData)
  252. {
  253. commandInterpreter_ = static_cast<Text*>(interpreters_->GetSelectedItem())->GetText();
  254. lineEdit_->SetFocus(true);
  255. }
  256. void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
  257. {
  258. using namespace TextFinished;
  259. String line = lineEdit_->GetText();
  260. if (!line.Empty())
  261. {
  262. // Send the command as an event for script subsystem
  263. using namespace ConsoleCommand;
  264. VariantMap& eventData = GetEventDataMap();
  265. eventData[P_COMMAND] = line;
  266. eventData[P_ID] = static_cast<Text*>(interpreters_->GetSelectedItem())->GetText();
  267. SendEvent(E_CONSOLECOMMAND, eventData);
  268. // Store to history, then clear the lineedit
  269. history_.Push(line);
  270. if (history_.Size() > historyRows_)
  271. history_.Erase(history_.Begin());
  272. historyPosition_ = history_.Size();
  273. currentRow_.Clear();
  274. lineEdit_->SetText(currentRow_);
  275. }
  276. }
  277. void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
  278. {
  279. if (!historyRows_)
  280. return;
  281. using namespace UnhandledKey;
  282. bool changed = false;
  283. switch (eventData[P_KEY].GetInt())
  284. {
  285. case KEY_UP:
  286. if (historyPosition_ > 0)
  287. {
  288. if (historyPosition_ == history_.Size())
  289. currentRow_ = lineEdit_->GetText();
  290. --historyPosition_;
  291. changed = true;
  292. }
  293. break;
  294. case KEY_DOWN:
  295. if (historyPosition_ < history_.Size())
  296. {
  297. ++historyPosition_;
  298. changed = true;
  299. }
  300. break;
  301. }
  302. if (changed)
  303. {
  304. if (historyPosition_ < history_.Size())
  305. lineEdit_->SetText(history_[historyPosition_]);
  306. else
  307. lineEdit_->SetText(currentRow_);
  308. }
  309. }
  310. void Console::HandleCloseButtonPressed(StringHash eventType, VariantMap& eventData)
  311. {
  312. SetVisible(false);
  313. }
  314. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  315. {
  316. UpdateElements();
  317. }
  318. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  319. {
  320. // If printing a log message causes more messages to be logged (error accessing font), disregard them
  321. if (printing_)
  322. return;
  323. using namespace LogMessage;
  324. int level = eventData[P_LEVEL].GetInt();
  325. // The message may be multi-line, so split to rows in that case
  326. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  327. for (unsigned i = 0; i < rows.Size(); ++i)
  328. pendingRows_.Push(MakePair(level, rows[i]));
  329. if (autoVisibleOnError_ && level == LOG_ERROR && !IsVisible())
  330. SetVisible(true);
  331. }
  332. void Console::HandlePostUpdate(StringHash eventType, VariantMap& eventData)
  333. {
  334. // Ensure UI-elements are not detached
  335. if (!background_->GetParent())
  336. {
  337. UI* ui = GetSubsystem<UI>();
  338. UIElement* uiRoot = ui->GetRoot();
  339. uiRoot->AddChild(background_);
  340. uiRoot->AddChild(closeButton_);
  341. }
  342. if (!rowContainer_->GetNumItems() || pendingRows_.Empty())
  343. return;
  344. printing_ = true;
  345. rowContainer_->DisableLayoutUpdate();
  346. Text* text;
  347. for (unsigned i = 0; i < pendingRows_.Size(); ++i)
  348. {
  349. rowContainer_->RemoveItem((unsigned)0);
  350. text = new Text(context_);
  351. text->SetText(pendingRows_[i].second_);
  352. // Make error message highlight
  353. text->SetStyle(pendingRows_[i].first_ == LOG_ERROR ? "ConsoleHighlightedText" : "ConsoleText");
  354. rowContainer_->AddItem(text);
  355. }
  356. pendingRows_.Clear();
  357. rowContainer_->EnsureItemVisibility(text);
  358. rowContainer_->EnableLayoutUpdate();
  359. rowContainer_->UpdateLayout();
  360. UpdateElements(); // May need to readjust the height due to scrollbar visibility changes
  361. printing_ = false;
  362. }
  363. }
  364. #endif