Parcourir la source

Command history for Console

Thomas Buck il y a 10 ans
Parent
révision
29acbfcc06
5 fichiers modifiés avec 81 ajouts et 20 suppressions
  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 Voir le fichier

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

+ 0
- 2
TODO.md Voir le fichier

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

+ 6
- 1
include/Console.h Voir le fichier

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

+ 45
- 1
src/Console.cpp Voir le fichier

@@ -5,16 +5,20 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <cstring>
9
+
8 10
 #include "global.h"
9 11
 #include "Log.h"
10 12
 #include "UI.h"
11 13
 #include "commands/Command.h"
12 14
 #include "Console.h"
13 15
 
14
-char Console::buffer[bufferLength] = "";
16
+char Console::buffer[bufferLength + 1] = "";
15 17
 bool Console::scrollToBottom = false;
16 18
 bool Console::focusInput = false;
17 19
 unsigned long Console::lastLogLength = 0;
20
+std::vector<std::string> Console::lastCommands;
21
+long Console::lastCommandIndex = -1;
18 22
 
19 23
 void Console::display() {
20 24
     if (ImGui::Begin("Console", NULL, ImVec2(600, 400), -1.0f)) {
@@ -38,6 +42,41 @@ void Console::display() {
38 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 80
         if (ImGui::InputText("Command", buffer, bufferLength,
42 81
                     ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
43 82
             getLog() << "> " << buffer << Log::endl;
@@ -45,6 +84,11 @@ void Console::display() {
45 84
             if (error != 0) {
46 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 92
             buffer[0] = '\0';
49 93
             scrollToBottom = true;
50 94
             focusInput = true;

+ 27
- 16
src/UI.cpp Voir le fichier

@@ -94,8 +94,6 @@ void UI::eventsFinished() {
94 94
 
95 95
     ImGui::NewFrame();
96 96
 
97
-    bool clicked = !clickEvents.empty();
98
-
99 97
     if (!visible) {
100 98
         while (!clickEvents.empty()) {
101 99
             auto i = clickEvents.front();
@@ -124,10 +122,10 @@ void UI::eventsFinished() {
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 129
             if (getMenu().isVisible()) {
132 130
                 getMenu().handleKeyboard(std::get<0>(i), std::get<1>(i));
133 131
             } else {
@@ -136,29 +134,42 @@ void UI::eventsFinished() {
136 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 141
                 if (getRunTime().getKeyBinding(menuAction) == std::get<0>(i)) {
142 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 148
                     if (!metaKeyIsActive)
146 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 174
     if (getWindow().getTextInput() != visible)
164 175
         getWindow().setTextInput(visible);

Chargement…
Annuler
Enregistrer