Console.h 7.2 KB

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