FCT
载入中...
搜索中...
未找到
CallBackHandler.h
浏览该文件的文档.
1#pragma once
2#include "./EventHandler.h"
3#include <vector>
4#include <functional>
5#include <unordered_map>
6
7namespace FCT {
8 using ResizeCallBack = std::function<void(Window* wnd, int width, int height)>;
10 public:
11 using LButtonDownCallBack = std::function<void(Window* wnd, int x, int y)>;
12 using LButtonUpCallBack = std::function<void(Window* wnd, int x, int y)>;
13 using RButtonDownCallBack = std::function<void(Window* wnd, int x, int y)>;
14 using RButtonUpCallBack = std::function<void(Window* wnd, int x, int y)>;
15 using MouseMoveCallBack = std::function<void(Window* wnd, int x, int y)>;
16 using MouseWheelCallBack = std::function<void(Window* wnd, int delta)>;
17 using KeyDownCallBack = std::function<void(Window* wnd, int key)>;
18 using KeyUpCallBack = std::function<void(Window* wnd, int key)>;
19
25 using CallbackId = size_t;
26
27 void onResize(Window* wnd, int width, int height) override {
28 for (const auto& pair : m_resizeCallbacks) {
29 pair.second(wnd, width, height);
30 }
31 }
32 void onMouseMove(Window* wnd, int x, int y) override {
33 for (const auto& pair : m_mouseMoveCallbacks) {
34 pair.second(wnd, x, y);
35 }
36 }
37 void onMouseWheel(Window* wnd, int delta) override {
38 for (const auto& pair : m_mouseWheelCallbacks) {
39 pair.second(wnd, delta);
40 }
41 }
42 void onKeyDown(Window* wnd, int key) override {
43 for (const auto& pair : m_keyDownCallbacks) {
44 pair.second(wnd, key);
45 }
46 }
47 void onKeyUp(Window* wnd, int key) override {
48 for (const auto& pair : m_keyUpCallbacks) {
49 pair.second(wnd, key);
50 }
51 }
52 void onLButtonDown(Window* wnd, int x, int y) override {
53 for (const auto& pair : m_lButtonDownCallbacks) {
54 pair.second(wnd, x, y);
55 }
56 }
57 void onLButtonUp(Window* wnd, int x, int y) override {
58 for (const auto& pair : m_lButtonUpCallbacks) {
59 pair.second(wnd, x, y);
60 }
61 }
62 void onRButtonDown(Window* wnd, int x, int y) override {
63 for (const auto& pair : m_rButtonDownCallbacks) {
64 pair.second(wnd, x, y);
65 }
66 }
67 void onRButtonUp(Window* wnd, int x, int y) override {
68 for (const auto& pair : m_rButtonUpCallbacks) {
69 pair.second(wnd, x, y);
70 }
71 }
72
74 CallbackId id = m_nextId++;
75 m_resizeCallbacks[id] = cb;
76 return id;
77 }
78
80 CallbackId id = m_nextId++;
82 return id;
83 }
85 CallbackId id = m_nextId++;
86 m_lButtonUpCallbacks[id] = cb;
87 return id;
88 }
90 CallbackId id = m_nextId++;
92 return id;
93 }
95 CallbackId id = m_nextId++;
96 m_rButtonUpCallbacks[id] = cb;
97 return id;
98 }
100 CallbackId id = m_nextId++;
101 m_mouseMoveCallbacks[id] = cb;
102 return id;
103 }
105 CallbackId id = m_nextId++;
106 m_mouseWheelCallbacks[id] = cb;
107 return id;
108 }
110 CallbackId id = m_nextId++;
111 m_keyDownCallbacks[id] = cb;
112 return id;
113 }
115 CallbackId id = m_nextId++;
116 m_keyUpCallbacks[id] = cb;
117 return id;
118 }
120 m_resizeCallbacks.erase(id);
121 }
122
142 m_keyDownCallbacks.erase(id);
143 }
145 m_keyUpCallbacks.erase(id);
146 }
147
149 m_resizeCallbacks.clear();
150 }
170 m_keyDownCallbacks.clear();
171 }
173 m_keyUpCallbacks.clear();
174 }
175 void invokeResizeCallbacks(Window* wnd, int width, int height) {
176 onResize(wnd, width, height);
177 }
178
179 private:
180 std::unordered_map<CallbackId, ResizeCallBack> m_resizeCallbacks;
181 std::unordered_map<CallbackId, LButtonDownCallBack> m_lButtonDownCallbacks;
182 std::unordered_map<CallbackId, LButtonUpCallBack> m_lButtonUpCallbacks;
183 std::unordered_map<CallbackId, RButtonDownCallBack> m_rButtonDownCallbacks;
184 std::unordered_map<CallbackId, RButtonUpCallBack> m_rButtonUpCallbacks;
185 std::unordered_map<CallbackId, MouseMoveCallBack> m_mouseMoveCallbacks;
186 std::unordered_map<CallbackId, MouseWheelCallBack> m_mouseWheelCallbacks;
187 std::unordered_map<CallbackId, KeyDownCallBack> m_keyDownCallbacks;
188 std::unordered_map<CallbackId, KeyUpCallBack> m_keyUpCallbacks;
190 };
191}
void removeRButtonUpCallback(CallbackId id)
std::function< void(Window *wnd, int x, int y)> MouseMoveCallBack
void removeResizeCallback(CallbackId id)
CallbackId addLButtonUpCallback(const LButtonUpCallBack &cb)
void invokeResizeCallbacks(Window *wnd, int width, int height)
std::unordered_map< CallbackId, MouseMoveCallBack > m_mouseMoveCallbacks
std::unordered_map< CallbackId, RButtonDownCallBack > m_rButtonDownCallbacks
CallbackId addMouseMoveCallback(const MouseMoveCallBack &cb)
CallbackId addResizeCallback(const ResizeCallBack &cb)
std::unordered_map< CallbackId, RButtonUpCallBack > m_rButtonUpCallbacks
void onRButtonDown(Window *wnd, int x, int y) override
void onLButtonDown(Window *wnd, int x, int y) override
std::function< void(Window *wnd, int x, int y)> RButtonDownCallBack
std::function< void(Window *wnd, int x, int y)> RButtonUpCallBack
std::function< void(Window *wnd, int delta)> MouseWheelCallBack
void onKeyDown(Window *wnd, int key) override
void removeMouseWheelCallback(CallbackId id)
std::function< void(Window *wnd, int x, int y)> LButtonDownCallBack
void onLButtonUp(Window *wnd, int x, int y) override
CallbackId addLButtonDownCallback(const LButtonDownCallBack &cb)
std::function< void(Window *wnd, int x, int y)> LButtonUpCallBack
std::unordered_map< CallbackId, LButtonDownCallBack > m_lButtonDownCallbacks
std::unordered_map< CallbackId, MouseWheelCallBack > m_mouseWheelCallbacks
void removeRButtonDownCallback(CallbackId id)
std::unordered_map< CallbackId, LButtonUpCallBack > m_lButtonUpCallbacks
void onMouseWheel(Window *wnd, int delta) override
void removeKeyUpCallback(CallbackId id)
CallbackId addKeyUpCallback(const KeyUpCallBack &cb)
std::function< void(Window *wnd, int key)> KeyDownCallBack
CallbackId addRButtonUpCallback(const RButtonUpCallBack &cb)
void removeMouseMoveCallback(CallbackId id)
std::unordered_map< CallbackId, ResizeCallBack > m_resizeCallbacks
std::unordered_map< CallbackId, KeyDownCallBack > m_keyDownCallbacks
void removeLButtonDownCallback(CallbackId id)
void removeKeyDownCallback(CallbackId id)
void onKeyUp(Window *wnd, int key) override
CallbackId addRButtonDownCallback(const RButtonDownCallBack &cb)
void onResize(Window *wnd, int width, int height) override
std::unordered_map< CallbackId, KeyUpCallBack > m_keyUpCallbacks
void onMouseMove(Window *wnd, int x, int y) override
std::function< void(Window *wnd, int key)> KeyUpCallBack
void onRButtonUp(Window *wnd, int x, int y) override
CallbackId addMouseWheelCallback(const MouseWheelCallBack &cb)
CallbackId addKeyDownCallback(const KeyDownCallBack &cb)
void removeLButtonUpCallback(CallbackId id)
std::function< void(Window *wnd, int width, int height)> ResizeCallBack