Last active
February 5, 2023 19:47
-
-
Save InfiniteCoder01/c6fdd003fe3c7851e8c0739aa518c145 to your computer and use it in GitHub Desktop.
ImGui backend for OOGL (https://github.com/Overv/OOGL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <imgui.h> | |
#include <GL/OOGL.hpp> | |
#include <backends/imgui_impl_opengl3.h> | |
#include <unordered_map> | |
// clang-format off | |
const std::unordered_map<GL::Key::key_t, ImGuiKey> keymap = { | |
{GL::Key::F1, ImGuiKey_F1}, {GL::Key::F2, ImGuiKey_F2}, {GL::Key::F3, ImGuiKey_F3}, {GL::Key::F4, ImGuiKey_F4}, {GL::Key::F5, ImGuiKey_F5}, {GL::Key::F6, ImGuiKey_F6}, | |
{GL::Key::F7, ImGuiKey_F7}, {GL::Key::F8, ImGuiKey_F8}, {GL::Key::F9, ImGuiKey_F9}, {GL::Key::F10, ImGuiKey_F10}, {GL::Key::F11, ImGuiKey_F11}, {GL::Key::F12, ImGuiKey_F12}, | |
{GL::Key::A, ImGuiKey_A}, {GL::Key::B, ImGuiKey_B}, {GL::Key::C, ImGuiKey_C}, {GL::Key::D, ImGuiKey_D}, {GL::Key::E, ImGuiKey_E}, {GL::Key::F, ImGuiKey_F}, {GL::Key::G, ImGuiKey_G}, | |
{GL::Key::H, ImGuiKey_H}, {GL::Key::I, ImGuiKey_I}, {GL::Key::J, ImGuiKey_J}, {GL::Key::K, ImGuiKey_K}, {GL::Key::L, ImGuiKey_L}, {GL::Key::M, ImGuiKey_M}, {GL::Key::N, ImGuiKey_N}, | |
{GL::Key::O, ImGuiKey_O}, {GL::Key::P, ImGuiKey_P}, {GL::Key::Q, ImGuiKey_Q}, {GL::Key::R, ImGuiKey_R}, {GL::Key::S, ImGuiKey_S}, {GL::Key::T, ImGuiKey_T}, {GL::Key::U, ImGuiKey_U}, | |
{GL::Key::V, ImGuiKey_V}, {GL::Key::W, ImGuiKey_W}, {GL::Key::X, ImGuiKey_X}, {GL::Key::Y, ImGuiKey_Y}, {GL::Key::Z, ImGuiKey_Z}, | |
{GL::Key::Num0, ImGuiKey_0}, {GL::Key::Num1, ImGuiKey_1}, {GL::Key::Num2, ImGuiKey_2}, {GL::Key::Num3, ImGuiKey_3}, {GL::Key::Num4, ImGuiKey_4}, | |
{GL::Key::Num5, ImGuiKey_5}, {GL::Key::Num6, ImGuiKey_6}, {GL::Key::Num7, ImGuiKey_7}, {GL::Key::Num8, ImGuiKey_8}, {GL::Key::Num9, ImGuiKey_9}, | |
{GL::Key::LeftBracket, ImGuiKey_LeftBracket}, {GL::Key::RightBracket, ImGuiKey_RightBracket}, {GL::Key::Semicolon, ImGuiKey_Semicolon}, {GL::Key::Comma, ImGuiKey_Comma}, | |
{GL::Key::Period, ImGuiKey_Period}, {GL::Key::Quote, ImGuiKey_Apostrophe}, {GL::Key::Slash, ImGuiKey_Slash}, {GL::Key::Backslash, ImGuiKey_Backslash}, | |
{GL::Key::Tilde, ImGuiKey_GraveAccent}, {GL::Key::Equals, ImGuiKey_Equal}, {GL::Key::Hyphen, ImGuiKey_Minus}, {GL::Key::Escape, ImGuiKey_Escape}, | |
{GL::Key::Control, ImGuiKey_ModCtrl}, {GL::Key::Shift, ImGuiKey_ModShift}, {GL::Key::Alt, ImGuiKey_ModAlt}, {GL::Key::Space, ImGuiKey_Space}, | |
{GL::Key::Enter, ImGuiKey_Enter}, {GL::Key::Backspace, ImGuiKey_Backspace}, {GL::Key::Tab, ImGuiKey_Tab}, {GL::Key::PageUp, ImGuiKey_PageUp}, | |
{GL::Key::PageDown, ImGuiKey_PageDown}, {GL::Key::End, ImGuiKey_End}, {GL::Key::Home, ImGuiKey_Home}, {GL::Key::Insert, ImGuiKey_Insert}, | |
{GL::Key::Delete, ImGuiKey_Delete}, {GL::Key::Pause, ImGuiKey_Pause}, | |
{GL::Key::Left, ImGuiKey_LeftArrow}, {GL::Key::Right, ImGuiKey_RightArrow}, {GL::Key::Up, ImGuiKey_UpArrow}, {GL::Key::Down, ImGuiKey_DownArrow}, | |
{GL::Key::Numpad0, ImGuiKey_Keypad0}, {GL::Key::Numpad1, ImGuiKey_Keypad1}, {GL::Key::Numpad2, ImGuiKey_Keypad2}, {GL::Key::Numpad3, ImGuiKey_Keypad3}, {GL::Key::Numpad4, ImGuiKey_Keypad4}, | |
{GL::Key::Numpad5, ImGuiKey_Keypad5}, {GL::Key::Numpad6, ImGuiKey_Keypad6}, {GL::Key::Numpad7, ImGuiKey_Keypad7}, {GL::Key::Numpad8, ImGuiKey_Keypad8}, {GL::Key::Numpad9, ImGuiKey_Keypad9}, | |
{GL::Key::Divide, ImGuiKey_KeypadDivide}, {GL::Key::Multiply, ImGuiKey_KeypadMultiply}, {GL::Key::Subtract, ImGuiKey_KeypadSubtract}, {GL::Key::Add, ImGuiKey_KeypadAdd}, | |
{GL::Key::Unknown, ImGuiKey_None}, | |
}; | |
// clang-format on | |
void ImGuiOOGL_Init(GL::Window& window) { | |
ImGui::CreateContext(); | |
ImGuiIO& io = ImGui::GetIO(); | |
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; | |
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; | |
ImGui_ImplOpenGL3_Init("#version 330 core"); | |
io.BackendPlatformUserData = (void*)&window.GetContext(); | |
io.BackendPlatformName = "imgui_impl_mova"; | |
ImGuiViewport* main_viewport = ImGui::GetMainViewport(); | |
main_viewport->PlatformHandle = (void*)&window; | |
} | |
void ImGuiOOGL_NewFrame() { | |
static float lastTime = 0; | |
ImGuiIO& io = ImGui::GetIO(); | |
float time = ((GL::Context*)io.BackendPlatformUserData)->Time(); | |
GL::Window* window = (GL::Window*)ImGui::GetMainViewport()->PlatformHandle; | |
io.DisplaySize = ImVec2(window->GetWidth(), window->GetHeight()); | |
io.DeltaTime = std::max(time - lastTime, 0.001f); | |
lastTime = time; | |
ImGui_ImplOpenGL3_NewFrame(); | |
ImGui::NewFrame(); | |
} | |
void ImGuiOOGL_AddEvent(GL::Event& event) { | |
ImGuiIO& io = ImGui::GetIO(); | |
if (event.Type == GL::Event::MouseDown || event.Type == GL::Event::MouseUp) { | |
if (event.Mouse.Button == GL::MouseButton::Left) { | |
io.AddMouseButtonEvent(ImGuiMouseButton_Left, event.Type == GL::Event::MouseDown); | |
} else if (event.Mouse.Button == GL::MouseButton::Middle) { | |
io.AddMouseButtonEvent(ImGuiMouseButton_Middle, event.Type == GL::Event::MouseDown); | |
} else if (event.Mouse.Button == GL::MouseButton::Right) { | |
io.AddMouseButtonEvent(ImGuiMouseButton_Right, event.Type == GL::Event::MouseDown); | |
} | |
} else if (event.Type == GL::Event::MouseMove) { | |
io.AddMousePosEvent(event.Mouse.X, event.Mouse.Y); | |
} else if (event.Type == GL::Event::MouseWheel) { | |
io.AddMouseWheelEvent(0, event.Mouse.Delta); | |
} else if (event.Type == GL::Event::KeyDown || event.Type == GL::Event::KeyUp) { | |
io.AddKeyEvent(keymap.at(event.Key.Code), event.Type == GL::Event::KeyDown); | |
io.AddKeyEvent(ImGuiKey_ModCtrl, event.Key.Control); | |
io.AddKeyEvent(ImGuiKey_ModShift, event.Key.Shift); | |
io.AddKeyEvent(ImGuiKey_ModAlt, event.Key.Alt); | |
// io.AddInputCharacter(character); | |
} | |
} | |
void ImGuiOOGL_Render() { | |
ImGui::Render(); | |
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); | |
} | |
void ImGuiOOGL_Shutdown() { | |
ImGui_ImplOpenGL3_Shutdown(); | |
ImGui::DestroyContext(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <imgui.h> | |
#include <GL/OOGL.hpp> | |
void ImGuiOOGL_Init(GL::Window& window); | |
void ImGuiOOGL_NewFrame(); | |
void ImGuiOOGL_AddEvent(GL::Event& event); | |
void ImGuiOOGL_Render(); | |
void ImGuiOOGL_Shutdown(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment