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/. */
9 #include "mozilla/dom/GamepadBinding.h"
14 NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad
)
15 NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad
)
17 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad
)
18 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
19 NS_INTERFACE_MAP_ENTRY(nsISupports
)
22 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Gamepad
, mParent
, mButtons
)
24 Gamepad::Gamepad(nsISupports
* aParent
,
25 const nsAString
& aID
, uint32_t aIndex
,
26 GamepadMappingType aMapping
,
27 uint32_t aNumButtons
, uint32_t aNumAxes
)
33 mButtons(aNumButtons
),
37 for (unsigned i
= 0; i
< aNumButtons
; i
++) {
38 mButtons
.InsertElementAt(i
, new GamepadButton(mParent
));
40 mAxes
.InsertElementsAt(0, aNumAxes
, 0.0f
);
44 Gamepad::SetIndex(uint32_t aIndex
)
50 Gamepad::SetConnected(bool aConnected
)
52 mConnected
= aConnected
;
56 Gamepad::SetButton(uint32_t aButton
, bool aPressed
, double aValue
)
58 MOZ_ASSERT(aButton
< mButtons
.Length());
59 mButtons
[aButton
]->SetPressed(aPressed
);
60 mButtons
[aButton
]->SetValue(aValue
);
64 Gamepad::SetAxis(uint32_t aAxis
, double aValue
)
66 MOZ_ASSERT(aAxis
< mAxes
.Length());
67 if (mAxes
[aAxis
] != aValue
) {
68 mAxes
[aAxis
] = aValue
;
69 GamepadBinding::ClearCachedAxesValue(this);
74 Gamepad::SyncState(Gamepad
* aOther
)
76 if (mButtons
.Length() != aOther
->mButtons
.Length() ||
77 mAxes
.Length() != aOther
->mAxes
.Length()) {
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());
87 for (uint32_t i
= 0; i
< mAxes
.Length(); ++i
) {
88 changed
= changed
|| (mAxes
[i
] != aOther
->mAxes
[i
]);
89 mAxes
[i
] = aOther
->mAxes
[i
];
92 GamepadBinding::ClearCachedAxesValue(this);
96 already_AddRefed
<Gamepad
>
97 Gamepad::Clone(nsISupports
* aParent
)
99 nsRefPtr
<Gamepad
> out
=
100 new Gamepad(aParent
, mID
, mIndex
, mMapping
,
101 mButtons
.Length(), mAxes
.Length());
102 out
->SyncState(this);
106 /* virtual */ JSObject
*
107 Gamepad::WrapObject(JSContext
* aCx
)
109 return GamepadBinding::Wrap(aCx
, this);
113 } // namespace mozilla