diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-23 23:46:56 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2022-10-23 23:46:56 +0200 |
commit | 17283c55122234e7cc2dc352262b273d05aecff8 (patch) | |
tree | cbc8658fc02438779a9256b31c29a66aa5a80371 | |
parent | 0efe01d0e7286e9eb60c4739ae748c0cb6e7a51f (diff) |
a
-rw-r--r-- | editor/app.cpp | 5 | ||||
-rw-r--r-- | editor/app.hpp | 14 | ||||
-rw-r--r-- | editor/camera.cpp | 22 | ||||
-rw-r--r-- | editor/events.cpp | 116 | ||||
-rw-r--r-- | editor/imgui.cpp | 2 | ||||
-rw-r--r-- | editor/keyboard.cpp | 28 | ||||
-rw-r--r-- | editor/update.cpp | 2 | ||||
-rw-r--r-- | main/debug.cpp | 4 | ||||
-rw-r--r-- | main/floormat-app.hpp | 6 | ||||
-rw-r--r-- | main/floormat-events.cpp | 32 | ||||
-rw-r--r-- | main/floormat-events.hpp | 3 | ||||
-rw-r--r-- | main/floormat-main-impl.cpp | 1 | ||||
-rw-r--r-- | main/floormat-main-impl.hpp | 3 | ||||
-rw-r--r-- | main/floormat-main.hpp | 3 |
14 files changed, 164 insertions, 77 deletions
diff --git a/editor/app.cpp b/editor/app.cpp index dd4433f9..da507563 100644 --- a/editor/app.cpp +++ b/editor/app.cpp @@ -5,11 +5,12 @@ namespace floormat { app::app() : + FM{ floormat_main::create(*this, {})}, + _dummy{(void(fmain->register_debug_callback()), nullptr)}, _floor1{loader.tile_atlas("floor-tiles", {44, 4})}, _floor2{loader.tile_atlas("metal1", {2, 2})}, _wall1{loader.tile_atlas("wood2", {1, 1})}, - _wall2{loader.tile_atlas("wood1", {1, 1})}, - _fmain{floormat_main::create(*this, {})} + _wall2{loader.tile_atlas("wood1", {1, 1})} { } diff --git a/editor/app.hpp b/editor/app.hpp index 856d8a64..b5a22f28 100644 --- a/editor/app.hpp +++ b/editor/app.hpp @@ -23,7 +23,7 @@ struct tile_editor; struct cursor_state final { std::optional<Vector2i> pixel; - std::optional<global_coords> coord; + std::optional<global_coords> tile; bool in_imgui = false; }; @@ -40,11 +40,10 @@ struct app final : floormat_app void draw() override; bool on_mouse_move(const mouse_move_event& event) noexcept override; - bool on_mouse_down(const mouse_button_event& event) noexcept override; - bool on_mouse_up(const mouse_button_event& event) noexcept override; + bool on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept override; bool on_mouse_scroll(const mouse_scroll_event& event) noexcept override; - bool on_key_down(const key_event& event) noexcept override; - bool on_key_up(const key_event& event) noexcept override; + bool on_key_up_down(const key_event& event, bool is_down) noexcept override; + bool on_text_input_event(const text_input_event& event) noexcept override; bool on_text_editing_event(const text_editing_event& event) noexcept override; void on_viewport_event(const Magnum::Math::Vector2<int>& size) noexcept override; @@ -84,14 +83,15 @@ private: void draw_fps(); void draw_cursor_coord(); + Containers::Pointer<floormat_main> M; + [[maybe_unused]] void* _dummy; std::shared_ptr<tile_atlas> _floor1, _floor2, _wall1, _wall2; - Containers::Pointer<floormat_main> _fmain; ImGuiIntegration::Context _imgui{NoCreate}; wireframe_mesh<wireframe::quad> _wireframe_quad; wireframe_mesh<wireframe::box> _wireframe_box; editor _editor; enum_bitset<key> _keys; - cursor_state _cursor; + cursor_state cursor; }; } // namespace floormat diff --git a/editor/camera.cpp b/editor/camera.cpp index 52496762..0ae5a26d 100644 --- a/editor/camera.cpp +++ b/editor/camera.cpp @@ -1,5 +1,7 @@ #include "app.hpp" #include "src/global-coords.hpp" +#include "shaders/tile-shader.hpp" +#include "main/floormat-main.hpp" #include <Magnum/GL/DefaultFramebuffer.h> namespace floormat { @@ -38,32 +40,22 @@ void app::do_camera(flota dt) return; } recalc_cursor_tile(); - if (_cursor_tile) + if (cursor.tile) do_mouse_move(*_cursor_tile); } void app::reset_camera_offset() { - _shader.set_camera_offset(tile_shader::project({TILE_MAX_DIM*-.5*dTILE_SIZE[0], TILE_MAX_DIM*-.5*dTILE_SIZE[1], 0})); + M->shader().set_camera_offset(tile_shader::project({TILE_MAX_DIM*-.5*dTILE_SIZE[0], TILE_MAX_DIM*-.5*dTILE_SIZE[1], 0})); recalc_cursor_tile(); } void app::recalc_cursor_tile() { - if (_cursor_pixel && !_cursor_in_imgui) - _cursor_tile = pixel_to_tile(Vector2d(*_cursor_pixel)); + if (cursor.pixel && !cursor.in_imgui) + cursor.tile = M->pixel_to_tile(Vector2d(*cursor.pixel)); else - _cursor_tile = std::nullopt; -} - -global_coords app::pixel_to_tile(Vector2d position) const -{ - constexpr Vector2d pixel_size{dTILE_SIZE[0], dTILE_SIZE[1]}; - constexpr Vector2d half{.5, .5}; - const Vector2d px = position - Vector2d{windowSize()}*.5 - _shader.camera_offset()*.5; - const Vector2d vec = tile_shader::unproject(px) / pixel_size + half; - const auto x = (std::int32_t)std::floor(vec[0]), y = (std::int32_t)std::floor(vec[1]); - return { x, y }; + cursor.tile = std::nullopt; } } // namespace floormat diff --git a/editor/events.cpp b/editor/events.cpp new file mode 100644 index 00000000..109a3a39 --- /dev/null +++ b/editor/events.cpp @@ -0,0 +1,116 @@ +#include "app.hpp" +#include "main/floormat-main.hpp" +#include "main/floormat-events.hpp" +#include "src/world.hpp" +#include <utility> +#include <Magnum/ImGuiIntegration/Context.hpp> + +namespace floormat { + +#define accessor(type, name) \ + type m_##name = {}; auto name() const noexcept { return m_##name; } + +bool app::on_mouse_move(const mouse_move_event& event) noexcept +{ + struct { + accessor(Vector2i, position) + } e = {event.position}; + + cursor.in_imgui = _imgui.handleMouseMoveEvent(e); + + if (!cursor.in_imgui) + cursor.pixel = event.position; + else + cursor.pixel = std::nullopt; + recalc_cursor_tile(); + + if (cursor.tile) + _editor.on_mouse_move(M->world(), *cursor.tile); + + return true; +} + +bool app::on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept +{ + enum class Button : std::underlying_type_t<mouse_button> { + Left = mouse_button_left, + Right = mouse_button_right, + Middle = mouse_button_middle, + }; + struct ev { + using Button = Button; + accessor(Vector2i, position) + accessor(Button, button) + } e = {event.position, Button(event.button)}; + + if (!(cursor.in_imgui = is_down + ? _imgui.handleMousePressEvent(e) + : _imgui.handleMouseReleaseEvent(e))) + { + cursor.pixel = event.position; + recalc_cursor_tile(); + if (cursor.tile) + { + if (event.button == mouse_button_left && is_down) + _editor.on_click(M->world(), *cursor.tile); + else + _editor.on_release(); + } + } + + return true; +} + +bool app::on_mouse_scroll(const mouse_scroll_event& event) noexcept +{ + struct { + accessor(Vector2, offset) + accessor(Vector2i, position) + } e = {event.offset, event.position}; + _imgui.handleMouseScrollEvent(e); + return true; +} + +bool app::on_key_up_down(const floormat::key_event& event, bool is_down) noexcept +{ + enum Modifier : std::underlying_type_t<SDL_Keymod> { + None = KMOD_NONE, + Ctrl = KMOD_CTRL, + Shift = KMOD_SHIFT, + Alt = KMOD_ALT, + Super = KMOD_GUI, + }; + struct { + accessor(SDL_Keymod, modifiers) + } e = {}; + if (!(is_down ? _imgui.handleKeyPressEvent(e) : _imgui.handleKeyReleaseEvent(e))) + { + const key x = fm_begin(switch (event.key) { + default: return key::COUNT; + case SDLK_w: return key::camera_up; + case SDLK_a: return key::camera_left; + case SDLK_s: return key::camera_down; + case SDLK_d: return key::camera_right; + case SDLK_HOME: return key::camera_reset; + case SDLK_r: return key::rotate_tile; + case SDLK_F5: return key::quicksave; + case SDLK_F9: return key::quickload; + case SDLK_ESCAPE: return key::quit; + }); + if (x != key::COUNT) + _keys[x] = is_down && !event.is_repeated; + } + return true; +} + +bool app::on_text_input_event(const floormat::text_input_event& event) noexcept +{ + struct { + accessor(Containers::StringView, text) + } e = {event.text}; + if (_imgui.handleTextInputEvent(e)) + _keys = {}; + return true; +} + +} // namespace floormat diff --git a/editor/imgui.cpp b/editor/imgui.cpp index b0777d5d..a64330e4 100644 --- a/editor/imgui.cpp +++ b/editor/imgui.cpp @@ -231,7 +231,7 @@ void app::draw_cursor_coord() ImGui::SetNextWindowSize(size); if (auto flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoInputs | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoBackground; - auto b = begin_window("tile-coord", ImGuiWindowFlags_(flags))) + auto b = begin_window("tile-tile", ImGuiWindowFlags_(flags))) { ImGui::Text("%s", buf); } diff --git a/editor/keyboard.cpp b/editor/keyboard.cpp deleted file mode 100644 index d5521af7..00000000 --- a/editor/keyboard.cpp +++ /dev/null @@ -1,28 +0,0 @@ -#include "app.hpp" -#include "main/floormat-events.hpp" - -namespace floormat { - -bool app::on_key_down(const key_event& event) noexcept -{ - if _imgui. - - const key x = fm_begin(switch (event.key) - { - default: return key::COUNT; - case SDLK_w: return key::camera_up; - case SDLK_a: return key::camera_left; - case SDLK_s: return key::camera_down; - case SDLK_d: return key::camera_right; - case SDLK_HOME: return key::camera_reset; - case SDLK_r: return key::rotate_tile; - case SDLK_F5: return key::quicksave; - case SDLK_F9: return key::quickload; - case SDLK_ESCAPE: return key::quit; - }); - - if (x != key::COUNT) - keys[x] = event.is_down && !event.is_repeated; -} - -} // namespace floormat diff --git a/editor/update.cpp b/editor/update.cpp index b20e0728..579d640e 100644 --- a/editor/update.cpp +++ b/editor/update.cpp @@ -43,7 +43,7 @@ void app::do_mouse_release(int button) void app::do_mouse_move(global_coords pos) { - _editor.on_mouse_move(_world, pos); + //_editor.on_mouse_move(_world, pos); } void app::update(float dt) diff --git a/main/debug.cpp b/main/debug.cpp index 7e012b9d..efc47a6c 100644 --- a/main/debug.cpp +++ b/main/debug.cpp @@ -58,7 +58,7 @@ void main_impl::_debug_callback(GL::DebugOutput::Source src, GL::DebugOutput::Ty static_cast<const main_impl*>(self)->debug_callback(src, type, id, severity, str); } -void main_impl::register_debug_callback() noexcept +void* main_impl::register_debug_callback() noexcept { GL::DebugOutput::setCallback(_debug_callback, this); @@ -66,6 +66,8 @@ void main_impl::register_debug_callback() noexcept /* Disable rather spammy "Buffer detailed info" debug messages on NVidia drivers */ GL::DebugOutput::setEnabled(GL::DebugOutput::Source::Api, GL::DebugOutput::Type::Other, {131185}, false); #endif + + return nullptr; } } // namespace floormat diff --git a/main/floormat-app.hpp b/main/floormat-app.hpp index 805718a0..f4f7cc25 100644 --- a/main/floormat-app.hpp +++ b/main/floormat-app.hpp @@ -26,11 +26,9 @@ struct floormat_app virtual void draw() = 0; virtual bool on_mouse_move(const mouse_move_event& event) noexcept = 0; - virtual bool on_mouse_down(const mouse_button_event& event) noexcept = 0; - virtual bool on_mouse_up(const mouse_button_event& event) noexcept = 0; + virtual bool on_mouse_up_down(const mouse_button_event& event, bool is_down) noexcept = 0; virtual bool on_mouse_scroll(const mouse_scroll_event& event) noexcept = 0; - virtual bool on_key_down(const key_event& event) noexcept = 0; - virtual bool on_key_up(const key_event& event) noexcept = 0; + virtual bool on_key_up_down(const key_event& event, bool is_down) noexcept = 0; virtual bool on_text_input_event(const text_input_event& event) noexcept = 0; virtual bool on_text_editing_event(const text_editing_event& event) noexcept = 0; virtual void on_viewport_event(const Magnum::Math::Vector2<int>& size) noexcept = 0; diff --git a/main/floormat-events.cpp b/main/floormat-events.cpp index d0ed797b..9091e663 100644 --- a/main/floormat-events.cpp +++ b/main/floormat-events.cpp @@ -17,19 +17,21 @@ void main_impl::viewportEvent(Platform::Sdl2Application::ViewportEvent& event) void main_impl::mousePressEvent(Platform::Sdl2Application::MouseEvent& event) { - if (app.on_mouse_down(mouse_button_event{event.position(), - (SDL_Keymod)(std::uint16_t)event.modifiers(), - mouse_button(event.button()), - std::uint8_t(std::min(255, event.clickCount()))})) + if (app.on_mouse_up_down({event.position(), + (SDL_Keymod)(std::uint16_t)event.modifiers(), + mouse_button(event.button()), + std::uint8_t(std::min(255, event.clickCount()))}, + true)) return event.setAccepted(); } void main_impl::mouseReleaseEvent(Platform::Sdl2Application::MouseEvent& event) { - if (app.on_mouse_up({event.position(), - (SDL_Keymod)(std::uint16_t)event.modifiers(), - mouse_button(event.button()), - std::uint8_t(std::min(255, event.clickCount()))})) + if (app.on_mouse_up_down({event.position(), + (SDL_Keymod)(std::uint16_t)event.modifiers(), + mouse_button(event.button()), + std::uint8_t(std::min(255, event.clickCount()))}, + false)) return event.setAccepted(); } @@ -62,17 +64,19 @@ void main_impl::textEditingEvent(Platform::Sdl2Application::TextEditingEvent& ev void main_impl::keyPressEvent(Platform::Sdl2Application::KeyEvent& event) { - if (app.on_key_down({(SDL_Keycode)(std::uint32_t)event.key(), - (SDL_Keymod)(std::uint16_t)event.modifiers(), - true, event.isRepeated()})) + if (app.on_key_up_down({(SDL_Keycode)(std::uint32_t)event.key(), + (SDL_Keymod)(std::uint16_t)event.modifiers(), + event.isRepeated()}, + true)) return event.setAccepted(); } void main_impl::keyReleaseEvent(Platform::Sdl2Application::KeyEvent& event) { - if (app.on_key_up({(SDL_Keycode)(std::uint32_t)event.key(), - (SDL_Keymod)(std::uint16_t)event.modifiers(), - false, event.isRepeated()})) + if (app.on_key_up_down({(SDL_Keycode)(std::uint32_t)event.key(), + (SDL_Keymod)(std::uint16_t)event.modifiers(), + event.isRepeated()}, + false)) return event.setAccepted(); } diff --git a/main/floormat-events.hpp b/main/floormat-events.hpp index 64f41efd..97c5f171 100644 --- a/main/floormat-events.hpp +++ b/main/floormat-events.hpp @@ -46,8 +46,7 @@ struct text_editing_event final { struct key_event final { SDL_Keycode key = SDLK_UNKNOWN; SDL_Keymod mods = KMOD_NONE; - std::uint8_t is_down : 1 = false; - std::uint8_t is_repeated : 1 = false; + std::uint8_t is_repeated = false; }; struct any_event final { diff --git a/main/floormat-main-impl.cpp b/main/floormat-main-impl.cpp index 0945daf4..7ba31490 100644 --- a/main/floormat-main-impl.cpp +++ b/main/floormat-main-impl.cpp @@ -219,6 +219,7 @@ SDL_Window* main_impl::window() noexcept Vector2i main_impl::window_size() const noexcept { return windowSize(); } tile_shader& main_impl::shader() noexcept { return _shader; } +const tile_shader& main_impl::shader() const noexcept { return _shader; } floormat_main* floormat_main::create(floormat_app& app, const fm_options& options) { diff --git a/main/floormat-main-impl.hpp b/main/floormat-main-impl.hpp index e84a0527..4ab6ef27 100644 --- a/main/floormat-main-impl.hpp +++ b/main/floormat-main-impl.hpp @@ -24,7 +24,8 @@ struct main_impl final : Platform::Sdl2Application, floormat_main Magnum::Math::Vector2<int> window_size() const noexcept override; tile_shader& shader() noexcept override; - void register_debug_callback() noexcept override; + const tile_shader& shader() const noexcept override; + void* register_debug_callback() noexcept override; struct world& world() noexcept override; SDL_Window* window() noexcept override; diff --git a/main/floormat-main.hpp b/main/floormat-main.hpp index ae08744e..b569c5d7 100644 --- a/main/floormat-main.hpp +++ b/main/floormat-main.hpp @@ -24,7 +24,8 @@ struct floormat_main virtual Magnum::Math::Vector2<int> window_size() const noexcept = 0; virtual tile_shader& shader() noexcept = 0; - virtual void register_debug_callback() noexcept = 0; + virtual const tile_shader& shader() const noexcept = 0; + virtual void* register_debug_callback() noexcept = 0; constexpr float smoothed_dt() const noexcept { return _frame_time; } virtual global_coords pixel_to_tile(Vector2d position) const noexcept = 0; |