OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4GamePadCon.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2010-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Gamepad control - forwards gamepad events of opened gamepads to Game.KeyboardInput */
19 
20 #ifndef INC_C4GamePadCon
21 #define INC_C4GamePadCon
22 
23 #include <memory>
24 
25 #ifdef HAVE_SDL
26 #include "gui/C4KeyboardInput.h"
27 #include <set>
28 #include <map>
29 
30 #include <SDL.h>
31 #endif
32 
33 class C4GamePadOpener;
34 
36 {
37 #ifdef HAVE_SDL
38 public:
39  enum {
40  FEED_BUTTONS = 1,
41  FEED_MOVED = 2,
42  };
43  // Called from C4AppSDL
44  void FeedEvent(const SDL_Event& e, int feed);
45  void CheckGamePad(const SDL_Event& e);
46 private:
47  std::set<C4KeyCode> PressedAxis; // for button emulation
48  std::map<C4KeyCode, SDL_Event> AxisEvents; // for analog movement events
49  std::map<int32_t, std::shared_ptr<C4GamePadOpener> > Gamepads; // gamepad instance id -> gamepad
50 #endif
51 public:
52  static const int32_t MaxStrength = 32767; // 2^15 - 1
53 
56  void Clear();
57  int GetGamePadCount();
58  void Execute();
59  void DoAxisInput(); // period axis strength update controls sent on each control frame creation
60 
61  std::shared_ptr<C4GamePadOpener> GetGamePad(int gamepad); // Gets the nth gamepad.
62  std::shared_ptr<C4GamePadOpener> GetGamePadByID(int32_t id); // Gets a gamepad by its instance id.
63  std::shared_ptr<C4GamePadOpener> GetAvailableGamePad(); // Looks for a gamepad that doesn't have an assigned player.
64 };
65 
67 {
68  int32_t player = -1;
69 
70 public:
71  C4GamePadOpener(int iGamePad);
73 
74  // A gamepad can be assigned to a player.
75  int32_t GetPlayer() const { return player; }
76  void SetPlayer(int32_t plr) { player = plr; }
77 
78  int32_t GetID(); // Returns the gamepad's instance id.
79  bool IsAttached(); // Returns whether the gamepad is currently attached.
80 
81  // Force feedback: simple rumbling
82  void PlayRumble(float strength, uint32_t length); // strength: 0-1, length: milliseconds
83  void StopRumble();
84 
85 #ifdef HAVE_SDL
86  SDL_GameController *controller;
87  SDL_Haptic *haptic;
88 #endif
89 };
90 
91 #endif
void SetPlayer(int32_t plr)
Definition: C4GamePadCon.h:76
static const int32_t MaxStrength
Definition: C4GamePadCon.h:52
std::shared_ptr< C4GamePadOpener > GetAvailableGamePad()
int32_t GetPlayer() const
Definition: C4GamePadCon.h:75
C4GamePadOpener(int iGamePad)
void PlayRumble(float strength, uint32_t length)
std::shared_ptr< C4GamePadOpener > GetGamePadByID(int32_t id)
std::shared_ptr< C4GamePadOpener > GetGamePad(int gamepad)