comparison modules/Action.lua @ 237:704f4a05a1d7

Demodulificate all bar options (except state options)
author Flick
date Thu, 24 Mar 2011 13:11:30 -0700
parents 0e20f65375d5
children
comparison
equal deleted inserted replaced
236:dcdc0235d489 237:704f4a05a1d7
10 -- module declaration 10 -- module declaration
11 local moduleID = "Action" 11 local moduleID = "Action"
12 local module = ReAction:NewModule( moduleID ) 12 local module = ReAction:NewModule( moduleID )
13 13
14 -- Class declarations 14 -- Class declarations
15 local Button = ReAction.Button.Action -- see /classes/ActionButton.lua
16 local Handle = { }
17 local PropHandler = { } 15 local PropHandler = { }
18 16
19 -- Event handlers 17 -- Event handlers
20 function module:OnInitialize() 18 function module:OnInitialize()
21 self.handles = setmetatable({ }, weak) 19 self.handles = setmetatable({ }, weak)
22
23 ReAction:RegisterBarOptionGenerator(self, "GetBarOptions")
24 end 20 end
25 21
26 function module:OnEnable() 22 function module:OnEnable()
27 ReAction:GetModule("State"):RegisterStateProperty("page", nil, PropHandler.GetOptions(), PropHandler) 23 ReAction:GetModule("State"):RegisterStateProperty("page", nil, PropHandler.GetOptions(), PropHandler)
28 end 24 end
29 25
30 function module:OnDisable() 26 function module:OnDisable()
31 ReAction:GetModule("State"):UnregisterStateProperty("page") 27 ReAction:GetModule("State"):UnregisterStateProperty("page")
32 end 28 end
33
34
35
36 ---- Interface ----
37 function module:GetBarOptions(bar)
38 self.handles[bar] = self.handles[bar] or Handle:New(bar)
39 return self.handles[bar]:GetOptions()
40 end
41
42
43 ---- Bar Handle ----
44
45 do
46 local options = {
47 hideEmpty = {
48 name = L["Hide Empty Buttons"],
49 order = 1,
50 type = "toggle",
51 width = "double",
52 get = "GetHideEmpty",
53 set = "SetHideEmpty",
54 },
55 lockButtons = {
56 name = L["Lock Buttons"],
57 desc = L["Prevents picking up/dragging actions (use SHIFT to override this behavior)"],
58 order = 2,
59 type = "toggle",
60 get = "GetLockButtons",
61 set = "SetLockButtons",
62 },
63 lockOnlyCombat = {
64 name = L["Only in Combat"],
65 desc = L["Only lock the buttons when in combat"],
66 order = 3,
67 type = "toggle",
68 disabled = "LockButtonsCombatDisabled",
69 get = "GetLockButtonsCombat",
70 set = "SetLockButtonsCombat",
71 },
72 pages = {
73 name = L["# Pages"],
74 desc = L["Use the Dynamic State tab to specify page transitions"],
75 order = 4,
76 type = "range",
77 min = 1,
78 max = 10,
79 step = 1,
80 get = "GetNumPages",
81 set = "SetNumPages",
82 },
83 mindcontrol = {
84 name = L["Mind Control Support"],
85 desc = L["When possessing a target (e.g. via Mind Control), map the first 12 buttons of this bar to the possessed target's actions."],
86 order = 5,
87 type = "toggle",
88 width = "double",
89 set = "SetMindControl",
90 get = "GetMindControl",
91 },
92 vehicle = {
93 name = L["Vehicle Support"],
94 desc = L["When on a vehicle, map the first 6 buttons of this bar to the vehicle actions. The vehicle-exit button is mapped to the 7th button. Pitch controls are not supported."],
95 order = 6,
96 type = "toggle",
97 width = "double",
98 get = "GetVehicle",
99 set = "SetVehicle",
100 },
101 actions = {
102 name = L["Edit Action IDs"],
103 order = 7,
104 type = "group",
105 inline = true,
106 args = {
107 method = {
108 name = L["Assign"],
109 order = 1,
110 type = "select",
111 width = "double",
112 values = { [0] = L["Choose Method..."],
113 [1] = L["Individually"],
114 [2] = L["All at Once"], },
115 get = "GetActionEditMethod",
116 set = "SetActionEditMethod",
117 },
118 rowSelect = {
119 name = L["Row"],
120 desc = L["Rows are numbered top to bottom"],
121 order = 2,
122 type = "select",
123 width = "half",
124 hidden = "IsButtonSelectHidden",
125 values = "GetRowList",
126 get = "GetSelectedRow",
127 set = "SetSelectedRow",
128 },
129 colSelect = {
130 name = L["Col"],
131 desc = L["Columns are numbered left to right"],
132 order = 3,
133 type = "select",
134 width = "half",
135 hidden = "IsButtonSelectHidden",
136 values = "GetColumnList",
137 get = "GetSelectedColumn",
138 set = "SetSelectedColumn",
139 },
140 pageSelect = {
141 name = L["Page"],
142 order = 4,
143 type = "select",
144 width = "half",
145 hidden = "IsPageSelectHidden",
146 values = "GetPageList",
147 get = "GetSelectedPage",
148 set = "SetSelectedPage",
149 },
150 single = {
151 name = L["Action ID"],
152 usage = L["Specify ID 1-120"],
153 order = 5,
154 type = "input",
155 width = "half",
156 hidden = "IsButtonSelectHidden",
157 get = "GetActionID",
158 set = "SetActionID",
159 validate = "ValidateActionID",
160 },
161 multi = {
162 name = L["ID List"],
163 usage = L["Specify a comma-separated list of IDs for each button in the bar (in order). Separate multiple pages with semicolons (;)"],
164 order = 6,
165 type = "input",
166 multiline = true,
167 width = "double",
168 hidden = "IsMultiIDHidden",
169 get = "GetMultiID",
170 set = "SetMultiID",
171 validate = "ValidateMultiID",
172 },
173 },
174 },
175 }
176
177 local meta = { __index = Handle }
178
179 function Handle:New( bar )
180 return setmetatable(
181 {
182 bar = bar,
183 config = bar:GetConfig(),
184 },
185 meta)
186 end
187
188 function Handle:Refresh()
189 Button:SetupBar(bar)
190 end
191
192 function Handle:UpdateButtonLock()
193 Button.SetButtonLock(self.bar, self.config.lockButtons, self.config.lockButtonsCombat)
194 end
195
196 function Handle:GetLastButton()
197 return self.bar:GetButton(self.bar:GetNumButtons())
198 end
199
200 -- options handlers
201 function Handle:GetOptions()
202 return {
203 type = "group",
204 name = L["Action Buttons"],
205 handler = self,
206 args = options
207 }
208 end
209
210 function Handle:SetHideEmpty(info, value)
211 if value ~= self.config.hideEmpty then
212 self.config.hideEmpty = value
213 for _, b in self.bar:IterateButtons() do
214 b:ShowGrid(not value)
215 end
216 end
217 end
218
219 function Handle:GetHideEmpty()
220 return self.config.hideEmpty
221 end
222
223 function Handle:GetLockButtons()
224 return self.config.lockButtons
225 end
226
227 function Handle:SetLockButtons(info, value)
228 self.config.lockButtons = value
229 self:UpdateButtonLock()
230 end
231
232 function Handle:GetLockButtonsCombat()
233 return self.config.lockButtonsCombat
234 end
235
236 function Handle:SetLockButtonsCombat(info, value)
237 self.config.lockButtonsCombat = value
238 self:UpdateButtonLock()
239 end
240
241 function Handle:LockButtonsCombatDisabled()
242 return not self.config.lockButtons
243 end
244
245 function Handle:GetNumPages()
246 return self.config.nPages
247 end
248
249 function Handle:SetNumPages(info, value)
250 self.config.nPages = value
251 self:Refresh()
252 end
253
254 function Handle:GetMindControl()
255 return self.config.mindcontrol
256 end
257
258 function Handle:SetMindControl(info, value)
259 self.config.mindcontrol = value
260 self:Refresh()
261 end
262
263 function Handle:GetVehicle()
264 return self.config.vehicle
265 end
266
267 function Handle:SetVehicle(info, value)
268 self.config.vehicle = value
269 self:Refresh()
270 end
271
272 function Handle:GetActionEditMethod()
273 return self.editMethod or 0
274 end
275
276 function Handle:SetActionEditMethod(info, value)
277 self.editMethod = value
278 end
279
280 function Handle:IsButtonSelectHidden()
281 return self.editMethod ~= 1
282 end
283
284 function Handle:GetRowList()
285 local r,c = self.bar:GetButtonGrid()
286 if self.rowList == nil or #self.rowList ~= r then
287 local list = { }
288 for i = 1, r do
289 table.insert(list,i)
290 end
291 self.rowList = list
292 end
293 return self.rowList
294 end
295
296 function Handle:GetSelectedRow()
297 local r, c = self.bar:GetButtonGrid()
298 local row = self.selectedRow or 1
299 if row > r then
300 row = 1
301 end
302 self.selectedRow = row
303 return row
304 end
305
306 function Handle:SetSelectedRow(info, value)
307 self.selectedRow = value
308 end
309
310 function Handle:GetColumnList()
311 local r,c = self.bar:GetButtonGrid()
312 if self.columnList == nil or #self.columnList ~= c then
313 local list = { }
314 for i = 1, c do
315 table.insert(list,i)
316 end
317 self.columnList = list
318 end
319 return self.columnList
320 end
321
322 function Handle:GetSelectedColumn()
323 local r, c = self.bar:GetButtonGrid()
324 local col = self.selectedColumn or 1
325 if col > c then
326 col = 1
327 end
328 self.selectedColumn = col
329 return col
330 end
331
332 function Handle:SetSelectedColumn(info, value)
333 self.selectedColumn = value
334 end
335
336 function Handle:IsPageSelectHidden()
337 return self.editMethod ~= 1 or (self.config.nPages or 1) < 2
338 end
339
340 function Handle:GetPageList()
341 local n = self.config.nPages or 1
342 if self.pageList == nil or #self.pageList ~= n then
343 local p = { }
344 for i = 1, n do
345 table.insert(p,i)
346 end
347 self.pageList = p
348 end
349 return self.pageList
350 end
351
352 function Handle:GetSelectedPage()
353 local p = self.selectedPage or 1
354 if p > (self.config.nPages or 1) then
355 p = 1
356 end
357 self.selectedPage = p
358 return p
359 end
360
361 function Handle:SetSelectedPage(info, value)
362 self.selectedPage = value
363 end
364
365 function Handle:GetActionID()
366 local row = self.selectedRow or 1
367 local col = self.selectedColumn or 1
368 local r, c = self.bar:GetButtonGrid()
369 local n = (row-1) * c + col
370 local btn = self.bar:GetButton(n)
371 if btn then
372 return tostring(btn:GetActionID(self.selectedPage or 1))
373 end
374 end
375
376 function Handle:SetActionID(info, value)
377 local row = self.selectedRow or 1
378 local col = self.selectedColumn or 1
379 local r, c = self.bar:GetButtonGrid()
380 local n = (row-1) * c + col
381 local btn = self.bar:GetButton(n)
382 if btn then
383 btn:SetActionID(tonumber(value), self.selectedPage or 1)
384 end
385 end
386
387 function Handle:ValidateActionID(info, value)
388 value = tonumber(value)
389 if value == nil or value < 1 or value > 120 then
390 return L["Specify ID 1-120"]
391 end
392 return true
393 end
394
395 function Handle:IsMultiIDHidden()
396 return self.editMethod ~= 2
397 end
398
399 function Handle:GetMultiID()
400 local p = { }
401 for i = 1, self.config.nPages or 1 do
402 local b = { }
403 for _, btn in self.bar:IterateButtons() do
404 table.insert(b, btn:GetActionID(i))
405 end
406 table.insert(p, table.concat(b,","))
407 end
408 return table.concat(p,";\n")
409 end
410
411
412 local function ParseMultiID(nBtns, nPages, s)
413 if s:match("[^%d%s,;]") then
414 return nil
415 end
416 local p = { }
417 for list in s:gmatch("[^;]+") do
418 local pattern = ("^%s?$"):format(("%s*(%d+)%s*,"):rep(nBtns))
419 local ids = { list:match(pattern) }
420 if #ids ~= nBtns then
421 return nil
422 end
423 table.insert(p,ids)
424 end
425 if #p ~= nPages then
426 return nil
427 end
428 return p
429 end
430
431 function Handle:SetMultiID(info, value)
432 local p = ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value)
433 for page, b in ipairs(p) do
434 for button, id in ipairs(b) do
435 self.bar:GetButton(button):SetActionID(id, page)
436 end
437 end
438 end
439
440 function Handle:ValidateMultiID(info, value)
441 local bad = L["Invalid action ID list string"]
442 if value == nil or ParseMultiID(self.bar:GetNumButtons(), self.config.nPages or 1, value) == nil then
443 return bad
444 end
445 return true
446 end
447 end
448
449 29
450 ------ State property options ------ 30 ------ State property options ------
451 do 31 do
452 local pageOptions = { 32 local pageOptions = {
453 page = { 33 page = {