aboutsummaryrefslogtreecommitdiff
path: root/src/commands
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-03-20 13:17:16 +0100
committerHampusM <hampus@hampusmat.com>2022-06-13 17:56:56 +0200
commit78f5d8cf644383adf20933ad64c160c07c2c54fb (patch)
tree2a348d90f8f150665c379bf2f3741731999dd679 /src/commands
parentb6061eff99907495de282f611e8389e6468a1fb0 (diff)
refactor: improve input configuring structure
Diffstat (limited to 'src/commands')
-rw-r--r--src/commands/insert_cell.cpp24
-rw-r--r--src/commands/insert_cell.hpp20
-rw-r--r--src/commands/move_cursor.cpp37
-rw-r--r--src/commands/move_cursor.hpp27
-rw-r--r--src/commands/quit.cpp8
-rw-r--r--src/commands/quit.hpp9
6 files changed, 125 insertions, 0 deletions
diff --git a/src/commands/insert_cell.cpp b/src/commands/insert_cell.cpp
new file mode 100644
index 0000000..86a5a52
--- /dev/null
+++ b/src/commands/insert_cell.cpp
@@ -0,0 +1,24 @@
+#include "insert_cell.hpp"
+
+#include <iostream>
+
+InsertCellCommand::InsertCellCommand(
+ const std::shared_ptr<ICursorController> &cursor_controller,
+ const std::shared_ptr<IScene> &scene) noexcept
+ : _cursor_controller(cursor_controller), _scene(scene)
+{
+}
+
+void InsertCellCommand::execute() noexcept
+{
+ const auto position = _cursor_controller->where();
+
+ std::cout.put('x');
+ std::cout.flush();
+
+ _cursor_controller->move_to(position);
+
+ auto matrix = _scene->get_matrix();
+
+ matrix->set(position - Vector2({.x = 0U, .y = 1U}), "#");
+}
diff --git a/src/commands/insert_cell.hpp b/src/commands/insert_cell.hpp
new file mode 100644
index 0000000..cf04203
--- /dev/null
+++ b/src/commands/insert_cell.hpp
@@ -0,0 +1,20 @@
+#pragma once
+
+#include "interfaces/command.hpp"
+#include "interfaces/cursor.hpp"
+#include "interfaces/scene.hpp"
+
+#include <memory>
+
+class InsertCellCommand : public ICommand
+{
+public:
+ InsertCellCommand(const std::shared_ptr<ICursorController> &cursor_controller,
+ const std::shared_ptr<IScene> &scene) noexcept;
+
+ void execute() noexcept override;
+
+private:
+ const std::shared_ptr<ICursorController> &_cursor_controller;
+ const std::shared_ptr<IScene> &_scene;
+};
diff --git a/src/commands/move_cursor.cpp b/src/commands/move_cursor.cpp
new file mode 100644
index 0000000..8dd7db7
--- /dev/null
+++ b/src/commands/move_cursor.cpp
@@ -0,0 +1,37 @@
+#include "move_cursor.hpp"
+
+#include "strings.hpp"
+
+#include <fmt/core.h>
+
+MoveCursorCommand::MoveCursorCommand(
+ const Vector2 &direction, const std::shared_ptr<ICursorController> &cursor_controller,
+ const std::shared_ptr<IScene> &scene, const std::shared_ptr<IWindow> &window) noexcept
+ : _direction(direction),
+ _cursor_controller(cursor_controller),
+ _scene(scene),
+ _window(window)
+
+{
+}
+
+void MoveCursorCommand::execute() noexcept
+{
+ constexpr int32_t amount = 1;
+
+ const auto new_position =
+ _cursor_controller->where().to_direction(_direction, amount);
+
+ const auto window_size = _window->size();
+
+ if (window_size.validate_coords(new_position) != CoordsValidation::VALID)
+ {
+ return;
+ }
+
+ _cursor_controller->move_to(new_position);
+
+ _scene->write_status(fmt::format(STATUS_BAR_COORDINATES,
+ fmt::arg("x", new_position.get_x()),
+ fmt::arg("y", new_position.get_y())));
+}
diff --git a/src/commands/move_cursor.hpp b/src/commands/move_cursor.hpp
new file mode 100644
index 0000000..14a87e0
--- /dev/null
+++ b/src/commands/move_cursor.hpp
@@ -0,0 +1,27 @@
+#pragma once
+
+#include "interfaces/command.hpp"
+#include "interfaces/cursor.hpp"
+#include "interfaces/scene.hpp"
+#include "interfaces/window.hpp"
+
+#include "engine/data/vector2.hpp"
+
+#include <memory>
+
+class MoveCursorCommand : public ICommand
+{
+public:
+ MoveCursorCommand(const Vector2 &direction,
+ const std::shared_ptr<ICursorController> &cursor_controller,
+ const std::shared_ptr<IScene> &scene,
+ const std::shared_ptr<IWindow> &window) noexcept;
+
+ void execute() noexcept override;
+
+private:
+ Vector2 _direction;
+ const std::shared_ptr<ICursorController> &_cursor_controller;
+ const std::shared_ptr<IScene> &_scene;
+ const std::shared_ptr<IWindow> &_window;
+};
diff --git a/src/commands/quit.cpp b/src/commands/quit.cpp
new file mode 100644
index 0000000..c2a68e7
--- /dev/null
+++ b/src/commands/quit.cpp
@@ -0,0 +1,8 @@
+#include "quit.hpp"
+
+#include <cstdlib>
+
+void QuitCommand::execute() noexcept
+{
+ exit(EXIT_SUCCESS);
+}
diff --git a/src/commands/quit.hpp b/src/commands/quit.hpp
new file mode 100644
index 0000000..cb1aefd
--- /dev/null
+++ b/src/commands/quit.hpp
@@ -0,0 +1,9 @@
+#pragma once
+
+#include "interfaces/command.hpp"
+
+class QuitCommand : public ICommand
+{
+public:
+ void execute() noexcept override;
+};