Console.cpp 14 KB

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