-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEventManager.hpp
114 lines (96 loc) · 2.91 KB
/
EventManager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <vector>
#include <unordered_map>
#include <functional>
#include <fstream>
#include <sstream>
#include <iostream>
enum class EventType {
KeyDown = sf::Event::KeyPressed,
KeyUp = sf::Event::KeyReleased,
MButtonDown = sf::Event::MouseButtonPressed,
MButtonUp = sf::Event::MouseButtonReleased,
MouseWheel = sf::Event::MouseWheelMoved,
WindowResized = sf::Event::Resized,
GainedFocus = sf::Event::GainedFocus,
LostFocus = sf::Event::LostFocus,
MouseEntered = sf::Event::MouseEntered,
MouseLeft = sf::Event::MouseLeft,
Closed = sf::Event::Closed,
TextEntered = sf::Event::TextEntered,
Keyboard = sf::Event::Count + 1, Mouse, Joystick
};
struct EventInfo {
EventInfo() { m_code = 0; }
EventInfo(int l_event) { m_code = l_event; }
union {
int m_code;
};
};
using Events = std::vector<std::pair<EventType, EventInfo>>;
struct EventDetails {
EventDetails(const std::string& l_bindName) : m_name(l_bindName) {
Clear();
}
std::string m_name;
sf::Vector2i m_size;
sf::Uint32 m_textEntered;
sf::Vector2i m_mouse;
int m_mouseWheelDelta;
int m_keyCode;
void Clear() {
m_size = sf::Vector2i(0, 0);
m_textEntered = 0;
m_mouse = sf::Vector2i(0, 0);
m_mouseWheelDelta = 0;
m_keyCode = -1;
}
};
struct Binding {
Binding(const std::string& l_name) : m_name(l_name), c(0), m_details(l_name) {}
void BindEvent(EventType l_type, EventInfo l_info = EventInfo()) {
m_events.emplace_back(l_type, l_info);
}
Events m_events;
std::string m_name;
int c;
EventDetails m_details;
};
using Bindings = std::unordered_map<std::string, Binding*>;
using Callbacks = std::unordered_map<std::string, std::function<void(EventDetails*)>>;
class EventManager {
public:
EventManager();
~EventManager();
bool AddBinding(Binding* l_binding);
bool RemoveBinding(std::string l_name);
void setFocus(const bool& l_focus);
template<class T>
bool AddCallback(
const std::string& l_name,
void(T::*l_func)(EventDetails*),
T* l_instance
){
auto temp = std::bind(l_func, l_instance, std::placeholders::_1);
return m_callbacks.emplace(l_name, temp).second;
}
void RemoveCallback(const std::string& l_name) {
m_callbacks.erase(l_name);
}
void HandleEvent(sf::Event& l_event);
void Update();
sf::Vector2i GetMousePos(sf::RenderWindow* l_wind = nullptr) {
return (
l_wind ?
sf::Mouse::getPosition(*l_wind) :
sf::Mouse::getPosition()
);
}
private:
void LoadBindings();
Bindings m_bindings;
Callbacks m_callbacks;
bool m_hasFocus;
};