refactor(mirror): adjusted code for new surface & input module changes
This commit is contained in:
parent
3020f17507
commit
3c1193eedc
2 changed files with 160 additions and 28 deletions
|
@ -4,7 +4,8 @@ target_link_libraries(
|
|||
libmirror
|
||||
INTERFACE
|
||||
app
|
||||
opengl::opengl
|
||||
time
|
||||
input
|
||||
surface
|
||||
)
|
||||
|
||||
|
@ -16,4 +17,4 @@ add_test_module(libmirror
|
|||
)
|
||||
|
||||
add_executable_module(mirror entrypoint/mirror.cpp)
|
||||
target_link_libraries(mirror PRIVATE libmirror)
|
||||
target_link_libraries(mirror PRIVATE libmirror input)
|
||||
|
|
|
@ -1,15 +1,100 @@
|
|||
#include <X11/keysym.h>
|
||||
#include <app/application.hpp>
|
||||
#include <app/entrypoint.hpp>
|
||||
#include <app/system.hpp>
|
||||
#include <ecs/entity.hpp>
|
||||
#include <input/components.hpp>
|
||||
#include <input/system.hpp>
|
||||
#include <math/vec2.hpp>
|
||||
#include <surface/events/keyboard.hpp>
|
||||
#include <surface/events/surface.hpp>
|
||||
#include <surface/system.hpp>
|
||||
#include <time/timer.hpp>
|
||||
|
||||
namespace lt {
|
||||
|
||||
template<class... Ts>
|
||||
struct overloads: Ts...
|
||||
class MirrorSystem: public lt::app::ISystem
|
||||
{
|
||||
using Ts::operator()...;
|
||||
public:
|
||||
MirrorSystem(
|
||||
Ref<ecs::Registry> registry,
|
||||
lt::input::InputAction::Key quit_action_key,
|
||||
std::array<lt::input::InputAction::Key, 4> debug_action_keys
|
||||
)
|
||||
: m_registry(std::move(registry))
|
||||
, m_quit_action_key(quit_action_key)
|
||||
, m_debug_action_keys(debug_action_keys)
|
||||
{
|
||||
using Surface = lt::surface::SurfaceComponent;
|
||||
using Input = lt::input::InputComponent;
|
||||
|
||||
auto view = m_registry->get_entt_registry().view<Surface, Input>();
|
||||
|
||||
view.each([&](Surface &surface, Input &input) {});
|
||||
}
|
||||
|
||||
auto tick() -> bool override
|
||||
{
|
||||
using Surface = lt::surface::SurfaceComponent;
|
||||
using Input = lt::input::InputComponent;
|
||||
|
||||
static lt::Timer timer;
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds { 10 });
|
||||
auto should_quit = false;
|
||||
auto view = m_registry->get_entt_registry().view<Surface, Input>();
|
||||
view.each([&](Surface &surface, Input &input) {
|
||||
using State = lt::input::InputAction::State;
|
||||
const auto &[x, y] = surface.get_position();
|
||||
const auto &[width, height] = surface.get_resolution();
|
||||
|
||||
if (input.get_action(m_quit_action_key).state == State::active)
|
||||
{
|
||||
log_dbg("Quit triggered! BYE!");
|
||||
should_quit = true;
|
||||
}
|
||||
if (input.get_action(m_debug_action_keys[0]).state == State::active)
|
||||
{
|
||||
log_dbg("Deubg action 1");
|
||||
surface.push_request(surface::ModifyPositionRequest({ x + 5, y + 5 }));
|
||||
}
|
||||
|
||||
if (input.get_action(m_debug_action_keys[1]).state == State::active)
|
||||
{
|
||||
log_dbg("Deubg action 2");
|
||||
surface.push_request(surface::ModifyPositionRequest({ x - 5, y - 5 }));
|
||||
}
|
||||
|
||||
if (input.get_action(m_debug_action_keys[2]).state == State::active)
|
||||
{
|
||||
log_dbg("Deubg action 3");
|
||||
surface.push_request(surface::ModifyResolutionRequest({ width + 5, height + 5 }));
|
||||
}
|
||||
|
||||
if (input.get_action(m_debug_action_keys[3]).state == State::active)
|
||||
{
|
||||
log_dbg("Deubg action 4");
|
||||
surface.push_request(surface::ModifyResolutionRequest({ width - 5, height - 5 }));
|
||||
}
|
||||
});
|
||||
|
||||
timer.reset();
|
||||
return should_quit;
|
||||
}
|
||||
|
||||
void on_register() override
|
||||
{
|
||||
}
|
||||
|
||||
void on_unregister() override
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
Ref<ecs::Registry> m_registry;
|
||||
|
||||
lt::input::InputAction::Key m_quit_action_key;
|
||||
std::array<lt::input::InputAction::Key, 4> m_debug_action_keys {};
|
||||
};
|
||||
|
||||
class Mirror: public app::Application
|
||||
|
@ -20,53 +105,99 @@ public:
|
|||
m_editor_registry = create_ref<ecs::Registry>();
|
||||
|
||||
setup_window_system();
|
||||
setup_input_system();
|
||||
|
||||
register_systems();
|
||||
}
|
||||
|
||||
m_window_system->add_event_listener(
|
||||
m_window,
|
||||
[&](const surface::SurfaceComponent::Event &event) {
|
||||
const auto visitor = overloads {
|
||||
[&](const lt::surface::KeyPressedEvent &event) {
|
||||
std::cout << "key pressed: " << event.to_string() << std::endl;
|
||||
void on_window_close()
|
||||
{
|
||||
log_inf("Window close requested...");
|
||||
|
||||
if (event.get_key() == 81)
|
||||
{
|
||||
unregister_system(m_window_system);
|
||||
log_inf("Quitting...");
|
||||
}
|
||||
return true;
|
||||
},
|
||||
[](const auto &) { return false; },
|
||||
};
|
||||
|
||||
return std::visit(visitor, event);
|
||||
}
|
||||
);
|
||||
unregister_system(m_input_system);
|
||||
unregister_system(m_surface_system);
|
||||
unregister_system(m_mirror_system);
|
||||
}
|
||||
|
||||
void setup_window_system()
|
||||
{
|
||||
using lt::input::InputComponent;
|
||||
using lt::surface::SurfaceComponent;
|
||||
m_window_system = create_ref<lt::surface::System>(m_editor_registry);
|
||||
m_surface_system = create_ref<lt::surface::System>(m_editor_registry);
|
||||
|
||||
m_window = m_editor_registry->create_entity("Editor Window");
|
||||
m_window.add_component<SurfaceComponent>(SurfaceComponent::CreateInfo {
|
||||
.title = "Editor Window",
|
||||
.resolution = { 800u, 600u },
|
||||
.resolution = { 400u, 400u },
|
||||
.vsync = true,
|
||||
.visible = true,
|
||||
});
|
||||
|
||||
auto &input = m_window.add_component<InputComponent>();
|
||||
auto quit_action_key = input.add_action(
|
||||
input::InputAction {
|
||||
.name = "quit",
|
||||
.trigger = input::Trigger { .mapped_keycode = XK_q },
|
||||
}
|
||||
);
|
||||
|
||||
auto debug_action_keys = std::array<lt::input::InputAction::Key, 4> {};
|
||||
|
||||
debug_action_keys[0] = input.add_action(
|
||||
input::InputAction {
|
||||
.name = "debug_1",
|
||||
.trigger = input::Trigger { .mapped_keycode = XK_1 },
|
||||
}
|
||||
);
|
||||
|
||||
debug_action_keys[1] = input.add_action(
|
||||
input::InputAction {
|
||||
.name = "debug_2",
|
||||
.trigger = input::Trigger { .mapped_keycode = XK_2 },
|
||||
}
|
||||
);
|
||||
|
||||
debug_action_keys[2] = input.add_action(
|
||||
input::InputAction {
|
||||
.name = "debug_3",
|
||||
.trigger = input::Trigger { .mapped_keycode = XK_3 },
|
||||
}
|
||||
);
|
||||
|
||||
debug_action_keys[3] = input.add_action(
|
||||
input::InputAction {
|
||||
.name = "debug_4",
|
||||
.trigger = input::Trigger { .mapped_keycode = XK_4 },
|
||||
}
|
||||
);
|
||||
|
||||
m_input_system = create_ref<input::System>(m_editor_registry);
|
||||
m_mirror_system = create_ref<MirrorSystem>(
|
||||
m_editor_registry,
|
||||
quit_action_key,
|
||||
debug_action_keys
|
||||
);
|
||||
}
|
||||
|
||||
void setup_input_system()
|
||||
{
|
||||
}
|
||||
|
||||
void register_systems()
|
||||
{
|
||||
register_system(m_window_system);
|
||||
register_system(m_surface_system);
|
||||
register_system(m_input_system);
|
||||
register_system(m_mirror_system);
|
||||
}
|
||||
|
||||
private:
|
||||
Ref<ecs::Registry> m_editor_registry;
|
||||
|
||||
Ref<lt::surface::System> m_window_system;
|
||||
Ref<lt::surface::System> m_surface_system;
|
||||
|
||||
Ref<lt::input::System> m_input_system;
|
||||
|
||||
Ref<MirrorSystem> m_mirror_system;
|
||||
|
||||
lt::ecs::Entity m_window;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue