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