Cogs.Foundation
Loading...
Searching...
No Matches
Keyboard.h
Go to the documentation of this file.
1#pragma once
2#include "../FoundationBase.h"
3
4#include <variant>
5#include <vector>
6#include <string>
7
8namespace Cogs
9{
10 enum class Key
11 {
12 A, B, C, D, E,
13 F, G, H, I, J,
14 K, L, M, N, O,
15 P, Q, R, S, T,
16 U, V, W, X, Y,
17 Z,
18
19 Zero, One, Two, Three, Four,
21
22 Left, Right, Up, Down,
23
27
29 Tab,
30 Escape,
31 Enter,
32 Space,
33
35 Home, End,
37
38 F1, F2, F3, F4,
39 F5, F6, F7, F8,
40 F9, F10, F11, F12,
41
42 Count,
43 None = -1,
44 };
45
47 {
48 public:
49 struct State
50 {
51 bool keysDown[static_cast<size_t>(Key::Count)] = {};
52 std::string chars;
53 };
54
55 struct Event {
56 enum class Type {
57 Press,
58 Release,
59 AddChar,
60 Reset
61 };
62
64 double timestamp_ms;
65
67 std::variant<Key, std::string> data;
68 };
69
70 void update();
71
72 void submitKeyDown(Key key, double timestamp);
73 void submitKeyUp(Key key, double timestamp);
74 void submitChar(std::string ch, double timestamp);
75 void submitReset(double timestamp);
76
77 // Convenience getters.
78 bool isKeyDown(Key key) const { return state.keysDown[static_cast<size_t>(key)]; }
79 bool isKeyUp(Key key) const { return !isKeyDown(key); }
80 bool hasChar(std::string_view ch) const { return state.chars.find(ch) != std::string::npos; }
81 bool wasKeyPressed(Key key) const;
82 bool wasKeyReleased(Key key) const;
83
84 // Getters for the full state and events.
86 const State& getState() const { return state; }
88 const std::vector<Event>& getEvents() const { return events; }
89
90 private:
92 std::vector<Event> events;
93
94 std::vector<Event> eventQueue;
95 };
96}
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Definition: Keyboard.h:47
bool isKeyDown(Key key) const
Definition: Keyboard.h:78
std::vector< Event > eventQueue
Definition: Keyboard.h:94
const State & getState() const
Get the previous frame's keyboard state. Updated at the beginning of each new frame.
Definition: Keyboard.h:86
std::vector< Event > events
Definition: Keyboard.h:92
const std::vector< Event > & getEvents() const
Get the previous frame's event list. Updated at the beginning of each new frame.
Definition: Keyboard.h:88
bool isKeyUp(Key key) const
Definition: Keyboard.h:79
bool hasChar(std::string_view ch) const
Definition: Keyboard.h:80
State state
Definition: Keyboard.h:91
Main Cogs namespace.
Definition: MortonCode.h:5
@ Left
Definition: Mouse.h:16
@ Right
Definition: Mouse.h:17
@ Count
Definition: Mouse.h:20
Key
Definition: Keyboard.h:11
Definition: Keyboard.h:55
Type
Definition: Keyboard.h:56
std::variant< Key, std::string > data
Data associated with the event: Key for Press and Release events, std::string for AddChar events and ...
Definition: Keyboard.h:67
Type type
Event type.
Definition: Keyboard.h:63
double timestamp_ms
Timestamp in miliseconds.
Definition: Keyboard.h:64
Definition: Keyboard.h:50
std::string chars
List of characters generated by the keyboard this frame.
Definition: Keyboard.h:52