UNPKG

aframe-speech-command-component

Version:
1 lines 1.82 kB
!function(t){function e(i){if(n[i])return n[i].exports;var a=n[i]={exports:{},id:i,loaded:!1};return t[i].call(a.exports,a,a.exports,e),a.loaded=!0,a.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e){if("undefined"==typeof AFRAME)throw new Error("Component attempted to register before AFRAME was available.");AFRAME.registerSystem("speech-command",{init:function(){this.entities=[]},registerMe:function(t){this.entities.push(t)},unregisterMe:function(t){var e=this.entities.indexOf(t);this.entities.splice(e,1)},onSceneLoaded:function(t){},play:function(){}}),AFRAME.registerComponent("speech-command",{multiple:!0,schema:{command:{type:"string"},type:{type:"string"},targetElement:{type:"selector"},targetComponent:{type:"string"},function:{type:"string"},attribute:{type:"string"},value:{type:"string"},keyCode:{type:"string"}},init:function(){this.system.registerMe(this),this.data.targetElement||(this.data.targetElement=this.el),this.data.keyCode&&window.addEventListener("keyup",this.onKeyup.bind(this))},remove:function(){this.system.unregisterMe(this)},play:function(){},executeCommand:function(){var t=this.data.targetElement;if("attribute"==this.data.type)t.setAttribute(this.data.attribute,this.data.value);else if("function"==this.data.type){var e=t.components[this.data.targetComponent];e[this.data.function]()}},onKeyup:function(t){t.keyCode==this.data.keyCode&&this.executeCommand()}}),AFRAME.registerComponent("annyang-speech-recognition",{init:function(){},play:function(){if(annyang){for(var t=document.querySelector("a-scene").systems["speech-command"],e={},n={},i=0;i<t.entities.length;i++){var a=t.entities[i];n[a.data.command]=a,e[a.data.command]=function(){}}annyang.addCommands(e),annyang.addCallback("resultMatch",function(t,e,i){var a=n[e];a.executeCommand()}),annyang.start()}}})}]);