Skip to content

Commit

Permalink
Finalize input code
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgallivan committed May 24, 2024
1 parent 73eb996 commit 157646c
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 117 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ set(ENGINE_SOURCES
engine/graphics/graphics.cpp
engine/graphics/sprite.cpp
engine/image/image.cpp
engine/input/input.cpp
engine/input/input_manager.cpp
engine/input/input_profile.cpp
engine/log/log.cpp
Expand Down
27 changes: 0 additions & 27 deletions engine/input/input.cpp

This file was deleted.

89 changes: 0 additions & 89 deletions engine/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,3 @@
#include "input_event.h"
#include "input_manager.h"
#include "input_profile.h"

#include "log.h"

#include <functional>
#include <iostream>
#include <unordered_map>
#include <vector>

namespace Cairn {
namespace Input {

/** An input action is a key press, release, or hold. */
enum class Action {
Release = 0,
Press = 1,
Hold = 2,
};

/** A mapping from an action to a string representation. */
static std::unordered_map<Action, std::string> action_to_string = {
{Action::Press, "Press"},
{Action::Release, "Release"},
{Action::Hold, "Hold"},
};

/** An input key is a key on the keyboard. */
enum class Key {
Esc = 256,
W = 87,
A = 65,
S = 83,
D = 68,
};

/** A mapping from a key to a string representation. */
static std::unordered_map<Key, std::string> key_to_string = {
{Key::Esc, "Esc"}, {Key::W, "W"}, {Key::A, "A"}, {Key::S, "S"}, {Key::D, "D"},
};

/** A callback is a function that is called when an input event occurs. */
using Callback = std::function<void()>;

/** A profile is a collection of bindings and their associated callbacks. */
class Profile {

public:
void bind(Key key, Action action, Callback callback);

void fire(Key key, Action action);

void set_debug(bool should_debug) { this->should_debug = should_debug; }

private:
std::unordered_map<Key, std::unordered_map<Action, std::vector<Callback>>> key_callbacks;

bool should_debug = false;
};

class Manager {

public:
void add(Profile* profile) {
if (!profile) {
Log::error(Log::Category::Input, "Profile is null");
return;
}

profiles.push_back(profile);
}

void fire(int key, int action) {
for (auto& profile : profiles) {
profile->fire(static_cast<Key>(key), static_cast<Action>(action));
}
}

void fire_mouse(double x_pos, double y_pos) {
Log::info(Log::Category::Input, "Fire (" + std::to_string(x_pos) + ", " + std::to_string(y_pos) + ")");

// TODO: Fire a porfile?
}

private:
std::vector<Profile*> profiles;
};

} // namespace Input

} // namespace Cairn
1 change: 1 addition & 0 deletions engine/input/input_action.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Cairn {

/** An input action is a callback that is called when an input event is received. */
using InputAction = std::function<void(const InputEvent&)>;

} // namespace Cairn

0 comments on commit 157646c

Please sign in to comment.