Browse Source

Command history for Console

Thomas Buck 10 years ago
parent
commit
29acbfcc06
5 changed files with 81 additions and 20 deletions
  1. 3
    0
      ChangeLog.md
  2. 0
    2
      TODO.md
  3. 6
    1
      include/Console.h
  4. 45
    1
      src/Console.cpp
  5. 27
    16
      src/UI.cpp

+ 3
- 0
ChangeLog.md View File

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20141015 ]
6
+    * Added rudimentary command history support for Console
7
+
5
     [ 20141011 ]
8
     [ 20141011 ]
6
     * Added simple level loader unit test driver
9
     * Added simple level loader unit test driver
7
     * Fixed the parsing bug in the new loader that sometimes caused strange hangs on level load
10
     * Fixed the parsing bug in the new loader that sometimes caused strange hangs on level load

+ 0
- 2
TODO.md View File

1
 # To-Do List
1
 # To-Do List
2
 
2
 
3
-* Console no longer has command history (arrow up, down)
4
-
5
 ## General
3
 ## General
6
 
4
 
7
 * Move to newer OpenGL (GL ES or 2.1 or 3.x or 4.x?)
5
 * Move to newer OpenGL (GL ES or 2.1 or 3.x or 4.x?)

+ 6
- 1
include/Console.h View File

8
 #ifndef _CONSOLE_H_
8
 #ifndef _CONSOLE_H_
9
 #define _CONSOLE_H_
9
 #define _CONSOLE_H_
10
 
10
 
11
+#include <string>
12
+#include <vector>
13
+
11
 class Console {
14
 class Console {
12
 public:
15
 public:
13
     static void display();
16
     static void display();
14
 
17
 
15
 private:
18
 private:
16
     const static int bufferLength = 256;
19
     const static int bufferLength = 256;
17
-    static char buffer[bufferLength];
20
+    static char buffer[bufferLength + 1];
18
     static bool scrollToBottom;
21
     static bool scrollToBottom;
19
     static bool focusInput;
22
     static bool focusInput;
20
     static unsigned long lastLogLength;
23
     static unsigned long lastLogLength;
24
+    static std::vector<std::string> lastCommands;
25
+    static long lastCommandIndex;
21
 };
26
 };
22
 
27
 
23
 #endif
28
 #endif

+ 45
- 1
src/Console.cpp View File

5
  * \author xythobuz
5
  * \author xythobuz
6
  */
6
  */
7
 
7
 
8
+#include <cstring>
9
+
8
 #include "global.h"
10
 #include "global.h"
9
 #include "Log.h"
11
 #include "Log.h"
10
 #include "UI.h"
12
 #include "UI.h"
11
 #include "commands/Command.h"
13
 #include "commands/Command.h"
12
 #include "Console.h"
14
 #include "Console.h"
13
 
15
 
14
-char Console::buffer[bufferLength] = "";
16
+char Console::buffer[bufferLength + 1] = "";
15
 bool Console::scrollToBottom = false;
17
 bool Console::scrollToBottom = false;
16
 bool Console::focusInput = false;
18
 bool Console::focusInput = false;
17
 unsigned long Console::lastLogLength = 0;
19
 unsigned long Console::lastLogLength = 0;
20
+std::vector<std::string> Console::lastCommands;
21
+long Console::lastCommandIndex = -1;
18
 
22
 
