1
/*
2
 * Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13 1
import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
14
import { XRProvider, XROptions, SceneOptions } from './types';
15 1
import { SumerianProvider } from './Providers/SumerianProvider';
16 1
import { XRProviderNotConfigured } from './Errors';
17

18 1
const logger = new Logger('XR');
19

20 1
const DEFAULT_PROVIDER_NAME = 'SumerianProvider';
21

22 1
export class XRClass {
23
	private _options: XROptions;
24

25
	private _pluggables: { [key: string]: XRProvider };
26
	private _defaultProvider: string;
27

28
	/**
29
	 * Initialize XR with AWS configurations
30
	 *
31
	 * @param {XROptions} options - Configuration object for XR
32
	 */
33
	constructor(options: XROptions) {
34 1
		this._options = options;
35 1
		logger.debug('XR Options', this._options);
36 1
		this._defaultProvider = DEFAULT_PROVIDER_NAME;
37 1
		this._pluggables = {};
38

39
		// Add default provider
40 1
		this.addPluggable(new SumerianProvider());
41
	}
42

43
	/**
44
	 * Configure XR part with configurations
45
	 *
46
	 * @param {XROptions} config - Configuration for XR
47
	 * @return {Object} - The current configuration
48
	 */
49 1
	configure(options: XROptions) {
50 1
		const opt = options ? options.XR || options : {};
51 1
		logger.debug('configure XR', { opt });
52

53 1
		this._options = Object.assign({}, this._options, opt);
54

55 1
		Object.entries(this._pluggables).map(([name, provider]) => {
56 1
			if (name === this._defaultProvider && !opt[this._defaultProvider]) {
57 1
				provider.configure(this._options);
58
			} else {
59 0
				provider.configure(this._options[name]);
60
			}
61
		});
62

63 1
		return this._options;
64
	}
65

66
	/**
67
	 * add plugin into XR category
68
	 * @param {Object} pluggable - an instance of the plugin
69
	 */
70 1
	public async addPluggable(pluggable: XRProvider) {
71 1
		if (pluggable && pluggable.getCategory() === 'XR') {
72 1
			this._pluggables[pluggable.getProviderName()] = pluggable;
73 1
			const config = pluggable.configure(this._options);
74

75 1
			return config;
76
		}
77
	}
78

79 1
	public async loadScene(
80
		sceneName: string,
81
		domElementId: string,
82 1
		sceneOptions: SceneOptions = {},
83 1
		provider: string = this._defaultProvider
84
	) {
85 1
		if (!this._pluggables[provider])
86 0
			throw new XRProviderNotConfigured(
87
				`Provider '${provider}' not configured`
88
			);
89 1
		return await this._pluggables[provider].loadScene(
90
			sceneName,
91
			domElementId,
92
			sceneOptions
93
		);
94
	}
95

96 1
	public isSceneLoaded(
97
		sceneName: string,
98 1
		provider: string = this._defaultProvider
99
	) {
100 1
		if (!this._pluggables[provider])
101 0
			throw new XRProviderNotConfigured(
102
				`Provider '${provider}' not configured`
103
			);
104 1
		return this._pluggables[provider].isSceneLoaded(sceneName);
105
	}
106

107 1
	public getSceneController(
108
		sceneName: string,
109 1
		provider: string = this._defaultProvider
110
	) {
111 1
		if (!this._pluggables[provider])
112 1
			throw new XRProviderNotConfigured(
113
				`Provider '${provider}' not configured`
114
			);
115 1
		return this._pluggables[provider].getSceneController(sceneName);
116
	}
117

118 1
	public isVRCapable(
119
		sceneName: string,
120 1
		provider: string = this._defaultProvider
121
	) {
122 1
		if (!this._pluggables[provider])
123 0
			throw new XRProviderNotConfigured(
124
				`Provider '${provider}' not configured`
125
			);
126 1
		return this._pluggables[provider].isVRCapable(sceneName);
127
	}
128

129 1
	public isVRPresentationActive(
130
		sceneName: string,
131 1
		provider: string = this._defaultProvider
132
	) {
133 1
		if (!this._pluggables[provider])
134 0
			throw new XRProviderNotConfigured(
135
				`Provider '${provider}' not configured`
136
			);
137 0
		return this._pluggables[provider].isVRPresentationActive(sceneName);
138
	}
139

140 1
	public start(sceneName: string, provider: string = this._defaultProvider) {
141 1
		if (!this._pluggables[provider])
142 0
			throw new XRProviderNotConfigured(
143
				`Provider '${provider}' not configured`
144
			);
145 0
		return this._pluggables[provider].start(sceneName);
146
	}
147

148 1
	public enterVR(sceneName: string, provider: string = this._defaultProvider) {
149 1
		if (!this._pluggables[provider])
150 0
			throw new XRProviderNotConfigured(
151
				`Provider '${provider}' not configured`
152
			);
153 0
		return this._pluggables[provider].enterVR(sceneName);
154
	}
155

156 1
	public exitVR(sceneName: string, provider: string = this._defaultProvider) {
157 1
		if (!this._pluggables[provider])
158 0
			throw new XRProviderNotConfigured(
159
				`Provider '${provider}' not configured`
160
			);
161 0
		return this._pluggables[provider].exitVR(sceneName);
162
	}
163

164 1
	public isMuted(sceneName: string, provider: string = this._defaultProvider) {
165 1
		if (!this._pluggables[provider])
166 0
			throw new XRProviderNotConfigured(
167
				`Provider '${provider}' not configured`
168
			);
169 1
		return this._pluggables[provider].isMuted(sceneName);
170
	}
171

172 1
	public setMuted(
173
		sceneName: string,
174
		muted: boolean,
175 1
		provider: string = this._defaultProvider
176
	) {
177 1
		if (!this._pluggables[provider])
178 0
			throw new XRProviderNotConfigured(
179
				`Provider '${provider}' not configured`
180
			);
181 1
		return this._pluggables[provider].setMuted(sceneName, muted);
182
	}
183

184 1
	public onSceneEvent(
185
		sceneName: string,
186
		eventName: string,
187
		eventHandler: Function,
188 1
		provider: string = this._defaultProvider
189
	) {
190 1
		if (!this._pluggables[provider])
191 0
			throw new XRProviderNotConfigured(
192
				`Provider '${provider}' not configured`
193
			);
194 0
		return this._pluggables[provider].onSceneEvent(
195
			sceneName,
196
			eventName,
197
			eventHandler
198
		);
199
	}
200

201 1
	public enableAudio(
202
		sceneName: string,
203 1
		provider: string = this._defaultProvider
204
	) {
205 1
		if (!this._pluggables[provider])
206 0
			throw new XRProviderNotConfigured(
207
				`Provider '${provider}' not configured`
208
			);
209 0
		return this._pluggables[provider].enableAudio(sceneName);
210
	}
211 1
}
212

213 1
export const XR = new XRClass(null);
214 1
Amplify.register(XR);

Read our documentation on viewing source code .

Loading