Mercurial > wow > reaction
comparison BagButton.lua @ 257:920d17851a93 stable
Merge 1.1 beta 4 to stable
author | Flick |
---|---|
date | Tue, 12 Apr 2011 16:06:31 -0700 |
parents | 65f2805957a0 |
children | c918ff9ac787 |
comparison
equal
deleted
inserted
replaced
210:b2b105747466 | 257:920d17851a93 |
---|---|
1 local addonName, addonTable = ... | |
2 local ReAction = addonTable.ReAction | |
3 local L = ReAction.L | |
4 local _G = _G | |
5 local CreateFrame = CreateFrame | |
6 local format = string.format | |
7 local GetCVar = GetCVar | |
8 local ContainerIDToInventoryID = ContainerIDToInventoryID | |
9 local NUM_CONTAINER_FRAMES = NUM_CONTAINER_FRAMES | |
10 local IsModifiedClick = IsModifiedClick | |
11 local CursorHasItem = CursorHasItem | |
12 local GetInventoryItemTexture = GetInventoryItemTexture | |
13 local GetInventorySlotInfo = GetInventorySlotInfo | |
14 local PickupBagFromSlot = PickupBagFromSlot | |
15 local CursorCanGoInSlot = CursorCanGoInSlot | |
16 | |
17 -- class declarations | |
18 local buttonTypeID = "Bag" | |
19 local weak = { __mode = "k" } | |
20 local Super = ReAction.Button | |
21 local BagBase = setmetatable( | |
22 { | |
23 defaultBarConfig = { | |
24 type = buttonTypeID, | |
25 btnWidth = 30, | |
26 btnHeight = 30, | |
27 btnRows = 1, | |
28 btnColumns = 6, | |
29 spacing = 4, | |
30 buttons = { } | |
31 }, | |
32 | |
33 barType = L["Bag Bar"], | |
34 buttonTypeID = buttonTypeID, | |
35 | |
36 allButtons = setmetatable( { }, weak ) | |
37 }, | |
38 { __index = Super } ) | |
39 | |
40 local Bag = setmetatable( { }, { __index = BagBase } ) | |
41 local Backpack = setmetatable( { }, { __index = BagBase } ) | |
42 local Keyring = setmetatable( { }, { __index = BagBase } ) | |
43 | |
44 ReAction.Button.Bag = BagBase | |
45 ReAction:RegisterBarType(BagBase) | |
46 | |
47 -- | |
48 -- Bag Button base class | |
49 -- | |
50 | |
51 function BagBase:New( btnCfg, bar, idx, idHint ) | |
52 local name = format("ReAction_%s_Bag_%d",bar:GetName(),idx) | |
53 | |
54 -- use a variable private leaf implementation class | |
55 -- unlike traditional OO programming, we can initialize the leaf | |
56 -- class before initializing its parent | |
57 local class = Bag | |
58 if idx == 1 then | |
59 class = Backpack | |
60 elseif idx == 6 then | |
61 class = Keyring | |
62 end | |
63 self = class:New(name, btnCfg, bar, idx) | |
64 | |
65 local f = self:GetFrame() | |
66 local config = self:GetConfig() | |
67 | |
68 -- set up the bag ID pool | |
69 self:SetActionIDPool("bag",6) | |
70 config.bagID = self:AcquireActionID(config.bagID, idHint, true) | |
71 | |
72 -- non secure scripts | |
73 f:SetScript("OnEvent", function(frame, ...) self:OnEvent(...) end) | |
74 f:SetScript("OnEnter", function(frame) self:OnEnter() end) | |
75 f:SetScript("OnLeave", function(frame) self:OnLeave() end) | |
76 f:SetScript("OnReceiveDrag", function(frame, ...) self:OnReceiveDrag(...) end) | |
77 f:SetScript("OnClick", function(frame, ...) self:OnClick(...) end) | |
78 | |
79 -- secure handlers | |
80 -- (none) | |
81 | |
82 -- event registration | |
83 f:EnableMouse(true) | |
84 f:RegisterForClicks("LeftButtonUp","RightButtonUp") | |
85 f:RegisterEvent("UPDATE_BINDINGS") | |
86 | |
87 -- frame setup | |
88 f:SetID(self:GetBagID()) | |
89 | |
90 if not f.hotkey then | |
91 local h = f:CreateFontString(name.."HotKey","ARTWORK","NumberFontNormalSmallGray") | |
92 h:SetWidth(30) | |
93 h:SetHeight(10) | |
94 h:SetJustifyH("RIGHT") | |
95 h:SetPoint("TOPLEFT",f,"TOPLEFT",-2,-2) | |
96 h:Show() | |
97 f.hotkey = h | |
98 end | |
99 | |
100 if not _G[name.."ItemAnim"] then | |
101 local anim = CreateFrame("Model",name.."ItemAnim",f,"ItemAnimTemplate") | |
102 anim:SetPoint("BOTTOMRIGHT",f,"BOTTOMRIGHT",-10,0) | |
103 anim:Hide() | |
104 end | |
105 | |
106 if not f.border then | |
107 local b = f:CreateTexture(name.."Border","OVERLAY") | |
108 b:SetAllPoints() | |
109 b:SetWidth(f:GetWidth()*(62/36)) | |
110 b:SetHeight(f:GetHeight()*(62/36)) | |
111 b:SetTexture("Interface\\Buttons\UI-ActionButton-Border") | |
112 b:SetBlendMode("ADD") | |
113 b:Hide() | |
114 f.border = b | |
115 end | |
116 | |
117 self.frames.count:SetDrawLayer("ARTWORK") | |
118 | |
119 self.frames.hotkey = f.hotkey | |
120 self.frames.border = _G[name.."Border"] | |
121 self.frames.icon = _G[name.."IconTexture"] | |
122 self.frames.anim = _G[name.."ItemAnim"] | |
123 | |
124 -- initial display | |
125 if ReAction:GetConfigMode() then | |
126 self:GetFrame():Show() | |
127 end | |
128 | |
129 self:Refresh() | |
130 | |
131 BagBase.allButtons[self] = true | |
132 | |
133 return self | |
134 end | |
135 | |
136 function BagBase:Destroy() | |
137 BagBase.allButtons[self] = nil | |
138 Super.Destroy(self) | |
139 end | |
140 | |
141 | |
142 function BagBase:GetActionID() | |
143 return self.config.bagID | |
144 end | |
145 | |
146 function BagBase:GetBagID() | |
147 return self:GetActionID() - 1 | |
148 end | |
149 | |
150 function BagBase:Refresh() | |
151 Super.Refresh(self) | |
152 self:UpdateHotkey() | |
153 self:Update() | |
154 end | |
155 | |
156 function BagBase:Update() | |
157 self:UpdateChecked() | |
158 end | |
159 | |
160 function BagBase:UpdateChecked(force) | |
161 if force == nil then | |
162 for i=1, NUM_CONTAINER_FRAMES do | |
163 local c = _G["ContainerFrame"..i] | |
164 if c:GetID() == self:GetBagID() and c:IsShown() then | |
165 self:GetFrame():SetChecked(1) | |
166 return | |
167 end | |
168 end | |
169 self:GetFrame():SetChecked(0) | |
170 end | |
171 self:GetFrame():SetChecked(force) | |
172 end | |
173 | |
174 function BagBase:OnEvent(evt, ...) | |
175 if self[evt] then | |
176 self[evt](self, ...) | |
177 end | |
178 end | |
179 | |
180 function BagBase:OnEnter() | |
181 self:SetTooltip() | |
182 end | |
183 | |
184 function BagBase:OnLeave() | |
185 GameTooltip:Hide() | |
186 end | |
187 | |
188 function BagBase:UPDATE_BINDINGS() | |
189 self:UpdateHotkey() | |
190 end | |
191 | |
192 function BagBase:IterateAllButtons() | |
193 return pairs(self.allButtons) | |
194 end | |
195 | |
196 | |
197 -- | |
198 -- Bag Button class | |
199 -- | |
200 function Bag:New(name, cfg, bar, idx) | |
201 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" ) | |
202 | |
203 local f = self:GetFrame() | |
204 | |
205 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight") | |
206 | |
207 f:RegisterEvent("CURSOR_UPDATE") | |
208 f:RegisterEvent("BAG_UPDATE") | |
209 f:RegisterEvent("BAG_CLOSED") | |
210 f:SetScript("OnDragStart", function(frame, ...) self:OnDragStart(...) end) | |
211 f:RegisterForDrag("LeftButton") | |
212 | |
213 -- attach to skinner | |
214 bar:SkinButton(self, | |
215 { | |
216 Icon = _G[name.."IconTexture"] | |
217 } | |
218 ) | |
219 | |
220 return self | |
221 end | |
222 | |
223 function Bag:GetInventorySlot() | |
224 return ContainerIDToInventoryID(self:GetBagID()) | |
225 end | |
226 | |
227 function Bag:GetInventorySlotName() | |
228 return "Bag"..(self:GetBagID()-1).."Slot" | |
229 end | |
230 | |
231 function Bag:SetTooltip() | |
232 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT") | |
233 if not GameTooltip:SetInventoryItem("player", self:GetInventorySlot()) then | |
234 GameTooltip:SetText(EQUIP_CONTAINER, 1.0, 1.0, 1.0) | |
235 end | |
236 end | |
237 | |
238 function Bag:Update() | |
239 local texture = GetInventoryItemTexture("player", self:GetInventorySlot()) | |
240 if texture then | |
241 self.frames.icon:SetTexture(texture) | |
242 self.frames.icon:Show() | |
243 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot2") | |
244 else | |
245 local _, bgTex = GetInventorySlotInfo(self:GetInventorySlotName()) | |
246 self.frames.icon:SetTexture(bgTex) | |
247 self:GetFrame():SetNormalTexture("Interface\\Buttons\\UI-Quickslot") | |
248 end | |
249 self:UpdateChecked() | |
250 end | |
251 | |
252 function Bag:OnClick() | |
253 if IsModifiedClick("OPENALLBAGS") then | |
254 OpenAllBags() | |
255 else | |
256 if not PutItemInBag(self:GetInventorySlot()) then | |
257 ToggleBag(self:GetBagID()) | |
258 end | |
259 end | |
260 self:UpdateChecked() | |
261 end | |
262 | |
263 function Bag:OnReceiveDrag() | |
264 if CursorHasItem() then | |
265 PutItemInBag(self:GetInventorySlot()) | |
266 end | |
267 end | |
268 | |
269 function Bag:OnDragStart() | |
270 PickupBagFromSlot(self:GetInventorySlot()) | |
271 self:Update() | |
272 end | |
273 | |
274 function Bag:BAG_UPDATE(bag) | |
275 if bag == self:GetBagID() then | |
276 self:Update() | |
277 end | |
278 end | |
279 | |
280 function Bag:CURSOR_UPDATE() | |
281 if CursorCanGoInSlot(self:GetInventorySlot()) then | |
282 self:GetFrame():LockHighlight() | |
283 else | |
284 self:GetFrame():UnlockHighlight() | |
285 end | |
286 end | |
287 | |
288 function Bag:BAG_CLOSED(bag) | |
289 if bag == self:GetBagID() then | |
290 self:Update() | |
291 end | |
292 end | |
293 | |
294 | |
295 -- | |
296 -- Backpack Button class | |
297 -- | |
298 function Backpack:New(name, cfg, bar, idx) | |
299 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" ) | |
300 | |
301 local f = self:GetFrame() | |
302 local icon = _G[name.."IconTexture"] | |
303 icon:SetTexture("Interface\\Buttons\\Button-Backpack-Up") | |
304 icon:Show() | |
305 f:SetCheckedTexture("Interface\\Buttons\\CheckButtonHilight") | |
306 f:RegisterEvent("PLAYER_ENTERING_WORLD"); | |
307 f:RegisterEvent("CVAR_UPDATE"); | |
308 f:SetScript("OnShow", function(frame, ...) self:OnShow(...) end) | |
309 | |
310 -- attach to skinner | |
311 bar:SkinButton(self, | |
312 { | |
313 Icon = _G[name.."IconTexture"] | |
314 } | |
315 ) | |
316 | |
317 return self | |
318 end | |
319 | |
320 function Backpack:Update() | |
321 self:UpdateFreeSlots() | |
322 self:UpdateChecked() | |
323 end | |
324 | |
325 function Backpack:UpdateFreeSlots() | |
326 if GetCVar("displayFreeBagSlots") == "1" then | |
327 local total = 0 | |
328 for i = BACKPACK_CONTAINER, NUM_BAG_SLOTS do | |
329 local free, family = GetContainerNumFreeSlots(i) | |
330 if family == 0 then | |
331 total = total + free | |
332 end | |
333 end | |
334 | |
335 self.freeSlots = total | |
336 self.frames.count:SetText(format("(%s)", self.freeSlots)) | |
337 self.frames.count:Show() | |
338 elseif self.frames.count:IsShown() then | |
339 self.frames.count:Hide() | |
340 end | |
341 end | |
342 | |
343 function Backpack:SetTooltip() | |
344 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_LEFT") | |
345 GameTooltip:SetText(BACKPACK_TOOLTIP, 1.0, 1.0, 1.0) | |
346 GameTooltip:AddLine(string.format(NUM_FREE_SLOTS, (self.freeSlots or 0))) | |
347 GameTooltip:Show(); | |
348 end | |
349 | |
350 function Backpack:OnShow() | |
351 self:UpdateFreeSlots() | |
352 end | |
353 | |
354 function Backpack:OnClick() | |
355 if IsModifiedClick("OPENALLBAGS") then | |
356 OpenAllBags() | |
357 else | |
358 if not PutItemInBackpack() then | |
359 ToggleBackpack() | |
360 end | |
361 end | |
362 self:UpdateChecked() | |
363 end | |
364 | |
365 function Backpack:OnReceiveDrag() | |
366 if CursorHasItem() then | |
367 PutItemInBackpack() | |
368 end | |
369 end | |
370 | |
371 function Backpack:PLAYER_ENTERING_WORLD() | |
372 self:CVAR_UPDATE("DISPLAY_FREE_BAG_SLOTS", GetCVar("displayFreeBagSlots")) | |
373 end | |
374 | |
375 function Backpack:CVAR_UPDATE( cvar, value ) | |
376 if cvar == "DISPLAY_FREE_BAG_SLOTS" then | |
377 if value == "1" then | |
378 self:GetFrame():RegisterEvent("BAG_UPDATE") | |
379 else | |
380 self:GetFrame():UnregisterEvent("BAG_UPDATE") | |
381 end | |
382 self:UpdateFreeSlots() | |
383 end | |
384 end | |
385 | |
386 function Backpack:BAG_UPDATE(bag) | |
387 if bag >= BACKPACK_CONTAINER and bag <= NUM_BAG_SLOTS then | |
388 self:UpdateFreeSlots() | |
389 end | |
390 end | |
391 | |
392 | |
393 -- | |
394 -- Keyring Button class | |
395 -- | |
396 function Keyring:New(name, cfg, bar, idx) | |
397 self = Super.New(self, name, cfg, bar, idx, "ItemButtonTemplate" ) | |
398 | |
399 local f = self:GetFrame() | |
400 | |
401 f:SetWidth(18) | |
402 f:SetHeight(39) | |
403 | |
404 local tex = f:GetNormalTexture() | |
405 tex:ClearAllPoints() | |
406 tex:SetAllPoints() | |
407 | |
408 f:SetNormalTexture("Interface\\Buttons\\UI-Button-KeyRing") | |
409 f:SetHighlightTexture("Interface\\Buttons\\UI-Button-KeyRing-Highlight") | |
410 f:SetPushedTexture("Interface\\Buttons\\UI-Button-KeyRing-Down") | |
411 f:GetNormalTexture():SetTexCoord(0,0.5625,0,0.609375) | |
412 f:GetHighlightTexture():SetTexCoord(0,0.5625,0,0.609375) | |
413 f:GetPushedTexture():SetTexCoord(0,0.5625,0,0.609375) | |
414 | |
415 if not HasKey() then | |
416 f:Hide() | |
417 end | |
418 | |
419 -- DO NOT attach to skinner | |
420 | |
421 return self | |
422 end | |
423 | |
424 function Keyring:GetBagID() | |
425 return KEYRING_CONTAINER | |
426 end | |
427 | |
428 function Keyring:Refresh() | |
429 local f = self:GetFrame() | |
430 self.bar:PlaceButton( self, f:GetHeight(), f:GetHeight() ) -- use height x height since it's an odd size | |
431 self:UpdateHotkey() | |
432 self:Update() | |
433 end | |
434 | |
435 function Keyring:SetTooltip() | |
436 GameTooltip:SetOwner(self:GetFrame(), "ANCHOR_RIGHT"); | |
437 GameTooltip:SetText(KEYRING, HIGHLIGHT_FONT_COLOR.r, HIGHLIGHT_FONT_COLOR.g, HIGHLIGHT_FONT_COLOR.b); | |
438 GameTooltip:AddLine(); | |
439 end | |
440 | |
441 function Keyring:OnReceiveDrag() | |
442 if CursorHasItem() then | |
443 PutKeyInKeyRing() | |
444 end | |
445 end | |
446 | |
447 function Keyring:OnClick() | |
448 if CursorHasItem() then | |
449 PutKeyInKeyRing() | |
450 else | |
451 ToggleKeyRing() | |
452 end | |
453 self:UpdateChecked() | |
454 end | |
455 | |
456 function Keyring:ShowGridTemp(show) | |
457 if not HasKey() then | |
458 if show then | |
459 self:GetFrame():Show() | |
460 else | |
461 self:GetFrame():Hide() | |
462 end | |
463 end | |
464 end | |
465 | |
466 | |
467 | |
468 -- hook some functions to propagate to our bag buttons | |
469 hooksecurefunc("Disable_BagButtons", | |
470 function() | |
471 for b in BagBase:IterateAllButtons() do | |
472 local f = b:GetFrame() | |
473 f:Disable() | |
474 SetDesaturation(b.frames.icon,1) | |
475 end | |
476 end) | |
477 | |
478 hooksecurefunc("Enable_BagButtons", | |
479 function() | |
480 for b in BagBase:IterateAllButtons() do | |
481 local f = b:GetFrame() | |
482 f:Enable() | |
483 SetDesaturation(b.frames.icon,nil) | |
484 end | |
485 end) | |
486 | |
487 hooksecurefunc("ContainerFrame_OnHide", | |
488 function() | |
489 for b in BagBase:IterateAllButtons() do | |
490 b:Update() | |
491 end | |
492 end) | |
493 | |
494 hooksecurefunc("ContainerFrame_OnShow", | |
495 function() | |
496 for b in BagBase:IterateAllButtons() do | |
497 b:Update() | |
498 end | |
499 end) |