summaryrefslogtreecommitdiffstats
path: root/src/common/event.h
blob: dc50ee6e2f115656a8ea9996d8c567e2c295b3fa (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// * This file is part of the COLOBOT source code
// * Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
// *
// * This program is free software: you can redistribute it and/or modify
// * it under the terms of the GNU General Public License as published by
// * the Free Software Foundation, either version 3 of the License, or
// * (at your option) any later version.
// *
// * This program is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU General Public License for more details.
// *
// * You should have received a copy of the GNU General Public License
// * along with this program. If not, see  http://www.gnu.org/licenses/.

/**
 * \file common/event.h
 * \brief Event types, structs and event queue
 */

#pragma once


#include "common/key.h"
#include "common/event_ids.h"
#include "math/point.h"
#include "math/vector.h"

class CInstanceManager;


/**
 * \struct KeyEventData
 * \brief Additional data for keyboard event
 */
struct KeyEventData
{
    //! If true, the key is a virtual code generated by certain key modifiers or joystick buttons
    bool virt;
    //! Key symbol: KEY(...) macro value or virtual key VIRTUAL_... (from common/key.h)
    unsigned int key;
    //! Unicode character
    //! NOTE: applicable only to EVENT_KEY_DOWN events!
    unsigned int unicode;
};

/**
 * \enum MouseButton
 * \brief Mouse button
 *
 * Values are a bitmask to have a state bitmask
 */
enum MouseButton
{
    MOUSE_BUTTON_LEFT   = (1<<1),
    MOUSE_BUTTON_MIDDLE = (1<<2),
    MOUSE_BUTTON_RIGHT  = (1<<3),
    //! There may be additional mouse buttons >= this value
    MOUSE_BUTTON_OTHER  = (1<<4)
};

/**
 * \struct MouseButtonEventData
 * \brief Additional data mouse button event
 */
struct MouseButtonEventData
{
    //! The mouse button
    MouseButton button;
};

/**
 * \enum WheelDirection
 * \brief Direction of mouse wheel movement
 */
enum WheelDirection
{
    WHEEL_UP,
    WHEEL_DOWN
};

/**
 * \enum MouseWheelEventData
 * \brief Additional data for mouse wheel event.
 */
struct MouseWheelEventData
{
    //! Wheel direction
    WheelDirection dir;
};

/**
 * \struct JoyAxisEventData
 * \brief Additional data for joystick axis event
 */
struct JoyAxisEventData
{
    //! The joystick axis index
    unsigned char axis;
    //! The axis value (range: -32768 to 32767)
    int value;
};

/**
 * \struct JoyButtonEventData
 * \brief Additional data for joystick button event
 */
struct JoyButtonEventData
{
    //! The joystick button index
    unsigned char button;
};

/**
 * \enum ActiveEventFlags
 * \brief Type of focus gained/lost
 */
enum ActiveEventFlags
{
    //! Application window focus
    ACTIVE_APP   = 0x01,
    //! Input focus
    ACTIVE_INPUT = 0x02,
    //! Mouse focus
    ACTIVE_MOUSE = 0x04

};

/**
 * \struct ActiveEventData
 * \brief Additional data for active event
 */
struct ActiveEventData
{
    //! Flags (bitmask of enum values ActiveEventFlags)
    unsigned char flags;
    //! True if the focus was gained; false otherwise
    bool gain;
};


/**
 * \struct Event
 * \brief Event sent by system, interface or game
 *
 * Event is described by its type (EventType) and the union
 * \a data contains additional data about the event.
 * Different members of the union are filled with different event types.
 * With some events, nothing is filled (it's zeroed out).
 * The union contains roughly the same information as SDL_Event struct
 * but packaged to independent structs and fields.
 **/
struct Event
{
    //! Type of event
    EventType type;

    //! If true, the event was produced by system in CApplication; else, it has come from game engine
    bool systemEvent;

    //! Relative time since last EVENT_FRAME
    //! Scope: only EVENT_FRAME events
    // TODO: gradually replace the usage of this with new CApplication's time functions
    float        rTime;

    //! Motion vector set by keyboard or joystick (managed by CRobotMain)
    //! Scope: all system events
    Math::Vector motionInput;

    //! Current state of keyboard modifier keys: bitmask made of KEY_MOD(...) macro values (from common/key.h)
    //! Scope: all system events
    unsigned int kmodState;

    //! Current state of tracked keys: bitmask of TrackedKey enum values
    //! Scope: all system events
    unsigned int trackedKeysState;

    //! Current position of mouse cursor in interface coords
    //! Scope: all system events
    Math::Point  mousePos;

    //! Current state of mouse buttons: bitmask of MouseButton enum values
    //! Scope: all system events
    unsigned int mouseButtonsState;

    //! Custom parameter that may be set for some events
    //! Scope: some interface events
    long         customParam;

    //! Union with additional data, applicable only to some events
    union
    {
        //! Additional data for EVENT_KEY_DOWN and EVENT_KEY_UP
        KeyEventData key;
        //! Additional data for EVENT_MOUSE_BUTTON_DOWN and EVENT_MOUSE_BUTTON_UP
        MouseButtonEventData mouseButton;
        //! Additional data for EVENT_MOUSE_WHEEL
        MouseWheelEventData mouseWheel;
        //! Additional data for EVENT_JOY
        JoyAxisEventData joyAxis;
        //! Additional data for EVENT_JOY_AXIS
        JoyButtonEventData joyButton;
        //! Additional data for EVENT_ACTIVE
        ActiveEventData active;
    };

    Event(EventType type = EVENT_NULL)
    {
        this->type = type;

        systemEvent = false;
        rTime = 0.0f;
        mouseButtonsState = 0;
        trackedKeysState = 0;
        customParam = 0;
    }
};


//! Returns an unique event type (above the standard IDs)
EventType GetUniqueEventType();


/**
 * \class CEventQueue
 * \brief Global event queue
 *
 * Provides an interface to a global FIFO queue with events (both system- and user-generated).
 * The queue has a fixed maximum size but it should not be a problem.
 */
class CEventQueue
{
public:
    //! Constant maximum size of queue
    static const int MAX_EVENT_QUEUE = 100;

public:
    //! Object's constructor
    CEventQueue(CInstanceManager* iMan);
    //! Object's destructor
    ~CEventQueue();

    //! Empties the FIFO of events
    void    Flush();
    //! Adds an event to the queue
    bool    AddEvent(const Event &event);
    //! Removes and returns an event from queue front
    bool    GetEvent(Event &event);

protected:
    CInstanceManager* m_iMan;
    Event        m_fifo[MAX_EVENT_QUEUE];
    int          m_head;
    int          m_tail;
    int          m_total;
};