xrsh-com/com/launcher.js

360 lines
12 KiB
JavaScript
Raw Normal View History

2025-02-28 17:05:43 +01:00
/**
* ## [launcher](com/launcher.js)
2024-01-22 09:38:16 +00:00
*
2025-02-28 17:05:43 +01:00
* displays app (icons) in 2D and 3D handmenu (enduser can launch desktop-like 'apps')
2024-01-22 09:38:16 +00:00
*
2025-02-28 17:05:43 +01:00
* ```html
* <a-entity launcher="attach: #left-hand"></a-entity>
*
* <a-assets>
* <a-mixin id="menuitem" geometry="primitive: plane; width: 0.15; height: 0.15; depth: 0.02" obb-collider="size: 0.03 0.03 0.03" ></a-mixin>
* <a-mixin id="menubutton" geometry="primitive: circle; radius: 0.025" material="side: double; color:#4C73FE"
* obb-collider="size: 0.03 0.03 0.03" position="-0.003 -0.027 -0.077" rotation="94.53 -6.35 -59.0"></a-mixin>
* </a-assets>
2024-01-22 09:38:16 +00:00
* ```
2024-04-19 16:42:46 +02:00
*
2024-01-22 09:38:16 +00:00
* | property | type | example |
* |--------------|--------------------|----------------------------------------------------------------------------------------|
2025-02-28 17:05:43 +01:00
* | `attach` | `selector` | hand or object to attach menu to |
* | `registries` | `array` of strings | `<a-entity launcher="registers: https://foo.com/index.json, ./index.json"/>` |
2024-01-22 09:38:16 +00:00
*
* | event | target | info |
* |--------------|-------------------------------------------------------------------------------------------------------------|
2024-04-19 16:42:46 +02:00
* | `launcher` | an app | when pressing an app icon, `launcher` event will be send to the respective app |
2025-02-28 17:05:43 +01:00
*
* 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')
* ```
*
*/
2024-01-22 09:38:16 +00:00
2024-06-28 16:59:27 +02:00
AFRAME.registerComponent('launch', { // use this component to auto-launch component
init: function(){
this.el.sceneEl.addEventListener('loaded', () => {
setTimeout( () => this.el.emit('launcher',{}), 1000 )
})
}
})
2024-01-22 09:38:16 +00:00
AFRAME.registerComponent('launcher', {
2024-04-19 16:42:46 +02:00
schema: {
2025-02-28 17:05:43 +01:00
attach: { type:"selector", default:"#left-hand"},
2024-05-24 14:37:05 +00:00
padding: { type:"number","default":0.15},
2025-02-28 17:05:43 +01:00
fingerTip: {type:"selector", default:"#right-hand"},
2024-05-24 14:37:05 +00:00
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 }
2024-01-22 09:38:16 +00:00
},
2025-02-28 17:05:43 +01:00
requires:{
dom: "com/dom.js",
htmlinxr: "com/html-as-texture-in-xr.js",
data2events: "com/data2event.js"
2024-09-06 08:30:24 +00:00
},
2024-05-23 12:14:53 +00:00
init: async function () {
2025-02-28 17:05:43 +01:00
await AFRAME.utils.require(this.requires)
2024-05-24 14:37:05 +00:00
this.worldPosition = new THREE.Vector3()
2024-01-22 09:38:16 +00:00
2024-05-23 12:14:53 +00:00
this.el.setAttribute("dom","")
this.render()
2024-05-24 14:37:05 +00:00
2025-02-28 17:05:43 +01:00
// this.tick = AFRAME.utils.throttleTick( this.tick, 100, this );
// this.el.sceneEl.addEventListener('enter-vr', (e) => this.preventAccidentalButtonPresses() )
// this.el.sceneEl.addEventListener('enter-ar', (e) => this.preventAccidentalButtonPresses() )
// this.el.sceneEl.addEventListener('exit-vr', (e) => this.preventAccidentalButtonPresses() )
// this.el.sceneEl.addEventListener('exit-ar', (e) => this.preventAccidentalButtonPresses() )
//if( this.data.attach ){
// if( this.isHand(this.data.attach) ){
// this.data.attach.addEventListener('model-loaded', () => {
// this.ready = true
// //this.data.attach.appendChild(this.el)
// let armature = this.data.attach.object3D.getObjectByName('Armature')
// if( !armature ) return console.warn('cannot find armature')
// let object3D = this.el.object3D.children[0]
// this.el.remove()
// setTimeout( () => this.data.attach.object3D.add(object3D), 500)
// })
// }else this.data.attach.appendChild(this.el)
//}else console.warn("launcher.js: attach-option not given")
2024-05-24 14:37:05 +00:00
},
isHand: (el) => {
return el.getAttributeNames().filter( (n) => n.match(/^hand-tracking/) ? n : null ).length ? true : false
2024-01-22 09:38:16 +00:00
},
dom: {
2025-02-28 17:05:43 +01:00
scale: 1,
2024-01-22 09:38:16 +00:00
events: ['click'],
html: (me) => `<div class="iconmenu">loading components..</div>`,
css: (me) => `.iconmenu {
2024-01-22 09:38:16 +00:00
z-index: 1000;
display: flex;
2024-04-19 16:42:46 +02:00
flex-direction: row;
align-items: flex-start;
height: 50px;
overflow:hidden;
2024-01-22 09:38:16 +00:00
position: fixed;
2024-04-19 16:42:46 +02:00
right: 162px;
2024-05-29 16:13:53 +00:00
bottom: 10px;
2024-04-19 16:42:46 +02:00
left:20px;
background: transparent;
padding-bottom: 54px;
2024-01-22 09:38:16 +00:00
box-sizing: border-box;
pointer-events: none;
2024-01-22 11:13:41 +00:00
visibility: visible !important;
2024-01-22 09:38:16 +00:00
}
.iconmenu > button {
2024-04-19 16:42:46 +02:00
line-height:0px;
2024-01-22 09:38:16 +00:00
pointer-events:all;
width: 58px;
2024-04-19 16:42:46 +02:00
height: 34px;
padding: 12px 0px 12px 0px;
2024-01-22 09:38:16 +00:00
border-radius: 0px;
color: var(--xrsh-primary);
background: #FFF;
2024-04-19 16:42:46 +02:00
border-top: 2px solid #BBB;
border-bottom: 2px solid #BBB;
font-size:18px;
2024-01-22 09:38:16 +00:00
}
.iconmenu > button:first-child {
2024-04-19 16:42:46 +02:00
border-radius: 5px 0px 0px 5px;
border-bottom: 2px solid #BBB;
border-left: 2px solid #BBB;
padding-bottom:16px;
2024-01-22 09:38:16 +00:00
}
.iconmenu > button:last-child {
2024-04-19 16:42:46 +02:00
border-radius:0px 5px 5px 0px;
border-top: 2px solid #BBB;
border-right: 2px solid #BBB;
2024-01-22 09:38:16 +00:00
padding-top:13px;
}
2025-02-28 17:05:43 +01:00
.iconmenu > button:only-child{
border-radius:5px 5px 5px 5px;
}
.iconmenu > button > img {
2024-04-19 16:42:46 +02:00
transform: translate(0px,-14px);
2024-01-22 09:38:16 +00:00
opacity:0.5;
padding: 5px;
border-radius: 0px;
}
.iconmenu > button > img:hover{
2024-01-22 11:13:41 +00:00
background: #AAA;
2024-01-22 09:38:16 +00:00
transition:0.gg3s;
border-radius: 50%;
}`
},
events:{
2025-02-28 17:05:43 +01:00
DOMready: function(){
this.el.setAttribute("html-as-texture-in-xr", `domid: #${this.el.dom.id}; faceuser: true`)
2024-05-24 14:37:05 +00:00
}
},
preventAccidentalButtonPresses: function(){
this.data.paused = true
setTimeout( () => this.data.paused = false, 500 ) // prevent menubutton press collide with animated buttons
},
2024-01-22 09:38:16 +00:00
2024-06-14 15:52:44 +02:00
render: async function(els){
2024-05-23 12:14:53 +00:00
if( !this.el.dom ) return // too early (dom.js component not ready)
let requires = []
2024-05-24 14:37:05 +00:00
let i = 0
let j = 0
let colors = this.data.colors
const add2D = (launchCom,el,manifest) => {
2024-05-23 12:14:53 +00:00
let btn = document.createElement('button')
2025-02-28 17:05:43 +01:00
let iconDefault = "data:image/svg+xml;base64,PHN2ZwogIHdpZHRoPSIyNCIKICBoZWlnaHQ9IjI0IgogIHZpZXdCb3g9IjAgMCAyNCAyNCIKICBmaWxsPSJub25lIgogIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIKPgogIDxwYXRoCiAgICBmaWxsLXJ1bGU9ImV2ZW5vZGQiCiAgICBjbGlwLXJ1bGU9ImV2ZW5vZGQiCiAgICBkPSJNMjAuMTcwMiAzTDIwLjE2NjMgMy4wMDQ1M0MyMS43NDU4IDMuMDkwODQgMjMgNC4zOTg5NiAyMyA2VjE4QzIzIDE5LjY1NjkgMjEuNjU2OSAyMSAyMCAyMUg0QzIuMzQzMTUgMjEgMSAxOS42NTY5IDEgMThWNkMxIDQuMzQzMTUgMi4zNDMxNSAzIDQgM0gyMC4xNzAyWk0xMC40NzY0IDVIMTYuNDc2NEwxMy4wODkgOUg3LjA4ODk5TDEwLjQ3NjQgNVpNNS4wODg5OSA5TDguNDc2NDQgNUg0QzMuNDQ3NzIgNSAzIDUuNDQ3NzIgMyA2VjlINS4wODg5OVpNMyAxMVYxOEMzIDE4LjU1MjMgMy40NDc3MiAxOSA0IDE5SDIwQzIwLjU1MjMgMTkgMjEgMTguNTUyMyAyMSAxOFYxMUgzWk0yMSA5VjZDMjEgNS40NDc3MSAyMC41NTIzIDUgMjAgNUgxOC40NzY0TDE1LjA4OSA5SDIxWiIKICAgIGZpbGw9ImN1cnJlbnRDb2xvciIKICAvPgo8L3N2Zz4="
2024-05-23 12:14:53 +00:00
btn.innerHTML = `${ manifest?.icons?.length > 0
2025-02-28 17:05:43 +01:00
? `<img src='${manifest.icons[0].src || iconDefault}' title='${manifest.name}: ${manifest.description}'/>`
: `${manifest.short_name || manifest.name}`
2024-05-23 12:14:53 +00:00
}`
btn.addEventListener('click', () => el.emit('launcher',{}) )
this.el.dom.appendChild(btn)
}
// finally render them!
this.el.dom.innerHTML = '' // clear
2025-02-28 17:05:43 +01:00
els = els || this.system.launchables
2024-06-14 15:52:44 +02:00
els.map( (c) => {
2025-02-28 17:05:43 +01:00
// console.warn(`could not find component '${launchComponentKey}' (forgot to include script-tag?)`)
const manifest = c.manifest
2024-05-23 12:14:53 +00:00
if( manifest ){
2025-02-28 17:05:43 +01:00
add2D(c,c.el,manifest)
2024-05-23 12:14:53 +00:00
}
})
2024-04-19 16:42:46 +02:00
2024-05-24 14:37:05 +00:00
},
tick: function(){
2024-01-22 09:38:16 +00:00
},
manifest: { // HTML5 manifest to identify app to xrsh
2024-04-19 16:42:46 +02:00
"short_name": "launcher",
"name": "App Launcher",
2024-01-22 09:38:16 +00:00
"icons": [
{
"src": "https://css.gg/browser.svg",
"type": "image/svg+xml",
"sizes": "512x512"
}
],
2024-04-19 16:42:46 +02:00
"category":"system",
2024-01-22 09:38:16 +00:00
"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":`
2024-04-19 16:42:46 +02:00
Helloworld application
2024-01-22 09:38:16 +00:00
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.
`
}
});
2024-05-23 12:14:53 +00:00
AFRAME.registerSystem('launcher',{
init: function(){
2025-02-28 17:05:43 +01:00
this.launchables = []
this.dom = []
this.registered = []
2024-05-24 14:37:05 +00:00
// observe HTML changes in <a-scene>
2024-05-23 12:14:53 +00:00
observer = new MutationObserver( (a,b) => this.getLaunchables(a,b) )
observer.observe( this.sceneEl, {characterData: false, childList: true, attributes: false});
2025-02-28 17:05:43 +01:00
window.launcher = this
for( let i = 0; i < 10; i++){
window.launcher.register({
name:"foo"+i,
// icon: "https://..."
description: "lorem ipsum",
cb: () => alert("foo")
})
}
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 )
2024-05-23 12:14:53 +00:00
},
getLaunchables: function(mutationsList,observer){
let searchEvent = 'launcher'
let els = [...this.sceneEl.getElementsByTagName("*")]
2024-05-29 16:13:53 +00:00
let seen = {}
2025-02-28 17:05:43 +01:00
this.launchables = [];
// collect manually registered launchables
this.registered.map( (launchable) => this.launchables.push(launchable) )
2024-05-23 12:14:53 +00:00
2025-02-28 17:05:43 +01:00
// collect launchables in aframe dom elements
this.dom = els.filter( (el) => {
2024-05-23 12:14:53 +00:00
let hasEvent = false
if( el.components ){
for( let i in el.components ){
2024-05-29 16:13:53 +00:00
if( el.components[i].events && el.components[i].events[searchEvent] && !seen[i] ){
2025-02-28 17:05:43 +01:00
this.launchables.push(hasEvent = seen[i] = el.components[i])
2024-05-23 12:14:53 +00:00
}
}
}
return hasEvent ? el : null
})
this.updateLauncher()
2025-02-28 17:05:43 +01:00
return this.launchables
2024-05-23 12:14:53 +00:00
},
updateLauncher: function(){
let launcher = document.querySelector('[launcher]')
2025-02-28 17:05:43 +01:00
if( launcher && launcher.components.launcher) launcher.components.launcher.render()
2024-05-23 12:14:53 +00:00
}
})