xrsh-com/com/launcher.js

378 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-07-01 16:12:10 +02:00
* They can be temporary processes (icon disappears after component is removed) or permanent icons
* when registered via .register({...})
*
2025-02-28 17:05:43 +01:00
* ```html
* <a-entity launcher>
* <a-entity launch="component: helloworld; foo: bar"><a-entity>
* </a-entity>
*
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: {
2024-05-24 14:37:05 +00:00
colors: { type:"array", "default": [
'#4C73FE',
'#554CFE',
'#864CFE',
'#B44CFE',
'#E24CFE',
'#FE4CD3',
'#333333',
]},
paused: { type:"boolean","default":false},
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
this.el.object3D.visible = false;
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","")
2025-02-28 17:05:43 +01:00
this.el.setAttribute("pressable","")
this.el.sceneEl.addEventListener('enter-vr', () => this.centerMenu() )
this.el.sceneEl.addEventListener('enter-ar', () => this.centerMenu() )
2024-05-23 12:14:53 +00:00
this.render()
2024-01-22 09:38:16 +00:00
},
dom: {
2025-02-28 17:05:43 +01:00
scale: 0.8,
2024-01-22 09:38:16 +00:00
events: ['click'],
html: (me) => `<div class="iconmenu">loading components..</div>`,
2025-02-28 17:05:43 +01:00
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;
2025-05-22 16:24:53 +02:00
width:764px;
2024-04-19 16:42:46 +02:00
overflow:hidden;
2024-01-22 09:38:16 +00:00
position: fixed;
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;
2025-02-28 17:05:43 +01:00
color: #777;
line-height: 7px;
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
click: function(e){ },
DOMready: function(){
this.el.setAttribute("html-as-texture-in-xr", `domid: #${this.el.dom.id}; faceuser: false`)
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)
2024-05-24 14:37:05 +00:00
let colors = this.data.colors
2025-02-28 17:05:43 +01:00
const add2D = (launchCom,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="
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
})
2024-05-23 12:14:53 +00:00
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
const manifest = c.manifest
2024-05-23 12:14:53 +00:00
if( manifest ){
2025-02-28 17:05:43 +01:00
add2D(c,manifest)
2024-05-23 12:14:53 +00:00
}
})
2024-04-19 16:42:46 +02:00
2025-02-28 17:05:43 +01:00
this.centerMenu();
2024-05-24 14:37:05 +00:00
},
2024-04-19 16:42:46 +02:00
2025-02-28 17:05:43 +01:00
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
2024-05-24 14:37:05 +00:00
}
})
2025-02-28 17:05:43 +01:00
this.el.object3D.visible = true // ensure visibility
2024-05-24 14:37:05 +00:00
},
2025-02-28 17:05:43 +01:00
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);
2024-05-24 14:37:05 +00:00
},
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
this.getLaunchables()
},
register: function(launchable){
try{
let {name, description, cb} = launchable
2025-05-22 16:24:53 +02:00
const exist = this.registered.find( (r) => r.manifest.name == name )
if( exist ) return // already registered
2025-02-28 17:05:43 +01:00
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 )
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 = [
/*
* {
* manifest: {...}
* launcher: () => ....
* }
*/
];
// collect manually registered launchables
2025-07-01 16:12:10 +02:00
this.registered.map( (launchable) => {
if( launchable.component ) seen[ launchable.component ] = true
this.launchables.push(launchable)
})
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
let com = hasEvent = seen[i] = el.components[i]
2025-07-01 16:12:10 +02:00
let alreadyAdded = this.launchables.find( (l) => l.manifest.name == com.manifest.name )
if( !alreadyAdded ){
com.launcher = () => com.el.emit('launcher',null,false) // important: no bubble
this.launchables.push({manifest: com.manifest, launcher: com.launcher})
}
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
}
})