dom/gamepad/Gamepad.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/gamepad/Gamepad.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "Gamepad.h"
     1.9 +#include "nsAutoPtr.h"
    1.10 +#include "nsTArray.h"
    1.11 +#include "nsVariant.h"
    1.12 +#include "mozilla/dom/GamepadBinding.h"
    1.13 +
    1.14 +namespace mozilla {
    1.15 +namespace dom {
    1.16 +
    1.17 +NS_IMPL_CYCLE_COLLECTING_ADDREF(Gamepad)
    1.18 +NS_IMPL_CYCLE_COLLECTING_RELEASE(Gamepad)
    1.19 +
    1.20 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Gamepad)
    1.21 +  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    1.22 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.23 +NS_INTERFACE_MAP_END
    1.24 +
    1.25 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(Gamepad, mParent, mButtons)
    1.26 +
    1.27 +Gamepad::Gamepad(nsISupports* aParent,
    1.28 +                 const nsAString& aID, uint32_t aIndex,
    1.29 +                 GamepadMappingType aMapping,
    1.30 +                 uint32_t aNumButtons, uint32_t aNumAxes)
    1.31 +  : mParent(aParent),
    1.32 +    mID(aID),
    1.33 +    mIndex(aIndex),
    1.34 +    mMapping(aMapping),
    1.35 +    mConnected(true),
    1.36 +    mButtons(aNumButtons),
    1.37 +    mAxes(aNumAxes)
    1.38 +{
    1.39 +  SetIsDOMBinding();
    1.40 +  for (unsigned i = 0; i < aNumButtons; i++) {
    1.41 +    mButtons.InsertElementAt(i, new GamepadButton(mParent));
    1.42 +  }
    1.43 +  mAxes.InsertElementsAt(0, aNumAxes, 0.0f);
    1.44 +}
    1.45 +
    1.46 +void
    1.47 +Gamepad::SetIndex(uint32_t aIndex)
    1.48 +{
    1.49 +  mIndex = aIndex;
    1.50 +}
    1.51 +
    1.52 +void
    1.53 +Gamepad::SetConnected(bool aConnected)
    1.54 +{
    1.55 +  mConnected = aConnected;
    1.56 +}
    1.57 +
    1.58 +void
    1.59 +Gamepad::SetButton(uint32_t aButton, bool aPressed, double aValue)
    1.60 +{
    1.61 +  MOZ_ASSERT(aButton < mButtons.Length());
    1.62 +  mButtons[aButton]->SetPressed(aPressed);
    1.63 +  mButtons[aButton]->SetValue(aValue);
    1.64 +}
    1.65 +
    1.66 +void
    1.67 +Gamepad::SetAxis(uint32_t aAxis, double aValue)
    1.68 +{
    1.69 +  MOZ_ASSERT(aAxis < mAxes.Length());
    1.70 +  if (mAxes[aAxis] != aValue) {
    1.71 +    mAxes[aAxis] = aValue;
    1.72 +    GamepadBinding::ClearCachedAxesValue(this);
    1.73 +  }
    1.74 +}
    1.75 +
    1.76 +void
    1.77 +Gamepad::SyncState(Gamepad* aOther)
    1.78 +{
    1.79 +  if (mButtons.Length() != aOther->mButtons.Length() ||
    1.80 +      mAxes.Length() != aOther->mAxes.Length()) {
    1.81 +    return;
    1.82 +  }
    1.83 +
    1.84 +  mConnected = aOther->mConnected;
    1.85 +  for (uint32_t i = 0; i < mButtons.Length(); ++i) {
    1.86 +    mButtons[i]->SetPressed(aOther->mButtons[i]->Pressed());
    1.87 +    mButtons[i]->SetValue(aOther->mButtons[i]->Value());
    1.88 +  }
    1.89 +  bool changed = false;
    1.90 +  for (uint32_t i = 0; i < mAxes.Length(); ++i) {
    1.91 +    changed = changed || (mAxes[i] != aOther->mAxes[i]);
    1.92 +    mAxes[i] = aOther->mAxes[i];
    1.93 +  }
    1.94 +  if (changed) {
    1.95 +    GamepadBinding::ClearCachedAxesValue(this);
    1.96 +  }
    1.97 +}
    1.98 +
    1.99 +already_AddRefed<Gamepad>
   1.100 +Gamepad::Clone(nsISupports* aParent)
   1.101 +{
   1.102 +  nsRefPtr<Gamepad> out =
   1.103 +    new Gamepad(aParent, mID, mIndex, mMapping,
   1.104 +                mButtons.Length(), mAxes.Length());
   1.105 +  out->SyncState(this);
   1.106 +  return out.forget();
   1.107 +}
   1.108 +
   1.109 +/* virtual */ JSObject*
   1.110 +Gamepad::WrapObject(JSContext* aCx)
   1.111 +{
   1.112 +  return GamepadBinding::Wrap(aCx, this);
   1.113 +}
   1.114 +
   1.115 +} // namespace dom
   1.116 +} // namespace mozilla

mercurial