====== Differences ====== This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
script_event_bus [2015/05/30 19:07] cdfa created |
script_event_bus [2015/06/01 06:16] (current) cdfa |
||
---|---|---|---|
Line 6: | Line 6: | ||
====== How to use the script ====== | ====== How to use the script ====== | ||
- | Import this script, when you want to subscribe to a eventbus, copy/paste this in your script: LL.runScript("eventBus", JSON.stringify({funct:"sub", obId:id_of_the_object_the_event_is_fired_on, name:"name of the event", action:EventHandler.action_you_want_to_execute, extra:any_extra_data_you_want_to_pass})) | + | Import this script, when you want to subscribe to a eventbus, copy/paste this in your script: LL.runScript("Eventbus", JSON.stringify({funct:"sub", obId:id_of_the_object_the_event_is_fired_on, name:"name of the event", action:EventHandler.action_you_want_to_execute, extra:any_extra_data_you_want_to_pass})) |
so for example LL.runScript("eventBus", JSON.stringify({funct:"sub", obId:196666, name:"i.tap", action:EventHandler.RUN_SCRIPT, extra:LL.getScriptByName("External Editor Script Importer").getId()})) | so for example LL.runScript("eventBus", JSON.stringify({funct:"sub", obId:196666, name:"i.tap", action:EventHandler.RUN_SCRIPT, extra:LL.getScriptByName("External Editor Script Importer").getId()})) | ||
Line 14: | Line 14: | ||
====== Script code ====== | ====== Script code ====== | ||
<sxh javascript> | <sxh javascript> | ||
- | script = LL.getCurrentScript() | + | var script = LL.getCurrentScript(); |
- | event = LL.getEvent() | + | var event = LL.getEvent(); |
- | src = event.getSource() | + | var src = event.getSource(); |
- | data = JSON.parse(event.getData()) | + | var data = JSON.parse(event.getData()); |
- | cont = event.getContainer() | + | var cont = event.getContainer(); |
+ | var i, id | ||
function getOb(id){ | function getOb(id){ | ||
- | if(obId.toString().length==6){ | + | if(id.toString().length==6){ |
- | ob=LL.getItemById(obId) | + | ob=LL.getItemById(id); |
}else{ | }else{ | ||
- | ob=LL.getContainerById(obId) | + | ob=LL.getContainerById(id); |
} | } | ||
return ob | return ob | ||
Line 30: | Line 31: | ||
if(src=="RUN_SCRIPT"){ | if(src=="RUN_SCRIPT"){ | ||
- | //alert(JSON.stringify(data)) | + | if(data==null)throw "object is null"; |
- | if(data==null)throw "object is null" | + | var obId=data.obId; |
- | obId=data.obId | + | var name=data.name; |
- | name=data.name | + | var tagName = obId+name; |
- | tagName = obId+name | + | var eventBus = JSON.parse(script.getTag(tagName)); |
- | eventBus = JSON.parse(script.getTag(tagName)) | + | if(eventBus==null)eventBus=[] |
- | evHa = {action:data.action, data:data.extra} | + | var evHa = {action:data.action, data:data.extra}; |
+ | var reAdding = false | ||
+ | for(i=0;i<eventBus.length;i++){ | ||
+ | if(eventBus[i].action==data.action && eventBus[i].data==data.extra){ | ||
+ | reAdding=true | ||
+ | break; | ||
+ | } | ||
+ | } | ||
if(data.funct=="sub"){ | if(data.funct=="sub"){ | ||
- | //subEvent(data.obId, data.name, data.action, data.extra) | + | if(!reAdding){ |
- | ob = getOb(obId) | + | var ob = getOb(obId); |
- | if(eventBus==null)eventBus=[] | + | if(eventBus==null)eventBus=[]; |
- | prop = ob.getProperties() | + | var prop = ob.getProperties(); |
- | curEvha = prop.getEventHandler(name) | + | var curEvha = prop.getEventHandler(name); |
- | curAction = curEvha.getAction() | + | var curAction = curEvha.getAction(); |
- | curData = curEvha.getData() | + | var curData = curEvha.getData(); |
- | if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ | + | if(curAction!=EventHandler.RUN_SCRIPT || curData!=script.getId().toString()){ |
- | if(curAction!=0)eventBus.push({action:curAction, data:curData}) | + | if(curAction!=0 && curAction!=EventHandler.UNSET)eventBus.push({action:curAction, data:curData}); |
- | prop.edit().setEventHandler(name, EventHandler.RUN_SCRIPT, script.getId()).commit() | + | prop.edit().setEventHandler(name, EventHandler.RUN_SCRIPT, script.getId()).commit(); |
+ | } | ||
+ | eventBus.push(evHa); | ||
} | } | ||
- | eventBus.push(evHa) | ||
}else if(data.funct=="unsub"){ | }else if(data.funct=="unsub"){ | ||
- | for(i=0;i<eventBus.length;i++){ | + | if(reAdding){ |
- | if(eventBus[i].action==data.action && eventBus[i].data==data.extra){ | + | eventBus.splice(i, 1); |
- | eventBus.splice(i, 1) | + | |
- | break | + | |
- | } | + | |
} | } | ||
if(eventBus.length==0){ | if(eventBus.length==0){ | ||
- | ob = getOb(obId) | + | var ob = getOb(obId); |
- | ob.getProperties().edit().setEventHandler(name, EventHandler.UNSET, null).commit() | + | ob.getProperties().edit().setEventHandler(name, EventHandler.UNSET, null).commit(); |
- | ob.setTag(tagName, null) | + | ob.setTag(tagName, null); |
} | } | ||
} | } | ||
- | script.setTag(tagName, JSON.stringify(eventBus)) | + | script.setTag(tagName, JSON.stringify(eventBus)); |
}else{ | }else{ | ||
+ | var name; | ||
switch(src.slice(0, 1)){ | switch(src.slice(0, 1)){ | ||
case "K": | case "K": | ||
- | name = src.slice(2).toLowerCase()+"Key" | + | name = src.slice(2).toLowerCase()+"Key"; |
if(src.slice(-1)=="L"){ | if(src.slice(-1)=="L"){ | ||
- | name = "long"+src.slice(2, 3)+src.slice(3,6).toLowerCase()+"Key" | + | name = "long"+src.slice(2, 3)+src.slice(3,6).toLowerCase()+"Key"; |
} | } | ||
- | break | + | break; |
case "I": | case "I": | ||
if(src.slice(-5)=="CLICK"){ | if(src.slice(-5)=="CLICK"){ | ||
if(src.length==7){ | if(src.length==7){ | ||
- | name = "i.tap" | + | name = "i.tap"; |
}else{ | }else{ | ||
- | name="i.longTap" | + | name="i.longTap"; |
} | } | ||
}else if(src.slice(2,7)=="SWIPE"){ | }else if(src.slice(2,7)=="SWIPE"){ | ||
- | name="i.swipe"+ src.slice(8,9)+src.slice(9).toLowerCase() | + | name="i.swipe"+ src.slice(8,9)+src.slice(9).toLowerCase(); |
}else{ | }else{ | ||
- | name="i."+src.slice(2).toLowerCase() | + | name="i."+src.slice(2).toLowerCase(); |
} | } | ||
break | break | ||
Line 88: | Line 95: | ||
if(src.slice(-5)=="CLICK"){ | if(src.slice(-5)=="CLICK"){ | ||
if(src.length==7){ | if(src.length==7){ | ||
- | name = "bg.tap" | + | name = "bg.tap"; |
}else if(src.length==12){ | }else if(src.length==12){ | ||
- | name="bg.longTap" | + | name="bg.longTap"; |
}else{ | }else{ | ||
- | name="bg.doubleTap" | + | name="bg.doubleTap"; |
} | } | ||
}else if(src.slice(2,7)=="SWIPE"){ | }else if(src.slice(2,7)=="SWIPE"){ | ||
if(src.slice(7, 8)=="2"){ | if(src.slice(7, 8)=="2"){ | ||
- | name="swipe2"+src.slice(9,10)+src.slice(10).toLowerCase() | + | name="swipe2"+src.slice(9,10)+src.slice(10).toLowerCase(); |
}else{ | }else{ | ||
- | name="swipe"+src.slice(8,9)+src.slice(9).toLowerCase() | + | name="swipe"+src.slice(8,9)+src.slice(9).toLowerCase(); |
} | } | ||
}else if(src=="C_LOADED"){ | }else if(src=="C_LOADED"){ | ||
- | name="load" | + | name="load"; |
}else if(src=="C_POSITION_CHANGED"){ | }else if(src=="C_POSITION_CHANGED"){ | ||
- | nams="posChanged" | + | nams="posChanged"; |
}else{ | }else{ | ||
- | name="i."+src.slice(2).toLowerCase() | + | name=src.slice(2).toLowerCase(); |
} | } | ||
- | break | + | break; |
case "P": | case "P": | ||
- | name="orientationPortrait" | + | name="orientationPortrait"; |
- | break | + | break; |
case "L": | case "L": | ||
- | name="orientationLandscape" | + | name="orientationLandscape"; |
- | break | + | break; |
} | } | ||
- | cont = event.getContainer() | + | var cont = event.getContainer(); |
- | if(cont==null)cont=LL.getCurrentDesktop() | + | if(cont==null) cont=LL.getCurrentDesktop(); |
- | id = cont.getId() | + | var id = cont.getId(); |
- | item=event.getItem() | + | var item=event.getItem(); |
if(item==null){ | if(item==null){ | ||
- | eventBus = JSON.parse(script.getTag(id+name)) | + | var eventBus = JSON.parse(script.getTag(id+name)); |
for(i=0;i<eventBus.length;i++){ | for(i=0;i<eventBus.length;i++){ | ||
- | //alert(eventBus[i].action+", "+eventBus[i].data) | + | var action = eventBus[i].action |
- | alert(JSON.stringify(eventBus)) | + | var data = eventBus[i].data |
- | LL.runAction(eventBus[i].action, eventBus[i].data) | + | if(action==EventHandler.RUN_SCRIPT){ |
+ | if(typeof data=="number"){ | ||
+ | id = data | ||
+ | }else{ | ||
+ | var slash = data.indexOf("/") | ||
+ | id = data.slice(0,slash) | ||
+ | data = data.slice(slash+1) | ||
+ | } | ||
+ | LL.runScript(LL.getScriptById(id).getName(), data) | ||
+ | }else{ | ||
+ | LL.runAction(action, data); | ||
+ | } | ||
} | } | ||
}else{ | }else{ | ||
- | id=item.getId() | + | var id=item.getId() |
- | eventBus = JSON.parse(script.getTag(id+name)) | + | var eventBus = JSON.parse(script.getTag(id+name)) |
for(i=0;i<eventBus.length;i++){ | for(i=0;i<eventBus.length;i++){ | ||
- | //alert(eventBus[i].action+", "+eventBus[i].data) | + | LL.runAction(eventBus[i].action, item, eventBus[i].data); |
- | alert(JSON.stringify(eventBus)) | + | |
- | LL.runAction(eventBus[i].action, item, eventBus[i].data) | + | |
} | } | ||
} | } |