/*
This file is part of Magnum.
Original authors — credit is appreciated but not required:
2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019,
2020, 2021, 2022, 2023, 2024 — Vladimír Vondruš <mosra@centrum.cz>
2018 — ShaddyAQN <ShaddyAQN@gmail.com>
2018 — Jonathan Hale <squareys@googlemail.com>
2018 — Tomáš Skřivan <skrivantomas@seznam.cz>
2018 — Natesh Narain <nnaraindev@gmail.com>
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute
this software, either in source code form or as a compiled binary, for any
purpose, commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of
this software dedicate any and all copyright interest in the software to
the public domain. We make this dedication for the benefit of the public
at large and to the detriment of our heirs and successors. We intend this
dedication to be an overt act of relinquishment in perpetuity of all
present and future rights to this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Time.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/Renderer.h>
#include <Magnum/ImGuiIntegration/Context.hpp>
#ifdef CORRADE_TARGET_ANDROID
#include <Magnum/Platform/AndroidApplication.h>
#elif defined(CORRADE_TARGET_EMSCRIPTEN)
#include <Magnum/Platform/EmscriptenApplication.h>
#else
#include <Magnum/Platform/Sdl2Application.h>
#endif
namespace Magnum { namespace Examples {
using namespace Math::Literals;
class ImGuiExample: public Platform::Application {
public:
explicit ImGuiExample(const Arguments& arguments);
void drawEvent() override;
void viewportEvent(ViewportEvent& event) override;
void keyPressEvent(KeyEvent& event) override;
void keyReleaseEvent(KeyEvent& event) override;
void pointerPressEvent(PointerEvent& event) override;
void pointerReleaseEvent(PointerEvent& event) override;
void pointerMoveEvent(PointerMoveEvent& event) override;
void scrollEvent(ScrollEvent& event) override;
void textInputEvent(TextInputEvent& event) override;
private:
ImGuiIntegration::Context _imgui{NoCreate};
bool _showDemoWindow = true;
bool _showAnotherWindow = false;
Color4 _clearColor = 0x72909aff_rgbaf;
Float _floatValue = 0.0f;
};
ImGuiExample::ImGuiExample(const Arguments& arguments): Platform::Application{arguments,
Configuration{}.setTitle("Magnum ImGui Example")
.setWindowFlags(Configuration::WindowFlag::Resizable)}
{
_imgui = ImGuiIntegration::Context(Vector2{windowSize()}/dpiScaling(),
windowSize(), framebufferSize());
/* Set up proper blending to be used by ImGui. There's a great chance
you'll need this exact behavior for the rest of your scene. If not, set
this only for the drawFrame() call. */
GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add,
GL::Renderer::BlendEquation::Add);
GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha,
GL::Renderer::BlendFunction::OneMinusSourceAlpha);
#if !defined(MAGNUM_TARGET_WEBGL) && !defined(CORRADE_TARGET_ANDROID)
/* Have some sane speed, please */
setMinimalLoopPeriod(16.0_msec);
#endif
}
void ImGuiExample::drawEvent() {
GL::defaultFramebuffer.clear(GL::FramebufferClear::Color);
_imgui.newFrame();
/* Enable text input, if needed */
if(ImGui::GetIO().WantTextInput && !isTextInputActive())
startTextInput();
else if(!ImGui::GetIO().WantTextInput && isTextInputActive())
stopTextInput();
/* 1. Show a simple window.
Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appear in
a window called "Debug" automatically */
{
ImGui::Text("Hello, world!");
ImGui::SliderFloat("Float", &_floatValue, 0.0f, 1.0f);
if(ImGui::ColorEdit3("Clear Color", _clearColor.data()))
GL::Renderer::setClearColor(_clearColor);
if(ImGui::Button("Test Window"))
_showDemoWindow ^= true;
if(ImGui::Button("Another Window"))
_showAnotherWindow ^= true;
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0/Double(ImGui::GetIO().Framerate), Double(ImGui::GetIO().Framerate));
}
/* 2. Show another simple window, now using an explicit Begin/End pair */
if(_showAnotherWindow) {
ImGui::SetNextWindowSize(ImVec2(500, 100), ImGuiCond_FirstUseEver);
ImGui::Begin("Another Window", &_showAnotherWindow);
ImGui::Text("Hello");
ImGui::End();
}
/* 3. Show the ImGui demo window. Most of the sample code is in
ImGui::ShowDemoWindow() */
if(_showDemoWindow) {
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver);
ImGui::ShowDemoWindow();
}
/* Update application cursor */
_imgui.updateApplicationCursor(*this);
/* Set appropriate states. If you only draw ImGui, it is sufficient to
just enable blending and scissor test in the constructor. */
GL::Renderer::enable(GL::Renderer::Feature::Blending);
GL::Renderer::enable(GL::Renderer::Feature::ScissorTest);
GL::Renderer::disable(GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable(GL::Renderer::Feature::DepthTest);
_imgui.drawFrame();
/* Reset state. Only needed if you want to draw something else with
different state after. */
GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
GL::Renderer::enable(GL::Renderer::Feature::FaceCulling);
GL::Renderer::disable(GL::Renderer::Feature::ScissorTest);
GL::Renderer::disable(GL::Renderer::Feature::Blending);
swapBuffers();
redraw();
}
void ImGuiExample::viewportEvent(ViewportEvent& event) {
GL::defaultFramebuffer.setViewport({{}, event.framebufferSize()});
_imgui.relayout(Vector2{event.windowSize()}/event.dpiScaling(),
event.windowSize(), event.framebufferSize());
}
void ImGuiExample::keyPressEvent(KeyEvent& event) {
if(_imgui.handleKeyPressEvent(event)) return;
}
void ImGuiExample::keyReleaseEvent(KeyEvent& event) {
if(_imgui.handleKeyReleaseEvent(event)) return;
}
void ImGuiExample::pointerPressEvent(PointerEvent& event) {
if(_imgui.handlePointerPressEvent(event)) return;
}
void ImGuiExample::pointerReleaseEvent(PointerEvent& event) {
if(_imgui.handlePointerReleaseEvent(event)) return;
}
void ImGuiExample::pointerMoveEvent(PointerMoveEvent& event) {
if(_imgui.handlePointerMoveEvent(event)) return;
}
void ImGuiExample::scrollEvent(ScrollEvent& event) {
if(_imgui.handleScrollEvent(event)) {
/* Prevent scrolling the page */
event.setAccepted();
return;
}
}
void ImGuiExample::textInputEvent(TextInputEvent& event) {
if(_imgui.handleTextInputEvent(event)) return;
}
}}
MAGNUM_APPLICATION_MAIN(Magnum::Examples::ImGuiExample)