Skip to content

Commit

Permalink
Merge pull request #154 from floooh/dcimgui-fixes
Browse files Browse the repository at this point in the history
Fixes for switch from cimgui to dear_bindings.
  • Loading branch information
floooh authored Dec 7, 2024
2 parents 4835374 + 955bdfd commit d78739f
Show file tree
Hide file tree
Showing 24 changed files with 396 additions and 355 deletions.
8 changes: 3 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# project: sokol-samples
#
cmake_minimum_required(VERSION 3.21)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
if (FIPS_IOS)
set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0" CACHE STRING "")
else()
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING "")
endif()
set(CMAKE_CXX_STANDARD 17)
project(sokol-samples)

if (SOKOL_USE_WGPU_DAWN)
Expand All @@ -24,9 +24,7 @@ fips_ide_group("Imports")
if (NOT USE_SOKOL_APP AND NOT SOKOL_USE_WGPU_DAWN AND NOT SOKOL_USE_D3D11 AND NOT SOKOL_USE_METAL)
fips_import_fips_glfw_glfw3()
endif()
fips_import_fips_imgui_imgui()
fips_import_fips_cimgui_cimgui()
fips_import_fips_imgui_dock_imgui_dock()
fips_import_dcimgui_dcimgui()
if (NOT FIPS_UWP)
fips_import_fips_libmodplug_libmodplug()
endif()
Expand Down
47 changes: 24 additions & 23 deletions d3d11/imgui-d3d11.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct {
} vs_params_t;

static void draw_imgui(ImDrawData*);
static ImGuiKey as_imgui_key(int keycode);

int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
(void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
Expand All @@ -44,37 +45,20 @@ int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _
stm_setup();

// input forwarding
d3d11_mouse_pos([] (float x, float y) { ImGui::GetIO().MousePos = ImVec2(x, y); });
d3d11_mouse_btn_down([] (int btn) { ImGui::GetIO().MouseDown[btn] = true; });
d3d11_mouse_btn_up([] (int btn) { ImGui::GetIO().MouseDown[btn] = false; });
d3d11_mouse_wheel([](float v) { ImGui::GetIO().MouseWheel = v; });
d3d11_mouse_pos([] (float x, float y) { ImGui::GetIO().AddMousePosEvent(x, y); });
d3d11_mouse_btn_down([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, true); });
d3d11_mouse_btn_up([] (int btn) { ImGui::GetIO().AddMouseButtonEvent(btn, false); });
d3d11_mouse_wheel([](float v) { ImGui::GetIO().AddMouseWheelEvent(0, v); });
d3d11_char([] (wchar_t c) { ImGui::GetIO().AddInputCharacter(c); });
d3d11_key_down([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = true; });
d3d11_key_up([] (int key) { if (key < 512) ImGui::GetIO().KeysDown[key] = false; });
d3d11_key_down([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), true); });
d3d11_key_up([] (int key) { ImGui::GetIO().AddKeyEvent(as_imgui_key(key), false); });

// setup Dear Imgui
ImGui::CreateContext();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.Fonts->AddFontDefault();
io.KeyMap[ImGuiKey_Tab] = VK_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
io.KeyMap[ImGuiKey_Home] = VK_HOME;
io.KeyMap[ImGuiKey_End] = VK_END;
io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z';

// dynamic vertex- and index-buffers for imgui-generated geometry
sg_buffer_desc vbuf_desc = { };
Expand Down Expand Up @@ -269,3 +253,20 @@ void draw_imgui(ImDrawData* draw_data) {
}
}
}

static ImGuiKey as_imgui_key(int keycode) {
switch (keycode) {
case VK_TAB: return ImGuiKey_Tab;
case VK_LEFT: return ImGuiKey_LeftArrow;
case VK_RIGHT: return ImGuiKey_RightArrow;
case VK_UP: return ImGuiKey_UpArrow;
case VK_DOWN: return ImGuiKey_DownArrow;
case VK_HOME: return ImGuiKey_Home;
case VK_END: return ImGuiKey_End;
case VK_DELETE: return ImGuiKey_Delete;
case VK_BACK: return ImGuiKey_Backspace;
case VK_RETURN: return ImGuiKey_Enter;
case VK_ESCAPE: return ImGuiKey_Escape;
default: return ImGuiKey_None;
}
}
8 changes: 2 additions & 6 deletions fips.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ imports:
git: https://github.com/floooh/sokol-tools-bin
fips-glfw:
git: https://github.com/fips-libs/fips-glfw
fips-cimgui:
git: https://github.com/fips-libs/fips-cimgui.git
fips-imgui:
git: https://github.com/fips-libs/fips-imgui.git
fips-imgui-dock:
git: https://github.com/fips-libs/fips-imgui-dock.git
fips-libmodplug:
git: https://github.com/fips-libs/fips-libmodplug
fips-utils:
git: https://github.com/fips-libs/fips-utils
fips-dawn:
git: https://github.com/fips-libs/fips-dawn
dcimgui:
git: https://github.com/floooh/dcimgui
156 changes: 130 additions & 26 deletions glfw/imgui-glfw.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ typedef struct {
} vs_params_t;

