Console.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. //
  2. // Copyright (c) 2008-2020 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. #pragma once
  23. #include "../Core/Object.h"
  24. namespace Urho3D
  25. {
  26. class Button;
  27. class BorderImage;
  28. class DropDownList;
  29. class Engine;
  30. class Font;
  31. class LineEdit;
  32. class ListView;
  33. class Text;
  34. class UIElement;
  35. class XMLFile;
  36. /// %Console window with log history and command line prompt.
  37. class URHO3D_API Console : public Object
  38. {
  39. URHO3D_OBJECT(Console, Object);
  40. public:
  41. /// Construct.
  42. explicit Console(Context* context);
  43. /// Destruct.
  44. ~Console() override;
  45. /// Set UI elements' style from an XML file.
  46. /// @property
  47. void SetDefaultStyle(XMLFile* style);
  48. /// Show or hide.
  49. /// @property
  50. void SetVisible(bool enable);
  51. /// Toggle visibility.
  52. void Toggle();
  53. /// Automatically set console to visible when receiving an error log message.
  54. /// @property
  55. void SetAutoVisibleOnError(bool enable) { autoVisibleOnError_ = enable; }
  56. /// Set the command interpreter.
  57. /// @property
  58. void SetCommandInterpreter(const String& interpreter) { commandInterpreter_ = interpreter; }
  59. /// Set number of buffered rows.
  60. /// @property
  61. void SetNumBufferedRows(unsigned rows);
  62. /// Set number of displayed rows.
  63. /// @property
  64. void SetNumRows(unsigned rows);
  65. /// Set command history maximum size, 0 disables history.
  66. /// @property
  67. void SetNumHistoryRows(unsigned rows);
  68. /// Set whether to automatically focus the line edit when showing. Default true on desktops and false on mobile devices, as on mobiles it would pop up the screen keyboard.
  69. /// @property
  70. void SetFocusOnShow(bool enable);
  71. /// Add auto complete option.
  72. void AddAutoComplete(const String& option);
  73. /// Remove auto complete option.
  74. void RemoveAutoComplete(const String& option);
  75. /// Update elements to layout properly. Call this after manually adjusting the sub-elements.
  76. void UpdateElements();
  77. /// Return the UI style file.
  78. /// @property
  79. XMLFile* GetDefaultStyle() const;
  80. /// Return the background element.
  81. /// @property
  82. BorderImage* GetBackground() const { return background_; }
  83. /// Return the line edit element.
  84. /// @property
  85. LineEdit* GetLineEdit() const { return lineEdit_; }
  86. /// Return the close butoon element.
  87. /// @property
  88. Button* GetCloseButton() const { return closeButton_; }
  89. /// Return whether is visible.
  90. /// @property
  91. bool IsVisible() const;
  92. /// Return true when console is set to automatically visible when receiving an error log message.
  93. /// @property
  94. bool IsAutoVisibleOnError() const { return autoVisibleOnError_; }
  95. /// Return the last used command interpreter.
  96. /// @property
  97. const String& GetCommandInterpreter() const { return commandInterpreter_; }
  98. /// Return number of buffered rows.
  99. /// @property
  100. unsigned GetNumBufferedRows() const;
  101. /// Return number of displayed rows.
  102. /// @property
  103. unsigned GetNumRows() const { return displayedRows_; }
  104. /// Copy selected rows to system clipboard.
  105. void CopySelectedRows() const;
  106. /// Return history maximum size.
  107. /// @property
  108. unsigned GetNumHistoryRows() const { return historyRows_; }
  109. /// Return current history position.
  110. /// @property
  111. unsigned GetHistoryPosition() const { return historyPosition_; }
  112. /// Return history row at index.
  113. /// @property
  114. const String& GetHistoryRow(unsigned index) const;
  115. /// Return whether automatically focuses the line edit when showing.
  116. /// @property
  117. bool GetFocusOnShow() const { return focusOnShow_; }
  118. private:
  119. /// Populate the command line interpreters that could handle the console command.
  120. bool PopulateInterpreter();
  121. /// Handle interpreter being selected on the drop down list.
  122. void HandleInterpreterSelected(StringHash eventType, VariantMap& eventData);
  123. /// Handle text change in the line edit.
  124. void HandleTextChanged(StringHash eventType, VariantMap& eventData);
  125. /// Handle enter pressed on the line edit.
  126. void HandleTextFinished(StringHash eventType, VariantMap& eventData);
  127. /// Handle unhandled key on the line edit for scrolling the history.
  128. void HandleLineEditKey(StringHash eventType, VariantMap& eventData);
  129. /// Handle close button being pressed.
  130. void HandleCloseButtonPressed(StringHash eventType, VariantMap& eventData);
  131. /// Handle UI root resize.
  132. void HandleRootElementResized(StringHash eventType, VariantMap& eventData);
  133. /// Handle a log message.
  134. void HandleLogMessage(StringHash eventType, VariantMap& eventData);
  135. /// Handle the application post-update.
  136. void HandlePostUpdate(StringHash eventType, VariantMap& eventData);
  137. /// Auto visible on error flag.
  138. bool autoVisibleOnError_;
  139. /// Background.
  140. SharedPtr<BorderImage> background_;
  141. /// Container for text rows.
  142. ListView* rowContainer_;
  143. /// Container for the command line.
  144. UIElement* commandLine_;
  145. /// Interpreter drop down list.
  146. DropDownList* interpreters_;
  147. /// Line edit.
  148. LineEdit* lineEdit_;
  149. /// Close button.
  150. SharedPtr<Button> closeButton_;
  151. /// Last used command interpreter.
  152. String commandInterpreter_;
  153. /// Command history.
  154. Vector<String> history_;
  155. /// Pending log message rows.
  156. Vector<Pair<int, String> > pendingRows_;
  157. /// Current row being edited.
  158. String currentRow_;
  159. /// Maximum displayed rows.
  160. unsigned displayedRows_{};
  161. /// Command history maximum rows.
  162. unsigned historyRows_;
  163. /// Command history current position.
  164. unsigned historyPosition_;
  165. /**
  166. Command auto complete options.
  167. down arrow key
  168. Unless currently going through history options, will loop through next auto complete options.
  169. up arrow key
  170. Unless currently going through history options, will go through previous auto complete options.
  171. When no previous options are left will start going through history options.
  172. */
  173. Vector<String> autoComplete_;
  174. /// Command auto complete current position.
  175. unsigned autoCompletePosition_;
  176. /// Store the original line which is being auto-completed.
  177. String autoCompleteLine_;
  178. /// Flag when printing messages to prevent endless loop.
  179. bool printing_;
  180. /// Flag for automatically focusing the line edit on showing the console.
  181. bool focusOnShow_;
  182. /// Internal flag whether currently in an autocomplete or history change.
  183. bool historyOrAutoCompleteChange_;
  184. };
  185. }