cgv
Loading...
Searching...
No Matches
gamepad_server.h
1#pragma once
2
3#include <cgv/gui/key_event.h>
4#include <cgv/gui/throttle_event.h>
5#include <cgv/gui/stick_event.h>
6#include <cgv/gui/window.h>
7#include <cgv/signal/signal.h>
8#include <cgv/signal/bool_signal.h>
9#include <gamepad/gamepad.h>
10
11#include "lib_begin.h"
12
13namespace cgv {
14 namespace gui {
17 {
18 protected:
23 public:
25 gamepad_key_event(void* _device_handle, const gamepad::gamepad_state& _state, unsigned short _key = 0, KeyAction _action = KA_PRESS, unsigned char _char = 0, double _time = 0);
27 void* get_device_handle() const { return device_handle; }
29 const gamepad::gamepad_state& get_state() const { return state; }
31 void stream_out(std::ostream& os) const;
32 };
33
36 {
37 protected:
42 public:
44 gamepad_throttle_event(void* _device_handle, gamepad::gamepad_state& _state,
45 float _x, float _dx, unsigned _player_index = 0, unsigned _controller_index = 0, unsigned _throttle_index = 0, double _time = 0);
47 void* get_device_handle() const { return device_handle; }
49 const gamepad::gamepad_state& get_state() const { return state; }
51 void stream_out(std::ostream& os) const;
52 };
53
56 {
57 protected:
62 public:
64 gamepad_stick_event(void* _device_handle, gamepad::gamepad_state& _state,
65 StickAction _action, float _x, float _y, float _dx, float _dy,
66 unsigned _player_index = 0, unsigned _controller_index = 0, unsigned _stick_index = 0, double _time = 0);
68 void* get_device_handle() const;
70 const gamepad::gamepad_state& get_state() const;
72 void stream_out(std::ostream& os) const;
73 };
74
77 {
78 GPE_NONE = 0,
79 GPE_DEVICE = 1,
80 GPE_KEY = 2,
81 GPE_THROTTLE = 4,
82 GPE_STICK = 8,
83 GPE_STICK_KEY = 16, // whether stick touch and press actions should be translated to key events
84 GPE_ALL = 31
85 };
86
87 class CGV_API gamepad_server
88 {
89 protected:
90 GamepadEventTypeFlags event_flags;
91 double last_device_scan;
92 double device_scan_interval;
93 std::vector<gamepad::gamepad_state> last_states;
94 std::vector<void*> device_handles;
95 public:
99 GamepadEventTypeFlags get_event_type_flags() const { return event_flags; }
101 void set_event_type_flags(GamepadEventTypeFlags flags);
103 void set_device_scan_interval(double duration);
105 void check_and_emit_events(double time);
107 cgv::signal::bool_signal<cgv::gui::event&> on_event;
109 cgv::signal::signal<void*, bool> on_device_change;
110 };
111
113 extern CGV_API gamepad_server& ref_gamepad_server();
114
117 }
118}
119
120#include <cgv/config/lib_end.h>
gamepad key events use the key codes defined in gamepad::GamepadKeys
const gamepad::gamepad_state & get_state() const
access to current gamepad state
void * get_device_handle() const
return the device id, by default returns 0
const gamepad::gamepad_state & state
public access to game state allows skipping of library dependency
void * device_handle
store id of gamepad
cgv::signal::bool_signal< cgv::gui::event & > on_event
signal emitted to dispatch events
cgv::signal::signal< void *, bool > on_device_change
signal emitted to notify about device changes, first argument is device handle and second whether dev...
GamepadEventTypeFlags get_event_type_flags() const
query the currently set event type flags
gamepad extension of stick events
void * device_handle
store id of gamepad
const gamepad::gamepad_state & state
public access to game state allows skipping of library dependency
gamepad extension of throttle events
const gamepad::gamepad_state & get_state() const
access to current gamepad state
const gamepad::gamepad_state & state
public access to game state allows skipping of library dependency
void * get_device_handle() const
return the device id, by default returns 0
void * device_handle
store id of gamepad
class to represent all possible keyboard events with the EID_KEY
Definition key_event.h:23
class to represent stick events with the EID_STICK
Definition stick_event.h:29
class to represent events that inform on a change in a one axis controller with the EID_THROTTLE
KeyAction
different actions that a key can perform
Definition key_event.h:12
gamepad_server & ref_gamepad_server()
return a reference to gamepad server singleton
StickAction
different actions that a stick can perform
Definition stick_event.h:15
GamepadEventTypeFlags
flags to define which events should be generated by server
void connect_gamepad_server(cgv::gui::window_ptr w)
connect the gamepad server to the given window or the first window of the application,...
the cgv namespace
Definition print.h:11
see https://upload.wikimedia.org/wikipedia/commons/2/2c/360_controller.svg for an explanation of the ...
Definition gamepad.h:152