From 9da4d8f26852e40cd40cdc007ee64960da827f97 Mon Sep 17 00:00:00 2001 From: Mark Hu Date: Sun, 14 Jun 2020 20:15:09 +0800 Subject: [PATCH] add sensor control --- example/lib/main.dart | 4 +++- example/pubspec.lock | 16 ++++++++-------- lib/panorama.dart | 20 ++++++++++++++++++++ pubspec.lock | 17 +++++++++-------- pubspec.yaml | 1 + 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/example/lib/main.dart b/example/lib/main.dart index 1d75f9e..bf555a7 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -35,7 +35,9 @@ class _MyHomePageState extends State { title: Text(widget.title), ), body: Panorama( - animSpeed: 2.0, + animSpeed: 0, + interactive: false, + sensorControl: SensorControl.Orientation, child: _imageFile != null ? Image.file(_imageFile) : Image.asset('assets/panorama.jpg'), ), floatingActionButton: FloatingActionButton( diff --git a/example/pubspec.lock b/example/pubspec.lock index db367e9..3531fc1 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -109,6 +109,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.8" + motion_sensors: + dependency: transitive + description: + name: motion_sensors + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1" panorama: dependency: "direct main" description: @@ -123,13 +130,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.4" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.0+1" petitparser: dependency: transitive description: @@ -190,7 +190,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.11" + version: "0.2.15" typed_data: dependency: transitive description: diff --git a/lib/panorama.dart b/lib/panorama.dart index 46d6db7..70221fd 100644 --- a/lib/panorama.dart +++ b/lib/panorama.dart @@ -5,6 +5,12 @@ import 'dart:ui' as ui; import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:flutter_cube/flutter_cube.dart'; +import 'package:motion_sensors/motion_sensors.dart'; + +enum SensorControl { + None, + Orientation, +} class Panorama extends StatefulWidget { Panorama({ @@ -24,6 +30,7 @@ class Panorama extends StatefulWidget { this.latSegments = 32, this.lonSegments = 64, this.interactive = true, + this.sensorControl = SensorControl.None, this.child, }) : super(key: key); @@ -72,6 +79,9 @@ class Panorama extends StatefulWidget { /// Interact with the panorama. default to true final bool interactive; + /// Control the panorama with motion sensors. + final SensorControl sensorControl; + /// Specify an Image(equirectangular image) widget to the panorama. final Image child; @@ -148,6 +158,16 @@ class _PanoramaState extends State with SingleTickerProviderStateMixin latitude = widget.latitude; longitude = widget.longitude; + if (widget.sensorControl == SensorControl.Orientation) { + motionSensors.orientation.listen((OrientationEvent event) { + Quaternion q = Quaternion.euler(-event.roll, event.pitch, event.yaw); + q *= Quaternion.axisAngle(Vector3(1, 0, 0), math.pi * 0.5); + q.rotate(scene.camera.target..setFrom(Vector3(0, 0, -_radius))); + q.rotate(scene.camera.up..setFrom(Vector3(0, 1, 0))); + scene.update(); + }); + } + _controller = AnimationController(duration: Duration(milliseconds: 60000), vsync: this) ..addListener(() { if (scene == null) return; diff --git a/pubspec.lock b/pubspec.lock index 66ab63a..c09a35e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -95,6 +95,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.8" + motion_sensors: + dependency: "direct main" + description: + name: motion_sensors + url: "https://pub.dartlang.org" + source: hosted + version: "0.0.1" path: dependency: transitive description: @@ -102,13 +109,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.4" - pedantic: - dependency: transitive - description: - name: pedantic - url: "https://pub.dartlang.org" - source: hosted - version: "1.8.0+1" petitparser: dependency: transitive description: @@ -169,7 +169,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.11" + version: "0.2.15" typed_data: dependency: transitive description: @@ -193,3 +193,4 @@ packages: version: "3.5.0" sdks: dart: ">=2.4.0 <3.0.0" + flutter: ">=1.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index 468edb0..572b6a5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ dependencies: flutter: sdk: flutter flutter_cube: ^0.0.3 + motion_sensors: ^0.0.1 dev_dependencies: flutter_test: