summaryrefslogtreecommitdiffhomepage
path: root/main
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-13 12:13:56 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-13 12:13:56 +0200
commit67897701316d83495aed4baa94e96c9f84b818f8 (patch)
treec359b956e15b2cd9f9f36fe72b3c3cbe2297cac7 /main
parent713e473b65963ec6ca1b1eb185c297852fa336ea (diff)
a
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt1
-rw-r--r--main/app.cpp45
-rw-r--r--main/app.hpp12
-rw-r--r--main/camera.cpp6
-rw-r--r--main/keyboard.cpp6
-rw-r--r--main/loader-impl.cpp2
-rw-r--r--main/main.cpp2
-rw-r--r--main/menu.cpp83
8 files changed, 150 insertions, 7 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 9723e38b..fe810d77 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -3,6 +3,7 @@ file(GLOB sources "*.cpp" CONFIGURE_ARGS)
link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-draw)
link_libraries(Magnum::Sdl2Application Magnum::Trade)
+link_libraries(MagnumIntegration::ImGui)
corrade_add_resource(res ../resources.conf)
add_executable(${self} WIN32 "${sources}" "${res}")
diff --git a/main/app.cpp b/main/app.cpp
index cc5dbe78..ffebcc35 100644
--- a/main/app.cpp
+++ b/main/app.cpp
@@ -1,5 +1,9 @@
+#include <cstddef>
#include "app.hpp"
#include "compat/fpu.hpp"
+#include <Magnum/GL/DefaultFramebuffer.h>
+#include <Magnum/ImGuiIntegration/Context.h>
+#include <Magnum/ImGuiIntegration/Context.hpp>
namespace floormat {
@@ -18,8 +22,49 @@ app::app(const Arguments& arguments):
set_fp_mask();
reset_camera_offset();
update_window_scale(windowSize());
+ setMinimalLoopPeriod(5);
+ _imgui = ImGuiIntegration::Context(Vector2{windowSize()}/dpiScaling(), windowSize(), framebufferSize());
+ setup_menu();
timeline.start();
}
+void app::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
+{
+ update_window_scale(event.windowSize());
+ GL::defaultFramebuffer.setViewport({{}, event.windowSize()});
+ _imgui.relayout(Vector2{event.windowSize()}/event.dpiScaling(),
+ event.windowSize(), event.framebufferSize());
+}
+
+
+void app::mousePressEvent(Platform::Sdl2Application::MouseEvent& event)
+{
+ if (_imgui.handleMousePressEvent(event))
+ return event.setAccepted();
+}
+
+void app::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event)
+{
+ if (_imgui.handleMouseReleaseEvent(event))
+ return event.setAccepted();
+}
+
+void app::mouseMoveEvent(Platform::Sdl2Application::MouseMoveEvent& event)
+{
+ if (_imgui.handleMouseMoveEvent(event))
+ return event.setAccepted();
+}
+
+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))
+ return event.setAccepted();
+}
void app::update(float dt)
{
diff --git a/main/app.hpp b/main/app.hpp
index 2303a8b8..3efc390f 100644
--- a/main/app.hpp
+++ b/main/app.hpp
@@ -12,6 +12,7 @@
#include <Magnum/Timeline.h>
#include <Magnum/Platform/Sdl2Application.h>
#include <Magnum/GL/DebugOutput.h>
+#include <Magnum/ImGuiIntegration/Context.h>
#include <memory>
namespace floormat {
@@ -29,12 +30,21 @@ struct app final : Platform::Application
void reset_camera_offset();
void keyPressEvent(KeyEvent& event) override;
void keyReleaseEvent(KeyEvent& event) override;
+ void mousePressEvent(MouseEvent& event) override;
+ void mouseReleaseEvent(MouseEvent& event) override;
+ void mouseMoveEvent(MouseMoveEvent& event) override;
+ void mouseScrollEvent(MouseScrollEvent& event) override;
+ void textInputEvent(TextInputEvent& event) override;
void do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated);
void draw_chunk(chunk& c);
void draw_wireframe_quad();
void draw_wireframe_box();
void update_window_scale(Vector2i window_size);
void viewportEvent(ViewportEvent& event) override;
+ void draw_menu();
+ void draw_menu_bar();
+ void setup_menu();
+ void display_menu();
void debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Type type, UnsignedInt id,
GL::DebugOutput::Severity severity, const std::string& str) const;
void* register_debug_callback();
@@ -59,6 +69,8 @@ struct app final : Platform::Application
wireframe_mesh<wireframe::quad> _wireframe_quad;
wireframe_mesh<wireframe::box> _wireframe_box;
+ ImGuiIntegration::Context _imgui{NoCreate};
+
Vector2 camera_offset;
enum_bitset<key> keys;
Magnum::Timeline timeline;
diff --git a/main/camera.cpp b/main/camera.cpp
index c91a743e..967e1496 100644
--- a/main/camera.cpp
+++ b/main/camera.cpp
@@ -32,10 +32,4 @@ void app::update_window_scale(Vector2i sz)
_shader.set_scale(Vector2{sz});
}
-void app::viewportEvent(Platform::Sdl2Application::ViewportEvent& event)
-{
- update_window_scale(event.windowSize());
- GL::defaultFramebuffer.setViewport({{}, event.windowSize()});
-}
-
} // namespace floormat
diff --git a/main/keyboard.cpp b/main/keyboard.cpp
index 50bc214a..041af2c8 100644
--- a/main/keyboard.cpp
+++ b/main/keyboard.cpp
@@ -1,4 +1,6 @@
#include "app.hpp"
+#include <Magnum/ImGuiIntegration/Context.hpp>
+
namespace floormat {
void app::do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated)
@@ -32,11 +34,15 @@ app::~app()
void app::keyPressEvent(Platform::Sdl2Application::KeyEvent& event)
{
+ if (_imgui.handleKeyPressEvent(event))
+ return event.setAccepted();
do_key(event.key(), event.modifiers(), true, event.isRepeated());
}
void app::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event)
{
+ if (_imgui.handleKeyReleaseEvent(event))
+ return event.setAccepted();
do_key(event.key(), event.modifiers(), false, false);
}
diff --git a/main/loader-impl.cpp b/main/loader-impl.cpp
index ff1bb08f..4d40250a 100644
--- a/main/loader-impl.cpp
+++ b/main/loader-impl.cpp
@@ -64,7 +64,7 @@ std::shared_ptr<tile_atlas> loader_impl::tile_atlas(Containers::StringView name,
Trade::ImageData2D loader_impl::tile_texture(Containers::StringView filename)
{
- if(!tga_importer || !tga_importer->openFile(filename)) {
+ if (!tga_importer || !tga_importer->openFile(filename)) {
const auto path = Utility::Path::currentDirectory();
MESSAGE("note: current working directory: '%s'", path->data());
ABORT("can't open tile image '%s'", filename.cbegin());
diff --git a/main/main.cpp b/main/main.cpp
index 62dd687c..a5acb0ca 100644
--- a/main/main.cpp
+++ b/main/main.cpp
@@ -43,6 +43,8 @@ void app::drawEvent() {
draw_chunk(_chunk);
draw_wireframe_quad();
draw_wireframe_box();
+ draw_menu();
+ display_menu();
swapBuffers();
redraw();
diff --git a/main/menu.cpp b/main/menu.cpp
new file mode 100644
index 00000000..92f1b980
--- /dev/null
+++ b/main/menu.cpp
@@ -0,0 +1,83 @@
+#include "app.hpp"
+#include <Magnum/GL/Renderer.h>
+#include <Magnum/ImGuiIntegration/Integration.h>
+
+namespace floormat {
+
+template<typename F>
+struct raii_wrapper final
+{
+ raii_wrapper(F&& fn) : dtor{fn} {}
+ [[no_unique_address]] F dtor;
+ inline ~raii_wrapper() { dtor(); }
+ raii_wrapper(const raii_wrapper<F>&) = delete;
+ raii_wrapper<F>& operator=(const raii_wrapper<F>&) = delete;
+};
+
+constexpr inline const auto* imgui_name = "floormat editor";
+
+[[nodiscard]] static auto gui_begin() {
+ ImGui::Begin(imgui_name, nullptr, ImGuiWindowFlags_::ImGuiWindowFlags_NoMove);
+ return raii_wrapper{[]{ ImGui::End(); }};
+}
+[[nodiscard]] static auto begin_main_menu() {
+ ImGui::BeginMainMenuBar();
+ return raii_wrapper{[] { ImGui::EndMainMenuBar(); }};
+}
+[[nodiscard]] static auto begin_menu(const char* name, bool enabled = true) {
+ ImGui::BeginMenu(name, enabled);
+ return raii_wrapper{[] { ImGui::EndMenu(); }};
+}
+
+void app::setup_menu()
+{
+ GL::Renderer::setBlendEquation(GL::Renderer::BlendEquation::Add, GL::Renderer::BlendEquation::Add);
+ GL::Renderer::setBlendFunction(GL::Renderer::BlendFunction::SourceAlpha, GL::Renderer::BlendFunction::OneMinusSourceAlpha);
+}
+
+void app::display_menu()
+{
+ 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();
+ GL::Renderer::enable(GL::Renderer::Feature::ScissorTest);
+}
+
+void app::draw_menu()
+{
+ _imgui.newFrame();
+
+ if (ImGui::GetIO().WantTextInput && !isTextInputActive())
+ startTextInput();
+ else if (!ImGui::GetIO().WantTextInput && isTextInputActive())
+ stopTextInput();
+
+ auto b = gui_begin();
+ draw_menu_bar();
+}
+
+void app::draw_menu_bar()
+{
+ auto bm = begin_main_menu();
+ {
+ auto m = begin_menu("File");
+ ImGui::MenuItem("Open");
+ ImGui::MenuItem("Recent");
+ ImGui::Separator();
+ ImGui::MenuItem("Save");
+ ImGui::MenuItem("Save as...");
+ ImGui::Separator();
+ ImGui::MenuItem("Close");
+ }
+ {
+ auto m = begin_menu("Mode");
+ ImGui::MenuItem("Select", "F1");
+ ImGui::MenuItem("Floors", "F2");
+ ImGui::MenuItem("Walls", "F3");
+ ImGui::MenuItem("Floors", "F4");
+ }
+}
+
+} // namespace floormat