1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_COMPONENT_H
4#define GAME_CLIENT_COMPONENT_H
5
6#if defined(CONF_VIDEORECORDER)
7#include <engine/shared/video.h>
8#endif
9
10#include <engine/input.h>
11
12class CGameClient;
13
14class CComponentInterfaces
15{
16public:
17 virtual void OnInterfacesInit(CGameClient *pClient);
18 virtual ~CComponentInterfaces() = default;
19
20protected:
21 /**
22 * Get a pointer to the game client.
23 */
24 class CGameClient *GameClient() const { return m_pClient; }
25
26 /**
27 * Get the client interface.
28 */
29 class IClient *Client() const;
30
31 /**
32 * Get the kernel interface.
33 */
34 class IKernel *Kernel() const;
35
36 /**
37 * Get the engine interface.
38 */
39 class IEngine *Engine() const;
40
41 /**
42 * Get the graphics interface.
43 */
44 class IGraphics *Graphics() const;
45
46 /**
47 * Get the text rendering interface.
48 */
49 class ITextRender *TextRender() const;
50
51 /**
52 * Get the input interface.
53 */
54 class IInput *Input() const;
55
56 /**
57 * Get the storage interface.
58 */
59 class IStorage *Storage() const;
60
61 /**
62 * Get the ui interface.
63 */
64 class CUi *Ui() const;
65
66 /**
67 * Get the sound interface.
68 */
69 class ISound *Sound() const;
70
71 /**
72 * Get the render tools interface.
73 */
74 class CRenderTools *RenderTools() const;
75
76 /**
77 * Get the render map interface.
78 */
79 class CRenderMap *RenderMap() const;
80
81 /**
82 * Get the config manager interface.
83 */
84 class IConfigManager *ConfigManager() const;
85
86 /**
87 * Get the config interface.
88 */
89 class CConfig *Config() const;
90
91 /**
92 * Get the console interface.
93 */
94 class IConsole *Console() const;
95
96 /**
97 * Get the demo player interface.
98 */
99 class IDemoPlayer *DemoPlayer() const;
100
101 /**
102 * Get the demo recorder interface.
103 *
104 * @param Recorder A member of the RECORDER_x enum
105 * @see RECORDER_MANUAL
106 * @see RECORDER_AUTO
107 * @see RECORDER_RACE
108 * @see RECORDER_REPLAYS
109 */
110 class IDemoRecorder *DemoRecorder(int Recorder) const;
111
112 /**
113 * Get the favorites interface.
114 */
115 class IFavorites *Favorites() const;
116
117 /**
118 * Get the server browser interface.
119 */
120 class IServerBrowser *ServerBrowser() const;
121
122 /**
123 * Get the layers interface.
124 */
125 class CLayers *Layers() const;
126
127 /**
128 * Get the collision interface.
129 */
130 class CCollision *Collision() const;
131
132#if defined(CONF_AUTOUPDATE)
133 /**
134 * Get the updater interface.
135 */
136 class IUpdater *Updater() const;
137#endif
138
139 /**
140 * Gets the current time.
141 * @see time_get()
142 */
143 int64_t time() const;
144
145 /**
146 * Gets the local time.
147 */
148 float LocalTime() const;
149
150 /**
151 * Get the HTTP interface
152 */
153 class IHttp *Http() const;
154
155private:
156 CGameClient *m_pClient = nullptr;
157};
158
159/**
160 * This class is inherited by all the client components.
161 *
162 * These components can implement the virtual methods such as OnInit(), OnMessage(int Msg, void *pRawMsg) to provide their functionality.
163 */
164class CComponent : public CComponentInterfaces
165{
166public:
167 /**
168 * Gets the size of the non-abstract component.
169 */
170 virtual int Sizeof() const = 0;
171
172 /**
173 * This method is called when the client changes state, e.g from offline to online.
174 * @see IClient::STATE_CONNECTING
175 * @see IClient::STATE_LOADING
176 * @see IClient::STATE_ONLINE
177 */
178 virtual void OnStateChange(int NewState, int OldState)
179 {
180 }
181
182 /**
183 * Called to let the components register their console commands.
184 */
185 virtual void OnConsoleInit()
186 {
187 }
188
189 /**
190 * Called to let the components run initialization code.
191 */
192 virtual void OnInit()
193 {
194 }
195
196 /**
197 * Called to cleanup the component.
198 * This method is called when the client is closed.
199 */
200 virtual void OnShutdown()
201 {
202 }
203
204 /**
205 * Called to reset the component.
206 * This method is usually called on your component constructor to avoid code duplication.
207 * @see CHud::CHud()
208 * @see CHud::OnReset()
209 */
210 virtual void OnReset()
211 {
212 }
213
214 /**
215 * Called when the window has been resized.
216 */
217 virtual void OnWindowResize()
218 {
219 }
220
221 /**
222 * Called when the component should get updated.
223 *
224 * The update order depends on the component insertion order.
225 */
226 virtual void OnUpdate()
227 {
228 }
229
230 /**
231 * Called when the component should get rendered.
232 *
233 * The render order depends on the component insertion order.
234 */
235 virtual void OnRender()
236 {
237 }
238
239 /**
240 * Called when a new snapshot is received.
241 */
242 virtual void OnNewSnapshot()
243 {
244 }
245
246 /**
247 * Called when the input gets released, for example when a text box loses focus.
248 */
249 virtual void OnRelease()
250 {
251 }
252
253 /**
254 * Called on map load.
255 */
256 virtual void OnMapLoad()
257 {
258 }
259
260 /**
261 * Called when receiving a network message.
262 * @param Msg The message type.
263 * @param pRawMsg The message data.
264 * @see NETMSGTYPE_SV_DDRACETIME
265 * @see CNetMsg_Sv_DDRaceTime
266 */
267 virtual void OnMessage(int Msg, void *pRawMsg)
268 {
269 }
270
271 /**
272 * Called on mouse movement, where the x and y values are deltas.
273 *
274 * @param x The amount of change in the x coordinate since the last call.
275 * @param y The amount of change in the y coordinate since the last call.
276 * @param CursorType The type of cursor that caused the movement.
277 */
278 virtual bool OnCursorMove(float x, float y, IInput::ECursorType CursorType)
279 {
280 return false;
281 }
282
283 /**
284 * Called on a input event.
285 * @param Event The input event.
286 */
287 virtual bool OnInput(const IInput::CEvent &Event)
288 {
289 return false;
290 }
291
292 /**
293 * Called with all current touch finger states.
294 *
295 * @param vTouchFingerStates The touch finger states to be handled.
296 *
297 * @return `true` if the component used the touch events, `false` otherwise.
298 */
299 virtual bool OnTouchState(const std::vector<IInput::CTouchFingerState> &vTouchFingerStates)
300 {
301 return false;
302 }
303};
304
305#endif
306