/**
* ## [launcher](com/launcher.js)
*
* displays app (icons) in 2D and 3D handmenu (enduser can launch desktop-like 'apps')
*
* ```html
*
*
*
*
*
*
* ```
*
* | property | type | example |
* |--------------|--------------------|----------------------------------------------------------------------------------------|
* | `attach` | `selector` | hand or object to attach menu to |
* | `registries` | `array` of strings | `` |
*
* | 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: {
attach: { type:"selector", default:"#left-hand"},
padding: { type:"number","default":0.15},
fingerTip: {type:"selector", default:"#right-hand"},
fingerDistance: {type:"number", "default":0.25},
open: { type:"boolean", "default":true},
colors: { type:"array", "default": [
'#4C73FE',
'#554CFE',
'#864CFE',
'#B44CFE',
'#E24CFE',
'#FE4CD3',
'#333333',
]},
paused: { type:"boolean","default":false},
cols: { type:"number", "default": 5 }
},
requires:{
dom: "com/dom.js",
htmlinxr: "com/html-as-texture-in-xr.js",
data2events: "com/data2event.js"
},
init: async function () {
await AFRAME.utils.require(this.requires)
this.worldPosition = new THREE.Vector3()
this.el.setAttribute("dom","")
this.el.setAttribute("pressable","")
this.render()
},
isHand: (el) => {
return el.getAttributeNames().filter( (n) => n.match(/^hand-tracking/) ? n : null ).length ? true : false
},
dom: {
scale: 0.4,
events: ['click'],
html: (me) => `
`,
css: (me) => `.iconmenu {
z-index: 1000;
display: flex;
flex-direction: row;
align-items: flex-start;
height: 50px;
overflow:hidden;
position: fixed;
right: 162px;
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;
}
.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:{
DOMready: function(){
this.el.setAttribute("html-as-texture-in-xr", `domid: #${this.el.dom.id}; faceuser: true`)
}
},
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 requires = []
let i = 0
let j = 0
let colors = this.data.colors
const add2D = (launchCom,manifest) => {
let btn = document.createElement('button')
let iconDefault = "data:image/svg+xml;base64,PHN2ZwogIHdpZHRoPSIyNCIKICBoZWlnaHQ9IjI0IgogIHZpZXdCb3g9IjAgMCAyNCAyNCIKICBmaWxsPSJub25lIgogIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKPgogIDxwYXRoCiAgICBmaWxsLXJ1bGU9ImV2ZW5vZGQiCiAgICBjbGlwLXJ1bGU9ImV2ZW5vZGQiCiAgICBkPSJNMjAuMTcwMiAzTDIwLjE2NjMgMy4wMDQ1M0MyMS43NDU4IDMuMDkwODQgMjMgNC4zOTg5NiAyMyA2VjE4QzIzIDE5LjY1NjkgMjEuNjU2OSAyMSAyMCAyMUg0QzIuMzQzMTUgMjEgMSAxOS42NTY5IDEgMThWNkMxIDQuMzQzMTUgMi4zNDMxNSAzIDQgM0gyMC4xNzAyWk0xMC40NzY0IDVIMTYuNDc2NEwxMy4wODkgOUg3LjA4ODk5TDEwLjQ3NjQgNVpNNS4wODg5OSA5TDguNDc2NDQgNUg0QzMuNDQ3NzIgNSAzIDUuNDQ3NzIgMyA2VjlINS4wODg5OVpNMyAxMVYxOEMzIDE4LjU1MjMgMy40NDc3MiAxOSA0IDE5SDIwQzIwLjU1MjMgMTkgMjEgMTguNTUyMyAyMSAxOFYxMUgzWk0yMSA5VjZDMjEgNS40NDc3MSAyMC41NTIzIDUgMjAgNUgxOC40NzY0TDE1LjA4OSA5SDIxWiIKICAgIGZpbGw9ImN1cnJlbnRDb2xvciIKICAvPgo8L3N2Zz4="
btn.innerHTML = `${ manifest?.icons?.length > 0
? `
`
: `${manifest.short_name || manifest.name}`
}`
btn.addEventListener('click', () => launchCom.launcher() )
this.el.dom.appendChild(btn)
}
// finally render them!
this.el.dom.innerHTML = '' // clear
els = els || this.system.launchables
els.map( (c) => {
// console.warn(`could not find component '${launchComponentKey}' (forgot to include script-tag?)`)
const manifest = c.manifest
if( manifest ){
add2D(c,manifest)
}
})
this.setOriginToMiddle(this.el.object3D)
},
setOriginToMiddle: function(object) {
var box = new THREE.Box3().setFromObject(object);
var center = new THREE.Vector3();
box.getCenter(center);
object.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 [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
observer = new MutationObserver( (a,b) => this.getLaunchables(a,b) )
observer.observe( this.sceneEl, {characterData: false, childList: true, attributes: false});
window.launcher = this
for( let i = 0; i < 10; i++){
window.launcher.register({
name:"foo"+i,
// icon: "https://..."
description: "lorem ipsum",
cb: () => {
let el = document.createElement("a-box")
el.setAttribute("position", `${Math.random()*10} ${Math.random()*10} ${Math.random()*10}`)
this.el.sceneEl.appendChild(el)
}
})
}
this.getLaunchables()
},
register: function(launchable){
try{
let {name, description, cb} = launchable
this.registered.push({
manifest: {name, description, icons: [{src:launchable.icon}]},
launcher: cb
})
}catch(e){
console.error('AFRAME.systems.launcher.register({ name, description, icon, cb }) got invalid obj')
console.error(e)
}
},
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',{})
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()
}
})