19
 void Console::display() {
23
 void Console::display() {
20
     if (ImGui::Begin("Console", NULL, ImVec2(600, 400), -1.0f)) {
24
     if (ImGui::Begin("Console", NULL, ImVec2(600, 400), -1.0f)) {
38
             focusInput = false;
42
             focusInput = false;
39
         }
43
         }
40
 
44
 
45
+        if (ImGui::GetWindowIsFocused()) {
46
+            ImGuiIO& io = ImGui::GetIO();
47
+            static bool hold = false;
48
+            bool update = false;
49
+
50
+            if ((!hold) && (io.KeysDown[upKey] || io.KeysDown[downKey]))
51
+                update = hold = true;
52
+
53
+            if (hold && (!(io.KeysDown[upKey] || io.KeysDown[downKey])))
54
+                hold = false;
55
+
56
+            if (update) {
57
+                if (io.KeysDown[upKey]) {
58
+                    if (lastCommandIndex < static_cast<long>(lastCommands.size() - 1))
59
+                        lastCommandIndex++;
60
+                }
61
+
62
+                if (io.KeysDown[downKey]) {
63
+                    if (lastCommandIndex > -1)
64
+                        lastCommandIndex--;
65
+                }
66
+
67
+                if ((lastCommandIndex >= 0) && (lastCommandIndex < lastCommands.size())) {
68
+                    strncpy(buffer,
69
+                            lastCommands.at(lastCommands.size() - 1 - lastCommandIndex).c_str(),
70
+                            bufferLength);
71
+                    buffer[bufferLength] = '\0';
72
+                } else {
73
+                    buffer[0] = '\0';
74
+                }
75
+
76
+                update = false;
77
+            }
78
+        }
79
+
41
         if (ImGui::InputText("Command", buffer, bufferLength,
80
         if (ImGui::InputText("Command", buffer, bufferLength,
42
                     ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
81
                     ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
43
             getLog() << "> " << buffer << Log::endl;
82
             getLog() << "> " << buffer << Log::endl;
45
             if (error != 0) {
84
             if (error != 0) {
46
                 getLog() << "Error code: " << error << Log::endl;
85
                 getLog() << "Error code: " << error << Log::endl;
47
             }
86
             }
87
+
88
+            if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
89
+                lastCommands.push_back(std::string(buffer));
90
+            lastCommandIndex = -1;
91
+
48
             buffer[0] = '\0';
92
             buffer[0] = '\0';
49
             scrollToBottom = true;
93
             scrollToBottom = true;
50
             focusInput = true;
94
             focusInput = true;

+ 27
- 16
src/UI.cpp View File

94
 
94
 
95
     ImGui::NewFrame();
95
     ImGui::NewFrame();
96
 
96
 
97
-    bool clicked = !clickEvents.empty();
98
-
99
     if (!visible) {
97
     if (!visible) {
100
         while (!clickEvents.empty()) {
98
         while (!clickEvents.empty()) {
101
             auto i = clickEvents.front();
99
             auto i = clickEvents.front();
124
         }
122
         }
125
     }
123
     }
126
 
124
 
127
-    if ((!io.WantCaptureKeyboard) || (!visible)) {
128
-        while (!keyboardEvents.empty()) {
129
-            auto i = keyboardEvents.front();
125
+    while (!keyboardEvents.empty()) {
126
+        auto i = keyboardEvents.front();
130
 
127
 
128
+        if (!visible) {
131
             if (getMenu().isVisible()) {
129
             if (getMenu().isVisible()) {
132
                 getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
130
                 getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
133
             } else {
131
             } else {
136
                         getGame().handleAction((ActionEvents)n, !std::get<1>(i));
134
                         getGame().handleAction((ActionEvents)n, !std::get<1>(i));
137
                 }
135
                 }
138
             }
136
             }
137
+        }
139
 
138
 
140
-            if (std::get<1>(i)) {
139
+        if (std::get<1>(i)) {
140
+            if (!visible) {
141
                 if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
141
                 if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
142
                     getMenu().setVisible(!getMenu().isVisible());
142
                     getMenu().setVisible(!getMenu().isVisible());
143
-                    visible = false;
144
-                } else if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
143
+                }
144
+            }
145
+
146
+            if ((!io.WantCaptureKeyboard) || (!visible)) {
147
+                if (getRunTime().getKeyBinding(debugAction) == std::get<0>(i)) {
145
                     if (!metaKeyIsActive)
148
                     if (!metaKeyIsActive)
146
                         visible = !visible;
149
                         visible = !visible;
147
                 }
150
                 }
148
             }
151
             }
149
-
150
-            keyboardEvents.pop_front();
151
         }
152
         }
153
+
154
+        keyboardEvents.pop_front();
152
     }
155
     }
153
 
156
 
154
-    if ((!io.WantCaptureKeyboard) && (!io.WantCaptureMouse) && visible && clicked) {
155
-        visible = false;
157
+    bool clicked = !clickEvents.empty();
158
+    // Only already empty when !visible
159
+    if (visible) {
160
+        clickEvents.clear();
161
+        motionEvents.clear();
162
+        scrollEvents.clear();
156
     }
163
     }
157
 
164
 
158
-    keyboardEvents.clear();
159
-    clickEvents.clear();
160
-    motionEvents.clear();
161
-    scrollEvents.clear();
165
+    if (visible && (
166
+                ((!io.WantCaptureKeyboard) && io.KeysDown[escapeKey])
167
+                || ((!io.WantCaptureMouse) && clicked)
168
+            )) {
169
+        visible = false;
170
+
171
+        getLog() << io.WantCaptureKeyboard << io.WantCaptureMouse << io.KeysDown[escapeKey] << Log::endl;
172
+    }
162
 
173
 
163
     if (getWindow().getTextInput() != visible)
174
     if (getWindow().getTextInput() != visible)
164
         getWindow().setTextInput(visible);
175
         getWindow().setTextInput(visible);

Loading…
Cancel
Save