Mercurial > wow > reaction
comparison classes/Bar.lua @ 155:806a61b331a0
Pushed the state implementation into Bar
author | Flick <flickerstreak@gmail.com> |
---|---|
date | Fri, 15 May 2009 22:38:19 +0000 |
parents | df67685b340e |
children | 611e6ce08717 |
comparison
equal
deleted
inserted
replaced
154:df67685b340e | 155:806a61b331a0 |
---|---|
7 local format = string.format | 7 local format = string.format |
8 | 8 |
9 ReAction:UpdateRevision("$Revision$") | 9 ReAction:UpdateRevision("$Revision$") |
10 | 10 |
11 local KB = LibStub("LibKeyBound-1.0") | 11 local KB = LibStub("LibKeyBound-1.0") |
12 | |
13 ---- Secure snippets ---- | |
14 local _reaction_init = | |
15 [[ | |
16 anchorKeys = newtable("point","relPoint","x","y") | |
17 | |
18 state = nil | |
19 set_state = nil | |
20 state_override = nil | |
21 | |
22 showAll = false | |
23 hidden = false | |
24 | |
25 defaultAlpha = 1.0 | |
26 defaultScale = 1.0 | |
27 defaultAnchor = newtable() | |
28 | |
29 activeStates = newtable() | |
30 settings = newtable() | |
31 extensions = newtable() | |
32 ]] | |
33 | |
34 local _reaction_refresh = | |
35 [[ | |
36 local oldState = state | |
37 state = state_override or set_state or state | |
38 | |
39 if state then | |
40 local settings = settings[state] | |
41 if settings then | |
42 -- show/hide | |
43 local h = settings.hide and not showAll | |
44 if h ~= hidden then | |
45 if h then | |
46 self:Hide() | |
47 else | |
48 self:Show() | |
49 end | |
50 hidden = h | |
51 end | |
52 -- re-anchor | |
53 local old_anchor = activeStates.anchor | |
54 activeStates.anchor = settings.anchorEnable and state | |
55 if old_anchor ~= activeStates.anchor or not set_state then | |
56 if activeStates.anchor then | |
57 if settings.anchorPoint then | |
58 self:ClearAllPoints() | |
59 local f = self:GetAttribute("frameref-anchor-"..state) | |
60 if f then | |
61 self:SetPoint(settings.anchorPoint, f, settings.anchorRelPoint, settings.anchorX, settings.anchorY) | |
62 end | |
63 end | |
64 elseif defaultAnchor.point then | |
65 self:ClearAllPoints() | |
66 self:SetPoint(defaultAnchor.point, defaultAnchor.frame, | |
67 defaultAnchor.relPoint, defaultAnchor.x, defaultAnchor.y) | |
68 end | |
69 end | |
70 -- re-scale | |
71 local old_scale = activeStates.scale | |
72 activeStates.scale = settings.enableScale and state | |
73 if old_scale ~= activeStates.scale or not set_state then | |
74 self:SetScale(activeStates.scale and settings.scale or defaultScale) | |
75 end | |
76 -- alpha | |
77 local old_alpha = activeStates.alpha | |
78 activeStates.alpha = settings.enableAlpha and state | |
79 if old_alpha ~= activeStates.alpha or not set_state then | |
80 self:SetAlpha(activeStates.alpha and settings.alpha or defaultAlpha) | |
81 end | |
82 end | |
83 end | |
84 | |
85 for _, attr in pairs(extensions) do | |
86 control:RunAttribute(attr) | |
87 end | |
88 | |
89 control:ChildUpdate() | |
90 | |
91 if showAll then | |
92 control:CallMethod("UpdateHiddenLabel", state and settings[state] and settings[state].hide) | |
93 end | |
94 | |
95 if oldState ~= state then | |
96 control:CallMethod("StateRefresh", state) | |
97 end | |
98 ]] | |
99 | |
100 local _onstate_reaction = -- function( self, stateid, newstate ) | |
101 [[ | |
102 set_state = newstate | |
103 ]] .. _reaction_refresh | |
104 | |
105 local _onstate_showgrid = -- function( self, stateid, newstate ) | |
106 [[ | |
107 control:ChildUpdate(stateid,newstate) | |
108 control:CallMethod("UpdateShowGrid") | |
109 ]] | |
110 | |
111 local _onstate_unitexists = -- function( self, stateid, newstate ) | |
112 [[ | |
113 | |
114 ]] .. _reaction_refresh | |
115 | |
116 local _onclick = -- function( self, button, down ) | |
117 [[ | |
118 if state_override == button then | |
119 state_override = nil -- toggle | |
120 else | |
121 state_override = button | |
122 end | |
123 ]] .. _reaction_refresh | |
124 | |
12 | 125 |
13 ---- Bar class ---- | 126 ---- Bar class ---- |
14 local Bar = { } | 127 local Bar = { } |
15 local weak = { __mode = "k" } | 128 local weak = { __mode = "k" } |
16 local frameList = { } | 129 local frameList = { } |
49 f:SetHeight(self.height) | 162 f:SetHeight(self.height) |
50 f:SetAlpha(config.alpha or 1.0) | 163 f:SetAlpha(config.alpha or 1.0) |
51 f:Show() | 164 f:Show() |
52 f:EnableMouse(false) | 165 f:EnableMouse(false) |
53 f:SetClampedToScreen(true) | 166 f:SetClampedToScreen(true) |
54 | |
55 f:SetAttribute("_onstate-showgrid", | |
56 -- function(self,stateid,newstate) | |
57 [[ | |
58 control:ChildUpdate(stateid,newstate) | |
59 control:CallMethod("UpdateShowGrid") | |
60 ]]) | |
61 f.UpdateShowGrid = function(frame) | |
62 for button in self:IterateButtons() do | |
63 button:UpdateShowGrid() | |
64 end | |
65 end | |
66 ReAction.gridProxy:AddFrame(f) | 167 ReAction.gridProxy:AddFrame(f) |
168 | |
169 -- secure handlers | |
170 f:Execute(_reaction_init) | |
171 f:SetAttribute("_onstate-reaction", _onstate_reaction) | |
172 f:SetAttribute("_onstate-showgrid", _onstate_showgrid) | |
173 f:SetAttribute("_onstate-unitexists", _onstate_unitexists) | |
174 f:SetAttribute("_onclick", _onclick) | |
175 | |
176 -- secure handler CallMethod()s | |
177 f.UpdateShowGrid = function() self:UpdateShowGrid() end | |
178 f.StateRefresh = function() self:RefreshControls() end | |
179 f.UpdateHiddenLabel = function(f,hidden) self:SetLabelSubtext(hidden and L["Hidden"]) end | |
67 | 180 |
68 -- Override the default frame accessor to provide strict read-only access | 181 -- Override the default frame accessor to provide strict read-only access |
69 function self:GetFrame() | 182 function self:GetFrame() |
70 return f | 183 return f |
71 end | 184 end |
213 end | 326 end |
214 | 327 |
215 function Bar:SetAlpha(value) | 328 function Bar:SetAlpha(value) |
216 self.config.alpha = value | 329 self.config.alpha = value |
217 self:GetFrame():SetAlpha(value or 1.0) | 330 self:GetFrame():SetAlpha(value or 1.0) |
331 self:UpdateDefaultStateAlpha() | |
218 ReAction:RefreshBar(self) | 332 ReAction:RefreshBar(self) |
219 end | 333 end |
220 | 334 |
221 -- iterator returns button, idx and does NOT iterate in index order | |
222 function Bar:IterateButtons() | 335 function Bar:IterateButtons() |
336 -- iterator returns button, idx and does NOT iterate in index order | |
223 return pairs(self.buttons) | 337 return pairs(self.buttons) |
224 end | 338 end |
225 | 339 |
226 -- | 340 -- |
227 -- Methods | 341 -- Methods |
228 -- | 342 -- |
229 | 343 |
230 function Bar:SetConfigMode(mode) | 344 function Bar:SetConfigMode(mode) |
345 self:SetSecureData("showAll",mode) | |
346 self:UpdateUnitWatch() | |
231 self:ShowControls(mode) | 347 self:ShowControls(mode) |
232 if self.unitwatch then | |
233 if mode then | |
234 UnregisterUnitWatch(self:GetFrame()) | |
235 self:GetFrame():Show() | |
236 else | |
237 RegisterUnitWatch(self:GetFrame()) | |
238 end | |
239 end | |
240 for b in self:IterateButtons() do | 348 for b in self:IterateButtons() do |
241 b:ShowGridTemp(mode) | 349 b:ShowGridTemp(mode) |
242 b:UpdateActionIDLabel(mode) | 350 b:UpdateActionIDLabel(mode) |
243 end | 351 end |
244 end | 352 end |
245 | 353 |
246 function Bar:SetKeybindMode(mode) | 354 function Bar:SetKeybindMode(mode) |
247 if self.unitwatch then | 355 self:SetSecureData("showAll",mode) |
248 if mode then | 356 self:UpdateUnitWatch() |
249 UnregisterUnitWatch(self:GetFrame()) | |
250 self:GetFrame():Show() | |
251 else | |
252 RegisterUnitWatch(self:GetFrame()) | |
253 end | |
254 end | |
255 for b in self:IterateButtons() do | 357 for b in self:IterateButtons() do |
256 b:SetKeybindMode(mode) | 358 b:SetKeybindMode(mode) |
257 end | 359 end |
258 end | 360 end |
259 | 361 |
279 local fr = a or f:GetParent() | 381 local fr = a or f:GetParent() |
280 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0) | 382 f:SetPoint(p, a or f:GetParent(), c.relpoint, c.x or 0, c.y or 0) |
281 else | 383 else |
282 f:SetPoint("CENTER") | 384 f:SetPoint("CENTER") |
283 end | 385 end |
386 | |
387 self:UpdateDefaultStateAnchor() | |
284 end | 388 end |
285 | 389 |
286 function Bar:ClipNButtons( n ) | 390 function Bar:ClipNButtons( n ) |
287 local cfg = self.config | 391 local cfg = self.config |
288 local r = cfg.btnRows or 1 | 392 local r = cfg.btnRows or 1 |
329 | 433 |
330 function Bar:SkinButton() | 434 function Bar:SkinButton() |
331 -- does nothing by default | 435 -- does nothing by default |
332 end | 436 end |
333 | 437 |
438 function Bar:UpdateShowGrid() | |
439 for button in self:IterateButtons() do | |
440 button:UpdateShowGrid() | |
441 end | |
442 end | |
443 | |
444 function Bar:UpdateUnitWatch() | |
445 if self.unitwatch then | |
446 if self.unitwatchActive and (ReAction:GetConfigMode() or ReAction:GetKeybindMode()) then | |
447 UnregisterUnitWatch(self:GetFrame()) | |
448 self.unitwatchActive = false | |
449 elseif not self.unitwatchActive then | |
450 RegisterUnitWatch(self:GetFrame()) | |
451 self.unitwatchActive = true | |
452 end | |
453 self:RefreshSecureState() | |
454 end | |
455 end | |
456 | |
334 function Bar:ShowControls(show) | 457 function Bar:ShowControls(show) |
335 local f = self.overlay | |
336 if show then | 458 if show then |
337 if not f then | 459 if not self.overlay then |
338 f = Bar.Overlay:New(self) -- see Overlay.lua | 460 self.overlay = Bar.Overlay:New(self) -- see Overlay.lua |
339 self.overlay = f | 461 end |
340 end | 462 self.overlay:Show() |
341 f:Show() | 463 self:RefreshSecureState() |
342 elseif f then | 464 elseif self.overlay then |
343 f:Hide() | 465 self.overlay:Hide() |
344 end | 466 end |
345 end | 467 end |
346 | 468 |
347 function Bar:RefreshControls() | 469 function Bar:RefreshControls() |
348 if self.overlay and self.overlay:IsShown() then | 470 if self.overlay and self.overlay:IsShown() then |
358 | 480 |
359 -- | 481 -- |
360 -- Secure state functions | 482 -- Secure state functions |
361 -- | 483 -- |
362 | 484 |
485 function Bar:GetSecureState() | |
486 local env = GetManagedEnvironment(self:GetFrame()) | |
487 return env and env.state | |
488 end | |
489 | |
490 function Bar:GetStateProperty(state, propname) | |
491 -- override in modules/State.lua for now | |
492 end | |
493 | |
494 function Bar:SetStateProperty(state, propname, value) | |
495 -- override in modules/State.lua for now | |
496 end | |
497 | |
498 function Bar:RefreshSecureState() | |
499 self:GetFrame():Execute(_reaction_refresh) | |
500 end | |
501 | |
502 -- usage: SetSecureData(globalname, [tblkey1, tblkey2, ...], value) | |
503 function Bar:SetSecureData( ... ) | |
504 local n = select('#',...) | |
505 if n < 2 then | |
506 error("ReAction.Bar:SetSecureData() requires at least 2 arguments") | |
507 end | |
508 local f = self:GetFrame() | |
509 f:SetAttribute("data-depth",n-1) | |
510 f:SetAttribute("data-value",select(n,...)) | |
511 for i = 1, n-1 do | |
512 local key = select(i,...) | |
513 if key == nil then | |
514 error("ReAction.Bar:SetSecureData() - nil table key in argument list (#"..i..")") | |
515 end | |
516 f:SetAttribute("data-key-"..i, key) | |
517 end | |
518 f:Execute( | |
519 [[ | |
520 local n = self:GetAttribute("data-depth") | |
521 if n > 0 then | |
522 local value = self:GetAttribute("data-value") | |
523 local t = _G | |
524 for i = 1, n do | |
525 local key = self:GetAttribute("data-key-"..i) | |
526 if not key then return end | |
527 if not t[key] then | |
528 t[key] = newtable() | |
529 end | |
530 if i == n then | |
531 t[key] = value | |
532 else | |
533 t = t[key] | |
534 end | |
535 end | |
536 end | |
537 ]]) | |
538 self:RefreshSecureState() | |
539 end | |
540 | |
541 function Bar:SetSecureStateData( state, key, value ) | |
542 self:SetSecureData("settings",state,key,value) | |
543 end | |
544 | |
545 -- sets a snippet to be run as an extension to _onstate-reaction | |
546 function Bar:SetSecureStateExtension( id, snippet ) | |
547 if id == nil then | |
548 error("ReAction.Bar:SetSecureStateExtension() requires an id") | |
549 end | |
550 local f = self:GetFrame() | |
551 f:SetAttribute("input-secure-ext-id",id) | |
552 f:SetAttribute("secure-ext-"..id,snippet) | |
553 f:Execute( | |
554 [[ | |
555 local id = self:GetAttribute("input-secure-ext-id") | |
556 if id then | |
557 extensions[id] = self:GetAttribute("secure-ext-"..id) or nil | |
558 end | |
559 ]]) | |
560 self:RefreshSecureState() | |
561 end | |
562 | |
563 function Bar:SetFrameRef( name, refFrame ) | |
564 if refFrame then | |
565 local _, explicit = refFrame:IsProtected() | |
566 if not explicit then | |
567 refFrame = nil | |
568 end | |
569 end | |
570 if refFrame then | |
571 self:GetFrame():SetFrameRef(name,refFrame) | |
572 else | |
573 self:GetFrame():SetAttribute("frameref-"..name,nil) | |
574 end | |
575 end | |
576 | |
577 function Bar:SetStateDriver( rule ) | |
578 if rule then | |
579 RegisterStateDriver(self:GetFrame(),"reaction",rule) | |
580 elseif self.statedriver then | |
581 UnregisterStateDriver(self:GetFrame(),"reaction") | |
582 end | |
583 self.statedriver = rule | |
584 self:RefreshSecureState() | |
585 end | |
586 | |
363 -- pass unit=nil to set up the unit elsewhere, if you want something more complex | 587 -- pass unit=nil to set up the unit elsewhere, if you want something more complex |
364 function Bar:RegisterUnitWatch( unit, enable ) | 588 function Bar:RegisterUnitWatch( unit, enable ) |
365 local f = self:GetFrame() | 589 local f = self:GetFrame() |
366 if unit then | 590 if unit then |
367 f:SetAttribute("unit",unit) | 591 f:SetAttribute("unit",unit) |
368 end | 592 end |
369 if not ReAction:GetConfigMode() then | |
370 if enable then | |
371 RegisterUnitWatch(f) | |
372 elseif self.unitwatch then | |
373 UnregisterUnitWatch(f) | |
374 end | |
375 end | |
376 self.unitwatch = enable | 593 self.unitwatch = enable |
377 end | 594 self:UpdateUnitWatch() |
378 | 595 end |
596 | |
597 -- set a keybind to push a value into "state-reaction" attribute | |
598 function Bar:SetStateKeybind( key, state ) | |
599 local f = self:GetFrame() | |
600 local binds = self.statebinds | |
601 if not binds then | |
602 binds = { } | |
603 self.statebinds = binds | |
604 end | |
605 | |
606 -- clear the old binding, if any | |
607 if binds[state] then | |
608 SetOverrideBinding(f, false, binds[state], nil) | |
609 end | |
610 | |
611 if key then | |
612 SetOverrideBinding(f, false, key, state, nil) -- state name is virtual mouse button | |
613 end | |
614 binds[state] = key | |
615 end | |
616 | |
617 function Bar:GetStateKeybind( state ) | |
618 if self.statebinds and state then | |
619 return self.statebinds[state] | |
620 end | |
621 end | |
622 | |
623 function Bar:UpdateDefaultStateAnchor() | |
624 local point, frame, relPoint, x, y = self:GetAnchor() | |
625 local f = self:GetFrame() | |
626 f:SetAttribute("defaultAnchor-point",point) | |
627 f:SetAttribute("defaultAnchor-relPoint",relPoint) | |
628 f:SetAttribute("defaultAnchor-x",x) | |
629 f:SetAttribute("defaultAnchor-y",y) | |
630 self:SetFrameRef("defaultAnchor",_G[frame or "UIParent"]) | |
631 f:Execute([[ | |
632 for _, k in pairs(anchorKeys) do | |
633 defaultAnchor[k] = self:GetAttribute("defaultAnchor-"..k) | |
634 end | |
635 defaultAnchor.frame = self:GetAttribute("frameref-defaultAnchor") | |
636 ]]) | |
637 end | |
638 | |
639 function Bar:UpdateDefaultStateAlpha() | |
640 local f = self:GetFrame() | |
641 f:SetAttribute("defaultAlpha",self:GetAlpha()) | |
642 f:Execute([[ | |
643 defaultAlpha = self:GetAttribute("defaultAlpha") | |
644 ]]) | |
645 end |