static void draw_imgui(ImDrawData*);
static ImGuiKey glfw_key_to_imgui_key(int glfw_key);

int main() {

Expand All @@ -42,22 +43,19 @@ int main() {

// GLFW to ImGui input forwarding
glfwSetMouseButtonCallback(w, [](GLFWwindow*, int btn, int action, int /*mods*/) {
if ((btn >= 0) && (btn < 3)) {
ImGui::GetIO().MouseDown[btn] = (action == GLFW_PRESS);
if ((btn >= 0) && (btn < ImGuiMouseButton_COUNT)) {
ImGui::GetIO().AddMouseButtonEvent(btn, action == GLFW_PRESS);
}
});
glfwSetCursorPosCallback(w, [](GLFWwindow*, double pos_x, double pos_y) {
ImGui::GetIO().MousePos.x = float(pos_x);
ImGui::GetIO().MousePos.y = float(pos_y);
ImGui::GetIO().AddMousePosEvent((float)pos_x, (float)pos_y);
});
glfwSetScrollCallback(w, [](GLFWwindow*, double /*pos_x*/, double pos_y){
ImGui::GetIO().MouseWheel = float(pos_y);
glfwSetScrollCallback(w, [](GLFWwindow*, double wheel_x, double wheel_y){
ImGui::GetIO().AddMouseWheelEvent((float)wheel_x, (float)wheel_y);
});
glfwSetKeyCallback(w, [](GLFWwindow*, int key, int /*scancode*/, int action, int mods){
ImGuiIO& io = ImGui::GetIO();
if ((key >= 0) && (key < 512)) {
io.KeysDown[key] = (action==GLFW_PRESS)||(action==GLFW_REPEAT);
}
io.AddKeyEvent(glfw_key_to_imgui_key(key), (action == GLFW_PRESS));
io.KeyCtrl = (0 != (mods & GLFW_MOD_CONTROL));
io.KeyAlt = (0 != (mods & GLFW_MOD_ALT));
io.KeyShift = (0 != (mods & GLFW_MOD_SHIFT));
Expand All @@ -80,23 +78,6 @@ int main() {
ImGuiIO& io = ImGui::GetIO();
io.IniFilename = nullptr;
io.Fonts->AddFontDefault();
io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;

// dynamic vertex- and index-buffers for imgui-generated geometry
sg_buffer_desc vbuf_desc = { };
Expand Down Expand Up @@ -287,3 +268,126 @@ void draw_imgui(ImDrawData* draw_data) {
}
}
}

static ImGuiKey glfw_key_to_imgui_key(int glfw_key) {
switch (glfw_key) {
case GLFW_KEY_TAB: return ImGuiKey_Tab;
case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow;
case GLFW_KEY_UP: return ImGuiKey_UpArrow;
case GLFW_KEY_DOWN: return ImGuiKey_DownArrow;
case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp;
case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown;
case GLFW_KEY_HOME: return ImGuiKey_Home;
case GLFW_KEY_END: return ImGuiKey_End;
case GLFW_KEY_INSERT: return ImGuiKey_Insert;
case GLFW_KEY_DELETE: return ImGuiKey_Delete;
case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace;
case GLFW_KEY_SPACE: return ImGuiKey_Space;
case GLFW_KEY_ENTER: return ImGuiKey_Enter;
case GLFW_KEY_ESCAPE: return ImGuiKey_Escape;
case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe;
case GLFW_KEY_COMMA: return ImGuiKey_Comma;
case GLFW_KEY_MINUS: return ImGuiKey_Minus;
case GLFW_KEY_PERIOD: return ImGuiKey_Period;
case GLFW_KEY_SLASH: return ImGuiKey_Slash;
case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon;
case GLFW_KEY_EQUAL: return ImGuiKey_Equal;
case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket;
case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash;
case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket;
case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent;
case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock;
case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock;
case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock;
case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen;
case GLFW_KEY_PAUSE: return ImGuiKey_Pause;
case GLFW_KEY_KP_0: return ImGuiKey_Keypad0;
case GLFW_KEY_KP_1: return ImGuiKey_Keypad1;
case GLFW_KEY_KP_2: return ImGuiKey_Keypad2;
case GLFW_KEY_KP_3: return ImGuiKey_Keypad3;
case GLFW_KEY_KP_4: return ImGuiKey_Keypad4;
case GLFW_KEY_KP_5: return ImGuiKey_Keypad5;
case GLFW_KEY_KP_6: return ImGuiKey_Keypad6;
case GLFW_KEY_KP_7: return ImGuiKey_Keypad7;
case GLFW_KEY_KP_8: return ImGuiKey_Keypad8;
case GLFW_KEY_KP_9: return ImGuiKey_Keypad9;
case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal;
case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide;
case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract;
case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd;
case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter;
case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual;
case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift;
case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl;
case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt;
case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper;
case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift;
case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl;
case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt;
case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper;
case GLFW_KEY_MENU: return ImGuiKey_Menu;
case GLFW_KEY_0: return ImGuiKey_0;
case GLFW_KEY_1: return ImGuiKey_1;
case GLFW_KEY_2: return ImGuiKey_2;
case GLFW_KEY_3: return ImGuiKey_3;
case GLFW_KEY_4: return ImGuiKey_4;
case GLFW_KEY_5: return ImGuiKey_5;
case GLFW_KEY_6: return ImGuiKey_6;
case GLFW_KEY_7: return ImGuiKey_7;
case GLFW_KEY_8: return ImGuiKey_8;
case GLFW_KEY_9: return ImGuiKey_9;
case GLFW_KEY_A: return ImGuiKey_A;
case GLFW_KEY_B: return ImGuiKey_B;
case GLFW_KEY_C: return ImGuiKey_C;
case GLFW_KEY_D: return ImGuiKey_D;
case GLFW_KEY_E: return ImGuiKey_E;
case GLFW_KEY_F: return ImGuiKey_F;
case GLFW_KEY_G: return ImGuiKey_G;
case GLFW_KEY_H: return ImGuiKey_H;
case GLFW_KEY_I: return ImGuiKey_I;
case GLFW_KEY_J: return ImGuiKey_J;
case GLFW_KEY_K: return ImGuiKey_K;
case GLFW_KEY_L: return ImGuiKey_L;
case GLFW_KEY_M: return ImGuiKey_M;
case GLFW_KEY_N: return ImGuiKey_N;
case GLFW_KEY_O: return ImGuiKey_O;
case GLFW_KEY_P: return ImGuiKey_P;
case GLFW_KEY_Q: return ImGuiKey_Q;
case GLFW_KEY_R: return ImGuiKey_R;
case GLFW_KEY_S: return ImGuiKey_S;
case GLFW_KEY_T: return ImGuiKey_T;
case GLFW_KEY_U: return ImGuiKey_U;
case GLFW_KEY_V: return ImGuiKey_V;
case GLFW_KEY_W: return ImGuiKey_W;
case GLFW_KEY_X: return ImGuiKey_X;
case GLFW_KEY_Y: return ImGuiKey_Y;
case GLFW_KEY_Z: return ImGuiKey_Z;
case GLFW_KEY_F1: return ImGuiKey_F1;
case GLFW_KEY_F2: return ImGuiKey_F2;
case GLFW_KEY_F3: return ImGuiKey_F3;
case GLFW_KEY_F4: return ImGuiKey_F4;
case GLFW_KEY_F5: return ImGuiKey_F5;
case GLFW_KEY_F6: return ImGuiKey_F6;
case GLFW_KEY_F7: return ImGuiKey_F7;
case GLFW_KEY_F8: return ImGuiKey_F8;
case GLFW_KEY_F9: return ImGuiKey_F9;
case GLFW_KEY_F10: return ImGuiKey_F10;
case GLFW_KEY_F11: return ImGuiKey_F11;
case GLFW_KEY_F12: return ImGuiKey_F12;
case GLFW_KEY_F13: return ImGuiKey_F13;
case GLFW_KEY_F14: return ImGuiKey_F14;
case GLFW_KEY_F15: return ImGuiKey_F15;
case GLFW_KEY_F16: return ImGuiKey_F16;
case GLFW_KEY_F17: return ImGuiKey_F17;
case GLFW_KEY_F18: return ImGuiKey_F18;
case GLFW_KEY_F19: return ImGuiKey_F19;
case GLFW_KEY_F20: return ImGuiKey_F20;
case GLFW_KEY_F21: return ImGuiKey_F21;
case GLFW_KEY_F22: return ImGuiKey_F22;
case GLFW_KEY_F23: return ImGuiKey_F23;
case GLFW_KEY_F24: return ImGuiKey_F24;
default: return ImGuiKey_None;
}
}
Loading

0 comments on commit d78739f

Please sign in to comment.