]> git.tdb.fi Git - libs/game.git/blob - source/gameview/playerinput.h
Decorate things which constitute the public API of the library
[libs/game.git] / source / gameview / playerinput.h
1 #ifndef MSP_GAMEVIEW_PLAYERINPUT_H_
2 #define MSP_GAMEVIEW_PLAYERINPUT_H_
3
4 #include <memory>
5 #include <msp/input/bindings.h>
6 #include <msp/input/controlscheme.h>
7 #include <msp/input/hub.h>
8 #include <msp/input/keyboard.h>
9 #include <msp/input/mouse.h>
10 #include <msp/game/director.h>
11 #include "events.h"
12 #include "mspgameview_api.h"
13
14 namespace Msp::GameView {
15
16 class MSPGAMEVIEW_API PlayerInput: public NonCopyable
17 {
18 public:
19         using EventSource = Game::EventSource<Events::PlayerArrived, Events::PlayerDeparted>;
20
21         enum Mode
22         {
23                 ONE_LOCAL_PLAYER,
24                 LOCAL_MULTIPLAYER
25         };
26
27 private:
28         struct Player
29         {
30                 Input::Device *device = nullptr;
31                 std::unique_ptr<Input::ControlScheme> controls = nullptr;
32         };
33
34         using SchemeFactoryFunc = std::unique_ptr<Input::ControlScheme>();
35
36         EventSource event_source;
37         Input::Keyboard keyboard;
38         Input::Mouse mouse;
39         Input::Hub kbm_hub;
40         std::vector<Input::Bindings *> bindings;
41         SchemeFactoryFunc *scheme_factory = nullptr;
42         std::vector<Player> players;
43         Mode mode = ONE_LOCAL_PLAYER;
44
45 public:
46         PlayerInput(Game::Director &, Graphics::Window &);
47
48         template<typename T>
49                 requires std::is_base_of_v<Input::ControlScheme, T>
50         void set_control_scheme_type(Mode = ONE_LOCAL_PLAYER);
51
52 private:
53         void init_players();
54         void add_player(Input::Device &);
55
56 public:
57         void synthesize_initial_events(Game::EventObserver &);
58 };
59
60
61 template<typename T>
62         requires std::is_base_of_v<Input::ControlScheme, T>
63 void PlayerInput::set_control_scheme_type(Mode m)
64 {
65         if(scheme_factory)
66                 throw std::logic_error("scheme type already set");
67
68         mode = m;
69         scheme_factory = +[]() -> std::unique_ptr<Input::ControlScheme> { return std::make_unique<T>(); };
70         init_players();
71 }
72
73 } // namespace Msp::GameView
74
75 #endif