-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlauncher.js
365 lines (327 loc) · 11.8 KB
/
launcher.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/**
* ## [launcher](com/launcher.js)
*
* displays app (icons) in 2D and 3D handmenu (enduser can launch desktop-like 'apps')
*
* ```html
* <a-entity launcher>
* <a-entity launch="component: helloworld; foo: bar"><a-entity>
* </a-entity>
*
* ```
*
* | property | type | example |
* |--------------|--------------------|----------------------------------------------------------------------------------------|
* | `attach` | `selector` | hand or object to attach menu to |
* | `registries` | `array` of strings | `<a-entity launcher="registers: https://foo.com/index.json, ./index.json"/>` |
*
* | event | target | info |
* |--------------|-------------------------------------------------------------------------------------------------------------|
* | `launcher` | an app | when pressing an app icon, `launcher` event will be send to the respective app |
*
* There a multiple ways of letting the launcher know that an app can be launched:
*
* 1. any AFRAME component with an `launcher`-event + manifest is automatically added:
*
* ```javascript
* AFRAME.registerComponent('foo',{
* events:{
* launcher: function(){ ...launch something... }
* },
* manifest:{ // HTML5 manifesto JSON object
* // https://www.w3.org/TR/appmanifest/
* }
* }
* ```
*
* 2. dynamically in javascript
*
* ```javascript
* window.launcher.register({
* name:"foo",
* icon: "https://.../optional_icon.png"
* description: "lorem ipsum",
* cb: () => alert("foo")
* })
* //window.launcher.unregister('foo')
* ```
*
*/
AFRAME.registerComponent('launch', { // use this component to auto-launch component
init: function(){
this.el.sceneEl.addEventListener('loaded', () => {
setTimeout( () => this.el.emit('launcher',{}), 1000 )
})
}
})
AFRAME.registerComponent('launcher', {
schema: {
colors: { type:"array", "default": [
'#4C73FE',
'#554CFE',
'#864CFE',
'#B44CFE',
'#E24CFE',
'#FE4CD3',
'#333333',
]},
paused: { type:"boolean","default":false},
},
requires:{
dom: "com/dom.js",
htmlinxr: "com/html-as-texture-in-xr.js",
data2events: "com/data2event.js"
},
init: async function () {
this.el.object3D.visible = false;
await AFRAME.utils.require(this.requires)
this.worldPosition = new THREE.Vector3()
this.el.setAttribute("dom","")
this.el.setAttribute("pressable","")
this.el.sceneEl.addEventListener('enter-vr', () => this.centerMenu() )
this.el.sceneEl.addEventListener('enter-ar', () => this.centerMenu() )
this.render()
},
dom: {
scale: 0.8,
events: ['click'],
html: (me) => `<div class="iconmenu">loading components..</div>`,
css: (me) => `
.iconmenu {
z-index: 1000;
display: flex;
flex-direction: row;
align-items: flex-start;
height: 50px;
overflow:hidden;
position: fixed;
bottom: 10px;
left:20px;
background: transparent;
padding-bottom: 54px;
box-sizing: border-box;
pointer-events: none;
visibility: visible !important;
}
.iconmenu > button {
line-height:0px;
pointer-events:all;
width: 58px;
height: 34px;
padding: 12px 0px 12px 0px;
border-radius: 0px;
color: var(--xrsh-primary);
background: #FFF;
border-top: 2px solid #BBB;
border-bottom: 2px solid #BBB;
font-size:18px;
color: #777;
line-height: 7px;
}
.iconmenu > button:first-child {
border-radius: 5px 0px 0px 5px;
border-bottom: 2px solid #BBB;
border-left: 2px solid #BBB;
padding-bottom:16px;
}
.iconmenu > button:last-child {
border-radius:0px 5px 5px 0px;
border-top: 2px solid #BBB;
border-right: 2px solid #BBB;
padding-top:13px;
}
.iconmenu > button:only-child{
border-radius:5px 5px 5px 5px;
}
.iconmenu > button > img {
transform: translate(0px,-14px);
opacity:0.5;
padding: 5px;
border-radius: 0px;
}
.iconmenu > button > img:hover{
background: #AAA;
transition:0.gg3s;
border-radius: 50%;
}`
},
events:{
click: function(e){ },
DOMready: function(){
this.el.setAttribute("html-as-texture-in-xr", `domid: #${this.el.dom.id}; faceuser: false`)
}
},
preventAccidentalButtonPresses: function(){
this.data.paused = true
setTimeout( () => this.data.paused = false, 500 ) // prevent menubutton press collide with animated buttons
},
render: async function(els){
if( !this.el.dom ) return // too early (dom.js component not ready)
let colors = this.data.colors
const add2D = (launchCom,manifest) => {
let btn = document.createElement('button')
let iconDefault = "data:image/svg+xml;base64,PHN2ZwogIHdpZHRoPSIyNCIKICBoZWlnaHQ9IjI0IgogIHZpZXdCb3g9IjAgMCAyNCAyNCIKICBmaWxsPSJub25lIgogIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKPgogIDxwYXRoCiAgICBmaWxsLXJ1bGU9ImV2ZW5vZGQiCiAgICBjbGlwLXJ1bGU9ImV2ZW5vZGQiCiAgICBkPSJNMjAuMTcwMiAzTDIwLjE2NjMgMy4wMDQ1M0MyMS43NDU4IDMuMDkwODQgMjMgNC4zOTg5NiAyMyA2VjE4QzIzIDE5LjY1NjkgMjEuNjU2OSAyMSAyMCAyMUg0QzIuMzQzMTUgMjEgMSAxOS42NTY5IDEgMThWNkMxIDQuMzQzMTUgMi4zNDMxNSAzIDQgM0gyMC4xNzAyWk0xMC40NzY0IDVIMTYuNDc2NEwxMy4wODkgOUg3LjA4ODk5TDEwLjQ3NjQgNVpNNS4wODg5OSA5TDguNDc2NDQgNUg0QzMuNDQ3NzIgNSAzIDUuNDQ3NzIgMyA2VjlINS4wODg5OVpNMyAxMVYxOEMzIDE4LjU1MjMgMy40NDc3MiAxOSA0IDE5SDIwQzIwLjU1MjMgMTkgMjEgMTguNTUyMyAyMSAxOFYxMUgzWk0yMSA5VjZDMjEgNS40NDc3MSAyMC41NTIzIDUgMjAgNUgxOC40NzY0TDE1LjA4OSA5SDIxWiIKICAgIGZpbGw9ImN1cnJlbnRDb2xvciIKICAvPgo8L3N2Zz4="
let html = manifest?.icons?.length > 0 || !manifest.name ? `<img src='${manifest.icons[0].src || iconDefault}' title='${manifest.name}: ${manifest.description}'/>` : ""
if( manifest.name && !html ) html = `${manifest.short_name || manifest.name}`
btn.innerHTML = html
btn.addEventListener('click', (e) => {
launchCom.launcher()
e.stopPropagation()
// visual feedback to user
btn.style.filter = "brightness(0.5)"
this.el.components.html.rerender()
setTimeout( () => {
btn.style.filter = "brightness(1)"
this.el.components.html.rerender()
}, 500 )
return false
})
this.el.dom.appendChild(btn)
}
// finally render them!
this.el.dom.innerHTML = '' // clear
els = els || this.system.launchables
els.map( (c) => {
const manifest = c.manifest
if( manifest ){
add2D(c,manifest)
}
})
this.centerMenu();
},
centerMenu: function(){
// center along x-axis
this.el.object3D.traverse( (o) => {
if( o.constructor && String(o.constructor).match(/HTMLMesh/) ){
this.setOriginToMiddle(o, this.el.object3D, {x:true})
o.position.z = 0.012 // position a bit before the grab-line
o.position.y = -0.01 // position a bit before the grab-line
}
})
this.el.object3D.visible = true // ensure visibility
},
setOriginToMiddle: function(fromObject, toObject, axis) {
var boxFrom = new THREE.Box3().setFromObject(fromObject);
var boxTo = new THREE.Box3().setFromObject(toObject);
var center = new THREE.Vector3();
if( !toObject.positionOriginal ) toObject.positionOriginal = toObject.position.clone()
center.x = axis.x ? - (boxFrom.max.x/2) : 0
center.y = axis.y ? - (boxFrom.max.y/2) : 0
center.z = axis.z ? - (boxFrom.max.z/2) : 0
toObject.position.copy( toObject.positionOriginal )
toObject.position.sub(center);
},
manifest: { // HTML5 manifest to identify app to xrsh
"short_name": "launcher",
"name": "App Launcher",
"icons": [
{
"src": "https://css.gg/browser.svg",
"type": "image/svg+xml",
"sizes": "512x512"
}
],
"category":"system",
"id": "/?source=pwa",
"start_url": "/?source=pwa",
"background_color": "#3367D6",
"display": "standalone",
"scope": "/",
"theme_color": "#3367D6",
"shortcuts": [
{
"name": "What is the latest news?",
"cli":{
"usage": "helloworld <type> [options]",
"example": "helloworld news",
"args":{
"--latest": {type:"string"}
}
},
"short_name": "Today",
"description": "View weather information for today",
"url": "/today?source=pwa",
"icons": [{ "src": "/images/today.png", "sizes": "192x192" }]
}
],
"description": "Hello world information",
"screenshots": [
{
"src": "/images/screenshot1.png",
"type": "image/png",
"sizes": "540x720",
"form_factor": "narrow"
}
],
"help":`
Helloworld application
This is a help file which describes the application.
It will be rendered thru troika text, and will contain
headers based on non-punctualized lines separated by linebreaks,
in above's case "\nHelloworld application\n" will qualify as header.
`
}
});
AFRAME.registerSystem('launcher',{
init: function(){
this.launchables = []
this.dom = []
this.registered = []
// observe HTML changes in <a-scene>
observer = new MutationObserver( (a,b) => this.getLaunchables(a,b) )
observer.observe( this.sceneEl, {characterData: false, childList: true, attributes: false});
window.launcher = this
this.getLaunchables()
},
register: function(launchable){
try{
let {name, description, cb} = launchable
this.registered.push({
manifest: {name, description, icons: launchable.icon ? [{src:launchable.icon}] : [] },
launcher: cb
})
}catch(e){
console.error('AFRAME.systems.launcher.register({ name, description, icon, cb }) got invalid obj')
console.error(e)
}
this.getLaunchables()
},
unregister: function(launchableName){
this.registered = this.registered.filter( (l) => l.name != launchableName )
},
getLaunchables: function(mutationsList,observer){
let searchEvent = 'launcher'
let els = [...this.sceneEl.getElementsByTagName("*")]
let seen = {}
this.launchables = [
/*
* {
* manifest: {...}
* launcher: () => ....
* }
*/
];
// collect manually registered launchables
this.registered.map( (launchable) => this.launchables.push(launchable) )
// collect launchables in aframe dom elements
this.dom = els.filter( (el) => {
let hasEvent = false
if( el.components ){
for( let i in el.components ){
if( el.components[i].events && el.components[i].events[searchEvent] && !seen[i] ){
let com = hasEvent = seen[i] = el.components[i]
com.launcher = () => com.el.emit('launcher',null,false) // important: no bubble
this.launchables.push(com)
}
}
}
return hasEvent ? el : null
})
this.updateLauncher()
return this.launchables
},
updateLauncher: function(){
let launcher = document.querySelector('[launcher]')
if( launcher && launcher.components.launcher) launcher.components.launcher.render()
}
})