more elaborate keyboard input handling, and debug gui improvements
[laserbrain_demo] / src / dbg_gui.cc
1 #include <ctype.h>
2 #include "dbg_gui.h"
3 #include "imgui/imgui.h"
4 #include "app.h"
5 #include "image.h"
6 #include "texture.h"
7
8 static void render_func(ImDrawData *ddat);
9
10 static ImGuiIO *io;
11 static Texture *tex;
12
13 bool init_debug_gui()
14 {
15         io = ImGui::GetIOPtr();
16         io->DisplaySize.x = win_width;
17         io->DisplaySize.y = win_height;
18         io->RenderDrawListsFn = render_func;
19
20         io->KeyMap[ImGuiKey_Tab] = '\t';
21         io->KeyMap[ImGuiKey_LeftArrow] = KEY_LEFT;
22         io->KeyMap[ImGuiKey_RightArrow] = KEY_RIGHT;
23         io->KeyMap[ImGuiKey_UpArrow] = KEY_UP;
24         io->KeyMap[ImGuiKey_DownArrow] = KEY_DOWN;
25         io->KeyMap[ImGuiKey_PageUp] = KEY_PGUP;
26         io->KeyMap[ImGuiKey_PageDown] = KEY_PGDOWN;
27         io->KeyMap[ImGuiKey_Home] = KEY_HOME;
28         io->KeyMap[ImGuiKey_End] = KEY_END;
29         io->KeyMap[ImGuiKey_Delete] = KEY_DEL;
30         io->KeyMap[ImGuiKey_Backspace] = '\b';
31         io->KeyMap[ImGuiKey_Enter] = '\n';
32         io->KeyMap[ImGuiKey_Escape] = 27;
33         io->KeyMap[ImGuiKey_A] = 'a';
34         io->KeyMap[ImGuiKey_C] = 'c';
35         io->KeyMap[ImGuiKey_V] = 'v';
36         io->KeyMap[ImGuiKey_X] = 'x';
37         io->KeyMap[ImGuiKey_Y] = 'y';
38         io->KeyMap[ImGuiKey_Z] = 'z';
39
40         unsigned char *pixels;
41         int width, height;
42         io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
43
44         Image img;
45         img.set_pixels(width, height, pixels, Image::FMT_RGBA);
46
47         tex = new Texture;
48         tex->set_image(img);
49         io->Fonts->TexID = (void*)tex;
50
51         return true;
52 }
53
54 void cleanup_debug_gui()
55 {
56         delete tex;
57         tex = 0;
58 }
59
60 void debug_gui_reshape(int x, int y)
61 {
62         io->DisplaySize.x = x;
63         io->DisplaySize.y = y;
64 }
65
66 void debug_gui_key(int key, bool press, unsigned int modstate)
67 {
68         if(key < 512) {
69                 io->KeysDown[key] = press;
70         }
71         io->KeyShift = (modstate & MOD_SHIFT) != 0;
72         io->KeyCtrl = (modstate & MOD_CTRL) != 0;
73         io->KeyAlt = (modstate & MOD_ALT) != 0;
74         io->KeySuper = false;
75
76         if(press && key < 256 && isprint(key)) {
77                 io->AddInputCharacter(key);
78         }
79 }
80
81 void debug_gui_mbutton(int bn, bool press, int x, int y)
82 {
83         io->MouseDown[bn] = press;
84         io->MousePos.x = x;
85         io->MousePos.y = y;
86 }
87
88 void debug_gui_mmotion(int x, int y)
89 {
90         io->MousePos.x = x;
91         io->MousePos.y = y;
92 }
93
94 void debug_gui_wheel(int dir)
95 {
96         io->MouseWheel = dir;
97 }
98
99 static void render_func(ImDrawData *ddat)
100 {
101         glViewport(0, 0, win_width, win_height);
102
103         glPushAttrib(GL_ENABLE_BIT);
104         glEnable(GL_BLEND);
105         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
106         glDisable(GL_CULL_FACE);
107         glDisable(GL_DEPTH_TEST);
108         glDisable(GL_LIGHTING);
109         glEnable(GL_SCISSOR_TEST);
110         glEnable(GL_TEXTURE_2D);
111         glUseProgram(0);
112
113         glMatrixMode(GL_PROJECTION);
114         glPushMatrix();
115         glLoadIdentity();
116         glOrtho(0, win_width, win_height, 0.0, -1, 1);
117
118         glMatrixMode(GL_MODELVIEW);
119         glPushMatrix();
120         glLoadIdentity();
121
122         for(int i=0; i<ddat->CmdListsCount; i++) {
123                 ImDrawList *cmdlist = ddat->CmdLists[i];
124                 ImDrawVert *vbuf = cmdlist->VtxBuffer.Data;
125                 ImDrawIdx *ibuf = cmdlist->IdxBuffer.Data;
126
127                 for(int j=0; j<cmdlist->CmdBuffer.Size; j++) {
128                         ImDrawCmd *cmd = &cmdlist->CmdBuffer[j];
129                         if(cmd->UserCallback) {
130                                 cmd->UserCallback(cmdlist, cmd);
131                         } else {
132
133                                 bind_texture((Texture*)cmd->TextureId);
134
135                                 glScissor(cmd->ClipRect.x, win_height - cmd->ClipRect.w,
136                                                 cmd->ClipRect.z - cmd->ClipRect.x, cmd->ClipRect.w - cmd->ClipRect.y);
137
138                                 glBindBuffer(GL_ARRAY_BUFFER, 0);
139                                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
140
141                                 glEnableClientState(GL_VERTEX_ARRAY);
142                                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
143                                 glEnableClientState(GL_COLOR_ARRAY);
144
145                                 glVertexPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].pos);
146                                 glTexCoordPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].uv);
147                                 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof *vbuf, &vbuf[0].col);
148
149                                 glDrawElements(GL_TRIANGLES, cmd->ElemCount, GL_UNSIGNED_SHORT, ibuf);
150
151                                 glDisableClientState(GL_VERTEX_ARRAY);
152                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
153                                 glDisableClientState(GL_COLOR_ARRAY);
154                         }
155                         ibuf += cmd->ElemCount;
156                 }
157         }
158
159         glMatrixMode(GL_PROJECTION);
160         glPopMatrix();
161
162         glMatrixMode(GL_MODELVIEW);
163         glPopMatrix();
164
165         glPopAttrib();
166 }
167