vOOlkan
An object oriented approach to Vulkan
KeyboardListener.h
Go to the documentation of this file.
1#ifndef VULKAN_KEYBOARDLISTENER
2#define VULKAN_KEYBOARDLISTENER
3
4#include <vector>
5#include <string>
6#include <functional>
7
8#include "Window.h"
9
10
12
14 public:
15 virtual void onKeyPress(int keyPressed) = 0;
16 };
17
18
20 public:
21 ConcreteKeyboardObserver(std::function<void(int)> action) : reactToKeyPress{ action }{}
22
23 void onKeyPress(int keyPressed) override {
24 reactToKeyPress(keyPressed);
25 }
26
27 private:
28 std::function<void(int)> reactToKeyPress;
29 };
30
31
33 public:
34 template<std::derived_from<KeyboardObserver>... Listeners>
35 KeyboardListener(const Window& window, Listeners&... listeners) : window{ window } {
36 (this->listeners.push_back(&listeners), ...);
37 }
38
39
41 for (int key = 32; key <= 96; ++key) {
42 if (glfwGetKey(+window, key)) {
43 notifyKeyPress(key);
44 }
45 }
46 for (int key = 256; key <= 348; ++key) {
47 if (glfwGetKey(+window, key)) {
48 notifyKeyPress(key);
49 }
50 }
51 }
52
53
54 private:
55
56 void notifyKeyPress(int keyPressed) {
57 for (auto& listener : listeners) {
58 listener->onKeyPress(keyPressed);
59 }
60 }
61
62
63 std::vector<KeyboardObserver*> listeners;
64 const Window& window;
65 };
66
67}
68
69
70#endif
Definition: KeyboardListener.h:19
ConcreteKeyboardObserver(std::function< void(int)> action)
Definition: KeyboardListener.h:21
void onKeyPress(int keyPressed) override
Definition: KeyboardListener.h:23
Definition: KeyboardListener.h:32
void checkKeyPressed()
Definition: KeyboardListener.h:40
KeyboardListener(const Window &window, Listeners &... listeners)
Definition: KeyboardListener.h:35
Definition: KeyboardListener.h:13
virtual void onKeyPress(int keyPressed)=0
Manages the creation and lifetime of an OS window.
Definition: Window.h:15
Definition: KeyboardListener.h:11