diff options
Diffstat (limited to 'editor')
| -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 |
7 files changed, 135 insertions, 54 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) |
