xrsh-com/com/launcher.js

387 lines
13 KiB
JavaScript
Raw Normal View History

2024-01-22 10:38:16 +01:00
/*
2024-04-19 16:42:46 +02:00
* ## launcher
2024-01-22 10:38:16 +01:00
*
* displays app (icons) for enduser to launch
*
* ```javascript
* <a-entity app="app/launcher.js"/>
* ```
2024-04-19 16:42:46 +02:00
*
2024-01-22 10:38:16 +01:00
* | property | type | example |
* |--------------|--------------------|----------------------------------------------------------------------------------------|
2024-04-19 16:42:46 +02:00
* | `registries` | `array` of strings | <a-entity app="app/launcher.js; registers: https://foo.com/index.json, ./index.json"/> |
2024-01-22 10:38:16 +01: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 |
2024-01-22 10:38:16 +01: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 10:38:16 +01:00
AFRAME.registerComponent('launcher', {
2024-04-19 16:42:46 +02:00
schema: {
2024-05-24 16:37:05 +02:00
attach: { type:"selector"},
padding: { type:"number","default":0.15},
fingerTip: {type:"selector"},
fingerDistance: {type:"number", "default":0.25},
rescale: {type:"number","default":0.4},
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 10:38:16 +01:00
},
2024-09-06 10:30:24 +02:00
dependencies:{
dom: "com/dom.js"
},
2024-05-23 14:14:53 +02:00
init: async function () {
2024-09-06 10:30:24 +02:00
await AFRAME.utils.require(this.dependencies)
2024-05-24 16:37:05 +02:00
this.worldPosition = new THREE.Vector3()
2024-01-22 10:38:16 +01:00
2024-05-23 14:14:53 +02:00
await AFRAME.utils.require({
2024-05-24 16:37:05 +02:00
html: "https://unpkg.com/aframe-htmlmesh@2.1.0/build/aframe-html.js", // html to AFRAME
dom: "./com/dom.js",
2024-05-29 12:22:35 +02:00
data2events: "./com/data2event.js"
2024-01-22 10:38:16 +01:00
})
2024-05-23 14:14:53 +02:00
this.el.setAttribute("dom","")
2024-05-29 18:13:53 +02:00
this.el.setAttribute("noxd","ignore") // hint to XD.js that we manage ourselve concerning 2D/3D switching
2024-05-23 14:14:53 +02:00
this.render()
2024-05-24 16:37:05 +02:00
if( this.data.attach ){
this.el.object3D.visible = false
if( this.isHand(this.data.attach) ){
2024-08-06 16:48:29 +02:00
this.data.attach.addEventListener('model-loaded', () => {
this.ready = true
this.attachMenu()
})
2024-05-24 16:37:05 +02:00
// add button
this.menubutton = this.createMenuButton()
this.menubutton.object3D.visible = false
this.data.attach.appendChild( this.menubutton )
}else this.data.attach.appendChild(this.el)
}
},
isHand: (el) => {
return el.getAttributeNames().filter( (n) => n.match(/^hand-tracking/) ? n : null ).length ? true : false
2024-01-22 10:38:16 +01:00
},
dom: {
scale: 3,
events: ['click'],
html: (me) => `<div class="iconmenu">loading components..</div>`,
css: (me) => `.iconmenu {
2024-01-22 10:38:16 +01: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 10:38:16 +01:00
position: fixed;
2024-04-19 16:42:46 +02:00
right: 162px;
2024-05-29 18:13:53 +02:00
bottom: 10px;
2024-04-19 16:42:46 +02:00
left:20px;
background: transparent;
padding-bottom: 54px;
2024-01-22 10:38:16 +01:00
box-sizing: border-box;
pointer-events: none;
2024-01-22 12:13:41 +01:00
visibility: visible !important;
2024-01-22 10:38:16 +01:00
}
.iconmenu > button {
2024-04-19 16:42:46 +02:00
line-height:0px;
2024-01-22 10:38:16 +01:00
pointer-events:all;
width: 58px;
2024-04-19 16:42:46 +02:00
height: 34px;
padding: 12px 0px 12px 0px;
2024-01-22 10:38:16 +01: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 10:38:16 +01: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 10:38:16 +01: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 10:38:16 +01:00
padding-top:13px;
}
.iconmenu > button > img {
2024-04-19 16:42:46 +02:00
transform: translate(0px,-14px);
2024-01-22 10:38:16 +01:00
opacity:0.5;
padding: 5px;
border-radius: 0px;
}
.iconmenu > button > img:hover{
2024-01-22 12:13:41 +01:00
background: #AAA;
2024-01-22 10:38:16 +01:00
transition:0.gg3s;
border-radius: 50%;
}`
},
events:{
2024-05-24 16:37:05 +02:00
open: function(){
this.preventAccidentalButtonPresses()
if( this.data.open ){
this.el.setAttribute("animation",`dur: 200; property: scale; from: 0 0 1; to: ${this.data.rescale} ${this.data.rescale} ${this.data.rescale}`)
this.menubutton.object3D.visible = false
}else{
this.el.setAttribute("animation",`dur: 200; property: scale; from: ${this.data.rescale} ${this.data.rescale} ${this.data.rescale}; to: 0 0 1`)
this.menubutton.object3D.visible = true
}
}
},
preventAccidentalButtonPresses: function(){
this.data.paused = true
setTimeout( () => this.data.paused = false, 500 ) // prevent menubutton press collide with animated buttons
},
2024-01-22 10:38:16 +01:00
2024-05-24 16:37:05 +02:00
createMenuButton: function(colo){
let aentity = document.createElement('a-entity')
aentity.setAttribute("mixin","menubutton")
aentity.addEventListener('obbcollisionstarted', this.onpress )
aentity.addEventListener('obbcollisionended', this.onreleased )
return aentity
2024-01-22 10:38:16 +01:00
},
2024-06-14 15:52:44 +02:00
render: async function(els){
2024-05-23 14:14:53 +02:00
if( !this.el.dom ) return // too early (dom.js component not ready)
let requires = []
2024-05-24 16:37:05 +02:00
let i = 0
let j = 0
let colors = this.data.colors
const add2D = (launchCom,el,manifest) => {
2024-05-23 14:14:53 +02:00
let btn = document.createElement('button')
btn.innerHTML = `${ manifest?.icons?.length > 0
? `<img src='${manifest.icons[0].src}' title='${manifest.name}: ${manifest.description}'/>`
: `${manifest.short_name}`
}`
btn.addEventListener('click', () => el.emit('launcher',{}) )
this.el.dom.appendChild(btn)
}
const add3D = (launchCom,el,manifest) => {
let aentity = document.createElement('a-entity')
let atext = document.createElement('a-entity')
2024-05-24 16:37:05 +02:00
let padding = this.data.padding
if( (i % this.data.cols) == 0 ) j++
2024-05-23 14:14:53 +02:00
aentity.setAttribute("mixin","menuitem")
2024-05-24 16:37:05 +02:00
aentity.setAttribute("position",`${padding+(i++ % this.data.cols) * padding} ${j*padding} 0`)
2024-05-23 14:14:53 +02:00
if( !aentity.getAttribute("material")){
aentity.setAttribute('material',`side: double; color: ${colors[ i % colors.length]}`)
2024-04-19 16:42:46 +02:00
}
2024-05-24 16:37:05 +02:00
aentity.addEventListener('obbcollisionstarted', this.onpress )
aentity.addEventListener('obbcollisionended', this.onreleased )
2024-05-23 14:14:53 +02:00
atext.setAttribute("text",`value: ${manifest.short_name}; align: baseline; anchor: align; align:center; wrapCount:7`)
atext.setAttribute("scale","0.1 0.1 0.1")
aentity.appendChild(atext)
this.el.appendChild(aentity)
2024-05-24 16:37:05 +02:00
aentity.launchCom = launchCom
2024-05-23 14:14:53 +02:00
return aentity
}
// finally render them!
this.el.dom.innerHTML = '' // clear
2024-06-14 15:52:44 +02:00
els = els || this.system.components
els.map( (c) => {
2024-05-24 16:37:05 +02:00
const launchComponentKey = c.getAttributeNames().shift()
2024-05-23 14:14:53 +02:00
const launchCom = c.components[ launchComponentKey ]
if( !launchCom ) return console.warn(`could not find component '${launchComponentKey}' (forgot to include script-tag?)`)
const manifest = launchCom.manifest
if( manifest ){
2024-05-24 16:37:05 +02:00
add2D(launchCom,c,manifest)
add3D(launchCom,c,manifest)
2024-05-23 14:14:53 +02:00
}
})
2024-04-19 16:42:46 +02:00
2024-05-24 16:37:05 +02:00
},
onpress: function(e){
const launcher = document.querySelector('[launcher]').components.launcher
if( launcher.data.paused ) return // prevent accidental pressed due to animation
if( e.detail.withEl.computedMixinStr == 'menuitem' ) return // dont react to menuitems touching eachother
// if user press menu button toggle menu
2024-06-14 15:52:44 +02:00
if( launcher && !launcher.data.open && e.srcElement.computedMixinStr == 'menubutton' ){
return (launcher.data.open = true)
2024-05-23 14:14:53 +02:00
}
2024-05-24 16:37:05 +02:00
if( launcher && !launcher.data.open ) return // dont process menuitems when menu is closed
let el = e.srcElement
if(!el) return
el.object3D.traverse( (o) => {
if( o.material && o.material.color ){
if( !o.material.colorOriginal ) o.material.colorOriginal = o.material.color.clone()
o.material.color.r *= 0.3
o.material.color.g *= 0.3
o.material.color.b *= 0.3
}
})
if( el.launchCom ){
console.log("launcher.js: launching "+el.launchCom.el.getAttributeNames().shift())
launcher.preventAccidentalButtonPresses()
el.launchCom.el.emit('launcher') // launch component!
2024-05-30 16:39:42 +02:00
this.data.open = false // close to prevent infinite loop of clicks when leaving immersive mode
2024-05-24 16:37:05 +02:00
}
},
2024-04-19 16:42:46 +02:00
2024-05-24 16:37:05 +02:00
onreleased: function(e){
if( e.detail.withEl.computedMixinStr == 'menuitem' ) return // dont react to menuitems touching eachother
let el = e.srcElement
el.object3D.traverse( (o) => {
if( o.material && o.material.color ){
if( o.material.colorOriginal ) o.material.color = o.material.colorOriginal.clone()
}
})
},
attachMenu: function(){
if( this.el.parentNode != this.data.attach ){
this.el.object3D.visible = true
let armature = this.data.attach.object3D.getObjectByName('Armature')
if( !armature ) return console.warn('cannot find armature')
this.data.attach.object3D.children[0].add(this.el.object3D)
this.el.object3D.scale.x = this.data.rescale
this.el.object3D.scale.y = this.data.rescale
this.el.object3D.scale.z = this.data.rescale
// add obb-collider to index finger-tip
let aentity = document.createElement('a-entity')
trackedObject3DVariable = 'parentNode.components.hand-tracking-controls.bones.9';
this.data.fingerTip.appendChild(aentity)
aentity.setAttribute('obb-collider', {trackedObject3D: trackedObject3DVariable, size: 0.015});
if( this.isHand(this.data.attach) ){
// shortly show and hide menu into palm (hint user)
setTimeout( () => { this.data.open = false }, 1500 )
}
}
},
tick: function(){
2024-08-06 16:48:29 +02:00
if( this.ready && this.data.open ){
2024-05-24 16:37:05 +02:00
let indexTipPosition = document.querySelector('#right-hand[hand-tracking-controls]').components['hand-tracking-controls'].indexTipPosition
this.el.object3D.getWorldPosition(this.worldPosition)
const lookingAtPalm = this.data.attach.components['hand-tracking-controls'].wristObject3D.rotation.z > 2.0
if( !lookingAtPalm ){ this.data.open = false }
}
2024-01-22 10:38:16 +01: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 10:38:16 +01: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 10:38:16 +01: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 10:38:16 +01: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 14:14:53 +02:00
AFRAME.registerSystem('launcher',{
init: function(){
this.components = []
2024-05-24 16:37:05 +02:00
// observe HTML changes in <a-scene>
2024-05-23 14:14:53 +02:00
observer = new MutationObserver( (a,b) => this.getLaunchables(a,b) )
observer.observe( this.sceneEl, {characterData: false, childList: true, attributes: false});
},
getLaunchables: function(mutationsList,observer){
let searchEvent = 'launcher'
let els = [...this.sceneEl.getElementsByTagName("*")]
2024-05-29 18:13:53 +02:00
let seen = {}
2024-05-23 14:14:53 +02:00
this.components = els.filter( (el) => {
let hasEvent = false
if( el.components ){
for( let i in el.components ){
2024-05-29 18:13:53 +02:00
if( el.components[i].events && el.components[i].events[searchEvent] && !seen[i] ){
hasEvent = seen[i] = true
2024-05-23 14:14:53 +02:00
}
}
}
return hasEvent ? el : null
})
this.updateLauncher()
2024-05-30 16:39:42 +02:00
return seen
2024-05-23 14:14:53 +02:00
},
updateLauncher: function(){
let launcher = document.querySelector('[launcher]')
2024-09-06 10:30:24 +02:00
if( launcher ) launcher.components.launcher.render()
2024-05-23 14:14:53 +02:00
}
})