2012-06-26 20:23:05 +00:00
|
|
|
// * 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/.
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
/**
|
|
|
|
* \file common/event.h
|
|
|
|
* \brief Event types, structs and event queue
|
|
|
|
*/
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
#include "common/key.h"
|
|
|
|
#include "common/event_ids.h"
|
|
|
|
#include "math/point.h"
|
2012-09-19 19:23:42 +00:00
|
|
|
#include "math/vector.h"
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
class CInstanceManager;
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \struct KeyEventData
|
|
|
|
* \brief Additional data for keyboard event
|
|
|
|
*/
|
2012-06-29 22:12:04 +00:00
|
|
|
struct KeyEventData
|
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
//! If true, the key is a virtual code generated by certain key modifiers or joystick buttons
|
2012-09-19 16:32:18 +00:00
|
|
|
bool virt;
|
|
|
|
//! Key symbol: KEY(...) macro value or virtual key VIRTUAL_... (from common/key.h)
|
2012-06-29 22:12:04 +00:00
|
|
|
unsigned int key;
|
|
|
|
//! Unicode character
|
2012-09-21 22:38:17 +00:00
|
|
|
//! NOTE: applicable only to EVENT_KEY_DOWN events!
|
2012-06-29 22:12:04 +00:00
|
|
|
unsigned int unicode;
|
|
|
|
};
|
|
|
|
|
2012-09-21 22:38:17 +00:00
|
|
|
/**
|
|
|
|
* \enum MouseButton
|
|
|
|
* \brief Mouse button
|
|
|
|
*
|
|
|
|
* Values are a bitmask to have a state bitmask
|
|
|
|
*/
|
|
|
|
enum MouseButton
|
2012-06-29 22:12:04 +00:00
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
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)
|
2012-06-29 22:12:04 +00:00
|
|
|
};
|
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \struct MouseButtonEventData
|
|
|
|
* \brief Additional data mouse button event
|
|
|
|
*/
|
2012-06-29 22:12:04 +00:00
|
|
|
struct MouseButtonEventData
|
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
//! The mouse button
|
|
|
|
MouseButton button;
|
2012-09-19 19:23:42 +00:00
|
|
|
};
|
2012-06-29 22:12:04 +00:00
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \enum WheelDirection
|
|
|
|
* \brief Direction of mouse wheel movement
|
|
|
|
*/
|
|
|
|
enum WheelDirection
|
|
|
|
{
|
|
|
|
WHEEL_UP,
|
|
|
|
WHEEL_DOWN
|
2012-06-29 22:12:04 +00:00
|
|
|
};
|
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
2012-09-27 18:43:20 +00:00
|
|
|
* \struct MouseWheelEventData
|
2012-09-19 19:23:42 +00:00
|
|
|
* \brief Additional data for mouse wheel event.
|
|
|
|
*/
|
|
|
|
struct MouseWheelEventData
|
|
|
|
{
|
|
|
|
//! Wheel direction
|
|
|
|
WheelDirection dir;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \struct JoyAxisEventData
|
|
|
|
* \brief Additional data for joystick axis event
|
|
|
|
*/
|
2012-06-29 22:12:04 +00:00
|
|
|
struct JoyAxisEventData
|
|
|
|
{
|
|
|
|
//! The joystick axis index
|
|
|
|
unsigned char axis;
|
|
|
|
//! The axis value (range: -32768 to 32767)
|
|
|
|
int value;
|
|
|
|
};
|
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \struct JoyButtonEventData
|
|
|
|
* \brief Additional data for joystick button event
|
|
|
|
*/
|
2012-06-29 22:12:04 +00:00
|
|
|
struct JoyButtonEventData
|
|
|
|
{
|
|
|
|
//! The joystick button index
|
|
|
|
unsigned char button;
|
2012-06-26 20:23:05 +00:00
|
|
|
};
|
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \enum ActiveEventFlags
|
|
|
|
* \brief Type of focus gained/lost
|
|
|
|
*/
|
2012-07-29 13:09:53 +00:00
|
|
|
enum ActiveEventFlags
|
|
|
|
{
|
|
|
|
//! Application window focus
|
|
|
|
ACTIVE_APP = 0x01,
|
|
|
|
//! Input focus
|
|
|
|
ACTIVE_INPUT = 0x02,
|
|
|
|
//! Mouse focus
|
|
|
|
ACTIVE_MOUSE = 0x04
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-09-19 19:23:42 +00:00
|
|
|
/**
|
|
|
|
* \struct ActiveEventData
|
|
|
|
* \brief Additional data for active event
|
|
|
|
*/
|
2012-07-29 13:09:53 +00:00
|
|
|
struct ActiveEventData
|
|
|
|
{
|
|
|
|
//! Flags (bitmask of enum values ActiveEventFlags)
|
2012-08-13 16:17:54 +00:00
|
|
|
unsigned char flags;
|
2012-07-29 13:09:53 +00:00
|
|
|
//! True if the focus was gained; false otherwise
|
|
|
|
bool gain;
|
|
|
|
};
|
2012-06-29 22:12:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-09-19 19:23:42 +00:00
|
|
|
* \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.
|
2012-06-29 22:12:04 +00:00
|
|
|
**/
|
2012-06-26 20:23:05 +00:00
|
|
|
struct Event
|
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
//! Type of event
|
2012-06-29 22:12:04 +00:00
|
|
|
EventType type;
|
2012-06-30 08:16:52 +00:00
|
|
|
|
2012-09-21 22:38:17 +00:00
|
|
|
//! If true, the event was produced by system in CApplication; else, it has come from game engine
|
2012-07-22 20:05:12 +00:00
|
|
|
bool systemEvent;
|
|
|
|
|
2012-09-21 22:38:17 +00:00
|
|
|
//! 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
|
2012-08-09 20:50:04 +00:00
|
|
|
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;
|
2012-09-19 19:23:42 +00:00
|
|
|
//! Additional data for EVENT_MOUSE_WHEEL
|
|
|
|
MouseWheelEventData mouseWheel;
|
2012-08-09 20:50:04 +00:00
|
|
|
//! Additional data for EVENT_JOY
|
|
|
|
JoyAxisEventData joyAxis;
|
|
|
|
//! Additional data for EVENT_JOY_AXIS
|
|
|
|
JoyButtonEventData joyButton;
|
|
|
|
//! Additional data for EVENT_ACTIVE
|
|
|
|
ActiveEventData active;
|
|
|
|
};
|
|
|
|
|
2012-09-21 22:38:17 +00:00
|
|
|
Event(EventType type = EVENT_NULL)
|
2012-08-09 20:50:04 +00:00
|
|
|
{
|
2012-09-21 22:38:17 +00:00
|
|
|
this->type = type;
|
2012-08-09 20:50:04 +00:00
|
|
|
|
2012-09-21 22:38:17 +00:00
|
|
|
systemEvent = false;
|
2012-08-09 20:50:04 +00:00
|
|
|
rTime = 0.0f;
|
2012-09-21 22:38:17 +00:00
|
|
|
mouseButtonsState = 0;
|
|
|
|
trackedKeysState = 0;
|
|
|
|
customParam = 0;
|
2012-08-09 20:50:04 +00:00
|
|
|
}
|
2012-06-26 20:23:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-09-09 15:51:10 +00:00
|
|
|
//! Returns an unique event type (above the standard IDs)
|
|
|
|
EventType GetUniqueEventType();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
/**
|
2012-09-19 19:23:42 +00:00
|
|
|
* \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.
|
2012-06-29 22:12:04 +00:00
|
|
|
*/
|
|
|
|
class CEventQueue
|
2012-06-26 20:23:05 +00:00
|
|
|
{
|
|
|
|
public:
|
2012-06-29 22:12:04 +00:00
|
|
|
//! Constant maximum size of queue
|
|
|
|
static const int MAX_EVENT_QUEUE = 100;
|
|
|
|
|
|
|
|
public:
|
|
|
|
//! Object's constructor
|
|
|
|
CEventQueue(CInstanceManager* iMan);
|
|
|
|
//! Object's destructor
|
|
|
|
~CEventQueue();
|
2012-06-26 20:23:05 +00:00
|
|
|
|
2012-06-29 22:12:04 +00:00
|
|
|
//! Empties the FIFO of events
|
2012-06-26 20:23:05 +00:00
|
|
|
void Flush();
|
2012-06-29 22:12:04 +00:00
|
|
|
//! Adds an event to the queue
|
2012-06-26 20:23:05 +00:00
|
|
|
bool AddEvent(const Event &event);
|
2012-09-21 22:38:17 +00:00
|
|
|
//! Removes and returns an event from queue front
|
2012-06-26 20:23:05 +00:00
|
|
|
bool GetEvent(Event &event);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
CInstanceManager* m_iMan;
|
2012-06-29 22:12:04 +00:00
|
|
|
Event m_fifo[MAX_EVENT_QUEUE];
|
|
|
|
int m_head;
|
|
|
|
int m_tail;
|
|
|
|
int m_total;
|
2012-06-26 20:23:05 +00:00
|
|
|
};
|