Repository navigation
Expand file tree
/
Copy pathPaging.lua
More file actions
304 lines (273 loc) · 12.1 KB
/
Copy pathPaging.lua
File metadata and controls
304 lines (273 loc) · 12.1 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
local ADDON, ns = ...
-- When the main bar shows another page.
--
-- === What is decided here ===
--
-- A warrior in a stance, a druid in cat form, a rogue in stealth and anyone
-- who mounted a vehicle see DIFFERENT slots on the main bar. The addon does
-- not decide that: the engine switches the page by a condition written as a
-- string - the same kind macros are written in.
--
-- Our business is to assemble that string and hand it to the secure state
-- handler. The engine evaluates it from then on, combat included, where we
-- have no reach.
--
-- === Why a condition and not a handler of ours ===
--
-- A handler computing the page in Lua would work out of combat and stay silent
-- in it: a slot cannot be assigned to a button from insecure code mid-fight. A
-- bar that does not switch in combat is a bar with the wrong spells in combat,
-- and only a missing bar is worse.
--
-- The string is handed to the engine once, when the settings are applied, and
-- lives until the next apply.
local Paging = {}
ns.Paging = Paging
-- The conditions under which the main bar's page differs, and the order to
-- check them in.
--
-- The order is not taste but precedence: the engine takes the FIRST match.
-- Vehicle and possess stand ahead of everything because they override both
-- stance and form: a druid who boarded a siege engine sees the engine's bar,
-- not the cat's.
--
-- The page numbers are the game's, not ours. Stances count from page seven:
-- "bonusbar:1" is page 7, and so on up to eleven.
local RULES = {
{ when = "[overridebar]", page = "possess" },
{ when = "[vehicleui]", page = "vehicle" },
{ when = "[possessbar]", page = "possess" },
{ when = "[shapeshift]", page = 7 },
{ when = "[bar:2]", page = 2 },
{ when = "[bar:3]", page = 3 },
{ when = "[bar:4]", page = 4 },
{ when = "[bar:5]", page = 5 },
{ when = "[bar:6]", page = 6 },
{ when = "[bonusbar:1]", page = 7 },
{ when = "[bonusbar:2]", page = 8 },
{ when = "[bonusbar:3]", page = 9 },
{ when = "[bonusbar:4]", page = 10 },
{ when = "[bonusbar:5]", page = 11 },
}
Paging.RULES = RULES
--- The page-switching condition string for the secure handler.
--
-- Pure: it has a right answer and that answer does not depend on the game.
--
-- The string is built the way macros read: "condition page; condition page;
-- default page". The engine walks it left to right and takes the first match,
-- so the order in RULES is the precedence.
--
-- Paging switched off answers with nothing rather than with a one. The
-- difference is not subtle: a one would mean "always page one", taking a
-- stance's own spells away from it; nothing means "there is no handler at all",
-- and the game runs the page as it did before the addon.
--
-- === A custom rule over the ready one ===
--
-- The ready string describes how the game's own MAIN bar pages, and that is
-- enough for almost everyone. Almost - because there remains the player who
-- wants something else: page two in combat, the same page in every form, page
-- three under shift. Bartender calls these custom conditions, and taking them
-- away would be saying "we know better than you what you want to see".
--
-- A custom string cancels the ready one entirely rather than extending it. Two
-- rules must not be mixed: the engine takes the first match, and a foreign rule
-- inserted in the middle of ours would change the meaning of its neighbours -
-- that is, behave inexplicably.
--
-- @param wanted any whether the ready paging is on
-- @param custom any the player's own condition string
-- @return string|nil the condition string, or nil
function Paging.Condition(wanted, custom)
if type(custom) == "string" then
local said = custom:match("^%s*(.-)%s*$")
if said ~= "" then return said end
end
if not wanted then return nil end
local parts = {}
for i = 1, #RULES do
local rule = RULES[i]
parts[#parts + 1] = rule.when .. " " .. tostring(rule.page)
end
-- A tail with no condition is the page the bar rests on.
parts[#parts + 1] = "1"
return table.concat(parts, "; ")
end
--- The show/hide condition string for a bar.
--
-- The same technique and the same reason: a bar hidden from insecure code will
-- not hide in combat. An empty setting string means "there is no rule" - the
-- bar is always visible, which is not the same as "[]show", which the engine
-- would read as a condition with no condition.
--
-- The string goes to the engine as it is, whole: we do not undertake to parse
-- someone else's syntax and answer for a typo in it - the engine answers that
-- itself and better.
--
-- @param value any the string from the settings
-- @return string|nil
function Paging.Visibility(value)
if type(value) ~= "string" then return nil end
local said = value:match("^%s*(.-)%s*$")
if said == "" then return nil end
return said
end
--============================================================================
-- Visibility by condition
--============================================================================
-- The hiding conditions in order of precedence.
--
-- === Why a list and not a walk over the settings ===
--
-- The engine reads the string left to right and takes the first match. So the
-- order of the conditions is their precedence, while an order taken from
-- walking a settings table would be random: a Lua table has no order at all.
-- A string assembled twice would come out different - and the bar would behave
-- differently after every login, inexplicably to the player.
--
-- Each row is a setting key and the macro condition it means. The condition
-- names are the game's: "possessbar", "vehicleui", "overridebar" are its
-- words, not ours.
local HIDE_RULES = {
{ key = "always", when = "" },
{ key = "possess", when = "[possessbar]" },
{ key = "overridebar", when = "[overridebar]" },
{ key = "vehicleui", when = "[vehicleui]" },
{ key = "vehicle", when = "[target=vehicle,exists]" },
{ key = "combat", when = "[combat]" },
{ key = "nocombat", when = "[nocombat]" },
{ key = "pet", when = "[pet]" },
{ key = "nopet", when = "[nopet]" },
{ key = "mounted", when = "[mounted]" },
}
Paging.HIDE_RULES = HIDE_RULES
--- The show/hide condition string for a bar.
--
-- Pure. Handed to the secure state handler, and the engine evaluates it -
-- combat included, where we have no reach. A bar hidden from insecure code
-- will not hide in combat.
--
-- A pet battle stands ahead of everything and is not asked about at all:
-- during one the screen belongs to the battle, and an action bar on it is a
-- foreign thing over a foreign window. Bartender does the same, and this is
-- the case where the player should have no choice.
--
-- A player's custom condition cancels every tick at once rather than extending
-- them: the ticks assemble into a string by precedence, and a foreign rule
-- inserted in the middle would change the meaning of its neighbours.
--
-- @param vis any the bar's visibility settings
-- @return string the condition string
function Paging.HideDriver(vis)
local parts = { "[petbattle]hide" }
if type(vis) == "table" then
local custom = type(vis.customText) == "string"
and vis.customText:match("^%s*(.-)%s*$") or ""
if vis.custom == true and custom ~= "" then
parts[#parts + 1] = custom
else
for i = 1, #HIDE_RULES do
local rule = HIDE_RULES[i]
if vis[rule.key] == true then
parts[#parts + 1] = rule.when .. "hide"
end
end
-- Stances: each has its own number, and the bar may hide in any of
-- them. Walked by number rather than over the table, for the same
-- reason the conditions above are listed by hand.
if type(vis.stance) == "table" then
for i = 1, 10 do
if vis.stance[i] == true then
parts[#parts + 1] = "[stance:" .. i .. "]hide"
end
end
end
end
end
-- A tail with no condition: the bar is visible when no rule matched.
parts[#parts + 1] = "show"
return table.concat(parts, ";")
end
--============================================================================
-- Page by state
--============================================================================
-- Modifier key prefixes in the order the engine asks for them. The order is
-- the same as Bartender's, and for the same reason as above: the string has to
-- assemble identically every time.
local MODIFIERS = { "ctrl", "alt", "shift" }
Paging.MODIFIERS = MODIFIERS
--- The page-switching condition string, assembled from the player's choices.
--
-- Pure. Precedence top to bottom, and it is not taste:
--
-- possess - ahead of everything: in someone else's body your own spells
-- cannot be cast at all, so any other page there is meaningless;
-- modifiers - a quick swap for the duration of a press, and it has to
-- outrank the permanent rules or it would not be seen;
-- the game's pages (shift-wheel and shift-1..6) - a permanent swap;
-- stances - the same, but by form;
-- the tail - the page the bar rests on when nothing matched.
--
-- @param state any the bar's state settings
-- @param home any the bar's resting page
-- @return string|nil the condition string, or nil when there is nothing to
-- switch
function Paging.PageDriver(state, home)
if type(state) ~= "table" or state.enabled ~= true then return nil end
local fallback = tonumber(home) or 1
local custom = type(state.customText) == "string"
and state.customText:match("^%s*(.-)%s*$") or ""
if state.custom == true then
if custom == "" then return nil end
return custom
end
local parts = {}
-- Possess-style control: vehicles and the override bar. Shapeshift is NOT
-- part of it, and that cost a player their bar.
--
-- Regression: this said "[overridebar][possessbar][shapeshift]possess", as
-- Bartender4 does. On a modern client "[shapeshift]" means the temporary
-- shapeshift bar - a thing of the same kind as a vehicle. In classic a form
-- is an ordinary stance, and the condition is true for any warrior in a
-- stance, any rogue in stealth, any druid in form. The bar went off to work
-- out which possess bar it had, found none (classic has no possess calls at
-- all) and ended up on a page that does not exist - empty.
--
-- Stances and forms switch the page through their own condition below:
-- "bonusbar". That is the condition the game itself uses, and the engine
-- evaluates it, not us.
if state.possess == true then
parts[#parts + 1] = "[overridebar][possessbar]possess"
end
for i = 1, #MODIFIERS do
local page = tonumber(state[MODIFIERS[i]])
if page and page > 0 then
parts[#parts + 1] = "[mod:" .. MODIFIERS[i] .. "]" .. page
end
end
-- Stances and forms. They stand ABOVE the game's pages, and that is not
-- arbitrary: in the game itself a form overrides the chosen page rather
-- than the other way round - a druid in cat form sees cat spells whatever
-- page they paged to.
if type(state.stance) == "table" then
for i = 1, 10 do
local page = tonumber(state.stance[i])
if page and page > 0 then
parts[#parts + 1] = "[bonusbar:" .. i .. "]" .. page
end
end
end
-- The game's pages: the ones switched by shift-wheel and shift-digit. The
-- page number matches the condition number - that is its own count, not
-- ours.
if state.actionbar == true then
for i = 2, 6 do
parts[#parts + 1] = "[bar:" .. i .. "]" .. i
end
end
local default = tonumber(state.default)
if default == nil or default < 1 then default = fallback end
parts[#parts + 1] = tostring(default)
return table.concat(parts, ";")
end