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 using FileDropCallBack = std::function<void(Window* wnd, const std::vector<std::string>& files)>;
20
26 using CallbackId = size_t;
27
28 void onResize(Window* wnd, int width, int height) override {
29 for (const auto& pair : m_resizeCallbacks) {
30 pair.second(wnd, width, height);
31 }
32 }
33 void onMouseMove(Window* wnd, int x, int y) override {
34 for (const auto& pair : m_mouseMoveCallbacks) {
35 pair.second(wnd, x, y);
36 }
37 }
38 void onMouseWheel(Window* wnd, int delta) override {
39 for (const auto& pair : m_mouseWheelCallbacks) {
40 pair.second(wnd, delta);
41 }
42 }
43 void onKeyDown(Window* wnd, int key) override {
44 for (const auto& pair : m_keyDownCallbacks) {
45 pair.second(wnd, key);
46 }
47 }
48 void onKeyUp(Window* wnd, int key) override {
49 for (const auto& pair : m_keyUpCallbacks) {
50 pair.second(wnd, key);
51 }
52 }
53 void onLButtonDown(Window* wnd, int x, int y) override {
54 for (const auto& pair : m_lButtonDownCallbacks) {
55 pair.second(wnd, x, y);
56 }
57 }
58 void onLButtonUp(Window* wnd, int x, int y) override {
59 for (const auto& pair : m_lButtonUpCallbacks) {
60 pair.second(wnd, x, y);
61 }
62 }
63 void onRButtonDown(Window* wnd, int x, int y) override {
64 for (const auto& pair : m_rButtonDownCallbacks) {
65 pair.second(wnd, x, y);
66 }
67 }
68 void onRButtonUp(Window* wnd, int x, int y) override {
69 for (const auto& pair : m_rButtonUpCallbacks) {
70 pair.second(wnd, x, y);
71 }
72 }
73
74 void onFileDrop(Window* wnd, const std::vector<std::string>& files) override
75 {
76 for (const auto& pair : m_fileDropCallbacks) {
77 pair.second(wnd, files);
78 }
79 }
80
82 CallbackId id = m_nextId++;
83 m_resizeCallbacks[id] = cb;
84 return id;
85 }
86
88 CallbackId id = m_nextId++;
90 return id;
91 }
93 CallbackId id = m_nextId++;
94 m_lButtonUpCallbacks[id] = cb;
95 return id;
96 }
98 CallbackId id = m_nextId++;
100 return id;
101 }
103 CallbackId id = m_nextId++;
104 m_rButtonUpCallbacks[id] = cb;
105 return id;
106 }
108 CallbackId id = m_nextId++;
109 m_mouseMoveCallbacks[id] = cb;
110 return id;
111 }
113 CallbackId id = m_nextId++;
114 m_mouseWheelCallbacks[id] = cb;
115 return id;
116 }
118 CallbackId id = m_nextId++;
119 m_keyDownCallbacks[id] = cb;
120 return id;
121 }
123 CallbackId id = m_nextId++;
124 m_keyUpCallbacks[id] = cb;
125 return id;
126 }
128 CallbackId id = m_nextId++;
129 m_fileDropCallbacks[id] = cb;
130 return id;
131 }
133 m_resizeCallbacks.erase(id);
134 }
135
155 m_keyDownCallbacks.erase(id);
156 }
158 m_keyUpCallbacks.erase(id);
159 }
163
165 m_resizeCallbacks.clear();
166 }
186 m_keyDownCallbacks.clear();
187 }
189 m_keyUpCallbacks.clear();
190 }
192 m_fileDropCallbacks.clear();
193 }
194 void invokeResizeCallbacks(Window* wnd, int width, int height) {
195 onResize(wnd, width, height);
196 }
197
198 private:
199 std::unordered_map<CallbackId, ResizeCallBack> m_resizeCallbacks;
200 std::unordered_map<CallbackId, LButtonDownCallBack> m_lButtonDownCallbacks;
201 std::unordered_map<CallbackId, LButtonUpCallBack> m_lButtonUpCallbacks;
202 std::unordered_map<CallbackId, RButtonDownCallBack> m_rButtonDownCallbacks;
203 std::unordered_map<CallbackId, RButtonUpCallBack> m_rButtonUpCallbacks;
204 std::unordered_map<CallbackId, MouseMoveCallBack> m_mouseMoveCallbacks;
205 std::unordered_map<CallbackId, MouseWheelCallBack> m_mouseWheelCallbacks;
206 std::unordered_map<CallbackId, KeyDownCallBack> m_keyDownCallbacks;
207 std::unordered_map<CallbackId, KeyUpCallBack> m_keyUpCallbacks;
208 std::unordered_map<CallbackId, FileDropCallBack> m_fileDropCallbacks;
210 };
211}
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
std::unordered_map< CallbackId, FileDropCallBack > m_fileDropCallbacks
std::function< void(Window *wnd, const std::vector< std::string > &files)> FileDropCallBack
void onFileDrop(Window *wnd, const std::vector< std::string > &files) override
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)
void removeFileDropCallback(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
CallbackId addFileDropCallback(const FileDropCallBack &cb)
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