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 #ifdef HAVE_SDL
24 #include "gui/C4KeyboardInput.h"
25 
26 #include <SDL.h>
27 #endif
28 
29 class C4GamePadOpener;
30 
32 {
33 #ifdef HAVE_SDL
34 public:
35  enum {
36  FEED_BUTTONS = 1,
37  FEED_MOVED = 2,
38  };
39  // Called from C4AppSDL
40  void FeedEvent(const SDL_Event& e, int feed);
41  void CheckGamePad(const SDL_Event& e);
42 private:
43  std::set<C4KeyCode> PressedAxis; // for button emulation
44  std::map<C4KeyCode, SDL_Event> AxisEvents; // for analog movement events
45  std::map<int32_t, std::shared_ptr<C4GamePadOpener> > Gamepads; // gamepad instance id -> gamepad
46 #endif
47 public:
48  static const int32_t MaxStrength = 32767; // 2^15 - 1
49 
52  void Clear();
53  int GetGamePadCount();
54  void Execute();
55  void DoAxisInput(); // period axis strength update controls sent on each control frame creation
56 
57  std::shared_ptr<C4GamePadOpener> GetGamePad(int gamepad); // Gets the nth gamepad.
58  std::shared_ptr<C4GamePadOpener> GetGamePadByID(int32_t id); // Gets a gamepad by its instance id.
59  std::shared_ptr<C4GamePadOpener> GetAvailableGamePad(); // Looks for a gamepad that doesn't have an assigned player.
60 };
61 
63 {
64  int32_t player = -1;
65 
66 public:
67  C4GamePadOpener(int iGamePad);
69 
70  // A gamepad can be assigned to a player.
71  int32_t GetPlayer() const { return player; }
72  void SetPlayer(int32_t plr) { player = plr; }
73 
74  int32_t GetID(); // Returns the gamepad's instance id.
75  bool IsAttached(); // Returns whether the gamepad is currently attached.
76 
77  // Force feedback: simple rumbling
78  void PlayRumble(float strength, uint32_t length); // strength: 0-1, length: milliseconds
79  void StopRumble();
80 
81 #ifdef HAVE_SDL
82  SDL_GameController *controller;
83  SDL_Haptic *haptic;
84 #endif
85 };
86 
87 #endif
void SetPlayer(int32_t plr)
Definition: C4GamePadCon.h:72
static const int32_t MaxStrength
Definition: C4GamePadCon.h:48
std::shared_ptr< C4GamePadOpener > GetAvailableGamePad()
int32_t GetPlayer() const
Definition: C4GamePadCon.h:71
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)