forked from ThornyFFXI/tCrossBar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindings.lua
More file actions
398 lines (361 loc) · 14 KB
/
Copy pathbindings.lua
File metadata and controls
398 lines (361 loc) · 14 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
local bindings = {
GlobalBindings = T{},
JobBindings = T{
Default = T{},
Palettes = T{ { Name="Base", Bindings = T{} } }
},
};
bindings.ActivePalette = bindings.JobBindings.Palettes[1];
bindings.ActivePaletteIndex = 1;
bindings.LastPaletteIndex = 1;
local function WriteBinding(writer, depth, hotkey, binding)
local pad1 = string.rep(' ', depth);
local pad2 = string.rep(' ', depth + 4);
local pad3 = string.rep(' ', depth + 8);
writer:write(string.format('%s[%q] = {\n', pad1, hotkey));
depth = depth + 4;
writer:write(string.format('%sActionType = \'%s\',\n', pad2, binding.ActionType));
if T{'Ability', 'Item', 'Spell', 'Trust', 'Weaponskill'}:contains(binding.ActionType) then
writer:write(string.format('%sId = %u,\n', pad2, binding.Id));
end
writer:write(string.format('%sMacro = T{\n', pad2));
for _,line in ipairs(binding.Macro) do
writer:write(string.format('%s%q,\n', pad3, line));
end
writer:write(string.format('%s},\n', pad2));
if (binding.CostOverride ~= nil) then
writer:write(string.format('%sCostOverride = T{ ', pad2));
local first = true;
for _,id in ipairs(binding.CostOverride) do
if not first then
writer:write(', ');
end
writer:write(tostring(id));
first = false;
end
writer:write(' },\n');
end
writer:write(string.format('%sLabel = %q,\n', pad2, binding.Label));
writer:write(string.format('%sImage = %q,\n', pad2, binding.Image));
writer:write(string.format('%sShowCost = %s,\n', pad2, binding.ShowCost and 'true' or 'false'));
writer:write(string.format('%sShowCross = %s,\n', pad2, binding.ShowCross and 'true' or 'false'));
writer:write(string.format('%sShowFade = %s,\n', pad2, binding.ShowFade and 'true' or 'false'));
writer:write(string.format('%sShowRecast = %s,\n', pad2, binding.ShowRecast and 'true' or 'false'));
writer:write(string.format('%sShowName = %s,\n', pad2, binding.ShowName and 'true' or 'false'));
writer:write(string.format('%sShowTrigger = %s,\n', pad2, binding.ShowTrigger and 'true' or 'false'));
writer:write(string.format('%sShowReadyPulse = %s,\n', pad2, binding.ShowReadyPulse and 'true' or 'false'));
writer:write(string.format('%sShowSkillchainIcon = %s,\n', pad2, binding.ShowSkillchainIcon and 'true' or 'false'));
writer:write(string.format('%sShowSkillchainAnimation = %s,\n', pad2, binding.ShowSkillchainAnimation and 'true' or 'false'));
writer:write(string.format('%sShowHotkey = %s,\n', pad2, binding.ShowHotkey and 'true' or 'false'));
writer:write(string.format('%s},\n', pad1));
end
local function WriteGlobals(globalBindings)
local writer = io.open(bindings.GlobalPath, 'w');
writer:write('return T{\n');
for hotkey,binding in pairs(bindings.GlobalBindings) do
WriteBinding(writer, 4, hotkey, binding);
end
writer:write('};');
writer:close();
end
local function WriteJob(jobBindings)
local writer = io.open(bindings.JobPath, 'w');
writer:write('return T{\n');
writer:write(' Default = T{\n');
for hotkey,binding in pairs(bindings.JobBindings.Default) do
WriteBinding(writer, 8, hotkey, binding);
end
writer:write(' },\n');
writer:write(' Palettes = T{\n');
for _,palette in ipairs(bindings.JobBindings.Palettes) do
writer:write(string.format(' { Name=%q, Bindings = T{\n', palette.Name));
for hotkey,binding in pairs(palette.Bindings) do
WriteBinding(writer, 12, hotkey, binding);
end
writer:write(' } },\n');
end
writer:write(' },\n');
writer:write('};');
writer:close();
end
local function FormatBindings()
local output = {};
for hotkey,binding in pairs(bindings.ActivePalette.Bindings) do
output[hotkey] = binding;
binding.Scope = 3;
end
for hotkey,binding in pairs(bindings.JobBindings.Default) do
if (output[hotkey] == nil) then
output[hotkey] = binding;
binding.Scope = 2;
end
end
for hotkey,binding in pairs(bindings.GlobalBindings) do
if (output[hotkey] == nil) then
output[hotkey] = binding;
binding.Scope = 1;
end
end
return output;
end
local function ApplyBindings()
local bindings = FormatBindings();
if (gSingleDisplay) then
gSingleDisplay:UpdateBindings(bindings);
end
if (gDoubleDisplay) then
gDoubleDisplay:UpdateBindings(bindings);
end
end
local exposed = {};
function exposed:LoadDefaults(name, id, job)
if (name == '') or (id == 0) then
bindings = {
GlobalBindings = T{},
JobBindings = T{
Default = T{},
Palettes = T{ { Name="Base", Bindings = T{} } }
}
};
bindings.ActivePalette = bindings.JobBindings.Palettes[1];
bindings.ActivePaletteIndex = 1;
bindings.LastPaletteIndex = 1;
ApplyBindings();
return;
end
--Check/Create binding folder..
local characterPath = string.format('%sconfig/addons/%s/%s_%u/bindings', AshitaCore:GetInstallPath(), addon.name, name, id);
if not (ashita.fs.exists(characterPath)) then
ashita.fs.create_directory(characterPath);
end
--Check/Create global file..
bindings.GlobalPath = string.format('%s/globals.lua', characterPath);
bindings.GlobalBindings = LoadFile_s(bindings.GlobalPath);
if (bindings.GlobalBindings == nil) then
bindings.GlobalBindings = T{};
if not (ashita.fs.exists(bindings.GlobalPath)) then
WriteGlobals();
end
end
--Check/Create job file..
bindings.JobPath = string.format('%s/%s.lua', characterPath, AshitaCore:GetResourceManager():GetString('jobs.names_abbr', job));
bindings.JobBindings = LoadFile_s(bindings.JobPath);
if (bindings.JobBindings == nil) then
bindings.JobBindings = T{
Default = T{},
Palettes = T{
{ Name = 'Base', Bindings = T{} },
}
};
if not (ashita.fs.exists(bindings.JobPath)) then
WriteJob();
end
end
if (type(bindings.JobBindings.Palettes) ~= 'table') or (#bindings.JobBindings.Palettes == 0) then
bindings.JobBindings.Palettes = T{
{ Name = 'Base', Bindings = T{} },
};
end
bindings.ActivePalette = bindings.JobBindings.Palettes[1];
bindings.ActivePaletteIndex = 1;
bindings.LastPaletteIndex = 1;
ApplyBindings();
end
--[[
/tc palette [add/change/remove/list/next/previous] [name]
]]--
function exposed:BindGlobal(hotkey, binding)
bindings.GlobalBindings[hotkey] = binding;
WriteGlobals();
ApplyBindings();
end
function exposed:BindJob(hotkey, binding)
bindings.JobBindings.Default[hotkey] = binding;
WriteJob();
ApplyBindings();
end
function exposed:BindPalette(hotkey, binding)
bindings.ActivePalette.Bindings[hotkey] = binding;
WriteJob();
ApplyBindings();
end
function exposed:GetFormattedBindings()
return FormatBindings();
end
function exposed:GetDisplayText()
if (bindings.ActivePalette == nil) then
return;
end
local paletteCount = #bindings.JobBindings.Palettes;
if (paletteCount == 1) then
return;
else
return string.format ('%s (%u/%u)', bindings.ActivePalette.Name, bindings.ActivePaletteIndex, paletteCount);
end
end
function exposed:PreviousPalette()
local paletteCount = #bindings.JobBindings.Palettes;
if (paletteCount == 1) then
Error('Current job only has one palette!');
return;
end
bindings.LastPaletteIndex = bindings.ActivePaletteIndex;
bindings.ActivePaletteIndex = bindings.ActivePaletteIndex - 1;
if (bindings.ActivePaletteIndex < 1) then
bindings.ActivePaletteIndex = paletteCount;
end
bindings.ActivePalette = bindings.JobBindings.Palettes[bindings.ActivePaletteIndex];
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
end
function exposed:NextPalette()
local paletteCount = #bindings.JobBindings.Palettes;
if (paletteCount == 1) then
Error('Current job only has one palette!');
return;
end
bindings.LastPaletteIndex = bindings.ActivePaletteIndex;
bindings.ActivePaletteIndex = bindings.ActivePaletteIndex + 1;
if (bindings.ActivePaletteIndex > paletteCount) then
bindings.ActivePaletteIndex = 1;
end
bindings.ActivePalette = bindings.JobBindings.Palettes[bindings.ActivePaletteIndex];
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
end
function exposed:HandleCommand(args)
if (#args < 3) then
return;
end
local cmd = string.lower(args[3]);
if (cmd == 'add') then
if (args[4] == nil) then
Error('Command Syntax: $H/tc palette add [name]$R.');
return;
end
local paletteName = string.lower(args[4]);
for _,palette in ipairs(bindings.JobBindings.Palettes) do
if (string.lower(palette.Name) == paletteName) then
Error('Palette already exists.');
return;
end
end
local newPalette = { Name = args[4], Bindings = T{} };
bindings.JobBindings.Palettes:append(newPalette);
bindings.LastPaletteIndex = bindings.ActivePaletteIndex;
bindings.ActivePalette = newPalette;
bindings.ActivePaletteIndex = #bindings.JobBindings.Palettes;
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
Message('Created palette!');
WriteJob();
elseif (cmd == 'remove') then
if (args[4] == nil) then
Error('Command Syntax: $H/tc palette remove [name]$R.');
return;
end
if (#bindings.JobBindings.Palettes == 1) then
Error('Cannot remove last palette.');
return;
end
local paletteName = string.lower(args[4]);
for index,palette in ipairs(bindings.JobBindings.Palettes) do
if (string.lower(palette.Name) == paletteName) then
table.remove(bindings.JobBindings.Palettes, index);
if (index == bindings.ActivePaletteIndex) then
bindings.ActivePalette = bindings.JobBindings.Palettes[1];
bindings.ActivePaletteIndex = 1;
bindings.LastPaletteIndex = 1;
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
end
Message('Removed palette!');
WriteJob();
return;
end
end
Error('Could not find palette to remove.');
elseif (cmd == 'rename') then
if (args[4] == nil) or (args[5] == nil) then
Error('Command Syntax: $H/tc palette rename [old name] [new name]$R.');
return;
end
local paletteName = string.lower(args[4]);
local newNameLower = string.lower(args[5]);
local targetPalette;
for index,palette in ipairs(bindings.JobBindings.Palettes) do
local lower = string.lower(palette.Name);
if (lower == paletteName) then
targetPalette = palette;
elseif (lower == newNameLower) then
Error('A palette with that name already exists.');
return;
end
end
if targetPalette then
targetPalette.Name = args[5];
Message('Renamed palette!');
WriteJob();
return;
end
Error('Could not find palette to rename.');
elseif (cmd == 'list') then
for index,palette in ipairs(bindings.JobBindings.Palettes) do
Message(string.format('[%u] %s%s', index, palette.Name, (bindings.ActivePaletteIndex == index) and ' - $HACTIVE$R' or ''));
end
elseif (cmd == 'next') then
self:NextPalette();
elseif (cmd == 'previous') then
self:PreviousPalette();
elseif (cmd == 'change') then
if (args[4] == nil) then
Error('Command Syntax: $H/tc palette change [name]$R.');
return;
end
local paletteName = string.lower(args[4]);
for index,palette in ipairs(bindings.JobBindings.Palettes) do
if (string.lower(palette.Name) == paletteName) then
if (bindings.ActivePaletteIndex ~= index) then
bindings.LastPaletteIndex = bindings.ActivePaletteIndex;
bindings.ActivePalette = palette;
bindings.ActivePaletteIndex = index;
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
end
return;
end
end
Error('Could not find palette to change to.');
elseif (cmd == 'last') then
local last = bindings.LastPaletteIndex;
local newPalette = bindings.JobBindings.Palettes[last];
if (newPalette == bindings.ActivePalette) then
Error('You have not changed palettes since previous palette edit. You cannot return to last palette.');
return;
elseif (newPalette == nil) then
Error('Your last palette doesn\'t exist. You cannot return to it.');
return;
end
bindings.LastPaletteIndex = bindings.ActivePaletteIndex;
bindings.ActivePalette = newPalette;
bindings.ActivePaletteIndex = last;
ApplyBindings();
if (gBindingGUI:GetActive()) then
gBindingGUI:UpdatePalette();
end
end
end
function exposed:Update()
ApplyBindings();
end
return exposed;