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