neoGFX
Cross-platform C++ app/game engine
Loading...
Searching...
No Matches
game_controller.hpp
Go to the documentation of this file.
1// game_controller.hpp
2/*
3 neogfx C++ App/Game Engine
4 Copyright (c) 2020 Leigh Johnston. All Rights Reserved.
5
6 This program is free software: you can redistribute it and / or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#pragma once
21
22#include <neogfx/neogfx.hpp>
23#include <boost/bimap.hpp>
27
28namespace neogfx
29{
30 class game_controller : public hid_device<i_game_controller>
31 {
32 public:
37 define_declared_event(RightTriggerMoved, right_trigger_moved, double, key_modifiers_e)
39 define_declared_event(RightThumbMoved, right_thumb_moved, const vec2&, key_modifiers_e)
41 define_declared_event(StickRotated, stick_rotated, const vec3&, key_modifiers_e)
43 public:
44 struct button_not_found : std::logic_error { button_not_found() : std::logic_error{ "neogfx::game_controller::button_not_found" } {} };
45 public:
47 public:
48 typedef boost::bimap<game_controller_button_ordinal, game_controller_button> button_map_type;
49 public:
50 game_controller(hid_device_subclass aSubclass, const hid_device_uuid& aProductId, const hid_device_uuid& aInstanceId, const button_map_type& aButtonMap = {});
51 public:
52 bool player_assigned() const override;
53 game_player player() const override;
54 void assign_player(game_player aPlayer) override;
55 void unassign_player() override;
56 public:
57 bool have_port() const override;
58 game_controller_port port() const override;
59 void set_port(game_controller_port aPort) override;
60 void clear_port() override;
61 public:
62 bool needs_calibrating() const override;
63 public:
64 bool is_button_pressed(game_controller_button_ordinal aButtonOrdinal) const override;
65 bool is_button_pressed(game_controller_button aButton) const override;
66 double left_trigger_position() const override;
67 double right_trigger_position() const override;
68 const vec2& left_thumb_position() const override;
69 const vec2& right_thumb_position() const override;
70 const vec3& stick_position() const override;
71 const vec3& stick_rotation() const override;
72 const vec2& slider_position() const override;
73 public:
74 uint32_t button_count() const override;
75 bool button_mapped(game_controller_button aButton) const override;
78 protected:
79 virtual void update_state() = 0;
80 protected:
82 void set_button_state(game_controller_button_ordinal aButtonOrdinal, bool aIsPressed);
83 void set_button_state(game_controller_button aButton, bool aIsPressed);
84 void set_left_trigger_position(double aPosition);
85 void set_right_trigger_position(double aPosition);
86 void set_left_thumb_position(const vec2& aPosition);
87 void set_right_thumb_position(const vec2& aPosition);
88 void set_stick_position(const vec3& aPosition);
89 void set_stick_rotation(const vec3& aRotation);
90 void set_slider_position(const vec2& aPosition);
91 private:
93 std::optional<game_player> iPlayer;
94 std::optional<game_controller_port> iPort;
95 button_map_type iButtonMap;
96 std::array<bool, MAX_BUTTONS> iButtonState = {};
97 double iLeftTriggerPosition = 0.0;
98 double iRightTriggerPosition = 0.0;
99 vec2 iLeftThumbPosition;
100 vec2 iRightThumbPosition;
101 vec3 iStickPosition;
102 vec3 iStickRotation;
103 vec2 iSliderPosition;
104 };
105}
virtual void update_state()=0
void set_stick_position(const vec3 &aPosition)
game_controller(hid_device_subclass aSubclass, const hid_device_uuid &aProductId, const hid_device_uuid &aInstanceId, const button_map_type &aButtonMap={})
key_modifiers_e key_modifiers_e const vec2 key_modifiers_e stick_moved
boost::bimap< game_controller_button_ordinal, game_controller_button > button_map_type
define_declared_event(ButtonPressed, button_pressed, game_controller_button, key_modifiers_e) define_declared_event(ButtonReleased
void unassign_player() override
double left_trigger_position() const override
void set_right_trigger_position(double aPosition)
bool button_mapped(game_controller_button aButton) const override
void clear_port() override
void set_right_thumb_position(const vec2 &aPosition)
const vec2 & left_thumb_position() const override
const vec3 & stick_position() const override
void set_left_trigger_position(double aPosition)
const vec2 & right_thumb_position() const override
bool is_button_pressed(game_controller_button aButton) const override
key_modifiers_e key_modifiers_e left_thumb_moved
void set_port(game_controller_port aPort) override
button_map_type & button_map()
game_player player() const override
bool have_port() const override
void set_stick_rotation(const vec3 &aRotation)
static constexpr game_controller_button_ordinal MAX_BUTTONS
void set_left_thumb_position(const vec2 &aPosition)
uint32_t button_count() const override
const vec3 & stick_rotation() const override
void set_button_state(game_controller_button_ordinal aButtonOrdinal, bool aIsPressed)
game_controller_port port() const override
bool is_button_pressed(game_controller_button_ordinal aButtonOrdinal) const override
bool needs_calibrating() const override
void assign_player(game_player aPlayer) override
key_modifiers_e key_modifiers_e const vec2 key_modifiers_e const vec3 key_modifiers_e slider_moved
game_controller_button_ordinal button_to_button_ordinal(game_controller_button aButton) const override
game_controller_button button_ordinal_to_button(game_controller_button_ordinal aButtonOrdinal) const override
key_modifiers_e left_trigger_moved
const vec2 & slider_position() const override
void set_button_state(game_controller_button aButton, bool aIsPressed)
bool player_assigned() const override
void set_slider_position(const vec2 &aPosition)
double right_trigger_position() const override
uint32_t game_controller_port
uint32_t game_controller_button_ordinal
Definition plf_hive.h:79
#define define_declared_event(name, declName,...)
Definition event.hpp:195