Tramway SDK
event.h
Go to the documentation of this file.
1// TRAMWAY DRIFT AND DUNGEON EXPLORATION SIMULATOR 2022
2// All rights reserved.
3
4#ifndef TRAM_SDK_FRAMEWORK_EVENT_H
5#define TRAM_SDK_FRAMEWORK_EVENT_H
6
7#include <cstdint>
8#include <cstddef>
9#include <type_traits>
10
11#include <framework/uid.h>
12
13namespace tram {
14
15typedef uint16_t event_t;
16typedef uint32_t id_t;
17typedef uint32_t listener_t;
18
19class EntityComponent;
20class Entity;
21
22struct Value;
23
24struct Event {
25 enum Type : event_t {
26 KEYPRESS, // keyboard key still pressed
27 KEYDOWN, // keyboard key pressed
28 KEYUP, // keyboard key released
29 KEYCHAR, // keyboard input registered
30 CURSORPOS, // cursor moved
31 TICK, // emitted every tick
32 SELECTED, // cursor on top of an interactable object
33 LOOK_AT, // entity is looking in a direction
35 };
36
37 static event_t Register(const char* name);
38 static event_t GetType(name_t name);
39 static name_t GetName(event_t type);
40 static event_t GetLast();
41 static void Post (const Event &event);
42 static void Dispatch();
44 static listener_t AddListener(event_t type, Entity* entity);
45 static listener_t AddListener(event_t type, void* data, void (*handler)(Event& event, void* data));
46 static listener_t AddListener(event_t type, void (*handler)(Event& event));
47 static void RemoveListener(listener_t listener_id);
48 static void* AllocateData(size_t ammount);
49 template <typename T> static T* AllocateData() { return (T*)AllocateData(sizeof(T)); }
50 template <typename T> static T* AllocateData(const T& data) { T* ptr = (T*)AllocateData(sizeof(T)); *ptr = data; return ptr; }
51
55
56 union {
57 void* data = nullptr;
58 uint64_t data_int;
60 };
61};
62
65public:
67 void make(event_t event, Entity* parent);
68 void make(event_t event, EntityComponent* parent);
69 void clear();
70 explicit operator bool() { return listener != 0; }
71protected:
73};
74
75}
76
77#endif // TRAM_SDK_FRAMEWORK_EVENT_H
Definition: entitycomponent.h:18
Definition: entity.h:20
Smart wrapper class for Event listeners.
Definition: event.h:64
~EventListener()
Definition: event.cpp:222
listener_t listener
Definition: event.h:72
void make(event_t event, Entity *parent)
Definition: event.cpp:226
void clear()
Definition: event.cpp:236
Generic, type-safe union.
Definition: value.h:18
Definition: api.h:9
uint16_t event_t
Definition: event.h:15
uint32_t id_t
Definition: core.h:11
uint32_t listener_t
Definition: event.h:17
Definition: event.h:24
static void RemoveListener(listener_t listener_id)
Deregisters a listener.
Definition: event.cpp:155
static T * AllocateData()
Definition: event.h:49
static event_t GetLast()
Definition: event.cpp:87
static void Dispatch()
Dispatches events from the event queue.
Definition: event.cpp:181
static void Post(const Event &event)
Adds an event to the event queue.
Definition: event.cpp:209
event_t subtype
Definition: event.h:53
void * data
Definition: event.h:57
Type
Definition: event.h:25
@ TICK
Definition: event.h:31
@ KEYCHAR
Definition: event.h:29
@ KEYUP
Definition: event.h:28
@ LAST_EVENT
Definition: event.h:34
@ CURSORPOS
Definition: event.h:30
@ SELECTED
Definition: event.h:32
@ KEYDOWN
Definition: event.h:27
@ LOOK_AT
Definition: event.h:33
@ KEYPRESS
Definition: event.h:26
event_t type
Definition: event.h:52
static event_t Register(const char *name)
Registers a new event type.
Definition: event.cpp:59
Value * data_value
Definition: event.h:59
uint64_t data_int
Definition: event.h:58
static T * AllocateData(const T &data)
Definition: event.h:50
static name_t GetName(event_t type)
Definition: event.cpp:83
static listener_t AddListener(event_t type, EntityComponent *component)
Registers a listener.
Definition: event.cpp:102
static event_t GetType(name_t name)
Definition: event.cpp:68
id_t poster_id
Definition: event.h:54
Definition: uid.h:11