-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathflutter_3d_datasource_web.dart
More file actions
147 lines (132 loc) · 4.26 KB
/
flutter_3d_datasource_web.dart
File metadata and controls
147 lines (132 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// ignore_for_file: avoid_web_libraries_in_flutter
import 'package:flutter_3d_controller/src/core/exception/flutter_3d_controller_exception.dart';
import 'package:flutter_3d_controller/src/data/datasources/i_flutter_3d_datasource.dart';
import 'dart:js' as js;
class Flutter3DDatasource implements IFlutter3DDatasource {
final String _viewerId;
Flutter3DDatasource(
this._viewerId, [
webViewController,
activeGestureInterceptor = false,
]);
@override
void playAnimation({
String? animationName,
int loopCount = 0,
}) {
String loopValue = loopCount <= 0 ? 'Infinity' : loopCount.toString();
animationName == null
? executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.updateComplete.then(() => {"
"modelViewer.play({repetitions: \"$loopValue\"});"
"});")
: executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.pause();"
"modelViewer.animationName = \"\";"
"modelViewer.animationName = \"$animationName\";"
"modelViewer.updateComplete.then(() => {"
"modelViewer.play({repetitions: \"$loopValue\"});"
"});");
}
@override
void pauseAnimation() {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.pause();",
);
}
@override
void resetAnimation() {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.pause();"
"modelViewer.currentTime = 0;"
"modelViewer.play();",
);
}
@override
void stopAnimation() {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.pause();"
"modelViewer.currentTime = 0;",
);
}
@override
Future<List<String>> getAvailableAnimations() async {
try {
final result = await executeCustomJsCodeWithResult(
"document.getElementById(\"$_viewerId\").availableAnimations;",
);
return result.map<String>((e) => e.toString()).toList();
} catch (e) {
throw Flutter3dControllerFormatException(
message: 'Failed to retrieve animation list, ${e.toString()}');
}
}
@override
void setTexture({required String textureName}) {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.variantName = \"$textureName\";",
);
}
@override
Future<List<String>> getAvailableTextures() async {
final result = await executeCustomJsCodeWithResult(
"document.getElementById(\"$_viewerId\").availableVariants;",
);
return result.map<String>((e) => e.toString()).toList();
}
@override
void setCameraTarget(double x, double y, double z) {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.cameraTarget = \"${x}m ${y}m ${z}m\";",
);
}
@override
void resetCameraTarget() {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.cameraTarget = \"auto auto auto\";",
);
}
@override
void setCameraOrbit(double theta, double phi, double radius, bool isAnimate) {
if (isAnimate) {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.cameraOrbit = \"${theta}deg ${phi}deg $radius%\";",
);
} else {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.cameraOrbit = \"${theta}deg ${phi}deg $radius%\";"
"modelViewer.jumpCameraToGoal();",
);
}
}
@override
void resetCameraOrbit() {
executeCustomJsCode(
"const modelViewer = document.getElementById(\"$_viewerId\");"
"modelViewer.cameraOrbit = \"0deg 75deg 105%\" ;",
);
}
@override
void executeCustomJsCode(String code) {
js.context.callMethod(
"customEvaluate",
[code],
);
}
@override
Future<dynamic> executeCustomJsCodeWithResult(String code) async {
final js.JsArray<dynamic> result =
await js.context.callMethod("customEvaluateWithResult", [code]);
return result.toList();
}
}