-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgui.cpp
More file actions
282 lines (236 loc) · 9.23 KB
/
Copy pathgui.cpp
File metadata and controls
282 lines (236 loc) · 9.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "emacs-module-helpers.h"
#include "emacs-module.h"
#include <cstdlib>
#include <glad/glad.h>
#include "imgui.h"
#include "imgui_impl_opengl3.h"
emacs_value Qdark;
emacs_value Qclassic;
emacs_value Qpress;
emacs_value Qrelease;
static emacs_value Fgl_helper_ui_init(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
float width = extract_double(env, args[0]);
float height = extract_double(env, args[1]);
emacs_value style = args[2];
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize.x = width;
io.DisplaySize.y = height;
io.BackendPlatformName = "emacs";
if (env->eq(env, style, Qdark))
ImGui::StyleColorsDark();
else
ImGui::StyleColorsClassic();
unsigned char* tex_pixels = NULL;
int tex_w, tex_h;
io.Fonts->GetTexDataAsRGBA32(&tex_pixels, &tex_w, &tex_h);
ImGui_ImplOpenGL3_Init("#version 150");
for (int i = 0; i < IM_ARRAYSIZE(io.KeyMap); i++)
io.KeyMap[i] = i;
return Qnil;
}
static emacs_value Fgl_helper_ui_new_frame(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
float delta = extract_double(env, args[0]);
ImGuiIO& io = ImGui::GetIO();
io.DeltaTime = delta;
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
return Qnil;
}
static emacs_value Fgl_helper_ui_render(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
ImGuiIO& io = ImGui::GetIO();
ImGui::EndFrame();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
memset(io.KeysDown, 0, sizeof(io.KeysDown));
return Qnil;
}
static emacs_value Fgl_helper_ui_cursor_pos_callback(emacs_env* env,
ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
ImGuiIO& io = ImGui::GetIO();
double x = extract_double(env, args[0]);
double y = extract_double(env, args[1]);
io.MousePos.x = x;
io.MousePos.y = y;
return Qnil;
}
static emacs_value
Fgl_helper_ui_mouse_button_callback(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
ImGuiIO& io = ImGui::GetIO();
int button = extract_integer(env, args[0]);
emacs_value action = args[1];
if (button > 0) io.MouseDown[button - 1] = env->eq(env, action, Qpress);
return Qnil;
}
static emacs_value Fgl_helper_ui_send_key(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
ImGuiIO& io = ImGui::GetIO();
char* str = copy_string_contents(env, args[0], NULL);
char key = '\0';
if (!strcmp(str, "SPC")) {
key = ' ';
} else if (!strcmp(str, "<backspace>")) {
io.KeysDown[ImGuiKey_Backspace] = true;
} else if (!strcmp(str, "<tab>")) {
io.KeysDown[ImGuiKey_Tab] = true;
} else if (!strcmp(str, "<return>")) {
io.KeysDown[ImGuiKey_Enter] = true;
} else if (!strcmp(str, "<left>")) {
io.KeysDown[ImGuiKey_LeftArrow] = true;
} else if (!strcmp(str, "<right>")) {
io.KeysDown[ImGuiKey_RightArrow] = true;
} else if (!strcmp(str, "<up>")) {
io.KeysDown[ImGuiKey_UpArrow] = true;
} else if (!strcmp(str, "<down>")) {
io.KeysDown[ImGuiKey_DownArrow] = true;
} else if (!strcmp(str, "<delete>")) {
io.KeysDown[ImGuiKey_Delete] = true;
} else if (!strcmp(str, "<home>")) {
io.KeysDown[ImGuiKey_Home] = true;
} else if (!strcmp(str, "<end>")) {
io.KeysDown[ImGuiKey_End] = true;
} else if (!strcmp(str, "<escape>")) {
io.KeysDown[ImGuiKey_Escape] = true;
} else {
key = *str;
}
free(str);
if (key) io.AddInputCharacter(key);
return Qnil;
}
static emacs_value Fgl_helper_ui_begin_window(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
char* title = copy_string_contents(env, args[0], NULL);
ImGui::Begin(title);
ImGui::SetWindowSize(ImVec2(0, 0));
free(title);
return Qnil;
}
static emacs_value Fgl_helper_ui_text(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
char* text = copy_string_contents(env, args[0], NULL);
ImGui::Text(text);
free(text);
return Qnil;
}
static emacs_value Fgl_helper_ui_button(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
char* text = copy_string_contents(env, args[0], NULL);
bool clicked = ImGui::Button(text);
free(text);
if (clicked) return Qt;
return Qnil;
}
static emacs_value Fgl_helper_ui_input_text(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
char buf[4096];
char* text = copy_string_contents(env, args[0], NULL);
char* str = copy_string_contents(env, args[1], NULL);
buf[0] = '\0';
strncat(buf, str, sizeof(buf) - 1);
free(str);
ImGui::InputText(text, buf, sizeof(buf));
free(text);
return env->make_string(env, buf, (ptrdiff_t)strlen(buf));
}
static emacs_value Fgl_helper_ui_slider_float(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
char* text = copy_string_contents(env, args[0], NULL);
float value = extract_double(env, args[1]);
float v_min = extract_double(env, args[2]);
float v_max = extract_double(env, args[3]);
if (text) {
ImGui::SliderFloat(text, &value, v_min, v_max);
free(text);
}
return env->make_float(env, value);
}
static emacs_value Fgl_helper_ui_end_window(emacs_env* env, ptrdiff_t nargs,
emacs_value args[],
void* data) EMACS_NOEXCEPT
{
ImGui::End();
return Qnil;
}
extern "C"
{
void gl_helper_gui_init(emacs_env* env)
{
Qdark = env->make_global_ref(env, env->intern(env, "dark"));
Qclassic = env->make_global_ref(env, env->intern(env, "classic"));
Qpress = env->make_global_ref(env, env->intern(env, "press"));
Qrelease = env->make_global_ref(env, env->intern(env, "release"));
DEFUN("gl-helper-ui-init", Fgl_helper_ui_init, 3, 3,
"Initialize GUI context.\n"
"The argument ARG1 is the width of the display area.\n"
"The argument ARG2 is the height of the display area.\n"
"The argument ARG3 is the style of GUI. "
"It can be 'dark or 'classic.\n",
NULL);
DEFUN(
"gl-helper-ui-new-frame", Fgl_helper_ui_new_frame, 1, 1,
"Prepare to draw GUI to a new frame.\n"
"The argument ARG1 is the delta time elapsed since the last frame",
NULL);
DEFUN("gl-helper-ui-render", Fgl_helper_ui_render, 0, 0,
"Render GUI to current frame.\n", NULL);
DEFUN("gl-helper-ui-cursor-pos-callback",
Fgl_helper_ui_cursor_pos_callback, 2, 2,
"Process cursor position update event.\n", NULL);
DEFUN("gl-helper-ui-mouse-button-callback",
Fgl_helper_ui_mouse_button_callback, 2, 2,
"Process mouse button event.\n", NULL);
DEFUN("gl-helper-ui-send-key", Fgl_helper_ui_send_key, 4, 4,
"Add a key to the GUI input buffer.\n", NULL);
DEFUN("gl-helper-ui-begin-window", Fgl_helper_ui_begin_window, 1, 1,
"Create a new window.\n"
"The argument ARG1 is the title of the window",
NULL);
DEFUN("gl-helper-ui-end-window", Fgl_helper_ui_end_window, 0, 0,
"Finish the current window.\n", NULL);
DEFUN("gl-helper-ui-text", Fgl_helper_ui_text, 1, 1,
"Display text on the current window.\n"
"The argument ARG1 is the text to display",
NULL);
DEFUN("gl-helper-ui-button", Fgl_helper_ui_button, 1, 1,
"Display a button on the current window.\n"
"The argument ARG1 is the caption of the button",
NULL);
DEFUN("gl-helper-ui-input-text", Fgl_helper_ui_input_text, 2, 2,
"Display a text input on the current window.\n"
"The argument ARG1 is the label to display",
NULL);
DEFUN("gl-helper-ui-slider-float", Fgl_helper_ui_slider_float, 4, 4,
"Display a slider on the current window.\n"
"The argument ARG1 is the label to display",
NULL);
}
}