-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapLock.js
200 lines (199 loc) · 7.97 KB
/
MapLock.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Github: https://github.com/shdwjk/Roll20API/blob/master/MapLock/MapLock.js
// By: The Aaron, Arcane Scriptomancer
// Contact: https://app.roll20.net/users/104025/the-aaron
var MapLock = MapLock || (function() {
'use strict';
var version = '0.4.4',
lastUpdate = 1490707622,
schemaVersion = 0.4,
checkInstall = function() {
log('-=> MapLock v'+version+' <=- ['+(new Date(lastUpdate*1000))+']');
if( ! _.has(state,'MapLock') || state.MapLock.version !== schemaVersion) {
log(' > Updating Schema to v'+schemaVersion+' <');
state.MapLock = {
version: schemaVersion,
highlighting: false,
locked: {}
};
}
},
ch = function (c) {
var entities = {
'<' : 'lt',
'>' : 'gt',
"'" : '#39',
'@' : '#64',
'{' : '#123',
'|' : '#124',
'}' : '#125',
'[' : '#91',
']' : '#93',
'"' : 'quot',
'-' : 'mdash',
' ' : 'nbsp'
};
if(_.has(entities,c) ){
return ('&'+entities[c]+';');
}
return '';
},
showHelp = function(who) {
sendChat('',
'/w "'+who+'" '
+'<div style="border: 1px solid black; background-color: white; padding: 3px 3px;">'
+'<div style="font-weight: bold; border-bottom: 1px solid black;font-size: 130%;">'
+'MapLock v'+version
+'</div>'
+'<div style="padding-left:10px;margin-bottom:3px;">'
+'<p>MapLock provides a way to lock individual graphics in place. This prevents them from being moved, resized, or rotated by reverting any of those changes that are made to them. It also provides tinting of locked items.</p>'
+'<p><b>Note:</b> Even though it'+ch("'")+'s called MapLock, it works for any graphics on any layers.'
+'</div>'
+'<b>Commands</b>'
+'<div style="padding-left:10px;">'
+'<b><span style="font-family: serif;">!map-lock '+ch('<')+' <i>lock</i> '+ch('|')+' <i>unlock</i> '+ch('|')+' <i>toggle</i> '+ch('|')+' <i>toggle-highlight</i> '+ch('|')+' <i>--help</i> '+ch('>')+'</span></b>'
+'<div style="padding-left: 10px;padding-right:20px">'
+'<p>Adjusts locking options for selected graphics.</p>'
+'<ul>'
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">lock</span></b> '+ch('-')+' Adds these graphics to the list of locked items.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">unlock</span></b> '+ch('-')+' Removes these graphics from the list of locked items.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">toggle</span></b> '+ch('-')+' Switched the selected graphics from locked to unlocked or vice versa.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">toggle-highlight</span></b> '+ch('-')+' Turns on or off the red tinting of locked graphics.'
+'</li> '
+'<li style="border-top: 1px solid #ccc;border-bottom: 1px solid #ccc;">'
+'<b><span style="font-family: serif;">--help</span></b> '+ch('-')+' Shows the help.'
+'</li> '
+'</ul>'
+'</div>'
+'</div>'
+'</div>'
);
},
msgPlayer = function(who,msg) {
sendChat('MapLock','/w "'+who+'" '+
'<div style="font-weight:bold;font-size: 90%; border: 1px solid #999999; background-color: #ffcccc;">'+
msg +
'</div>'
);
},
tintGraphics = function(gids, highlight) {
_.chain(gids)
.map(function(id){
return getObj('graphic',id);
})
.reject(_.isUndefined)
.each(function(o){
var t = state.MapLock.locked[o.id];
o.set({
tint_color: ( highlight ? '#ff0000' : t || 'transparent')
});
});
},
handleInput = function(msg) {
var args,who,ids,cnt=0;
if (msg.type !== "api" || !playerIsGM(msg.playerid)) {
return;
}
args = msg.content.split(/\s+/);
switch(args.shift()) {
case '!map-lock':
who=(getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
ids=_.pluck(msg.selected,'_id');
switch(args.shift()) {
case 'toggle':
if(ids) {
_.each(ids,function(id){
var o=getObj('graphic',id);
++cnt;
if( o && !_.has(state.MapLock.locked,id) ){
state.MapLock.locked[id]=o.get('tint_color');
tintGraphics([id], state.MapLock.highlighting);
} else {
tintGraphics([id], false);
delete state.MapLock.locked[id];
}
});
msgPlayer(who, 'Toggled '+cnt+' token'+(1 === cnt ? '' : 's'));
} else {
msgPlayer(who, '<span style="font-color: #ff0000;">ERROR:</span> Nothing selected.');
}
break;
case 'lock':
if(ids) {
_.each(ids,function(id){
var o=getObj('graphic',id);
if( o && !_.has(state.MapLock.locked,id) ){
++cnt;
state.MapLock.locked[id]=o.get('tint_color');
}
});
tintGraphics(ids, state.MapLock.highlighting);
msgPlayer(who, 'Locked '+cnt+' token'+(1 === cnt ? '' : 's'));
} else {
msgPlayer(who, '<span style="font-color: #ff0000;">ERROR:</span> Nothing selected.');
}
break;
case 'unlock':
if(ids) {
ids=_.intersection(ids,_.keys(state.MapLock.locked));
tintGraphics(ids, false);
_.each(ids,function(id){
delete state.MapLock.locked[id];
++cnt;
});
msgPlayer(who, 'Unlocked '+cnt+' token'+(1 === cnt ? '' : 's'));
} else {
msgPlayer(who, '<span style="font-color: #ff0000;">ERROR:</span> Nothing selected.');
}
break;
case 'toggle-highlight':
state.MapLock.highlighting = !state.MapLock.highlighting;
tintGraphics(_.keys(state.MapLock.locked), state.MapLock.highlighting);
break;
case '--help':
default:
showHelp(who);
break;
}
break;
}
},
HandleMove = function(obj,prev) {
if(_.has(state.MapLock.locked, obj.id)
&& (
obj.get('left') !== prev.left
|| obj.get('top') !== prev.top
|| obj.get('height') !== prev.height
|| obj.get('width') !== prev.width
|| obj.get('rotation') !== prev.rotation
)
) {
obj.set({
left: prev.left,
top: prev.top,
height: prev.height,
width: prev.width,
rotation: prev.rotation
});
}
},
registerEventHandlers = function() {
on('chat:message', handleInput);
on('change:graphic', HandleMove);
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers
};
}());
on('ready',function() {
'use strict';
MapLock.CheckInstall();
MapLock.RegisterEventHandlers();
});