Console.h 6.5 KB

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