|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 #ifndef mozilla_dom_gamepad_Gamepad_h |
|
6 #define mozilla_dom_gamepad_Gamepad_h |
|
7 |
|
8 #include "mozilla/ErrorResult.h" |
|
9 #include "mozilla/dom/GamepadButton.h" |
|
10 #include <stdint.h> |
|
11 #include "nsCOMPtr.h" |
|
12 #include "nsString.h" |
|
13 #include "nsTArray.h" |
|
14 #include "nsWrapperCache.h" |
|
15 |
|
16 namespace mozilla { |
|
17 namespace dom { |
|
18 |
|
19 enum GamepadMappingType |
|
20 { |
|
21 NoMapping = 0, |
|
22 StandardMapping = 1 |
|
23 }; |
|
24 |
|
25 class Gamepad : public nsISupports, |
|
26 public nsWrapperCache |
|
27 { |
|
28 public: |
|
29 Gamepad(nsISupports* aParent, |
|
30 const nsAString& aID, uint32_t aIndex, |
|
31 GamepadMappingType aMapping, |
|
32 uint32_t aNumButtons, uint32_t aNumAxes); |
|
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
|
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad) |
|
35 |
|
36 void SetConnected(bool aConnected); |
|
37 void SetButton(uint32_t aButton, bool aPressed, double aValue); |
|
38 void SetAxis(uint32_t aAxis, double aValue); |
|
39 void SetIndex(uint32_t aIndex); |
|
40 |
|
41 // Make the state of this gamepad equivalent to other. |
|
42 void SyncState(Gamepad* aOther); |
|
43 |
|
44 // Return a new Gamepad containing the same data as this object, |
|
45 // parented to aParent. |
|
46 already_AddRefed<Gamepad> Clone(nsISupports* aParent); |
|
47 |
|
48 nsISupports* GetParentObject() const |
|
49 { |
|
50 return mParent; |
|
51 } |
|
52 |
|
53 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
|
54 |
|
55 void GetId(nsAString& aID) const |
|
56 { |
|
57 aID = mID; |
|
58 } |
|
59 |
|
60 void GetMapping(nsAString& aMapping) const |
|
61 { |
|
62 if (mMapping == StandardMapping) { |
|
63 aMapping = NS_LITERAL_STRING("standard"); |
|
64 } else { |
|
65 aMapping = NS_LITERAL_STRING(""); |
|
66 } |
|
67 } |
|
68 |
|
69 bool Connected() const |
|
70 { |
|
71 return mConnected; |
|
72 } |
|
73 |
|
74 uint32_t Index() const |
|
75 { |
|
76 return mIndex; |
|
77 } |
|
78 |
|
79 void GetButtons(nsTArray<nsRefPtr<GamepadButton>>& aButtons) const |
|
80 { |
|
81 aButtons = mButtons; |
|
82 } |
|
83 |
|
84 void GetAxes(nsTArray<double>& aAxes) const |
|
85 { |
|
86 aAxes = mAxes; |
|
87 } |
|
88 |
|
89 private: |
|
90 virtual ~Gamepad() {} |
|
91 |
|
92 protected: |
|
93 nsCOMPtr<nsISupports> mParent; |
|
94 nsString mID; |
|
95 uint32_t mIndex; |
|
96 |
|
97 // The mapping in use. |
|
98 GamepadMappingType mMapping; |
|
99 |
|
100 // true if this gamepad is currently connected. |
|
101 bool mConnected; |
|
102 |
|
103 // Current state of buttons, axes. |
|
104 nsTArray<nsRefPtr<GamepadButton>> mButtons; |
|
105 nsTArray<double> mAxes; |
|
106 }; |
|
107 |
|
108 } // namespace dom |
|
109 } // namespace mozilla |
|
110 |
|
111 #endif // mozilla_dom_gamepad_Gamepad_h |