PixelBullet  0.0.1
A C++ game engine
Loading...
Searching...
No Matches
key_events.h
1#pragma once
2
3#include "pixelbullet/core/event.h"
4#include "pixelbullet/input/key_codes.h"
5
6#include <sstream>
7#include <string>
8
9namespace pixelbullet::input
10{
11class KeyEvent : public core::Event
12{
13public:
14 [[nodiscard]] KeyCode key_code() const
15 {
16 return key_code_;
17 }
18
19 PB_EVENT_CATEGORIES(core::EventCategory::Keyboard | core::EventCategory::Input)
20
21protected:
22 explicit KeyEvent(KeyCode keycode)
23 : key_code_(keycode)
24 {
25 }
26
27 KeyCode key_code_;
28};
29
30class KeyPressedEvent : public KeyEvent
31{
32public:
33 KeyPressedEvent(KeyCode keycode, bool isRepeat = false)
34 : KeyEvent(keycode)
35 , is_repeat_(isRepeat)
36 {
37 }
38
39 [[nodiscard]] bool is_repeat() const
40 {
41 return is_repeat_;
42 }
43
44 [[nodiscard]] std::string to_string() const override
45 {
46 std::stringstream ss;
47 ss << "KeyPressedEvent: " << key_code_ << " (repeat = " << is_repeat_ << ")";
48 return ss.str();
49 }
50
51 PB_EVENT_TYPE(KeyPressed)
52
53private:
54 bool is_repeat_;
55};
56
57class KeyReleasedEvent : public KeyEvent
58{
59public:
60 explicit KeyReleasedEvent(KeyCode keycode)
61 : KeyEvent(keycode)
62 {
63 }
64
65 [[nodiscard]] std::string to_string() const override
66 {
67 std::stringstream ss;
68 ss << "KeyReleasedEvent: " << key_code_;
69 return ss.str();
70 }
71
72 PB_EVENT_TYPE(KeyReleased)
73};
74
75} // namespace pixelbullet::input
Definition event.h:108