|
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 #include "Gamepad.h" |
|
6 #include "nsAutoPtr.h" |
|
7 #include "nsTArray.h" |
|
8 #include "nsVariant.h" |
|
9 #include "mozilla/dom/GamepadBinding.h" |
|
10 |
|
11 namespace mozilla { |
|
12 namespace dom { |
|
13 |
|
14 NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad) |
|
15 NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad) |
|
16 |
|
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad) |
|
18 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
|
19 NS_INTERFACE_MAP_ENTRY(nsISupports) |
|
20 NS_INTERFACE_MAP_END |
|
21 |
|
22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(Gamepad, mParent, mButtons) |
|
23 |
|
24 Gamepad::Gamepad(nsISupports* aParent, |
|
25 const nsAString& aID, uint32_t aIndex, |
|
26 GamepadMappingType aMapping, |
|
27 uint32_t aNumButtons, uint32_t aNumAxes) |
|
28 : mParent(aParent), |
|
29 mID(aID), |
|
30 mIndex(aIndex), |
|
31 mMapping(aMapping), |
|
32 mConnected(true), |
|
33 mButtons(aNumButtons), |
|
34 mAxes(aNumAxes) |
|
35 { |
|
36 SetIsDOMBinding(); |
|
37 for (unsigned i = 0; i < aNumButtons; i++) { |
|
38 mButtons.InsertElementAt(i, new GamepadButton(mParent)); |
|
39 } |
|
40 mAxes.InsertElementsAt(0, aNumAxes, 0.0f); |
|
41 } |
|
42 |
|
43 void |
|
44 Gamepad::SetIndex(uint32_t aIndex) |
|
45 { |
|
46 mIndex = aIndex; |
|
47 } |
|
48 |
|
49 void |
|
50 Gamepad::SetConnected(bool aConnected) |
|
51 { |
|
52 mConnected = aConnected; |
|
53 } |
|
54 |
|
55 void |
|
56 Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue) |
|
57 { |
|
58 MOZ_ASSERT(aButton < mButtons.Length()); |
|
59 mButtons[aButton]->SetPressed(aPressed); |
|
60 mButtons[aButton]->SetValue(aValue); |
|
61 } |
|
62 |
|
63 void |
|
64 Gamepad::SetAxis(uint32_t aAxis, double aValue) |
|
65 { |
|
66 MOZ_ASSERT(aAxis < mAxes.Length()); |
|
67 if (mAxes[aAxis] != aValue) { |
|
68 mAxes[aAxis] = aValue; |
|
69 GamepadBinding::ClearCachedAxesValue(this); |
|
70 } |
|
71 } |
|
72 |
|
73 void |
|
74 Gamepad::SyncState(Gamepad* aOther) |
|
75 { |
|
76 if (mButtons.Length() != aOther->mButtons.Length() || |
|
77 mAxes.Length() != aOther->mAxes.Length()) { |
|
78 return; |
|
79 } |
|
80 |
|
81 mConnected = aOther->mConnected; |
|
82 for (uint32_t i = 0; i < mButtons.Length(); ++i) { |
|
83 mButtons[i]->SetPressed(aOther->mButtons[i]->Pressed()); |
|
84 mButtons[i]->SetValue(aOther->mButtons[i]->Value()); |
|
85 } |
|
86 bool changed = false; |
|
87 for (uint32_t i = 0; i < mAxes.Length(); ++i) { |
|
88 changed = changed || (mAxes[i] != aOther->mAxes[i]); |
|
89 mAxes[i] = aOther->mAxes[i]; |
|
90 } |
|
91 if (changed) { |
|
92 GamepadBinding::ClearCachedAxesValue(this); |
|
93 } |
|
94 } |
|
95 |
|
96 already_AddRefed<Gamepad> |
|
97 Gamepad::Clone(nsISupports* aParent) |
|
98 { |
|
99 nsRefPtr<Gamepad> out = |
|
100 new Gamepad(aParent, mID, mIndex, mMapping, |
|
101 mButtons.Length(), mAxes.Length()); |
|
102 out->SyncState(this); |
|
103 return out.forget(); |
|
104 } |
|
105 |
|
106 /* virtual */ JSObject* |
|
107 Gamepad::WrapObject(JSContext* aCx) |
|
108 { |
|
109 return GamepadBinding::Wrap(aCx, this); |
|
110 } |
|
111 |
|
112 } // namespace dom |
|
113 } // namespace mozilla |