summary refs log tree commit diff
path: root/src/Event.c
blob: 1c0d97694d45db3dfde538179a8750865ea969c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "Event.h"
#include "Logger.h"

int EventAPIVersion = 3;
struct _EntityEventsList        EntityEvents;
struct _TabListEventsList       TabListEvents;
struct _TextureEventsList       TextureEvents;
struct _GfxEventsList           GfxEvents;
struct _UserEventsList          UserEvents;
struct _BlockEventsList         BlockEvents;
struct _WorldEventsList         WorldEvents;
struct _ChatEventsList          ChatEvents;
struct _WindowEventsList        WindowEvents;
struct _InputEventsList         InputEvents;
struct _PointerEventsList       PointerEvents;
struct _ControllerEventsList    ControllerEvents;
struct _NetEventsList           NetEvents;

void Event_Register(struct Event_Void* handlers, void* obj, Event_Void_Callback handler) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		/* Attempting to register the same handler twice is usually caused by a bug */
		if (handlers->Handlers[i] == handler && handlers->Objs[i] == obj) {
			Logger_Abort("Attempt to register event handler that was already registered");
		}
	}

	if (handlers->Count == EVENT_MAX_CALLBACKS) {
		Logger_Abort("Unable to register another event handler");
	} else {
		handlers->Handlers[handlers->Count] = handler;
		handlers->Objs[handlers->Count]     = obj;
		handlers->Count++;
	}
}

void Event_Unregister(struct Event_Void* handlers, void* obj, Event_Void_Callback handler) {
	int i, j;
	for (i = 0; i < handlers->Count; i++) {
		if (handlers->Handlers[i] != handler || handlers->Objs[i] != obj) continue;

		/* Remove this handler from the list, by shifting all following handlers left */
		for (j = i; j < handlers->Count - 1; j++) {
			handlers->Handlers[j] = handlers->Handlers[j + 1];
			handlers->Objs[j]     = handlers->Objs[j + 1];
		}
		
		handlers->Count--;
		handlers->Handlers[handlers->Count] = NULL;
		handlers->Objs[handlers->Count]     = NULL;
		return;
	}
}

void Event_UnregisterAll(void) {
	/* NOTE: This MUST be kept in sync with Event.h list of events */
	EntityEvents.Added.Count   = 0;
	EntityEvents.Removed.Count = 0;

	TabListEvents.Added.Count   = 0;
	TabListEvents.Changed.Count = 0;
	TabListEvents.Removed.Count = 0;

	TextureEvents.AtlasChanged.Count = 0;
	TextureEvents.PackChanged.Count  = 0;
	TextureEvents.FileChanged.Count  = 0;

	GfxEvents.ViewDistanceChanged.Count = 0;
	GfxEvents.LowVRAMDetected.Count     = 0;
	GfxEvents.ProjectionChanged.Count   = 0;
	GfxEvents.ContextLost.Count         = 0;
	GfxEvents.ContextRecreated.Count    = 0;

	UserEvents.BlockChanged.Count     = 0;
	UserEvents.HackPermsChanged.Count = 0;
	UserEvents.HeldBlockChanged.Count = 0;

	BlockEvents.PermissionsChanged.Count = 0;
	BlockEvents.BlockDefChanged.Count    = 0;

	WorldEvents.NewMap.Count    = 0;
	WorldEvents.Loading.Count   = 0;
	WorldEvents.MapLoaded.Count = 0;
	WorldEvents.EnvVarChanged.Count = 0;
	WorldEvents.LightingModeChanged.Count = 0;

	ChatEvents.FontChanged.Count    = 0;
	ChatEvents.ChatReceived.Count   = 0;
	ChatEvents.ChatSending.Count    = 0;
	ChatEvents.ColCodeChanged.Count = 0;

	WindowEvents.RedrawNeeded.Count = 0;
	WindowEvents.Resized.Count = 0;
	WindowEvents.Closing.Count = 0;
	WindowEvents.FocusChanged.Count = 0;
	WindowEvents.StateChanged.Count = 0;
	WindowEvents.Created.Count      = 0;
	WindowEvents.InactiveChanged.Count = 0;
	WindowEvents.Redrawing.Count    = 0;

	InputEvents.Press.Count = 0;
	InputEvents.Down.Count  = 0;
	InputEvents.Up.Count    = 0;
	InputEvents.Wheel.Count = 0;
	InputEvents.TextChanged.Count = 0;

	PointerEvents.Moved.Count = 0;
	PointerEvents.Down.Count  = 0;
	PointerEvents.Up.Count    = 0;
	PointerEvents.RawMoved.Count = 0;
	
	ControllerEvents.AxisUpdate.Count = 0;

	NetEvents.Connected.Count    = 0;
	NetEvents.Disconnected.Count = 0;
	NetEvents.PluginMessageReceived.Count = 0;
}

void Event_RaiseVoid(struct Event_Void* handlers) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i]);
	}
}

void Event_RaiseInt(struct Event_Int* handlers, int arg) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], arg);
	}
}

void Event_RaiseFloat(struct Event_Float* handlers, float arg) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], arg);
	}
}

void Event_RaiseEntry(struct Event_Entry* handlers, struct Stream* stream, const cc_string* name) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], stream, name);
	}
}

void Event_RaiseBlock(struct Event_Block* handlers, IVec3 coords, BlockID oldBlock, BlockID block) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], coords, oldBlock, block);
	}
}

void Event_RaiseChat(struct Event_Chat* handlers, const cc_string* msg, int msgType) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], msg, msgType);
	}
}

void Event_RaiseInput(struct Event_Input* handlers, int key, cc_bool repeating) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], key, repeating);
	}
}

void Event_RaiseString(struct Event_String* handlers, const cc_string* str) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], str);
	}
}

void Event_RaiseRawMove(struct Event_RawMove* handlers, float xDelta, float yDelta) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], xDelta, yDelta);
	}
}

void Event_RaisePadAxis(struct Event_PadAxis* handlers, int port, int axis, float x, float y) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], port, axis, x, y);
	}
}

void Event_RaisePluginMessage(struct Event_PluginMessage* handlers, cc_uint8 channel, cc_uint8* data) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], channel, data);
	}
}

void Event_RaiseLightingMode(struct Event_LightingMode* handlers, cc_uint8 oldMode, cc_bool fromServer) {
	int i;
	for (i = 0; i < handlers->Count; i++) {
		handlers->Handlers[i](handlers->Objs[i], oldMode, fromServer);
	}
}