summaryrefslogtreecommitdiffhomepage
path: root/editor
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-10-23 22:38:54 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-10-23 22:38:54 +0200
commit0efe01d0e7286e9eb60c4739ae748c0cb6e7a51f (patch)
tree09121d7fcbb17151ea10331bfb3de0474fe6a661 /editor
parentb6a067678ab9e225647b256595d54dde2ce6f2f5 (diff)
a
Diffstat (limited to 'editor')
-rw-r--r--editor/CMakeLists.txt8
-rw-r--r--editor/app.cpp21
-rw-r--r--editor/app.hpp78
-rw-r--r--editor/camera.cpp23
-rw-r--r--editor/editor.cpp32
-rw-r--r--editor/editor.hpp16
-rw-r--r--editor/keyboard.cpp40
-rw-r--r--editor/update.cpp16
8 files changed, 154 insertions, 80 deletions
diff --git a/editor/CMakeLists.txt b/editor/CMakeLists.txt
index 8d3a0bf5..dd308a02 100644
--- a/editor/CMakeLists.txt
+++ b/editor/CMakeLists.txt
@@ -1,14 +1,16 @@
set(self ${PROJECT_NAME}-editor)
-corrade_add_resource(res ../resources.conf)
+file(GLOB sources "*.cpp" CONFIGURE_ARGS)
+corrade_add_resource(res "../resources.conf")
if(MSVC)
- set_property(SOURCE "${res}" APPEND PROPERTY COMPILE_OPTIONS "-W0")
+ #set_property(SOURCE "${res}" APPEND PROPERTY COMPILE_OPTIONS "-W0")
+ set_property(SOURCE "${res}" PROPERTY COMPILE_OPTIONS "-W0")
else()
set_property(SOURCE "${res}" APPEND PROPERTY COMPILE_OPTIONS "-w")
endif()
-add_executable(${self} "${res}" "../loader/loader-impl.cpp")
+add_executable(${self} "${sources}" "${res}" "../loader/loader-impl.cpp")
target_link_libraries(${self} ${PROJECT_NAME}-main)
install(TARGETS ${self} RUNTIME DESTINATION bin)
diff --git a/editor/app.cpp b/editor/app.cpp
index e69de29b..dd4433f9 100644
--- a/editor/app.cpp
+++ b/editor/app.cpp
@@ -0,0 +1,21 @@
+#include "app.hpp"
+#include "main/floormat-main.hpp"
+#include "src/loader.hpp"
+
+namespace floormat {
+
+app::app() :
+ _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, {})}
+{
+}
+
+app::~app()
+{
+ loader_::destroy();
+}
+
+} // namespace floormat
diff --git a/editor/app.hpp b/editor/app.hpp
index 0b369936..856d8a64 100644
--- a/editor/app.hpp
+++ b/editor/app.hpp
@@ -1,19 +1,97 @@
#pragma once
+#include "compat/defs.hpp"
+#include "compat/enum-bitset.hpp"
+#include "editor.hpp"
+#include "src/global-coords.hpp"
#include "draw/wireframe-mesh.hpp"
#include "draw/wireframe-quad.hpp"
#include "draw/wireframe-box.hpp"
#include "main/floormat-app.hpp"
+#include <memory>
+#include <optional>
+
+#include <Magnum/ImGuiIntegration/Context.h>
+#include <Corrade/Containers/Pointer.h>
+
namespace floormat {
+struct chunk;
+struct floormat_main;
+struct tile_atlas;
+struct tile_editor;
+
+struct cursor_state final {
+ std::optional<Vector2i> pixel;
+ std::optional<global_coords> coord;
+ bool in_imgui = false;
+};
+
struct app final : floormat_app
{
app();
~app() override;
+ fm_DECLARE_DELETED_COPY_ASSIGNMENT(app);
+ fm_DECLARE_DEPRECATED_MOVE_ASSIGNMENT(app);
+
+ void update(float dt) override;
+ void draw_msaa() override;
+ 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_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_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;
+ void on_any_event(const any_event& event) noexcept override;
+ void on_focus_in() noexcept override;
+ void on_focus_out() noexcept override;
+ void on_mouse_leave() noexcept override;
+ void on_mouse_enter() noexcept override;
+
private:
+ using tile_atlas_ = std::shared_ptr<tile_atlas>;
+
+ enum class key : int {
+ camera_up, camera_left, camera_right, camera_down, camera_reset,
+ rotate_tile, quicksave, quickload,
+ quit,
+ MAX = quit, COUNT
+ };
+
+ void make_test_chunk(chunk& c);
+
+ void do_mouse_click(global_coords pos, int button);
+ void do_mouse_release(int button);
+ void do_mouse_move(global_coords pos);
+
+ void do_camera(double dt);
+ void reset_camera_offset();
+ void recalc_cursor_tile();
+ void init_imgui(Vector2i size);
+
+ void draw_cursor_tile();
+ void draw_wireframe_quad(global_coords pt);
+ void draw_wireframe_box(global_coords pt);
+ void draw_ui();
+ float draw_main_menu();
+ void draw_editor_pane(tile_editor& type, float main_menu_height);
+ void draw_fps();
+ void draw_cursor_coord();
+
+ 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;
};
} // namespace floormat
diff --git a/editor/camera.cpp b/editor/camera.cpp
index 7af77211..52496762 100644
--- a/editor/camera.cpp
+++ b/editor/camera.cpp
@@ -1,9 +1,10 @@
#include "app.hpp"
+#include "src/global-coords.hpp"
#include <Magnum/GL/DefaultFramebuffer.h>
namespace floormat {
-void app::do_camera(double dt)
+void app::do_camera(flota dt)
{
if (keys[key::camera_reset])
reset_camera_offset();
@@ -65,24 +66,4 @@ global_coords app::pixel_to_tile(Vector2d position) const
return { x, y };
}
-std::array<std::int16_t, 4> app::get_draw_bounds() const noexcept
-{
-
- using limits = std::numeric_limits<std::int16_t>;
- auto x0 = limits::max(), x1 = limits::min(), y0 = limits::max(), y1 = limits::min();
-
- for (const auto win = Vector2d(windowSize());
- auto p : {pixel_to_tile(Vector2d{0, 0}).chunk(),
- pixel_to_tile(Vector2d{win[0]-1, 0}).chunk(),
- pixel_to_tile(Vector2d{0, win[1]-1}).chunk(),
- pixel_to_tile(Vector2d{win[0]-1, win[1]-1}).chunk()})
- {
- x0 = std::min(x0, p.x);
- x1 = std::max(x1, p.x);
- y0 = std::min(y0, p.y);
- y1 = std::max(y1, p.y);
- }
- return {x0, x1, y0, y1};
-}
-
} // namespace floormat
diff --git a/editor/editor.cpp b/editor/editor.cpp
index f3c8b157..b353aa4d 100644
--- a/editor/editor.cpp
+++ b/editor/editor.cpp
@@ -15,12 +15,12 @@ namespace floormat {
static const std::filesystem::path image_path{IMAGE_PATH, std::filesystem::path::generic_format};
-tile_type::tile_type(editor_mode mode, Containers::StringView name) : _name{name}, _mode{mode}
+tile_editor::tile_editor(editor_mode mode, Containers::StringView name) : _name{ name}, _mode{ mode}
{
load_atlases();
}
-void tile_type::load_atlases()
+void tile_editor::load_atlases()
{
using atlas_array = std::vector<std::shared_ptr<tile_atlas>>;
for (auto& atlas : json_helper::from_json<atlas_array>(image_path/(_name + ".json")))
@@ -34,7 +34,7 @@ void tile_type::load_atlases()
}
}
-std::shared_ptr<tile_atlas> tile_type::maybe_atlas(Containers::StringView str)
+std::shared_ptr<tile_atlas> tile_editor::maybe_atlas(Containers::StringView str)
{
auto it = std::find_if(_atlases.begin(), _atlases.end(), [&](const auto& tuple) -> bool {
const auto& [x, _] = tuple;
@@ -46,7 +46,7 @@ std::shared_ptr<tile_atlas> tile_type::maybe_atlas(Containers::StringView str)
return it->second;
}
-std::shared_ptr<tile_atlas> tile_type::atlas(Containers::StringView str)
+std::shared_ptr<tile_atlas> tile_editor::atlas(Containers::StringView str)
{
if (auto ptr = maybe_atlas(str); ptr)
return ptr;
@@ -54,14 +54,14 @@ std::shared_ptr<tile_atlas> tile_type::atlas(Containers::StringView str)
fm_abort("no such atlas: %s", str.cbegin());
}
-void tile_type::clear_selection()
+void tile_editor::clear_selection()
{
_selected_tile = {};
_permutation = {};
_selection_mode = sel_none;
}
-void tile_type::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_t variant)
+void tile_editor::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_t variant)
{
fm_assert(atlas);
clear_selection();
@@ -69,7 +69,7 @@ void tile_type::select_tile(const std::shared_ptr<tile_atlas>& atlas, std::size_
_selected_tile = { atlas, variant % atlas->num_tiles() };
}
-void tile_type::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas)
+void tile_editor::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas)
{
fm_assert(atlas);
clear_selection();
@@ -77,19 +77,19 @@ void tile_type::select_tile_permutation(const std::shared_ptr<tile_atlas>& atlas
_permutation = { atlas, {} };
}
-bool tile_type::is_tile_selected(const std::shared_ptr<const tile_atlas>& atlas, std::size_t variant) const
+bool tile_editor::is_tile_selected(const std::shared_ptr<const tile_atlas>& atlas, std::size_t variant) const
{
return atlas && _selection_mode == sel_tile && _selected_tile &&
atlas == _selected_tile.atlas && variant == _selected_tile.variant;
}
-bool tile_type::is_permutation_selected(const std::shared_ptr<const tile_atlas>& atlas) const
+bool tile_editor::is_permutation_selected(const std::shared_ptr<const tile_atlas>& atlas) const
{
const auto& [perm, _] = _permutation;
return atlas && _selection_mode == sel_perm && perm == atlas;
}
-bool tile_type::is_atlas_selected(const std::shared_ptr<const tile_atlas>& atlas) const
+bool tile_editor::is_atlas_selected(const std::shared_ptr<const tile_atlas>& atlas) const
{
switch (_selection_mode)
{
@@ -115,7 +115,7 @@ void fisher_yates(T begin, T end)
}
}
-tile_image tile_type::get_selected_perm()
+tile_image tile_editor::get_selected_perm()
{
auto& [atlas, vec] = _permutation;
const std::size_t N = atlas->num_tiles();
@@ -132,7 +132,7 @@ tile_image tile_type::get_selected_perm()
return {atlas, idx};
}
-tile_image tile_type::get_selected()
+tile_image tile_editor::get_selected()
{
switch (_selection_mode)
{
@@ -148,7 +148,7 @@ tile_image tile_type::get_selected()
}
}
-void tile_type::place_tile(world& world, global_coords pos, tile_image& img)
+void tile_editor::place_tile(world& world, global_coords pos, tile_image& img)
{
const auto& [c, t] = world[pos];
const auto& [atlas, variant] = img;
@@ -181,7 +181,7 @@ void editor::set_mode(editor_mode mode)
on_release();
}
-const tile_type* editor::current() const
+const tile_editor* editor::current() const
{
switch (_mode)
{
@@ -197,9 +197,9 @@ const tile_type* editor::current() const
}
}
-tile_type* editor::current()
+tile_editor* editor::current()
{
- return const_cast<tile_type*>(static_cast<const editor&>(*this).current());
+ return const_cast<tile_editor*>(static_cast<const editor&>(*this).current());
}
void editor::on_release()
diff --git a/editor/editor.hpp b/editor/editor.hpp
index 28ba153c..d952b9ba 100644
--- a/editor/editor.hpp
+++ b/editor/editor.hpp
@@ -20,9 +20,9 @@ enum class editor_mode : unsigned char {
struct world;
-struct tile_type final
+struct tile_editor final
{
- tile_type(editor_mode mode, Containers::StringView name);
+ tile_editor(editor_mode mode, Containers::StringView name);
std::shared_ptr<tile_atlas> maybe_atlas(Containers::StringView str);
std::shared_ptr<tile_atlas> atlas(Containers::StringView str);
auto cbegin() const { return _atlases.cbegin(); }
@@ -68,14 +68,14 @@ struct editor final
[[nodiscard]] editor_mode mode() const { return _mode; }
void set_mode(editor_mode mode);
- tile_type& floor() { return _floor; }
- const tile_type& floor() const { return _floor; }
+ tile_editor& floor() { return _floor; }
+ const tile_editor& floor() const { return _floor; }
- tile_type* current();
- const tile_type* current() const;
+ tile_editor* current();
+ const tile_editor* current() const;
void on_click(world& world, global_coords pos);
- void on_mouse_move(world& world, const global_coords pos);
+ void on_mouse_move(world& world, global_coords pos);
void on_release();
editor();
@@ -84,7 +84,7 @@ struct editor final
fm_DECLARE_DELETED_COPY_ASSIGNMENT(editor);
private:
- tile_type _floor{editor_mode::floor, "floor"};
+ tile_editor _floor{ editor_mode::floor, "floor"};
std::optional<global_coords> _last_pos;
editor_mode _mode = editor_mode::select;
bool _dirty = false;
diff --git a/editor/keyboard.cpp b/editor/keyboard.cpp
index a700d0f4..d5521af7 100644
--- a/editor/keyboard.cpp
+++ b/editor/keyboard.cpp
@@ -1,38 +1,28 @@
#include "app.hpp"
+#include "main/floormat-events.hpp"
namespace floormat {
-void app::do_key(KeyEvent::Key k, KeyEvent::Modifiers m, bool pressed, bool repeated)
+bool app::on_key_down(const key_event& event) noexcept
{
- //using Mods = KeyEvent::Modifiers;
+ if _imgui.
- (void)m;
- (void)repeated;
-
- const key x = fm_begin(switch (k)
+ const key x = fm_begin(switch (event.key)
{
- using enum KeyEvent::Key;
- using enum key;
-
- default: return COUNT;
- case W: return camera_up;
- case A: return camera_left;
- case S: return camera_down;
- case D: return camera_right;
- case Home: return camera_reset;
- case R: return rotate_tile;
- case F5: return quicksave;
- case F9: return quickload;
- case Esc: return quit;
+ 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] = pressed;
-}
-
-app::~app()
-{
- loader_::destroy();
+ keys[x] = event.is_down && !event.is_repeated;
}
} // namespace floormat
diff --git a/editor/update.cpp b/editor/update.cpp
index ebd1881b..b20e0728 100644
--- a/editor/update.cpp
+++ b/editor/update.cpp
@@ -1,4 +1,6 @@
#include "app.hpp"
+#include "src/chunk.hpp"
+#include "src/tile-atlas.hpp"
namespace floormat {
@@ -11,7 +13,7 @@ void app::make_test_chunk(chunk& c)
#if defined FM_NO_BINDINGS
const auto& atlas = floor1;
#else
- const auto& atlas = pt.x != pt.y && (pt.x == N/2 || pt.y == N/2) ? floor2 : floor1;
+ const auto& atlas = pt.x == N/2 || pt.y == N/2 ? _floor2 : _floor1;
#endif
x.ground_image = { atlas, k % atlas->num_tiles() };
}
@@ -19,15 +21,15 @@ void app::make_test_chunk(chunk& c)
const auto& wall1 = floor1, wall2 = floor1;
#endif
constexpr auto K = N/2;
- c[{K, K }].wall_north = { wall1, 0 };
- c[{K, K }].wall_west = { wall2, 0 };
- c[{K, K+1}].wall_north = { wall1, 0 };
- c[{K+1, K }].wall_west = { wall2, 0 };
+ c[{K, K }].wall_north = { _wall1, 0 };
+ c[{K, K }].wall_west = { _wall2, 0 };
+ c[{K, K+1}].wall_north = { _wall1, 0 };
+ c[{K+1, K }].wall_west = { _wall2, 0 };
}
void app::do_mouse_click(const global_coords pos, int button)
{
- if (button == SDL_BUTTON_LEFT)
+ if (button == mouse_button_event)
_editor.on_click(_world, pos);
else
_editor.on_release();
@@ -44,7 +46,7 @@ void app::do_mouse_move(global_coords pos)
_editor.on_mouse_move(_world, pos);
}
-void app::update(double dt)
+void app::update(float dt)
{
do_camera(dt);
draw_ui();