Skip to content

Instantly share code, notes, and snippets.

@BaldarSilveraxe
Created December 20, 2014 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaldarSilveraxe/5e62d8c440fa295c910a to your computer and use it in GitHub Desktop.
Save BaldarSilveraxe/5e62d8c440fa295c910a to your computer and use it in GitHub Desktop.
Roll20 API to control tokens you can not select.
var NoSelectControl = NoSelectControl || (function() {
'use strict';
var version = 0.1,
handleMessages = function(msg)
{
if('api' !== msg.type ) {
return;
}
var args = msg.content.split(/\s+/);
if ('!thistoken' !== args.shift() ){
return;
}
actionNoSelectControl(args.shift(),args.shift());
},
actionNoSelectControl = function(direction,token)
{
var currentValue;
switch(direction) {
case 'l':
currentValue = getObj('graphic', token).get('left');
getObj('graphic', token).set('left', currentValue - 70);
break;
case 'r':
currentValue = getObj('graphic', token).get('left');
getObj('graphic', token).set('left', currentValue + 70);
break;
case 'u':
currentValue = getObj('graphic', token).get('top');
getObj('graphic', token).set('top', currentValue - 70);
break;
case 'd':
currentValue = getObj('graphic', token).get('top');
getObj('graphic', token).set('top', currentValue + 70);
break;
case 't':
currentValue = getObj('graphic', token).get('rotation');
getObj('graphic', token).set('rotation', currentValue + 45);
break;
}
},
validateNoSelectControl = function()
{
var allCharcters = findObjs({type: 'character'}),
allAbilities = findObjs({type: 'ability'}),
allTokens = findObjs({type: 'graphic', subtype:'token'}),
characterID;
_.each(allCharcters, function(eachCharcter) {
characterID = eachCharcter.get('id');
fixMakeNoSelectControl(characterID,'←Left','l',allTokens,allAbilities);
fixMakeNoSelectControl(characterID,'↑Up','u',allTokens,allAbilities);
fixMakeNoSelectControl(characterID,'→Right','r',allTokens,allAbilities);
fixMakeNoSelectControl(characterID,'↓Down','d',allTokens,allAbilities);
fixMakeNoSelectControl(characterID,'↷Rotate','t',allTokens,allAbilities);
});
},
fixMakeNoSelectControl = function(characterID,macroName,macroAction,allTokens,allAbilities)
{
var thisTokenID, thisMacroID, thisMacroAction;
thisTokenID = _.filter(allTokens, function(o){
return o.get('represents') === characterID;
});
if (thisTokenID.length > 0) {
thisTokenID = thisTokenID[0].get('id');
thisMacroID = _.filter(allAbilities, function(o){
return o.get('characterid') === characterID &&
o.get('name') === macroName;
});
if (thisMacroID.length > 0) {
thisMacroID = thisMacroID[0].get('id');
thisMacroAction = _.filter(allAbilities, function(o){
return o.get('id') === thisMacroID;
})[0].get('action');
if (thisMacroAction != '!thistoken ' + macroAction + ' ' + thisTokenID) {
getObj('ability', thisMacroID).set('action', '!thistoken ' + macroAction + ' ' + thisTokenID);
}
} else {
createObj('ability', {
name: macroName,
action: '!thistoken ' + macroAction + ' ' + thisTokenID,
characterid: characterID
});
}
}
},
checkInstall = function()
{
validateNoSelectControl();
},
registerEventHandlers = function()
{
on('chat:message',handleMessages);
on('change:graphic:represents', validateNoSelectControl);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function(){
'use strict';
NoSelectControl.RegisterEventHandlers();
NoSelectControl.CheckInstall();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment