VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
PlayerManager.h
1 /*
2  * Copyright (C) 2014 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef PLAYERMANAGER_H
18 #define PLAYERMANAGER_H
19 
20 #include <GameInput/ControllerManager.h>
21 #include <QList>
22 #include <QObject>
23 #include <QMap>
24 
25 class VirtualController;
26 
37  Q_OBJECT
38 public:
45  enum PlayerAction {
62  };
63 
68  explicit PlayerManager(QObject* parent = nullptr);
69 
70  virtual ~PlayerManager();
71 
76  bool isPlayerAvailable(int player) const { return mPlayerToDeviceMap.contains(player); }
81  QList<int> getAvailablePlayers() const { return mPlayerToDeviceMap.keys(); }
86  void reset();
87 
91  VirtualController* getPlayerController(int player) const;
92 
96  float getHorizontal(int player) const;
100  bool isMagnetized(int player) const;
101 
105  static PlayerManager* getInstance();
106 
107 signals:
114  void playerActivationChanged(int playerId, bool status);
115 
116 private:
120  void addPlayerController(int deviceId);
124  void removePlayerController(int deviceId);
125 
129  virtual void onDeviceConnectionChanged(int deviceId, bool isConnected) override;
130 
131  // Maps playerId to deviceId
132  QMap<int, int> mPlayerToDeviceMap;
133  // Reverse map from deviceId to playerId
134  QMap<int, int> mDeviceToPlayerMap;
135  // Lowest available player index which has yet to be assigned
136  int mMinAvailablePlayerId = 0;
137 
138  // Singleton instance of the player manager
139  static PlayerManager* sInstance;
140 };
141 Q_DECLARE_METATYPE(PlayerManager*)
142 
143 #endif // PLAYERMANAGER_H
Manages a mapping from unique player identifiers to different controllers that connect and disconnect...
Definition: PlayerManager.h:36
float getHorizontal(int player) const
Returns the horizontal movement value [-1.0f, 1.0f] for player.
bool contains(const Key &key) const
PlayerAction
Different actions or expressions of intent a player can make in the game.
Definition: PlayerManager.h:45
Software represention and state capable of describing almost all physical controllers.
Definition: VirtualController.h:36
void reset()
Resets all the player <-> controller mappings.
QList< Key > keys() const
Interface for listening to changes in device connection statuses.
Definition: ControllerManager.h:38
Action for magnetizing.
Definition: PlayerManager.h:61
void playerActivationChanged(int playerId, bool status)
Emitted when player playerId becomes either activated (i.e. connects a controller) or deactivated (i...
PlayerManager(QObject *parent=nullptr)
Constructs a PlayerManager.
static PlayerManager * getInstance()
Returns the global singleton instance of the PlayerManager, creating it if necessary.
bool isPlayerAvailable(int player) const
Returns whether or not player is available (i.e. currently connected with a controller).
Definition: PlayerManager.h:76
Bidirectional action for movement left or right (i.e. horizontal).
Definition: PlayerManager.h:57
QList< int > getAvailablePlayers() const
Returns the list of currently available players (i.e. players currently connected with a controller)...
Definition: PlayerManager.h:81
QObject * parent() const
Unidirectional action for movement to the right.
Definition: PlayerManager.h:53
bool isMagnetized(int player) const
Returns whether or not the the player has magnetism activated.
Unidirectional action for movement to the left.
Definition: PlayerManager.h:49
VirtualController * getPlayerController(int player) const
Returns the VirtualController for player.