GigaPasjans
Loading...
Searching...
No Matches
ActionRegistry.h
1// Plik utworzony przez Piotra ChudziƄskiego w dniu 12.04.2025
2#pragma once
3
4#include <foundation/Common.h>
5#include <foundation/WindowsKeycode.h>
6#include <functional>
7#include <unordered_map>
8#include <utility>
9#include "DrawContext.h"
10#include "foundation/KeyToName.h"
11
12namespace Andromenda
13{
18 {
22 int32 Key;
23
27 MouseButton Button;
28
29 bool operator==(const ActionInputMapping &rhs) const { return Key == rhs.Key; }
30 };
31} // namespace Andromenda
32
36template<>
37struct std::hash<Andromenda::ActionInputMapping>
38{
39 std::size_t operator()(const Andromenda::ActionInputMapping &k) const noexcept { return k.Key; }
40};
41
42namespace Andromenda
43{
44 using Callback = std::function<void()>;
45
50 struct Action
51 {
57
62 Callback OnAction;
63
64 explicit Action() : Text(L"") {}
65
66 explicit Action(const WideString &text, const Callback &callback) : Text(text), OnAction(callback) {}
67 };
68
73 class ActionRegistry
74 {
75 private:
80 std::unordered_map<ActionInputMapping, Action> m_ActionMap;
81
85 const Action *getByMouseButton(MouseButton button);
86
87 public:
88 ActionRegistry() = default;
89
94 void Register(const Keycode &keycode, const WideString &actionText, const Callback &callback,
95 MouseButton button = MouseButton::None);
96
101 void Clear();
102
108 void Handle(const int32 &key) const;
109
113 void HandleMouse(const MouseButton &button);
114
119 void DrawActionsAt(DrawContext &context, const Int2 &position) const;
120 };
121} // namespace Andromenda
void DrawActionsAt(DrawContext &context, const Int2 &position) const
Draws helper text with availible actions and corresponding keycode.
Definition ActionRegistry.cpp:47
void Register(const Keycode &keycode, const WideString &actionText, const Callback &callback, MouseButton button=MouseButton::None)
Registers given text and callback to given key.
Definition ActionRegistry.cpp:18
void Handle(const int32 &key) const
Lookups action with given key and calls action's callback.
Definition ActionRegistry.cpp:29
void HandleMouse(const MouseButton &button)
Lookups action with given mouse button and calls action's callback.
Definition ActionRegistry.cpp:38
void Clear()
Deletes all actions.
Definition ActionRegistry.cpp:27
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14
Represents key to callback named object.
Definition ActionRegistry.h:51
Callback OnAction
Callback that will be called when action is performed.
Definition ActionRegistry.h:62
WideString Text
Action text.
Definition ActionRegistry.h:56
Represents action mapping for keyboard bind and mouse button as well.
Definition ActionRegistry.h:18
MouseButton Button
Mouse bindings.
Definition ActionRegistry.h:27
int32 Key
Keyboard binding.
Definition ActionRegistry.h:22
A utility which contains all essential class instances and functions for rendering objects.
Definition DrawContext.h:14
Represents a two component vector. Components are 32-bit and they are signed.
Definition Int2.h:11