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"
|
|
|
|
|
|
|
|
static sgimgui_t sgimgui;
|
|
|
|
|
|
|
|
#include "jsffi.h"
|
|
|
|
|
|
|
|
JSC_CCALL(imgui_begin,
|
2024-07-24 14:17:32 -05:00
|
|
|
char *title = js2strdup(argv[0]);
|
|
|
|
bool active = true;
|
|
|
|
ImGui::Begin(title, &active, ImGuiWindowFlags_MenuBar);
|
|
|
|
free(title);
|
|
|
|
return boolean2js(active);
|
2024-06-05 16:07:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(imgui_end, ImGui::End())
|
|
|
|
|
|
|
|
JSC_CCALL(imgui_beginmenu,
|
|
|
|
char *title = js2strdup(argv[0]);
|
|
|
|
bool active = ImGui::BeginMenu(title);
|
|
|
|
free(title);
|
|
|
|
return boolean2js(active);
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(imgui_menuitem,
|
|
|
|
char *name = js2strdup(argv[0]);
|
|
|
|
char *hotkey = js2strdup(argv[1]);
|
|
|
|
if (ImGui::MenuItem(name,hotkey))
|
|
|
|
script_call_sym(argv[2], 0, NULL);
|
|
|
|
free(name);
|
|
|
|
free(hotkey);
|
|
|
|
)
|
|
|
|
|
2024-07-24 14:17:32 -05:00
|
|
|
JSC_SCALL(imgui_startplot,
|
|
|
|
ImPlot::SetNextAxisToFit(ImAxis_X1);
|
|
|
|
ImPlot::SetNextAxisToFit(ImAxis_Y1);
|
|
|
|
ImPlot::BeginPlot(str);
|
|
|
|
)
|
|
|
|
|
|
|
|
JSC_CCALL(imgui_endplot, ImPlot::EndPlot() )
|
|
|
|
|
|
|
|
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-06-05 16:07:44 -05:00
|
|
|
JSC_CCALL(imgui_beginmenubar, ImGui::BeginMenuBar())
|
|
|
|
JSC_CCALL(imgui_endmenubar, ImGui::EndMenuBar())
|
|
|
|
|
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-06-05 16:07:44 -05:00
|
|
|
static const JSCFunctionListEntry js_imgui_funcs[] = {
|
|
|
|
MIST_FUNC_DEF(imgui, begin, 1),
|
|
|
|
MIST_FUNC_DEF(imgui, end,0),
|
|
|
|
MIST_FUNC_DEF(imgui, beginmenu, 1),
|
|
|
|
MIST_FUNC_DEF(imgui, menuitem, 3),
|
|
|
|
MIST_FUNC_DEF(imgui, beginmenubar, 0),
|
2024-07-01 13:05:26 -05:00
|
|
|
MIST_FUNC_DEF(imgui, endmenubar, 0),
|
|
|
|
MIST_FUNC_DEF(imgui, textinput, 2),
|
2024-07-24 14:17:32 -05:00
|
|
|
MIST_FUNC_DEF(imgui, startplot,1),
|
|
|
|
MIST_FUNC_DEF(imgui,endplot,0),
|
|
|
|
MIST_FUNC_DEF(imgui,lineplot,2)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gui_input(sapp_event *e)
|
|
|
|
{
|
|
|
|
if (started)
|
|
|
|
simgui_handle_event(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 gfx_gui()
|
|
|
|
{
|
|
|
|
sgimgui_draw(&sgimgui);
|
|
|
|
sgimgui_draw_menu(&sgimgui, "sokol-gfx");
|
|
|
|
}
|
|
|
|
|
|
|
|
void gui_endframe()
|
|
|
|
{
|
|
|
|
simgui_render();
|
|
|
|
}
|
|
|
|
|
|
|
|
void gui_exit()
|
|
|
|
{
|
|
|
|
sgimgui_discard(&sgimgui);
|
|
|
|
}
|