浏览代码

Improved Console, added auto-completion

Thomas Buck 10 年前
父节点
当前提交
efa479c45d
共有 7 个文件被更改,包括 887 次插入415 次删除
  1. 3
    0
      ChangeLog.md
  2. 5
    0
      include/Console.h
  3. 1
    0
      include/commands/Command.h
  4. 66
    47
      src/Console.cpp
  5. 47
    3
      src/commands/Command.cpp
  6. 652
    282
      src/deps/imgui/imgui.cpp
  7. 113
    83
      src/deps/imgui/imgui.h

+ 3
- 0
ChangeLog.md 查看文件

@@ -5,6 +5,9 @@
5 5
     [ 20141203 ]
6 6
     * Renamed Vector3d to Vec3, small cleanup
7 7
     * Added preliminary math unit test
8
+    * Updated ImGui to version 1.18 wip
9
+    * Console now has support for auto-completion
10
+    * Fixed Consoles command history behavior thanks to new ImGui features
8 11
 
9 12
     [ 20141202 ]
10 13
     * No longer crashing when LoaderTR2 does not find an SFX file

+ 5
- 0
include/Console.h 查看文件

@@ -11,11 +11,15 @@
11 11
 #include <string>
12 12
 #include <vector>
13 13
 
14
+struct ImGuiTextEditCallbackData;
15
+
14 16
 class Console {
15 17
   public:
16 18
     static void display();
17 19
 
18 20
   private:
21
+    static void callback(ImGuiTextEditCallbackData* data);
22
+
19 23
     const static int bufferLength = 256;
20 24
     static char buffer[bufferLength + 1];
21 25
     static bool scrollToBottom;
@@ -23,6 +27,7 @@ class Console {
23 27
     static unsigned long lastLogLength;
24 28
     static std::vector<std::string> lastCommands;
25 29
     static long lastCommandIndex;
30
+    static std::string bufferedCommand;
26 31
 };
27 32
 
28 33
 #endif

+ 1
- 0
include/commands/Command.h 查看文件

@@ -24,6 +24,7 @@ class Command {
24 24
     static void fillCommandList();
25 25
     static int command(std::string c);
26 26
     static int executeFile(std::string file);
27
+    static std::string autoComplete(std::string begin);
27 28
 
28 29
   private:
29 30
     static std::vector<std::shared_ptr<Command>> commands;

+ 66
- 47
src/Console.cpp 查看文件

@@ -19,6 +19,54 @@ bool Console::focusInput = false;
19 19
 unsigned long Console::lastLogLength = 0;
20 20
 std::vector<std::string> Console::lastCommands;
21 21
 long Console::lastCommandIndex = -1;
22
+std::string Console::bufferedCommand;
23
+
24
+void Console::callback(ImGuiTextEditCallbackData* data) {
25
+    bool update = false;
26
+    std::string completion;
27
+
28
+    switch (data->EventKey) {
29
+        case ImGuiKey_Tab:
30
+            completion = Command::autoComplete(data->Buf);
31
+            if (completion.size() > 0) {
32
+                data->DeleteChars(0, data->BufSize);
33
+                data->InsertChars(0, completion.c_str());
34
+                data->CursorPos = strlen(data->Buf);
35
+            }
36
+            break;
37
+
38
+        case ImGuiKey_UpArrow:
39
+            update = true;
40
+            if (lastCommandIndex < (long)(lastCommands.size() - 1)) {
41
+                lastCommandIndex++;
42
+                if (lastCommandIndex == 0)
43
+                    bufferedCommand = data->Buf;
44
+            }
45
+            break;
46
+
47
+        case ImGuiKey_DownArrow:
48
+            update = true;
49
+            if (lastCommandIndex > -1)
50
+                lastCommandIndex--;
51
+            break;
52
+    }
53
+
54
+    if (update) {
55
+        if ((lastCommandIndex >= 0) && (lastCommandIndex < lastCommands.size())) {
56
+            if (lastCommands.at(lastCommands.size() - 1 - lastCommandIndex) != buffer) {
57
+                data->DeleteChars(0, data->BufSize);
58
+                data->InsertChars(0, lastCommands.at(lastCommands.size() - 1 - lastCommandIndex).c_str());
59
+            }
60
+        } else {
61
+            data->DeleteChars(0, data->BufSize);
62
+            if (bufferedCommand.size() > 0) {
63
+                data->InsertChars(0, bufferedCommand.c_str());
64
+            }
65
+        }
66
+
67
+        data->CursorPos = strlen(data->Buf);
68
+    }
69
+}
22 70
 
23 71
 void Console::display() {
24 72
     if (ImGui::Begin("Console", NULL, ImVec2(600, 400), -1.0f)) {
@@ -37,61 +85,32 @@ void Console::display() {
37 85
         }
38 86
         ImGui::EndChild();
39 87
 
40
-        if (focusInput) {
41
-            ImGui::SetKeyboardFocusHere();
42
-            focusInput = false;
43
-        }
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
-
80 88
         if (ImGui::InputText("Command", buffer, bufferLength,
81
-                             ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
89
+                             ImGuiInputTextFlags_EnterReturnsTrue
90
+                              | ImGuiInputTextFlags_CallbackCompletion
91
+                              | ImGuiInputTextFlags_CallbackHistory,
92
+                             &Console::callback)) {
82 93
             getLog() << "> " << buffer << Log::endl;
83
-            int error = Command::command(buffer);
84
-            if (error != 0) {
85
-                getLog() << "Error code: " << error << Log::endl;
94
+            if (strlen(buffer) > 0) {
95
+                int error = Command::command(buffer);
96
+                if (error != 0) {
97
+                    getLog() << "Error code: " << error << Log::endl;
98
+                }
99
+
100
+                if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
101
+                    lastCommands.push_back(std::string(buffer));
86 102
             }
87 103
 
88
-            if ((lastCommands.size() == 0) || (lastCommands[lastCommands.size() - 1] != buffer))
89
-                lastCommands.push_back(std::string(buffer));
90 104
             lastCommandIndex = -1;
91
-
92 105
             buffer[0] = '\0';
93 106
             scrollToBottom = true;
94 107
             focusInput = true;
108
+            bufferedCommand.clear();
109
+        }
110
+
111
+        if (ImGui::IsItemHovered() || focusInput) {
112
+            ImGui::SetKeyboardFocusHere(-1);
113
+            focusInput = false;
95 114
         }
96 115
     }
97 116
     ImGui::End();

+ 47
- 3
src/commands/Command.cpp 查看文件

@@ -65,7 +65,7 @@ int Command::command(std::string c) {
65 65
         return 0;
66 66
 
67 67
     // Print help
68
-    if (cmd.compare("help") == 0) {
68
+    if (cmd == "help") {
69 69
         std::string arg;
70 70
         command >> arg;
71 71
         if (arg.length() == 0) {
@@ -86,7 +86,7 @@ int Command::command(std::string c) {
86 86
             // Show help for a specific command
87 87
             for (auto& x : commands) {
88 88
                 if (x) {
89
-                    if (x->name().compare(arg) == 0) {
89
+                    if (x->name() == arg) {
90 90
                         x->printHelp();
91 91
                         return 0;
92 92
                     }
@@ -100,7 +100,7 @@ int Command::command(std::string c) {
100 100
     // Execute command
101 101
     for (auto& x : commands) {
102 102
         if (x) {
103
-            if (x->name().compare(cmd) == 0) {
103
+            if (x->name() == cmd) {
104 104
                 return x->execute(command);
105 105
             }
106 106
         }
@@ -133,3 +133,47 @@ int Command::executeFile(std::string file) {
133 133
     return 0;
134 134
 }
135 135
 
136
+std::string Command::autoComplete(std::string begin) {
137
+    std::vector<std::string> candidates;
138
+
139
+    std::string help("help");
140
+    if (begin.size() <= help.size()) {
141
+                if (begin.compare(0, begin.size(), help, 0, begin.size()) == 0) {
142
+                    candidates.push_back(help);
143
+                }
144
+            }
145
+
146
+    for (auto& x : commands) {
147
+        if (x) {
148
+            std::string name = x->name();
149
+            if (begin.size() <= name.size()) {
150
+                if (begin.compare(0, begin.size(), name, 0, begin.size()) == 0) {
151
+                    candidates.push_back(name);
152
+                }
153
+            }
154
+        }
155
+    }
156
+
157
+    if (candidates.size() == 0) {
158
+        return "";
159
+    } else if (candidates.size() == 1) {
160
+        return candidates.at(0);
161
+    } else {
162
+        std::string common = candidates.at(0);
163
+        for (int i = 0; i < candidates.size(); i++) {
164
+            getLog() << candidates.at(i);
165
+            if (i < (candidates.size() - 1))
166
+                getLog() << "/";
167
+
168
+            for (int c = 0; (c < common.size()) && (c < candidates.at(i).size()); c++) {
169
+                if (common.at(c) != candidates.at(i).at(c)) {
170
+                    common.erase(c);
171
+                    break;
172
+                }
173
+            }
174
+        }
175
+        getLog() << Log::endl;
176
+        return common;
177
+    }
178
+}
179
+

+ 652
- 282
src/deps/imgui/imgui.cpp
文件差异内容过多而无法显示
查看文件


+ 113
- 83
src/deps/imgui/imgui.h 查看文件

@@ -1,4 +1,4 @@
1
-// ImGui library v1.16 wip
1
+// ImGui library v1.18 wip
2 2
 // See .cpp file for commentary.
3 3
 // See ImGui::ShowTestWindow() for sample code.
4 4
 // Read 'Programmer guide' in .cpp for notes on how to setup ImGui in your codebase.
@@ -7,7 +7,7 @@
7 7
 #pragma once
8 8
 
9 9
 struct ImDrawList;
10
-struct ImBitmapFont;
10
+struct ImFont;
11 11
 struct ImGuiAabb;
12 12
 struct ImGuiIO;
13 13
 struct ImGuiStorage;
@@ -30,15 +30,15 @@ struct ImGuiWindow;
30 30
 #endif
31 31
 
32 32
 typedef unsigned int ImU32;
33
-typedef unsigned short ImWchar;
34
-typedef ImU32 ImGuiID;
33
+typedef unsigned short ImWchar;     // hold a character for display
34
+typedef ImU32 ImGuiID;              // hold widget unique ID
35 35
 typedef int ImGuiCol;               // enum ImGuiCol_
36 36
 typedef int ImGuiStyleVar;          // enum ImGuiStyleVar_
37 37
 typedef int ImGuiKey;               // enum ImGuiKey_
38 38
 typedef int ImGuiColorEditMode;     // enum ImGuiColorEditMode_
39 39
 typedef int ImGuiWindowFlags;       // enum ImGuiWindowFlags_
40 40
 typedef int ImGuiInputTextFlags;    // enum ImGuiInputTextFlags_
41
-typedef ImBitmapFont* ImFont;
41
+struct ImGuiTextEditCallbackData;
42 42
 
43 43
 struct ImVec2
44 44
 {
@@ -77,7 +77,7 @@ namespace ImGui
77 77
 template<typename T>
78 78
 class ImVector
79 79
 {
80
-private:
80
+protected:
81 81
     size_t                      Size;
82 82
     size_t                      Capacity;
83 83
     T*                          Data;
@@ -122,7 +122,7 @@ public:
122 122
 #endif // #ifndef ImVector
123 123
 
124 124
 // Helpers at bottom of the file:
125
-// - if (IMGUI_ONCE_UPON_A_FRAME)       // Execute a block of code once per frame only
125
+// - IMGUI_ONCE_UPON_A_FRAME            // Execute a block of code once per frame only (convenient for creating UI within deep-nested code that runs multiple times)
126 126
 // - struct ImGuiTextFilter             // Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
127 127
 // - struct ImGuiTextBuffer             // Text buffer for logging/accumulating text
128 128
 // - struct ImGuiStorage                // Custom key value storage (if you need to alter open/close states manually)
@@ -151,16 +151,16 @@ namespace ImGui
151 151
     IMGUI_API bool          GetWindowIsFocused();
152 152
     IMGUI_API ImVec2        GetWindowSize();
153 153
     IMGUI_API float         GetWindowWidth();
154
-    IMGUI_API void		    SetWindowSize(const ImVec2& size);                                  // set to ImVec2(0,0) to force an auto-fit
154
+    IMGUI_API void          SetWindowSize(const ImVec2& size);                                  // set to ImVec2(0,0) to force an auto-fit
155 155
     IMGUI_API ImVec2        GetWindowPos();                                                     // you should rarely need/care about the window position, but it can be useful if you want to use your own drawing.
156 156
     IMGUI_API void          SetWindowPos(const ImVec2& pos);                                    // set current window pos.
157 157
     IMGUI_API ImVec2        GetContentRegionMax();                                              // window or current column boundaries
158 158
     IMGUI_API ImVec2        GetWindowContentRegionMin();                                        // window boundaries
159 159
     IMGUI_API ImVec2        GetWindowContentRegionMax();
160 160
     IMGUI_API ImDrawList*   GetWindowDrawList();                                                // get rendering command-list if you want to append your own draw primitives.
161
-    IMGUI_API ImFont        GetWindowFont();
161
+    IMGUI_API ImFont*       GetWindowFont();
162 162
     IMGUI_API float         GetWindowFontSize();
163
-    IMGUI_API void          SetWindowFontScale(float scale);                                    // per-window font scale. Adjust IO.FontBaseScale if you want to scale all windows together.
163
+    IMGUI_API void          SetWindowFontScale(float scale);                                    // per-window font scale. Adjust IO.FontGlobalScale if you want to scale all windows.
164 164
     IMGUI_API void          SetScrollPosHere();                                                 // adjust scrolling position to center into the current cursor position.
165 165
     IMGUI_API void          SetKeyboardFocusHere(int offset = 0);                               // focus keyboard on the next widget. Use 'offset' to access sub components of a multiple component widget.
166 166
     IMGUI_API void          SetTreeStateStorage(ImGuiStorage* tree);                            // replace tree state storage with our own (if you want to manipulate it yourself, typically clear subsection of it).
@@ -172,10 +172,10 @@ namespace ImGui
172 172
     IMGUI_API void          PushAllowKeyboardFocus(bool v);                                     // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets.
173 173
     IMGUI_API void          PopAllowKeyboardFocus();
174 174
     IMGUI_API void          PushStyleColor(ImGuiCol idx, const ImVec4& col);
175
-    IMGUI_API void          PopStyleColor();
175
+    IMGUI_API void          PopStyleColor(int count = 1);
176 176
     IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, float val);
177 177
     IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, const ImVec2& val);
178
-    IMGUI_API void          PopStyleVar();
178
+    IMGUI_API void          PopStyleVar(int count = 1);
179 179
     IMGUI_API void          PushTextWrapPos(float wrap_pos_x = 0.0f);                           // word-wrapping for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space.
180 180
     IMGUI_API void          PopTextWrapPos();
181 181
 
@@ -238,7 +238,7 @@ namespace ImGui
238 238
     IMGUI_API bool          CheckboxFlags(const char* label, unsigned int* flags, unsigned int flags_value);
239 239
     IMGUI_API bool          RadioButton(const char* label, bool active);
240 240
     IMGUI_API bool          RadioButton(const char* label, int* v, int v_button);
241
-    IMGUI_API bool          InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0);
241
+    IMGUI_API bool          InputText(const char* label, char* buf, size_t buf_size, ImGuiInputTextFlags flags = 0, void (*callback)(ImGuiTextEditCallbackData*) = NULL, void* user_data = NULL);
242 242
     IMGUI_API bool          InputFloat(const char* label, float* v, float step = 0.0f, float step_fast = 0.0f, int decimal_precision = -1, ImGuiInputTextFlags extra_flags = 0);
243 243
     IMGUI_API bool          InputFloat2(const char* label, float v[2], int decimal_precision = -1);
244 244
     IMGUI_API bool          InputFloat3(const char* label, float v[3], int decimal_precision = -1);
@@ -295,7 +295,7 @@ namespace ImGui
295 295
     IMGUI_API int           GetFrameCount();
296 296
     IMGUI_API const char*   GetStyleColorName(ImGuiCol idx);
297 297
     IMGUI_API void          GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size);
298
-    IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_hash = true, float wrap_width = -1.0f);
298
+    IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = true, float wrap_width = -1.0f);
299 299
 
300 300
 } // namespace ImGui
301 301
 
@@ -308,11 +308,13 @@ enum ImGuiWindowFlags_
308 308
     ImGuiWindowFlags_NoResize               = 1 << 2,
309 309
     ImGuiWindowFlags_NoMove                 = 1 << 3,
310 310
     ImGuiWindowFlags_NoScrollbar            = 1 << 4,
311
-    ImGuiWindowFlags_ChildWindow            = 1 << 5,   // For internal use by BeginChild()
312
-    ImGuiWindowFlags_ChildWindowAutoFitX    = 1 << 6,   // For internal use by BeginChild()
313
-    ImGuiWindowFlags_ChildWindowAutoFitY    = 1 << 7,   // For internal use by BeginChild()
314
-    ImGuiWindowFlags_ComboBox               = 1 << 8,   // For internal use by ComboBox()
315
-    ImGuiWindowFlags_Tooltip                = 1 << 9    // For internal use by Render() when using Tooltip
311
+    ImGuiWindowFlags_NoScrollWithMouse      = 1 << 5,
312
+    ImGuiWindowFlags_AlwaysAutoResize       = 1 << 6,
313
+    ImGuiWindowFlags_ChildWindow            = 1 << 7,   // For internal use by BeginChild()
314
+    ImGuiWindowFlags_ChildWindowAutoFitX    = 1 << 8,   // For internal use by BeginChild()
315
+    ImGuiWindowFlags_ChildWindowAutoFitY    = 1 << 9,   // For internal use by BeginChild()
316
+    ImGuiWindowFlags_ComboBox               = 1 << 10,  // For internal use by ComboBox()
317
+    ImGuiWindowFlags_Tooltip                = 1 << 11   // For internal use by Render() when using Tooltip
316 318
 };
317 319
 
318 320
 // Flags for ImGui::InputText()
@@ -322,8 +324,11 @@ enum ImGuiInputTextFlags_
322 324
     ImGuiInputTextFlags_CharsDecimal        = 1 << 0,   // Allow 0123456789.+-*/
323 325
     ImGuiInputTextFlags_CharsHexadecimal    = 1 << 1,   // Allow 0123456789ABCDEFabcdef
324 326
     ImGuiInputTextFlags_AutoSelectAll       = 1 << 2,   // Select entire text when first taking focus
325
-    ImGuiInputTextFlags_EnterReturnsTrue    = 1 << 3    // Return 'true' when Enter is pressed (as opposed to when the value was modified)
326
-    //ImGuiInputTextFlags_AlignCenter       = 1 << 3,
327
+    ImGuiInputTextFlags_EnterReturnsTrue    = 1 << 3,   // Return 'true' when Enter is pressed (as opposed to when the value was modified)
328
+    ImGuiInputTextFlags_CallbackCompletion  = 1 << 4,   // Call user function on pressing TAB (for completion handling)
329
+    ImGuiInputTextFlags_CallbackHistory     = 1 << 5,   // Call user function on pressing Up/Down arrows (for history handling)
330
+    ImGuiInputTextFlags_CallbackAlways      = 1 << 6    // Call user function every frame
331
+    //ImGuiInputTextFlags_AlignCenter       = 1 << 6,
327 332
 };
328 333
 
329 334
 // User fill ImGuiIO.KeyMap[] array with indices into the ImGuiIO.KeysDown[512] array
@@ -450,12 +455,9 @@ struct ImGuiIO
450 455
     float       MouseDoubleClickTime;       // = 0.30f                  // Time for a double-click, in seconds.
451 456
     float       MouseDoubleClickMaxDist;    // = 6.0f                   // Distance threshold to stay in to validate a double-click, in pixels.
452 457
     int         KeyMap[ImGuiKey_COUNT];     // <unset>                  // Map of indices into the KeysDown[512] entries array
453
-    ImFont      Font;                       // <auto>                   // Gets passed to text functions. Typedef ImFont to the type you want (ImBitmapFont* or your own font).
454
-    float       FontYOffset;                // = 0.0f                   // Offset font rendering by xx pixels in Y axis.
455
-    ImVec2      FontTexUvForWhite;          // = (0.0f,0.0f)            // Font texture must have a white pixel at this UV coordinate. Adjust if you are using custom texture.
456
-    float       FontBaseScale;              // = 1.0f                   // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
457
-    bool        FontAllowUserScaling;       // = false                  // Set to allow scaling text with CTRL+Wheel.
458
-    ImWchar     FontFallbackGlyph;          // = '?'                    // Replacement glyph is one isn't found.
458
+    ImFont*     Font;                       // <auto>                   // Font (also see 'Settings' fields inside ImFont structure for details)
459
+    float       FontGlobalScale;            // = 1.0f                   // Global scale all fonts
460
+    bool        FontAllowUserScaling;       // = false                  // Allow user scaling text of individual window with CTRL+Wheel.
459 461
     float       PixelCenterOffset;          // = 0.0f                   // Try to set to 0.5f or 0.375f if rendering is blurry
460 462
 
461 463
     void*       UserData;                   // = NULL                   // Store your own data for retrieval by callbacks.
@@ -487,7 +489,7 @@ struct ImGuiIO
487 489
 
488 490
     ImVec2      MousePos;                   // Mouse position, in pixels (set to -1,-1 if no mouse / on another screen, etc.)
489 491
     bool        MouseDown[5];               // Mouse buttons. ImGui itself only uses button 0 (left button) but you can use others as storage for convenience.
490
-    int         MouseWheel;                 // Mouse wheel: -1,0,+1
492
+    float       MouseWheel;                 // Mouse wheel: 1 unit scrolls about 5 lines text.  
491 493
     bool        KeyCtrl;                    // Keyboard modifier pressed: Control
492 494
     bool        KeyShift;                   // Keyboard modifier pressed: Shift
493 495
     bool        KeysDown[512];              // Keyboard keys that are pressed (in whatever order user naturally has access to keyboard data)
@@ -524,15 +526,19 @@ struct ImGuiIO
524 526
 //-----------------------------------------------------------------------------
525 527
 
526 528
 // Helper: execute a block of code once a frame only
527
-// Usage: if (IMGUI_ONCE_UPON_A_FRAME) {/*do something once a frame*/)
528
-#define IMGUI_ONCE_UPON_A_FRAME         static ImGuiOncePerFrame im = ImGuiOncePerFrame()
529
-struct ImGuiOncePerFrame
529
+// Convenient if you want to quickly create an UI within deep-nested code that runs multiple times every frame.
530
+// Usage:
531
+//   IMGUI_ONCE_UPON_A_FRAME
532
+//   {
533
+//      // code block will be executed one per frame
534
+//   }
535
+// Attention! the macro expands into 2 statement so make sure you don't use it within e.g. an if() statement without curly braces.
536
+#define IMGUI_ONCE_UPON_A_FRAME    static ImGuiOnceUponAFrame imgui_oaf##__LINE__; if (imgui_oaf##__LINE__)
537
+struct ImGuiOnceUponAFrame
530 538
 {
531
-    ImGuiOncePerFrame() : LastFrame(-1) {}
532
-    operator bool() const { return TryIsNewFrame(); }
533
-private:
534
-    mutable int LastFrame;
535
-    bool        TryIsNewFrame() const   { const int current_frame = ImGui::GetFrameCount(); if (LastFrame == current_frame) return false; LastFrame = current_frame; return true; }
539
+    ImGuiOnceUponAFrame() { RefFrame = -1; }
540
+    mutable int RefFrame;
541
+    operator bool() const { const int current_frame = ImGui::GetFrameCount(); if (RefFrame == current_frame) return false; RefFrame = current_frame; return true; }
536 542
 };
537 543
 
538 544
 // Helper: Parse and apply text filters. In format "aaaaa[,bbbb][,ccccc]"
@@ -573,8 +579,8 @@ struct ImGuiTextBuffer
573 579
 
574 580
     ImGuiTextBuffer()   { Buf.push_back(0); }
575 581
     ~ImGuiTextBuffer()  { clear(); }
576
-    const char*         begin() const { return Buf.begin(); }
577
-    const char*         end() const { return Buf.end()-1; }
582
+    const char*         begin() const { return &Buf.front(); }
583
+    const char*         end() const { return &Buf.back(); }      // Buf is zero-terminated, so end() will point on the zero-terminator
578 584
     size_t              size() const { return Buf.size()-1; }
579 585
     bool                empty() { return Buf.empty(); }
580 586
     void                clear() { Buf.clear(); Buf.push_back(0); }
@@ -589,7 +595,7 @@ struct ImGuiTextBuffer
589 595
 struct ImGuiStorage
590 596
 {
591 597
     struct Pair { ImU32 key; int val; };
592
-    ImVector<Pair>  Data;
598
+    ImVector<Pair>    Data;
593 599
 
594 600
     IMGUI_API void    Clear();
595 601
     IMGUI_API int     GetInt(ImU32 key, int default_val = 0);
@@ -600,6 +606,24 @@ struct ImGuiStorage
600 606
     IMGUI_API void    Insert(ImU32 key, int val);
601 607
 };
602 608
 
609
+// Shared state of InputText(), passed to callback when a ImGuiInputTextFlags_Callback* flag is used.
610
+struct ImGuiTextEditCallbackData
611
+{
612
+    ImGuiKey            EventKey;       // Key pressed (Up/Down/TAB)        // Read-only    
613
+    char*               Buf;            // Current text                     // Read-write (pointed data only)
614
+    size_t              BufSize;        //                                  // Read-only
615
+    bool                BufDirty;       // Set if you modify Buf directly   // Write
616
+    ImGuiInputTextFlags Flags;          // What user passed to InputText()  // Read-only
617
+    int                 CursorPos;      //                                  // Read-write
618
+    int                 SelectionStart; //                                  // Read-write (== to SelectionEnd when no selection)
619
+    int                 SelectionEnd;   //                                  // Read-write
620
+    void*               UserData;       // What user passed to InputText()
621
+
622
+    // NB: calling those function loses selection.
623
+    void DeleteChars(int pos, int bytes_count);
624
+    void InsertChars(int pos, const char* text, const char* text_end = NULL);
625
+};
626
+
603 627
 //-----------------------------------------------------------------------------
604 628
 // Draw List
605 629
 // Hold a series of drawing commands. The user provide a renderer for ImDrawList
@@ -659,19 +683,61 @@ struct ImDrawList
659 683
     IMGUI_API void  AddCircle(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);
660 684
     IMGUI_API void  AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);
661 685
     IMGUI_API void  AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, int a_max, bool tris = false, const ImVec2& third_point_offset = ImVec2(0,0));
662
-    IMGUI_API void  AddText(ImFont font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f);
686
+    IMGUI_API void  AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f);
663 687
 };
664 688
 
665 689
 // Optional bitmap font data loader & renderer into vertices
666
-//  #define ImFont to ImBitmapFont to use
667 690
 // Using the .fnt format exported by BMFont
668 691
 //  - tool: http://www.angelcode.com/products/bmfont
669 692
 //  - file-format: http://www.angelcode.com/products/bmfont/doc/file_format.html
670 693
 // Assume valid file data (won't handle invalid/malicious data)
671
-// Handle a subset of parameters.
672
-//  - kerning pair are not supported (because ImGui code does per-character CalcTextSize calls, need to turn it into something more stateful to allow kerning)
673
-struct ImBitmapFont
694
+// Handle a subset of the options, namely:
695
+//  - kerning pair are not supported (because some ImGui code does per-character CalcTextSize calls, need to turn it into something more state-ful to allow for kerning)
696
+struct ImFont
674 697
 {
698
+    struct FntInfo;
699
+    struct FntCommon;
700
+    struct FntGlyph;
701
+    struct FntKerning;
702
+
703
+    // Settings
704
+    float                       Scale;              // = 1.0f          // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
705
+    ImVec2                      DisplayOffset;      // = (0.0f,0.0f    // Offset font rendering by xx pixels
706
+    ImVec2                      TexUvForWhite;      // = (0.0f,0.0f)   // Font texture must have a white pixel at this UV coordinate. Adjust if you are using custom texture.
707
+    ImWchar                     FallbackChar;       // = '?'           // Replacement glyph is one isn't found.
708
+
709
+    // Data
710
+    unsigned char*              Data;               // Raw data, content of .fnt file
711
+    size_t                      DataSize;           //
712
+    bool                        DataOwned;          // 
713
+    const FntInfo*              Info;               // (point into raw data)
714
+    const FntCommon*            Common;             // (point into raw data)
715
+    const FntGlyph*             Glyphs;             // (point into raw data)
716
+    size_t                      GlyphsCount;        //
717
+    const FntKerning*           Kerning;            // (point into raw data) - NB: kerning is unsupported
718
+    size_t                      KerningCount;       //
719
+    ImVector<const char*>       Filenames;          // (point into raw data)
720
+    ImVector<int>               IndexLookup;        // (built)
721
+    const FntGlyph*             FallbackGlyph;      // == FindGlyph(FontFallbackChar)
722
+
723
+    IMGUI_API ImFont();
724
+    IMGUI_API ~ImFont()         { Clear(); }
725
+
726
+    IMGUI_API bool              LoadFromMemory(const void* data, size_t data_size);
727
+    IMGUI_API bool              LoadFromFile(const char* filename);
728
+    IMGUI_API void              Clear();
729
+    IMGUI_API void              BuildLookupTable();
730
+    IMGUI_API const FntGlyph*   FindGlyph(unsigned short c) const;
731
+    IMGUI_API bool              IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
732
+
733
+    // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
734
+    // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
735
+    IMGUI_API ImVec2            CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
736
+    IMGUI_API ImVec2            CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
737
+    IMGUI_API void              RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
738
+
739
+    IMGUI_API const char*       CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
740
+
675 741
 #pragma pack(push, 1)
676 742
     struct FntInfo
677 743
     {
@@ -681,15 +747,13 @@ struct ImBitmapFont
681 747
         unsigned short  StretchH;
682 748
         unsigned char   AA;
683 749
         unsigned char   PaddingUp, PaddingRight, PaddingDown, PaddingLeft;
684
-        unsigned char   SpacingHoriz, SpacingVert;
685
-        unsigned char   Outline;
750
+        unsigned char   SpacingHoriz, SpacingVert, Outline;
686 751
         //char          FontName[];
687 752
     };
688 753
 
689 754
     struct FntCommon
690 755
     {
691
-        unsigned short  LineHeight;
692
-        unsigned short  Base;
756
+        unsigned short  LineHeight, Base;
693 757
         unsigned short  ScaleW, ScaleH;
694 758
         unsigned short  Pages;
695 759
         unsigned char   BitField;
@@ -699,8 +763,7 @@ struct ImBitmapFont
699 763
     struct FntGlyph
700 764
     {
701 765
         unsigned int    Id;
702
-        unsigned short  X, Y;
703
-        unsigned short  Width, Height;
766
+        unsigned short  X, Y, Width, Height;
704 767
         signed short    XOffset, YOffset;
705 768
         signed short    XAdvance;
706 769
         unsigned char   Page;
@@ -714,37 +777,4 @@ struct ImBitmapFont
714 777
         signed short    Amount;
715 778
     };
716 779
 #pragma pack(pop)
717
-
718
-    unsigned char*          Data;               // Raw data, content of .fnt file
719
-    size_t                  DataSize;           //
720
-    bool                    DataOwned;          // 
721
-    const FntInfo*          Info;               // (point into raw data)
722
-    const FntCommon*        Common;             // (point into raw data)
723
-    const FntGlyph*         Glyphs;             // (point into raw data)
724
-    size_t                  GlyphsCount;        //
725
-    const FntKerning*       Kerning;            // (point into raw data)
726
-    size_t                  KerningCount;       //
727
-    int                     TabCount;           // FIXME: mishandled (add fixed amount instead of aligning to column)
728
-    ImVector<const char*>   Filenames;          // (point into raw data)
729
-    ImVector<int>           IndexLookup;        // (built)
730
-
731
-    IMGUI_API ImBitmapFont();
732
-    IMGUI_API ~ImBitmapFont()      { Clear(); }
733
-
734
-    IMGUI_API bool                 LoadFromMemory(const void* data, size_t data_size);
735
-    IMGUI_API bool                 LoadFromFile(const char* filename);
736
-    IMGUI_API void                 Clear();
737
-    IMGUI_API void                 BuildLookupTable();
738
-    IMGUI_API const FntGlyph *     FindGlyph(unsigned short c, const FntGlyph* fallback = NULL) const;
739
-    IMGUI_API float                GetFontSize() const { return (float)Info->FontSize; }
740
-    IMGUI_API bool                 IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
741
-
742
-    // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
743
-    // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
744
-    IMGUI_API ImVec2               CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
745
-    IMGUI_API ImVec2               CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
746
-    IMGUI_API void                 RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
747
-
748
-private:
749
-    IMGUI_API const char*          CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width, const FntGlyph* fallback_glyph) const;
750 780
 };

正在加载...
取消
保存