-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ff2d13
commit 194adc8
Showing
3 changed files
with
141 additions
and
10 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/***************************************************************************** | ||
* VCLib * | ||
* Visual Computing Library * | ||
* * | ||
* Copyright(C) 2021-2024 * | ||
* Visual Computing Lab * | ||
* ISTI - Italian National Research Council * | ||
* * | ||
* All rights reserved. * | ||
* * | ||
* This program is free software; you can redistribute it and/or modify * | ||
* it under the terms of the Mozilla Public License Version 2.0 as published * | ||
* by the Mozilla Foundation; either version 2 of the License, or * | ||
* (at your option) any later version. * | ||
* * | ||
* This program is distributed in the hope that it will be useful, * | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
* Mozilla Public License Version 2.0 * | ||
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. * | ||
****************************************************************************/ | ||
|
||
#ifndef IMGUI_DRAWER_H | ||
#define IMGUI_DRAWER_H | ||
|
||
#include <vclib/render/drawers/event_drawer.h> | ||
#include <vclib/render/window_managers.h> | ||
|
||
// Include the render backand imgui implementation | ||
#ifdef VCLIB_RENDER_BACKEND_OPENGL2 | ||
#include <imgui_impl_opengl2.h> | ||
#elif defined(VCLIB_RENDER_BACKEND_BGFX) | ||
#include <vclib/bgfx_imgui/imgui_impl_bgfx.h> | ||
#endif | ||
|
||
// Include the event backend imgui implementations | ||
#ifdef VCLIB_WITH_GLFW | ||
#include <imgui_impl_glfw.h> | ||
#endif | ||
|
||
namespace vcl::imgui { | ||
|
||
template<typename DerivedRenderer> | ||
class ImguiDrawer : public EventDrawer<DerivedRenderer> | ||
{ | ||
protected: | ||
using DRT = DerivedRenderer; | ||
|
||
public: | ||
ImguiDrawer() | ||
{ | ||
static_assert( | ||
DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW, | ||
"ImGuiDrawer supports only GLFW window manager."); | ||
} | ||
|
||
ImguiDrawer(uint, uint) : ImguiDrawer() {} | ||
|
||
virtual void onInit(uint viewId) | ||
{ | ||
// setup ImGui context | ||
IMGUI_CHECKVERSION(); | ||
ImGui::CreateContext(); | ||
ImGuiIO& io = ImGui::GetIO(); | ||
// Enable Keyboard Controls | ||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; | ||
// Enable Gamepad Controls | ||
// io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; | ||
// setup ImGui style | ||
ImGui::StyleColorsDark(); | ||
|
||
if constexpr (DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) { | ||
GLFWwindow* mWindow = | ||
reinterpret_cast<GLFWwindow*>(DRT::DRW::windowPtr(derived())); | ||
// setup platform/renderer backends (GLFW and ImGui) | ||
#ifdef VCLIB_RENDER_BACKEND_OPENGL2 | ||
ImGui_ImplGlfw_InitForOpenGL(mWindow, true); | ||
ImGui_ImplOpenGL2_Init(); | ||
#elif defined(VCLIB_RENDER_BACKEND_BGFX) | ||
ImGui_ImplGlfw_InitForOther(mWindow, true); | ||
ImGui_ImplBgfx_Init(); | ||
#endif | ||
} | ||
} | ||
|
||
virtual void onDraw(uint) | ||
{ | ||
// imgui frame | ||
#ifdef VCLIB_RENDER_BACKEND_OPENGL2 | ||
ImGui_ImplOpenGL2_NewFrame(); | ||
#elif defined(VCLIB_RENDER_BACKEND_BGFX) | ||
ImGui_ImplBgfx_NewFrame(); | ||
#endif | ||
if constexpr (DRT::WINDOW_MANAGER_ID == WindowManagerId::GLFW_WINDOW) { | ||
ImGui_ImplGlfw_NewFrame(); | ||
} | ||
ImGui::NewFrame(); | ||
} | ||
|
||
virtual void onPostDraw() | ||
{ | ||
// imgui rendering | ||
ImGui::Render(); | ||
#ifdef VCLIB_RENDER_BACKEND_OPENGL2 | ||
ImGui_ImplOpenGL2_RenderDrawData(ImGui::GetDrawData()); | ||
#elif defined(VCLIB_RENDER_BACKEND_BGFX) | ||
ImGui_ImplBgfx_RenderDrawData(ImGui::GetDrawData()); | ||
#endif | ||
} | ||
|
||
private: | ||
auto* derived() { return static_cast<DRT*>(this); } | ||
|
||
const auto* derived() const { return static_cast<const DRT*>(this); } | ||
}; | ||
|
||
} // namespace vcl::imgui | ||
|
||
#endif // IMGUI_DRAWER_H |