dom/gamepad/Gamepad.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/gamepad/Gamepad.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,111 @@
     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 +#ifndef mozilla_dom_gamepad_Gamepad_h
     1.9 +#define mozilla_dom_gamepad_Gamepad_h
    1.10 +
    1.11 +#include "mozilla/ErrorResult.h"
    1.12 +#include "mozilla/dom/GamepadButton.h"
    1.13 +#include <stdint.h>
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsString.h"
    1.16 +#include "nsTArray.h"
    1.17 +#include "nsWrapperCache.h"
    1.18 +
    1.19 +namespace mozilla {
    1.20 +namespace dom {
    1.21 +
    1.22 +enum GamepadMappingType
    1.23 +{
    1.24 +  NoMapping = 0,
    1.25 +  StandardMapping = 1
    1.26 +};
    1.27 +
    1.28 +class Gamepad : public nsISupports,
    1.29 +                public nsWrapperCache
    1.30 +{
    1.31 +public:
    1.32 +  Gamepad(nsISupports* aParent,
    1.33 +          const nsAString& aID, uint32_t aIndex,
    1.34 +          GamepadMappingType aMapping,
    1.35 +          uint32_t aNumButtons, uint32_t aNumAxes);
    1.36 +  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
    1.37 +  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Gamepad)
    1.38 +
    1.39 +  void SetConnected(bool aConnected);
    1.40 +  void SetButton(uint32_t aButton, bool aPressed, double aValue);
    1.41 +  void SetAxis(uint32_t aAxis, double aValue);
    1.42 +  void SetIndex(uint32_t aIndex);
    1.43 +
    1.44 +  // Make the state of this gamepad equivalent to other.
    1.45 +  void SyncState(Gamepad* aOther);
    1.46 +
    1.47 +  // Return a new Gamepad containing the same data as this object,
    1.48 +  // parented to aParent.
    1.49 +  already_AddRefed<Gamepad> Clone(nsISupports* aParent);
    1.50 +
    1.51 +  nsISupports* GetParentObject() const
    1.52 +  {
    1.53 +    return mParent;
    1.54 +  }
    1.55 +
    1.56 +  virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
    1.57 +
    1.58 +  void GetId(nsAString& aID) const
    1.59 +  {
    1.60 +    aID = mID;
    1.61 +  }
    1.62 +
    1.63 +  void GetMapping(nsAString& aMapping) const
    1.64 +  {
    1.65 +    if (mMapping == StandardMapping) {
    1.66 +      aMapping = NS_LITERAL_STRING("standard");
    1.67 +    } else {
    1.68 +      aMapping = NS_LITERAL_STRING("");
    1.69 +    }
    1.70 +  }
    1.71 +
    1.72 +  bool Connected() const
    1.73 +  {
    1.74 +    return mConnected;
    1.75 +  }
    1.76 +
    1.77 +  uint32_t Index() const
    1.78 +  {
    1.79 +    return mIndex;
    1.80 +  }
    1.81 +
    1.82 +  void GetButtons(nsTArray<nsRefPtr<GamepadButton>>& aButtons) const
    1.83 +  {
    1.84 +    aButtons = mButtons;
    1.85 +  }
    1.86 +
    1.87 +  void GetAxes(nsTArray<double>& aAxes) const
    1.88 +  {
    1.89 +    aAxes = mAxes;
    1.90 +  }
    1.91 +
    1.92 +private:
    1.93 +  virtual ~Gamepad() {}
    1.94 +
    1.95 +protected:
    1.96 +  nsCOMPtr<nsISupports> mParent;
    1.97 +  nsString mID;
    1.98 +  uint32_t mIndex;
    1.99 +
   1.100 +  // The mapping in use.
   1.101 +  GamepadMappingType mMapping;
   1.102 +
   1.103 +  // true if this gamepad is currently connected.
   1.104 +  bool mConnected;
   1.105 +
   1.106 +  // Current state of buttons, axes.
   1.107 +  nsTArray<nsRefPtr<GamepadButton>> mButtons;
   1.108 +  nsTArray<double> mAxes;
   1.109 +};
   1.110 +
   1.111 +} // namespace dom
   1.112 +} // namespace mozilla
   1.113 +
   1.114 +#endif // mozilla_dom_gamepad_Gamepad_h

mercurial