summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-23 13:08:04 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-23 13:08:04 +0200
commitbbc20636c54b1e408156e397d688e4e591dd601b (patch)
tree947a1982baa0cdffb39339610ac274a01c0447b9
parent55f284c6e3d2da6774f758e047945b102b9f5b23 (diff)
move events to separate file
-rw-r--r--main/app.cpp116
-rw-r--r--main/app.hpp1
-rw-r--r--main/events.cpp121
-rw-r--r--main/imgui.cpp12
4 files changed, 132 insertions, 118 deletions
diff --git a/main/app.cpp b/main/app.cpp
index 1cbb5b2a..523bb49e 100644
--- a/main/app.cpp
+++ b/main/app.cpp
@@ -6,10 +6,6 @@
#include <Corrade/Utility/DebugStl.h>
#include <Magnum/GL/DefaultFramebuffer.h>
#include <Magnum/GL/TextureFormat.h>
-#include <Magnum/ImGuiIntegration/Context.h>
-#include <Magnum/ImGuiIntegration/Context.hpp>
-#include <SDL_events.h>
-#include <SDL_video.h>
#ifdef FM_MSAA
#include <Magnum/GL/RenderbufferFormat.h>
@@ -75,8 +71,7 @@ app::app(const Arguments& arguments, app_settings opts):
void app::recalc_viewport(Vector2i size)
{
_shader.set_scale(Vector2(size));
- _imgui.relayout(Vector2{size}, size, size);
-
+ init_imgui(size);
_cursor_pixel = std::nullopt;
recalc_cursor_tile();
@@ -90,115 +85,6 @@ void app::recalc_viewport(Vector2i size)
#endif
}
-void app::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
-{
- fm_assert(event.framebufferSize() == event.windowSize());
- recalc_viewport(event.windowSize());
-}
-
-void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event)
-{
- if (_imgui.handleMousePressEvent(event))
- return event.setAccepted();
- else if (_cursor_tile)
- {
- const auto& tile = *_cursor_tile;
- do_mouse_click(tile, (int)event.button());
- }
-}
-
-void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event)
-{
- if (_imgui.handleMouseReleaseEvent(event))
- return event.setAccepted();
- do_mouse_release((int)event.button());
-}
-
-void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event)
-{
- _cursor_in_imgui = _imgui.handleMouseMoveEvent(event);
- if (_cursor_in_imgui)
- _cursor_pixel = std::nullopt;
- else
- _cursor_pixel = event.position();
- recalc_cursor_tile();
- if (_cursor_tile)
- do_mouse_move(*_cursor_tile);
-}
-
-void app::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event)
-{
- if (_imgui.handleMouseScrollEvent(event))
- return event.setAccepted();
-}
-
-void app::textInputEvent(Platform::Sdl2Application::TextInputEvent& event)
-{
- if (_imgui.handleTextInputEvent(event))
- {
- keys = {};
- event.setAccepted();
- }
-}
-
-void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event)
-{
- if (_imgui.handleKeyPressEvent(event))
- {
- keys = {};
- return event.setAccepted();
- }
- do_key(event.key(), event.modifiers(), true, event.isRepeated());
-}
-
-void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event)
-{
- if (_imgui.handleKeyReleaseEvent(event))
- {
- keys = {};
- return event.setAccepted();
- }
- do_key(event.key(), event.modifiers(), false, false);
-}
-
-void app::anyEvent(SDL_Event& event)
-{
- if (event.type == SDL_WINDOWEVENT)
- switch (event.window.event)
- {
- case SDL_WINDOWEVENT_FOCUS_LOST:
- return event_focus_out();
- case SDL_WINDOWEVENT_FOCUS_GAINED:
- return event_focus_in();
- case SDL_WINDOWEVENT_LEAVE:
- return event_mouse_leave();
- case SDL_WINDOWEVENT_ENTER:
- return event_mouse_enter();
- default:
- std::fputs("", stdout); break; // put breakpoint here
- }
-}
-
-void app::event_focus_out()
-{
- _cursor_pixel = std::nullopt;
- recalc_cursor_tile();
-}
-
-void app::event_focus_in()
-{
-}
-
-void app::event_mouse_leave()
-{
- _cursor_pixel = std::nullopt;
- recalc_cursor_tile();
-}
-
-void app::event_mouse_enter()
-{
-}
-
} // namespace floormat
int main(int argc, char** argv)
diff --git a/main/app.hpp b/main/app.hpp
index 27508bdb..bf0330ce 100644
--- a/main/app.hpp
+++ b/main/app.hpp
@@ -52,6 +52,7 @@ private:
void reset_camera_offset();
void recalc_cursor_tile();
void recalc_viewport(Vector2i size);
+ void init_imgui(Vector2i size);
[[maybe_unused]] void viewportEvent(ViewportEvent& event) override;
[[maybe_unused]] void mousePressEvent(MouseEvent& event) override;
diff --git a/main/events.cpp b/main/events.cpp
new file mode 100644
index 00000000..83362bfe
--- /dev/null
+++ b/main/events.cpp
@@ -0,0 +1,121 @@
+#pragma once
+#include "app.hpp"
+#include <Magnum/ImGuiIntegration/Context.hpp>
+
+#include <cstdio>
+#include <SDL_events.h>
+#include <SDL_video.h>
+
+namespace floormat {
+
+void app::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
+{
+ fm_assert(event.framebufferSize() == event.windowSize());
+ recalc_viewport(event.windowSize());
+}
+
+void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event)
+{
+ if (_imgui.handleMousePressEvent(event))
+ return event.setAccepted();
+ else if (_cursor_tile)
+ {
+ const auto& tile = *_cursor_tile;
+ do_mouse_click(tile, (int)event.button());
+ }
+}
+
+void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event)
+{
+ if (_imgui.handleMouseReleaseEvent(event))
+ return event.setAccepted();
+ do_mouse_release((int)event.button());
+}
+
+void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event)
+{
+ _cursor_in_imgui = _imgui.handleMouseMoveEvent(event);
+ if (_cursor_in_imgui)
+ _cursor_pixel = std::nullopt;
+ else
+ _cursor_pixel = event.position();
+ recalc_cursor_tile();
+ if (_cursor_tile)
+ do_mouse_move(*_cursor_tile);
+}
+
+void app::mouseScrollEvent(Platform::Sdl2Application::MouseScrollEvent& event)
+{
+ if (_imgui.handleMouseScrollEvent(event))
+ return event.setAccepted();
+}
+
+void app::textInputEvent(Platform::Sdl2Application::TextInputEvent& event)
+{
+ if (_imgui.handleTextInputEvent(event))
+ {
+ keys = {};
+ event.setAccepted();
+ }
+}
+
+void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event)
+{
+ if (_imgui.handleKeyPressEvent(event))
+ {
+ keys = {};
+ return event.setAccepted();
+ }
+ do_key(event.key(), event.modifiers(), true, event.isRepeated());
+}
+
+void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event)
+{
+ if (_imgui.handleKeyReleaseEvent(event))
+ {
+ keys = {};
+ return event.setAccepted();
+ }
+ do_key(event.key(), event.modifiers(), false, false);
+}
+
+void app::anyEvent(SDL_Event& event)
+{
+ if (event.type == SDL_WINDOWEVENT)
+ switch (event.window.event)
+ {
+ case SDL_WINDOWEVENT_FOCUS_LOST:
+ return event_focus_out();
+ case SDL_WINDOWEVENT_FOCUS_GAINED:
+ return event_focus_in();
+ case SDL_WINDOWEVENT_LEAVE:
+ return event_mouse_leave();
+ case SDL_WINDOWEVENT_ENTER:
+ return event_mouse_enter();
+ default:
+ std::fputs("", stdout); break; // put breakpoint here
+ }
+}
+
+void app::event_focus_out()
+{
+ _cursor_pixel = std::nullopt;
+ recalc_cursor_tile();
+}
+
+void app::event_focus_in()
+{
+}
+
+void app::event_mouse_leave()
+{
+ _cursor_pixel = std::nullopt;
+ recalc_cursor_tile();
+}
+
+void app::event_mouse_enter()
+{
+}
+
+
+} // namespace floormat
diff --git a/main/imgui.cpp b/main/imgui.cpp
index 85661760..b0777d5d 100644
--- a/main/imgui.cpp
+++ b/main/imgui.cpp
@@ -1,14 +1,20 @@
#include "app.hpp"
#include <Magnum/GL/Renderer.h>
#include "imgui-raii.hpp"
-#ifndef __CLION_IDE__zz
-#include <Magnum/ImGuiIntegration/Integration.h>
-#endif
+#include <Magnum/ImGuiIntegration/Context.h>
namespace floormat {
using namespace floormat::imgui;
+void app::init_imgui(Vector2i size)
+{
+ if (!_imgui.context())
+ _imgui = ImGuiIntegration::Context(Vector2{size}, size, size);
+ else
+ _imgui.relayout(Vector2{size}, size, size);
+}
+
void app::render_menu()
{
GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add);