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