GamepadTouch.cpp (2296B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/GamepadTouch.h" 8 9 #include "mozilla/HoldDropJSObjects.h" 10 #include "mozilla/dom/GamepadManager.h" 11 #include "mozilla/dom/Promise.h" 12 #include "mozilla/dom/TypedArray.h" 13 14 namespace mozilla::dom { 15 16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(GamepadTouch, (mParent), 17 (mPosition, 18 mSurfaceDimensions)) 19 20 GamepadTouch::GamepadTouch(nsISupports* aParent) 21 : mParent(aParent), mPosition(nullptr), mSurfaceDimensions(nullptr) { 22 mozilla::HoldJSObjects(this); 23 mTouchState = GamepadTouchState(); 24 } 25 26 GamepadTouch::~GamepadTouch() { mozilla::DropJSObjects(this); } 27 28 /* virtual */ JSObject* GamepadTouch::WrapObject( 29 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 30 return GamepadTouch_Binding::Wrap(aCx, this, aGivenProto); 31 } 32 33 void GamepadTouch::GetPosition(JSContext* aCx, 34 JS::MutableHandle<JSObject*> aRetval, 35 ErrorResult& aRv) { 36 mPosition = Float32Array::Create(aCx, this, mTouchState.position, aRv); 37 if (aRv.Failed()) { 38 return; 39 } 40 41 aRetval.set(mPosition); 42 } 43 44 void GamepadTouch::GetSurfaceDimensions(JSContext* aCx, 45 JS::MutableHandle<JSObject*> aRetval, 46 ErrorResult& aRv) { 47 if (mTouchState.isSurfaceDimensionsValid) { 48 mSurfaceDimensions = 49 Uint32Array::Create(aCx, this, mTouchState.surfaceDimensions, aRv); 50 } else { 51 mSurfaceDimensions = Uint32Array::Create( 52 aCx, this, std::size(mTouchState.surfaceDimensions), aRv); 53 } 54 55 if (!mSurfaceDimensions) { 56 return; 57 } 58 59 aRetval.set(mSurfaceDimensions); 60 } 61 62 void GamepadTouch::SetTouchState(const GamepadTouchState& aTouch) { 63 mTouchState = aTouch; 64 } 65 66 void GamepadTouch::Set(const GamepadTouch* aOther) { 67 MOZ_ASSERT(aOther); 68 mTouchState = aOther->mTouchState; 69 } 70 71 } // namespace mozilla::dom