prosperon/source/engine/gui.cpp

266 lines
6.5 KiB
C++
Raw Normal View History

2024-06-05 16:07:44 -05:00
#include "gui.h"
2024-06-07 00:43:15 -05:00
2024-06-05 16:07:44 -05:00
#include "render.h"
#include "sokol/sokol_app.h"
#include "imgui.h"
2024-07-24 14:17:32 -05:00
#include "implot.h"
2024-06-05 16:07:44 -05:00
#define SOKOL_IMPL
#include "sokol/util/sokol_imgui.h"
#include "sokol/util/sokol_gfx_imgui.h"
2024-08-25 14:23:22 -05:00
#include <stdlib.h>
2024-06-05 16:07:44 -05:00
static sgimgui_t sgimgui;
#include "jsffi.h"
2024-09-03 14:08:46 -05:00
static int wantkeys = 0;
static int wantmouse = 0;
2024-08-15 12:26:37 -05:00
JSC_SCALL(imgui_window,
2024-07-24 14:17:32 -05:00
bool active = true;
2024-08-15 12:26:37 -05:00
ImGui::Begin(str, &active);
script_call_sym(argv[1], 0, NULL);
ImGui::End();
ret = boolean2js(active);
)
JSC_SCALL(imgui_menu,
if (ImGui::BeginMenu(str)) {
script_call_sym(argv[1], 0, NULL);
ImGui::EndMenu();
}
2024-06-05 16:07:44 -05:00
)
2024-08-15 12:26:37 -05:00
JSC_CCALL(imgui_menubar,
if (ImGui::BeginMenuBar()) {
script_call_sym(argv[0], 0, NULL);
ImGui::EndMenuBar();
}
)
2024-06-05 16:07:44 -05:00
2024-08-15 12:26:37 -05:00
JSC_CCALL(imgui_mainmenubar,
if (ImGui::BeginMainMenuBar()) {
script_call_sym(argv[0], 0, NULL);
ImGui::EndMainMenuBar();
}
2024-06-05 16:07:44 -05:00
)
JSC_CCALL(imgui_menuitem,
2024-08-19 07:47:27 -05:00
char *name = js2strdup(argv[0]);
2024-08-15 12:26:37 -05:00
char *keyfn = JS_IsUndefined(argv[1]) ? NULL : js2strdup(argv[1]);
bool on = JS_IsUndefined(argv[3]) ? false : js2boolean(argv[3]);
2024-08-19 07:47:27 -05:00
if (ImGui::MenuItem(JS_Is(argv[0]) ? name : "##empty" ,keyfn, &on))
2024-06-05 16:07:44 -05:00
script_call_sym(argv[2], 0, NULL);
2024-08-15 12:26:37 -05:00
2024-08-19 07:47:27 -05:00
if (JS_Is(argv[0])) free(name);
2024-08-15 12:26:37 -05:00
if (keyfn) free(keyfn);
return boolean2js(on);
2024-06-05 16:07:44 -05:00
)
2024-08-15 12:26:37 -05:00
JSC_SCALL(imgui_plot,
2024-07-24 14:17:32 -05:00
ImPlot::SetNextAxisToFit(ImAxis_X1);
ImPlot::SetNextAxisToFit(ImAxis_Y1);
ImPlot::BeginPlot(str);
2024-08-15 12:26:37 -05:00
script_call_sym(argv[1], 0, NULL);
ImPlot::EndPlot();
2024-07-24 14:17:32 -05:00
)
JSC_SCALL(imgui_lineplot,
double data[js_arrlen(argv[1])];
for (int i = 0; i < js_arrlen(argv[1]); i++)
data[i] = js2number(js_getpropidx(argv[1], i));
ImPlot::PlotLine(str, data, js_arrlen(argv[1]));
)
2024-07-01 13:05:26 -05:00
JSC_SSCALL(imgui_textinput,
char buffer[512];
strncpy(buffer, str2, 512);
ImGui::InputText(str, buffer, sizeof(buffer));
ret = str2js(buffer);
)
2024-08-31 00:38:26 -05:00
JSC_SSCALL(imgui_textbox,
char buffer[512];
strncpy(buffer, str2, 512);
ImGui::InputTextMultiline(str, buffer, sizeof(buffer));
ret = str2js(buffer);
2024-08-15 12:26:37 -05:00
)
2024-08-31 00:38:26 -05:00
JSC_SCALL(imgui_text, ImGui::Text(str) )
2024-08-15 12:26:37 -05:00
JSC_SCALL(imgui_button,
if (ImGui::Button(str))
2024-08-25 14:23:22 -05:00
script_call_sym(argv[1], 1, argv);
2024-08-15 12:26:37 -05:00
)
JSC_CCALL(imgui_sokol_gfx,
sgimgui_draw(&sgimgui);
if (ImGui::BeginMenu("sokol-gfx")) {
ImGui::MenuItem("Capabilities", 0, &sgimgui.caps_window.open);
ImGui::MenuItem("Frame Stats", 0, &sgimgui.frame_stats_window.open);
ImGui::MenuItem("Buffers", 0, &sgimgui.buffer_window.open);
ImGui::MenuItem("Images", 0, &sgimgui.image_window.open);
ImGui::MenuItem("Samplers", 0, &sgimgui.sampler_window.open);
ImGui::MenuItem("Shaders", 0, &sgimgui.shader_window.open);
ImGui::MenuItem("Pipelines", 0, &sgimgui.pipeline_window.open);
ImGui::MenuItem("Attachments", 0, &sgimgui.attachments_window.open);
ImGui::MenuItem("Calls", 0, &sgimgui.capture_window.open);
ImGui::EndMenu();
}
)
2024-08-24 18:40:29 -05:00
JSC_SCALL(imgui_slider,
float low = JS_IsUndefined(argv[2]) ? 0.0 : js2number(argv[2]);
float high = JS_IsUndefined(argv[3]) ? 1.0 : js2number(argv[3]);
2024-09-04 07:41:48 -05:00
if (JS_IsArray(js, argv[1])) {
int n = js_arrlen(argv[1]);
float a[n];
js2floatarr(argv[1], n, a);
switch(n) {
case 2:
ImGui::SliderFloat2(str, a, low, high);
break;
case 3:
ImGui::SliderFloat3(str, a, low, high);
break;
case 4:
ImGui::SliderFloat3(str, a, low, high);
break;
}
ret = floatarr2js(n, a);
} else {
float val = js2number(argv[1]);
ImGui::SliderFloat(str, &val, low, high, "%.3f");
ret = number2js(val);
}
2024-08-24 18:40:29 -05:00
)
2024-08-25 00:18:30 -05:00
JSC_SCALL(imgui_checkbox,
bool val = js2boolean(argv[1]);
ImGui::Checkbox(str, &val);
ret = boolean2js(val);
)
2024-08-25 14:23:22 -05:00
JSC_CCALL(imgui_pushid,
ImGui::PushID(js2number(argv[0]));
)
JSC_CCALL(imgui_popid, ImGui::PopID(); )
2024-08-25 15:29:35 -05:00
JSC_CCALL(imgui_image,
texture *tex = js2texture(argv[0]);
2024-08-30 18:22:53 -05:00
simgui_image_desc_t simgd;
simgd.image = tex->id;
simgd.sampler = std_sampler;
simgui_image_t simgui_img = simgui_make_image(&simgd);
2024-08-25 15:29:35 -05:00
ImTextureID tex_id = simgui_imtextureid(simgui_img);
2024-08-30 18:22:53 -05:00
ImGui::Image(tex_id, ImVec2(tex->width, tex->height), ImVec2(0,0), ImVec2(1,1));
2024-08-25 15:29:35 -05:00
simgui_destroy_image(simgui_img);
)
2024-08-30 18:22:53 -05:00
JSC_CCALL(imgui_sameline, ImGui::SameLine() )
JSC_SCALL(imgui_columns, ImGui::Columns(js2number(argv[1]), str) )
JSC_CCALL(imgui_nextcolumn, ImGui::NextColumn() )
JSC_SCALL(imgui_collapsingheader, ret = boolean2js(ImGui::CollapsingHeader(str)) )
2024-08-31 00:38:26 -05:00
JSC_SCALL(imgui_radio, ret = boolean2js(ImGui::RadioButton(str, js2boolean(argv[1]))))
2024-08-28 16:38:31 -05:00
2024-09-04 13:23:20 -05:00
JSC_SCALL(imgui_tree,
if (ImGui::TreeNode(str)) {
script_call_sym(argv[1],0,NULL);
ImGui::TreePop();
}
)
JSC_SCALL(imgui_listbox,
char **arr = js2strarr(argv[1]);
int n = js_arrlen(argv[1]);
int idx = js2number(argv[2]);
ImGui::ListBox(str, &idx, arr, n, 4);
for (int i = 0; i < n; i++)
free(arr[i]);
// arrfree(arr); // TODO: Doesn't this need freed?
ret = number2js(idx);
)
2024-06-05 16:07:44 -05:00
static const JSCFunctionListEntry js_imgui_funcs[] = {
2024-08-15 12:26:37 -05:00
MIST_FUNC_DEF(imgui, window, 2),
MIST_FUNC_DEF(imgui, menu, 2),
2024-08-28 13:49:24 -05:00
MIST_FUNC_DEF(imgui, sameline, 0),
2024-08-25 14:23:22 -05:00
MIST_FUNC_DEF(imgui, pushid, 1),
MIST_FUNC_DEF(imgui, popid, 0),
2024-08-24 18:40:29 -05:00
MIST_FUNC_DEF(imgui, slider, 4),
2024-08-15 12:26:37 -05:00
MIST_FUNC_DEF(imgui, menubar, 1),
MIST_FUNC_DEF(imgui, mainmenubar, 1),
2024-06-05 16:07:44 -05:00
MIST_FUNC_DEF(imgui, menuitem, 3),
2024-08-31 00:38:26 -05:00
MIST_FUNC_DEF(imgui, radio, 2),
2024-08-25 15:29:35 -05:00
MIST_FUNC_DEF(imgui, image, 1),
2024-07-01 13:05:26 -05:00
MIST_FUNC_DEF(imgui, textinput, 2),
2024-08-31 00:38:26 -05:00
MIST_FUNC_DEF(imgui, textbox, 2),
2024-08-15 12:26:37 -05:00
MIST_FUNC_DEF(imgui, button, 2),
2024-08-25 00:18:30 -05:00
MIST_FUNC_DEF(imgui, checkbox, 2),
2024-08-15 12:26:37 -05:00
MIST_FUNC_DEF(imgui, text, 1),
MIST_FUNC_DEF(imgui, plot,1),
2024-08-30 14:17:37 -05:00
MIST_FUNC_DEF(imgui, lineplot,2),
2024-08-15 12:26:37 -05:00
MIST_FUNC_DEF(imgui, sokol_gfx, 0),
2024-08-28 16:38:31 -05:00
MIST_FUNC_DEF(imgui, columns, 2),
MIST_FUNC_DEF(imgui, nextcolumn, 0),
2024-08-30 18:22:53 -05:00
MIST_FUNC_DEF(imgui, collapsingheader, 1),
2024-09-04 13:23:20 -05:00
MIST_FUNC_DEF(imgui, tree, 2),
MIST_FUNC_DEF(imgui, listbox, 3),
2024-06-05 16:07:44 -05:00
};
static int started = 0;
JSValue gui_init(JSContext *js)
{
simgui_desc_t sdesc = {0};
simgui_setup(&sdesc);
sgimgui_desc_t desc = {0};
sgimgui_init(&sgimgui, &desc);
2024-07-24 14:17:32 -05:00
ImPlot::CreateContext();
2024-06-05 16:07:44 -05:00
JSValue imgui = JS_NewObject(js);
JS_SetPropertyFunctionList(js, imgui, js_imgui_funcs, countof(js_imgui_funcs));
started = 1;
return imgui;
}
2024-09-03 14:08:46 -05:00
void gui_input(sapp_event *e)
2024-06-05 16:07:44 -05:00
{
2024-09-04 13:23:20 -05:00
if (!started) return;
2024-09-03 14:08:46 -05:00
simgui_handle_event(e);
ImGuiIO io = ImGui::GetIO();
wantkeys = io.WantCaptureKeyboard;
wantmouse = io.WantCaptureMouse;
2024-06-05 16:07:44 -05:00
}
void gui_newframe(int x, int y, float dt)
{
2024-06-07 00:43:15 -05:00
simgui_frame_desc_t frame = {
2024-06-05 16:07:44 -05:00
.width = x,
.height = y,
.delta_time = dt
2024-06-07 00:43:15 -05:00
};
simgui_new_frame(&frame);
2024-06-05 16:07:44 -05:00
}
void gui_endframe()
{
simgui_render();
}
void gui_exit()
{
sgimgui_discard(&sgimgui);
}
2024-09-03 14:08:46 -05:00
int gui_wantmouse() { return wantmouse; }
int gui_wantkeys() { return wantkeys; }