Skip to content

Commit 13e5801

Browse files
authored
Initial support for remote control (#29)
1 parent f28a9e4 commit 13e5801

5 files changed

Lines changed: 293 additions & 52 deletions

File tree

DebugGUI/src/DebugGUI.cxx

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,50 @@ namespace framework
2424
// @return an object of kind GLFWwindow* as void* to avoid having a direct dependency
2525
void* initGUI(const char* name, void(*error_callback)(int, char const*description))
2626
{
27-
// Setup window
28-
if (error_callback == nullptr) {
29-
glfwSetErrorCallback(default_error_callback);
27+
GLFWwindow* window = nullptr;
28+
if (name) {
29+
// Setup window
30+
if (error_callback == nullptr) {
31+
glfwSetErrorCallback(default_error_callback);
32+
}
33+
if (!glfwInit())
34+
return nullptr;
35+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
36+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
37+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
38+
#if __APPLE__
39+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
40+
#endif
41+
window = glfwCreateWindow(1280, 720, name, nullptr, nullptr);
42+
glfwMakeContextCurrent(window);
43+
gl3wInit();
44+
45+
// Setup ImGui binding
46+
ImGui_ImplGlfwGL3_Init(window, true);
47+
} else {
48+
ImGui::CreateContext();
49+
ImGuiIO& io = ImGui::GetIO();
50+
ImGui::StyleColorsDark();
51+
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
52+
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
53+
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
54+
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
55+
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
56+
io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
57+
io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
58+
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
59+
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
60+
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
61+
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
62+
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
63+
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
64+
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
65+
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
66+
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
67+
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
68+
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
69+
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
3070
}
31-
if (!glfwInit())
32-
return nullptr;
33-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
34-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
35-
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
36-
#if __APPLE__
37-
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
38-
#endif
39-
GLFWwindow* window = glfwCreateWindow(1280, 720, name, nullptr, nullptr);
40-
glfwMakeContextCurrent(window);
41-
gl3wInit();
42-
43-
// Setup ImGui binding
44-
ImGui_ImplGlfwGL3_Init(window, true);
4571

4672
// Load Fonts
4773
// (there is a default font, this is only if you want to change it. see extra_fonts/README.txt for more details)
@@ -50,7 +76,13 @@ void* initGUI(const char* name, void(*error_callback)(int, char const*descriptio
5076
static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_FA, 0 };
5177
ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true; icons_config.FontDataOwnedByAtlas = false;
5278
io.Fonts->AddFontFromMemoryTTF((void*)s_iconsFontAwesomeTtf, sizeof(s_iconsFontAwesomeTtf), 12.0f, &icons_config, icons_ranges);
53-
79+
80+
// this initializes the texture
81+
if (io.Fonts->ConfigData.empty())
82+
io.Fonts->AddFontDefault();
83+
io.Fonts->Build();
84+
io.DisplaySize = ImVec2(1280, 720);
85+
5486
ImPlot::CreateContext();
5587
return window;
5688
}
@@ -186,23 +218,31 @@ void getFrameRaw(void *data, void **raw_data, int *size)
186218
*raw_data = local_data_base;
187219
}
188220

189-
bool pollGUIPreRender(void* context)
221+
bool pollGUIPreRender(void* context, float delta)
190222
{
191-
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(context);
223+
if (context) {
224+
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(context);
192225

193-
if (glfwWindowShouldClose(window)) {
194-
return false;
226+
if (glfwWindowShouldClose(window)) {
227+
return false;
228+
}
229+
glfwPollEvents();
230+
ImGui_ImplGlfwGL3_NewFrame();
231+
232+
// Clearing the viewport
233+
int display_w, display_h;
234+
glfwGetFramebufferSize(window, &display_w, &display_h);
235+
glViewport(0, 0, display_w, display_h);
236+
ImVec4 clear_color = ImColor(114, 144, 154);
237+
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
238+
glClear(GL_COLOR_BUFFER_BIT);
239+
} else {
240+
// Just initialize new frame
241+
ImGuiIO& io = ImGui::GetIO();
242+
io.DeltaTime = delta;
243+
ImGui::NewFrame();
195244
}
196-
glfwPollEvents();
197-
ImGui_ImplGlfwGL3_NewFrame();
198245

199-
// Rendering
200-
int display_w, display_h;
201-
glfwGetFramebufferSize(window, &display_w, &display_h);
202-
glViewport(0, 0, display_w, display_h);
203-
ImVec4 clear_color = ImColor(114, 144, 154);
204-
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
205-
glClear(GL_COLOR_BUFFER_BIT);
206246

207247
return true;
208248
}
@@ -221,16 +261,18 @@ void *pollGUIRender(std::function<void(void)> guiCallback)
221261

222262
void pollGUIPostRender(void* context, void *draw_data)
223263
{
224-
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(context);
264+
if (context) {
265+
GLFWwindow* window = reinterpret_cast<GLFWwindow*>(context);
225266

226-
ImGui_ImplGlfwGL3_RenderDrawLists((ImDrawData*)draw_data);
227-
glfwSwapBuffers(window);
267+
ImGui_ImplGlfwGL3_RenderDrawLists((ImDrawData*)draw_data);
268+
glfwSwapBuffers(window);
269+
}
228270
}
229-
271+
230272
/// @return true if we do not need to exit, false if we do.
231273
bool pollGUI(void* context, std::function<void(void)> guiCallback)
232274
{
233-
if (!pollGUIPreRender(context)) {
275+
if (!pollGUIPreRender(context, 1.0f/60.0f)) {
234276
return false;
235277
}
236278
void *draw_data = pollGUIRender(guiCallback);

DebugGUI/src/DebugGUI.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void* initGUI(const char* name, decltype(default_error_callback) = nullptr);
2222
bool pollGUI(void* context, std::function<void(void)> guiCallback);
2323
void getFrameJSON(void *data, std::ostream& json_data);
2424
void getFrameRaw(void *data, void **raw_data, int *size);
25-
bool pollGUIPreRender(void* context);
25+
bool pollGUIPreRender(void* context, float delta);
2626
void* pollGUIRender(std::function<void(void)> guiCallback);
2727
void pollGUIPostRender(void* context, void* draw_data);
2828
void disposeGUI();

DebugGUI/src/imgui_impl_glfw_gl3.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -381,19 +381,15 @@ void ImGui_ImplGlfwGL3_NewFrame()
381381
double mouse_x, mouse_y;
382382
glfwGetCursorPos(g_Window, &mouse_x, &mouse_y);
383383
io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.)
384-
}
385-
else
386-
{
387-
io.MousePos = ImVec2(-1,-1);
384+
for (int i = 0; i < 3; i++)
385+
{
386+
io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
387+
g_MousePressed[i] = false;
388+
}
389+
io.MouseWheel = g_MouseWheel;
388390
}
389391

390-
for (int i = 0; i < 3; i++)
391-
{
392-
io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
393-
g_MousePressed[i] = false;
394-
}
395392

396-
io.MouseWheel = g_MouseWheel;
397393
g_MouseWheel = 0.0f;
398394

399395
// Hide OS mouse cursor if ImGui is drawing it

remote/dat.gui.min.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)