-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathimgui_lua_bindings.cpp
508 lines (440 loc) · 13.1 KB
/
imgui_lua_bindings.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include <stdio.h>
#include <imgui.h>
#include <deque>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
// THIS IS FOR LUA 5.3 although you can make a few changes for other versions
// define ENABLE_IM_LUA_END_STACK
// to keep track of end and begins and clean up the imgui stack
// if lua errors
// define this global before you call RunString or LoadImGuiBindings
lua_State* lState;
#ifdef ENABLE_IM_LUA_END_STACK
// Stack for imgui begin and end
std::deque<int> endStack;
static void AddToStack(int type) {
endStack.push_back(type);
}
static void PopEndStack(int type) {
if (!endStack.empty()) {
endStack.pop_back(); // hopefully the type matches
}
}
static void ImEndStack(int type);
#endif
// Example lua run string function
// returns NULL on success and error string on error
const char * RunString(const char* szLua) {
if (!lState) {
fprintf(stderr, "You didn't assign the global lState, either assign that or refactor LoadImguiBindings and RunString\n");
}
int iStatus = luaL_loadstring(lState, szLua);
if(iStatus) {
return lua_tostring(lState, -1);
//fprintf(stderr, "Lua syntax error: %s\n", lua_tostring(lState, -1));
//return;
}
#ifdef ENABLE_IM_LUA_END_STACK
endStack.clear();
#endif
iStatus = lua_pcall( lState, 0, 0, 0 );
#ifdef ENABLE_IM_LUA_END_STACK
bool wasEmpty = endStack.empty();
while(!endStack.empty()) {
ImEndStack(endStack.back());
endStack.pop_back();
}
#endif
if( iStatus )
{
return lua_tostring(lState, -1);
//fprintf(stderr, "Error: %s\n", lua_tostring( lState, -1 ));
//return;
}
#ifdef ENABLE_IM_LUA_END_STACK
else if (!wasEmpty) {
return "Script didn't clean up imgui stack properly";
}
#endif
return NULL;
}
#define IMGUI_FUNCTION_DRAW_LIST(name) \
static int impl_draw_list_##name(lua_State *L) { \
int max_args = lua_gettop(L); \
int arg = 1; \
int stackval = 0;
#define IMGUI_FUNCTION(name) \
static int impl_##name(lua_State *L) { \
int max_args = lua_gettop(L); \
int arg = 1; \
int stackval = 0;
// I use OpenGL so this is a GLuint
// Using unsigned int cause im lazy don't copy me
#define IM_TEXTURE_ID_ARG(name) \
const ImTextureID name = (ImTextureID)luaL_checkinteger(L, arg++);
#define OPTIONAL_LABEL_ARG(name) \
const char* name; \
if (arg <= max_args) { \
name = lua_tostring(L, arg++); \
} else { \
name = NULL; \
}
#define LABEL_ARG(name) \
size_t i_##name##_size; \
const char * name = luaL_checklstring(L, arg++, &(i_##name##_size));
#define IM_VEC_2_ARG(name)\
const lua_Number i_##name##_x = luaL_checknumber(L, arg++); \
const lua_Number i_##name##_y = luaL_checknumber(L, arg++); \
const ImVec2 name((double)i_##name##_x, (double)i_##name##_y);
#define OPTIONAL_IM_VEC_2_ARG(name, x, y) \
lua_Number i_##name##_x = x; \
lua_Number i_##name##_y = y; \
if (arg <= max_args - 1) { \
i_##name##_x = luaL_checknumber(L, arg++); \
i_##name##_y = luaL_checknumber(L, arg++); \
} \
const ImVec2 name((double)i_##name##_x, (double)i_##name##_y);
#define IM_VEC_4_ARG(name) \
const lua_Number i_##name##_x = luaL_checknumber(L, arg++); \
const lua_Number i_##name##_y = luaL_checknumber(L, arg++); \
const lua_Number i_##name##_z = luaL_checknumber(L, arg++); \
const lua_Number i_##name##_w = luaL_checknumber(L, arg++); \
const ImVec4 name((double)i_##name##_x, (double)i_##name##_y, (double)i_##name##_z, (double)i_##name##_w);
#define OPTIONAL_IM_VEC_4_ARG(name, x, y, z, w) \
lua_Number i_##name##_x = x; \
lua_Number i_##name##_y = y; \
lua_Number i_##name##_z = z; \
lua_Number i_##name##_w = w; \
if (arg <= max_args - 1) { \
i_##name##_x = luaL_checknumber(L, arg++); \
i_##name##_y = luaL_checknumber(L, arg++); \
i_##name##_z = luaL_checknumber(L, arg++); \
i_##name##_w = luaL_checknumber(L, arg++); \
} \
const ImVec4 name((double)i_##name##_x, (double)i_##name##_y, (double)i_##name##_z, (double)i_##name##_w);
#define NUMBER_ARG(name)\
lua_Number name = luaL_checknumber(L, arg++);
#define OPTIONAL_NUMBER_ARG(name, otherwise)\
lua_Number name = otherwise; \
if (arg <= max_args) { \
name = lua_tonumber(L, arg++); \
}
#define FLOAT_POINTER_ARG(name) \
float i_##name##_value = luaL_checknumber(L, arg++); \
float* name = &(i_##name##_value);
#define END_FLOAT_POINTER(name) \
if (name != NULL) { \
lua_pushnumber(L, i_##name##_value); \
stackval++; \
}
#define OPTIONAL_INT_ARG(name, otherwise)\
int name = otherwise; \
if (arg <= max_args) { \
name = (int)lua_tonumber(L, arg++); \
}
#define INT_ARG(name) \
const int name = (int)luaL_checknumber(L, arg++);
#define OPTIONAL_UINT_ARG(name, otherwise)\
unsigned int name = otherwise; \
if (arg <= max_args) { \
name = (unsigned int)lua_tounsigned(L, arg++); \
}
#define UINT_ARG(name) \
const unsigned int name = (unsigned int)luaL_checkinteger(L, arg++);
#define INT_POINTER_ARG(name) \
int i_##name##_value = (int)luaL_checkinteger(L, arg++); \
int* name = &(i_##name##_value);
#define END_INT_POINTER(name) \
if (name != NULL) { \
lua_pushnumber(L, i_##name##_value); \
stackval++; \
}
#define UINT_POINTER_ARG(name) \
unsigned int i_##name##_value = (unsigned int)luaL_checkinteger(L, arg++); \
unsigned int* name = &(i_##name##_value);
#define END_UINT_POINTER(name) \
if (name != NULL) { \
lua_pushnumber(L, i_##name##_value); \
stackval++; \
}
#define BOOL_POINTER_ARG(name) \
bool i_##name##_value = lua_toboolean(L, arg++); \
bool* name = &(i_##name##_value);
#define OPTIONAL_BOOL_POINTER_ARG(name) \
bool i_##name##_value; \
bool* name = NULL; \
if (arg <= max_args) { \
i_##name##_value = lua_toboolean(L, arg++); \
name = &(i_##name##_value); \
}
#define OPTIONAL_BOOL_ARG(name, otherwise) \
bool name = otherwise; \
if (arg <= max_args) { \
name = lua_toboolean(L, arg++); \
}
#define BOOL_ARG(name) \
bool name = lua_toboolean(L, arg++);
#define CALL_FUNCTION(name, retType,...) \
retType ret = ImGui::name(__VA_ARGS__);
#define DRAW_LIST_CALL_FUNCTION(name, retType,...) \
retType ret = ImGui::GetWindowDrawList()->name(__VA_ARGS__);
#define CALL_FUNCTION_NO_RET(name, ...) \
ImGui::name(__VA_ARGS__);
#define DRAW_LIST_CALL_FUNCTION_NO_RET(name, ...) \
ImGui::GetWindowDrawList()->name(__VA_ARGS__);
#define PUSH_STRING(name) \
lua_pushstring(L, name); \
stackval++;
#define PUSH_NUMBER(name) \
lua_pushnumber(L, name); \
stackval++;
#define PUSH_BOOL(name) \
lua_pushboolean(L, (int) name); \
stackval++;
#define END_BOOL_POINTER(name) \
if (name != NULL) { \
lua_pushboolean(L, (int)i_##name##_value); \
stackval++; \
}
#define END_IMGUI_FUNC \
return stackval; \
}
#ifdef ENABLE_IM_LUA_END_STACK
#define IF_RET_ADD_END_STACK(type) \
if (ret) { \
AddToStack(type); \
}
#define ADD_END_STACK(type) \
AddToStack(type);
#define POP_END_STACK(type) \
PopEndStack(type);
#define END_STACK_START \
static void ImEndStack(int type) { \
switch(type) {
#define END_STACK_OPTION(type, function) \
case type: \
ImGui::function(); \
break;
#define END_STACK_END \
} \
}
#else
#define END_STACK_START
#define END_STACK_OPTION(type, function)
#define END_STACK_END
#define IF_RET_ADD_END_STACK(type)
#define ADD_END_STACK(type)
#define POP_END_STACK(type)
#endif
#define START_ENUM(name)
#define MAKE_ENUM(c_name,lua_name)
#define END_ENUM(name)
#include "imgui_iterator.inl"
static const struct luaL_Reg imguilib [] = {
#undef IMGUI_FUNCTION
#define IMGUI_FUNCTION(name) {#name, impl_##name},
#undef IMGUI_FUNCTION_DRAW_LIST
#define IMGUI_FUNCTION_DRAW_LIST(name) {"DrawList_" #name, impl_draw_list_##name},
// These defines are just redefining everything to nothing so
// we can get the function names
#undef IM_TEXTURE_ID_ARG
#define IM_TEXTURE_ID_ARG(name)
#undef OPTIONAL_LABEL_ARG
#define OPTIONAL_LABEL_ARG(name)
#undef LABEL_ARG
#define LABEL_ARG(name)
#undef IM_VEC_2_ARG
#define IM_VEC_2_ARG(name)
#undef OPTIONAL_IM_VEC_2_ARG
#define OPTIONAL_IM_VEC_2_ARG(name, x, y)
#undef IM_VEC_4_ARG
#define IM_VEC_4_ARG(name)
#undef OPTIONAL_IM_VEC_4_ARG
#define OPTIONAL_IM_VEC_4_ARG(name, x, y, z, w)
#undef NUMBER_ARG
#define NUMBER_ARG(name)
#undef OPTIONAL_NUMBER_ARG
#define OPTIONAL_NUMBER_ARG(name, otherwise)
#undef FLOAT_POINTER_ARG
#define FLOAT_POINTER_ARG(name)
#undef END_FLOAT_POINTER
#define END_FLOAT_POINTER(name)
#undef OPTIONAL_INT_ARG
#define OPTIONAL_INT_ARG(name, otherwise)
#undef INT_ARG
#define INT_ARG(name)
#undef OPTIONAL_UINT_ARG
#define OPTIONAL_UINT_ARG(name, otherwise)
#undef UINT_ARG
#define UINT_ARG(name)
#undef INT_POINTER_ARG
#define INT_POINTER_ARG(name)
#undef END_INT_POINTER
#define END_INT_POINTER(name)
#undef UINT_POINTER_ARG
#define UINT_POINTER_ARG(name)
#undef END_UINT_POINTER
#define END_UINT_POINTER(name)
#undef BOOL_POINTER_ARG
#define BOOL_POINTER_ARG(name)
#undef OPTIONAL_BOOL_POINTER_ARG
#define OPTIONAL_BOOL_POINTER_ARG(name)
#undef OPTIONAL_BOOL_ARG
#define OPTIONAL_BOOL_ARG(name, otherwise)
#undef BOOL_ARG
#define BOOL_ARG(name)
#undef CALL_FUNCTION
#define CALL_FUNCTION(name, retType, ...)
#undef DRAW_LIST_CALL_FUNCTION
#define DRAW_LIST_CALL_FUNCTION(name, retType, ...)
#undef CALL_FUNCTION_NO_RET
#define CALL_FUNCTION_NO_RET(name, ...)
#undef DRAW_LIST_CALL_FUNCTION_NO_RET
#define DRAW_LIST_CALL_FUNCTION_NO_RET(name, ...)
#undef PUSH_STRING
#define PUSH_STRING(name)
#undef PUSH_NUMBER
#define PUSH_NUMBER(name)
#undef PUSH_BOOL
#define PUSH_BOOL(name)
#undef END_BOOL_POINTER
#define END_BOOL_POINTER(name)
#undef END_IMGUI_FUNC
#define END_IMGUI_FUNC
#undef END_STACK_START
#define END_STACK_START
#undef END_STACK_OPTION
#define END_STACK_OPTION(type, function)
#undef END_STACK_END
#define END_STACK_END
#undef IF_RET_ADD_END_STACK
#define IF_RET_ADD_END_STACK(type)
#undef ADD_END_STACK
#define ADD_END_STACK(type)
#undef POP_END_STACK
#define POP_END_STACK(type)
#undef START_ENUM
#define START_ENUM(name)
#undef MAKE_ENUM
#define MAKE_ENUM(c_name,lua_name)
#undef END_ENUM
#define END_ENUM(name)
#include "imgui_iterator.inl"
{"Button", impl_Button},
{NULL, NULL}
};
static void PushImguiEnums(lua_State* lState, const char* tableName) {
lua_pushstring(lState, tableName);
lua_newtable(lState);
#undef START_ENUM
#undef MAKE_ENUM
#undef END_ENUM
#define START_ENUM(name) \
lua_pushstring(lState, #name); \
lua_newtable(lState); \
{ \
int i = 1;
#define MAKE_ENUM(c_name,lua_name) \
lua_pushstring(lState, #lua_name); \
lua_pushnumber(lState, c_name); \
lua_rawset(lState, -3);
#define END_ENUM(name) \
} \
lua_rawset(lState, -3);
// These defines are just redefining everything to nothing so
// we get only the enums.
#undef IMGUI_FUNCTION
#define IMGUI_FUNCTION(name)
#undef IMGUI_FUNCTION_DRAW_LIST
#define IMGUI_FUNCTION_DRAW_LIST(name)
#undef IM_TEXTURE_ID_ARG
#define IM_TEXTURE_ID_ARG(name)
#undef OPTIONAL_LABEL_ARG
#define OPTIONAL_LABEL_ARG(name)
#undef LABEL_ARG
#define LABEL_ARG(name)
#undef IM_VEC_2_ARG
#define IM_VEC_2_ARG(name)
#undef OPTIONAL_IM_VEC_2_ARG
#define OPTIONAL_IM_VEC_2_ARG(name, x, y)
#undef IM_VEC_4_ARG
#define IM_VEC_4_ARG(name)
#undef OPTIONAL_IM_VEC_4_ARG
#define OPTIONAL_IM_VEC_4_ARG(name, x, y, z, w)
#undef NUMBER_ARG
#define NUMBER_ARG(name)
#undef OPTIONAL_NUMBER_ARG
#define OPTIONAL_NUMBER_ARG(name, otherwise)
#undef FLOAT_POINTER_ARG
#define FLOAT_POINTER_ARG(name)
#undef END_FLOAT_POINTER
#define END_FLOAT_POINTER(name)
#undef OPTIONAL_INT_ARG
#define OPTIONAL_INT_ARG(name, otherwise)
#undef INT_ARG
#define INT_ARG(name)
#undef OPTIONAL_UINT_ARG
#define OPTIONAL_UINT_ARG(name, otherwise)
#undef UINT_ARG
#define UINT_ARG(name)
#undef INT_POINTER_ARG
#define INT_POINTER_ARG(name)
#undef END_INT_POINTER
#define END_INT_POINTER(name)
#undef UINT_POINTER_ARG
#define UINT_POINTER_ARG(name)
#undef END_UINT_POINTER
#define END_UINT_POINTER(name)
#undef BOOL_POINTER_ARG
#define BOOL_POINTER_ARG(name)
#undef OPTIONAL_BOOL_POINTER_ARG
#define OPTIONAL_BOOL_POINTER_ARG(name)
#undef OPTIONAL_BOOL_ARG
#define OPTIONAL_BOOL_ARG(name, otherwise)
#undef BOOL_ARG
#define BOOL_ARG(name)
#undef CALL_FUNCTION
#define CALL_FUNCTION(name, retType, ...)
#undef DRAW_LIST_CALL_FUNCTION
#define DRAW_LIST_CALL_FUNCTION(name, retType, ...)
#undef CALL_FUNCTION_NO_RET
#define CALL_FUNCTION_NO_RET(name, ...)
#undef DRAW_LIST_CALL_FUNCTION_NO_RET
#define DRAW_LIST_CALL_FUNCTION_NO_RET(name, ...)
#undef PUSH_STRING
#define PUSH_STRING(name)
#undef PUSH_NUMBER
#define PUSH_NUMBER(name)
#undef PUSH_BOOL
#define PUSH_BOOL(name)
#undef END_BOOL_POINTER
#define END_BOOL_POINTER(name)
#undef END_IMGUI_FUNC
#define END_IMGUI_FUNC
#undef END_STACK_START
#define END_STACK_START
#undef END_STACK_OPTION
#define END_STACK_OPTION(type, function)
#undef END_STACK_END
#define END_STACK_END
#undef IF_RET_ADD_END_STACK
#define IF_RET_ADD_END_STACK(type)
#undef ADD_END_STACK
#define ADD_END_STACK(type)
#undef POP_END_STACK
#define POP_END_STACK(type)
#include "imgui_iterator.inl"
lua_rawset(lState, -3);
};
void LoadImguiBindings() {
if (!lState) {
fprintf(stderr, "You didn't assign the global lState, either assign that or refactor LoadImguiBindings and RunString\n");
}
lua_newtable(lState);
luaL_setfuncs(lState, imguilib, 0);
PushImguiEnums(lState, "constant");
lua_setglobal(lState, "imgui");
}