Console.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // Copyright (c) 2008-2013 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 "Font.h"
  26. #include "Graphics.h"
  27. #include "GraphicsEvents.h"
  28. #include "InputEvents.h"
  29. #include "IOEvents.h"
  30. #include "LineEdit.h"
  31. #include "Log.h"
  32. #include "ResourceCache.h"
  33. #include "Script.h"
  34. #include "Text.h"
  35. #include "UI.h"
  36. #include "UIEvents.h"
  37. #include "DebugNew.h"
  38. namespace Urho3D
  39. {
  40. static const int DEFAULT_CONSOLE_ROWS = 16;
  41. static const int DEFAULT_HISTORY_SIZE = 16;
  42. OBJECTTYPESTATIC(Console);
  43. Console::Console(Context* context) :
  44. Object(context),
  45. historyRows_(DEFAULT_HISTORY_SIZE),
  46. historyPosition_(0),
  47. inLogMessage_(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_->SetActive(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. }
  72. Console::~Console()
  73. {
  74. background_->Remove();
  75. }
  76. void Console::SetStyle(XMLFile* style)
  77. {
  78. if (!style)
  79. return;
  80. style_ = style;
  81. background_->SetStyle(style, "ConsoleBackground");
  82. for (unsigned i = 0; i < rows_.Size(); ++i)
  83. rows_[i]->SetStyle(style, "ConsoleText");
  84. lineEdit_->SetStyle(style, "ConsoleLineEdit");
  85. UpdateElements();
  86. }
  87. void Console::SetVisible(bool enable)
  88. {
  89. background_->SetVisible(enable);
  90. if (enable)
  91. GetSubsystem<UI>()->SetFocusElement(lineEdit_);
  92. else
  93. lineEdit_->SetFocus(false);
  94. }
  95. void Console::Toggle()
  96. {
  97. SetVisible(!IsVisible());
  98. }
  99. void Console::SetNumRows(unsigned rows)
  100. {
  101. if (!rows)
  102. return;
  103. rowContainer_->RemoveAllChildren();
  104. rows_.Resize(rows);
  105. for (unsigned i = 0; i < rows_.Size(); ++i)
  106. {
  107. if (!rows_[i])
  108. {
  109. rows_[i] = new Text(context_);
  110. rows_[i]->SetStyle(style_, "ConsoleText");
  111. }
  112. rowContainer_->AddChild(rows_[i]);
  113. }
  114. UpdateElements();
  115. }
  116. void Console::SetNumHistoryRows(unsigned rows)
  117. {
  118. historyRows_ = rows;
  119. if (history_.Size() > rows)
  120. history_.Resize(rows);
  121. if (historyPosition_ > rows)
  122. historyPosition_ = rows;
  123. }
  124. void Console::UpdateElements()
  125. {
  126. int width = GetSubsystem<Graphics>()->GetWidth();
  127. const IntRect& border = background_->GetLayoutBorder();
  128. background_->SetFixedWidth(width);
  129. rowContainer_->SetFixedWidth(width - border.left_ - border.right_);
  130. lineEdit_->SetFixedHeight(lineEdit_->GetTextElement()->GetRowHeight());
  131. }
  132. bool Console::IsVisible() const
  133. {
  134. return background_ ? background_->IsVisible() : false;
  135. }
  136. const String& Console::GetHistoryRow(unsigned index) const
  137. {
  138. return index < history_.Size() ? history_[index] : String::EMPTY;
  139. }
  140. void Console::HandleTextFinished(StringHash eventType, VariantMap& eventData)
  141. {
  142. using namespace TextFinished;
  143. String line = lineEdit_->GetText();
  144. if (!line.Empty())
  145. {
  146. Script* script = GetSubsystem<Script>();
  147. if (script)
  148. script->Execute(line);
  149. // Store to history, then clear the lineedit
  150. history_.Push(line);
  151. if (history_.Size() > historyRows_)
  152. history_.Erase(history_.Begin());
  153. historyPosition_ = history_.Size();
  154. currentRow_.Clear();
  155. lineEdit_->SetText(currentRow_);
  156. }
  157. }
  158. void Console::HandleLineEditKey(StringHash eventType, VariantMap& eventData)
  159. {
  160. if (!historyRows_)
  161. return;
  162. using namespace UnhandledKey;
  163. bool changed = false;
  164. switch (eventData[P_KEY].GetInt())
  165. {
  166. case KEY_UP:
  167. if (historyPosition_ > 0)
  168. {
  169. if (historyPosition_ == history_.Size())
  170. currentRow_ = lineEdit_->GetText();
  171. --historyPosition_;
  172. changed = true;
  173. }
  174. break;
  175. case KEY_DOWN:
  176. if (historyPosition_ < history_.Size())
  177. {
  178. ++historyPosition_;
  179. changed = true;
  180. }
  181. break;
  182. }
  183. if (changed)
  184. {
  185. if (historyPosition_ < history_.Size())
  186. lineEdit_->SetText(history_[historyPosition_]);
  187. else
  188. lineEdit_->SetText(currentRow_);
  189. }
  190. }
  191. void Console::HandleScreenMode(StringHash eventType, VariantMap& eventData)
  192. {
  193. UpdateElements();
  194. }
  195. void Console::HandleLogMessage(StringHash eventType, VariantMap& eventData)
  196. {
  197. // If the rows are not fully initialized yet, or we are recursing here, do not write the message
  198. if (inLogMessage_ || rows_.Empty() || !rows_.Back())
  199. return;
  200. inLogMessage_ = true;
  201. using namespace LogMessage;
  202. // Be prepared for possible multi-line messages
  203. Vector<String> rows = eventData[P_MESSAGE].GetString().Split('\n');
  204. for (unsigned i = 0; i < rows.Size(); ++i)
  205. {
  206. // Remove the first row, change its text and re-add to the bottom
  207. Text* text = static_cast<Text*>(rowContainer_->GetChild(0));
  208. rowContainer_->RemoveChild(text);
  209. text->SetText(rows[i]);
  210. rowContainer_->AddChild(text);
  211. }
  212. inLogMessage_ = false;
  213. }
  214. }