comparison Widgets.lua @ 62:fee06221176f

Seperated the config from Core.lua. Many other code cleaning up for readability. Added local references for much used globals. Moved widgets to a different file for readability. Re-added global function for slash command handling since we do need it for our chat-hyperlinks. Fixed queueing to properly use the track at property of virtual groups. Fixed queueing to display the item id instead of the item link if the item link could not be loaded. Speed slider at the summary now has an interval of 1% down from 5% and rounds rather than ceils it?s value so 101% will become 100% rather than 105%. Now using the right stock properties at the summary. Added a help group to the config.
author Zerotorescue
date Wed, 22 Dec 2010 19:56:55 +0100
parents
children 6216b754350d
comparison
equal deleted inserted replaced
61:d903b0a151d3 62:fee06221176f
1 local addon = select(2, ...);
2 local Widgets = addon:NewModule("Widgets");
3
4 local AceGUI = LibStub("AceGUI-3.0");
5
6 function Widgets:ItemLinkButton()
7 --[[
8 [ ItemLinkButton ]
9 This custom widget has to show an icon with the item link next to it.
10 Upon hover it must show the item tooltip.
11 Upon click it must execute the function provided through user data.
12
13 UserData: itemId, onClickEvent
14
15 OnEnter: tooltip show
16 OnLeave: tooltip hide
17 OnClick: UserData.onClickEvent
18 ]]
19
20 local widgetType = "ItemLinkButton";
21 local widgetVersion = 1;
22
23 local function Constructor()
24 local widget = AceGUI:Create("InteractiveLabel");
25 widget.type = widgetType;
26
27 -- We overwrite the OnAcquire as we want to set our callbacks even
28 -- when the widget is re-used from the widget pool
29 widget.originalOnAcquire = widget.OnAcquire;
30 widget.OnAcquire = function(self, ...)
31
32
33 -- We overwrite the setcallback because we don't want anything else
34 -- to overwrite our OnEnter, OnLeave and OnClick events
35 -- which would be done by the AceConfigDialog after a widget gets re-used
36 if not self.originalSetCallBack then
37 self.originalSetCallBack = self.SetCallback;
38 self.SetCallback = function(this, event, func, ...)
39 if event == "OnEnter" or event == "OnLeave" or event == "OnClick" then
40 -- Don't allow overwriting of these events
41 return;
42 elseif event == "CustomOnEnter" then
43 return this.originalSetCallBack(this, "OnEnter", func, ...);
44 elseif event == "CustomOnLeave" then
45 return this.originalSetCallBack(this, "OnLeave", func, ...);
46 elseif event == "CustomOnClick" then
47 return this.originalSetCallBack(this, "OnClick", func, ...);
48 else
49 return this.originalSetCallBack(this, event, func, ...);
50 end
51 end;
52 end
53
54
55
56 -- Set our own events, since we disabled the normal event-names, we'll call them our custom versions
57 self:SetCallback("CustomOnEnter", function(this)
58 local itemId = this:GetUserData("itemId");
59
60 if itemId then
61 GameTooltip:SetOwner(this.frame, "ANCHOR_TOPRIGHT");
62 GameTooltip:SetHyperlink(("item:%d"):format(itemId));
63 GameTooltip:Show();
64 end
65 end);
66 self:SetCallback("CustomOnLeave", function(this)
67 GameTooltip:Hide();
68 end);
69 self:SetCallback("CustomOnClick", function(this, ...)
70 -- Below is used in child widgets to prepare for onclick
71 if this.OnClick then
72 this.OnClick(this, ...);
73 end
74
75 local func = this:GetUserData("exec");
76 local itemId = this:GetUserData("itemId");
77
78 if func then
79 -- If this is a config option we will need the group id
80 local path = this:GetUserData("path");
81 local groupId = (path and path[2]) or nil;
82
83 func(groupId, itemId, ...);
84 end
85 end);
86
87
88
89 -- Then also do whatever it wanted to do
90 self.originalOnAcquire(self, ...);
91 end;
92
93 -- Remember the original SetText as this might get overwritten by the config-widget
94 widget.originalSetText = widget.SetText;
95
96 widget.SetItemId = function(self, itemId)
97 self:SetUserData("itemId", itemId);
98
99 -- Put the icon in front of it
100 self:SetImage(GetItemIcon(itemId));
101 -- Standardize the size
102 self:SetImageSize(16, 16);
103
104 -- Make readable font
105 self:SetFontObject(GameFontHighlight);
106
107 -- We don't want to set the itemId as text, but rather the item link, so get that.
108 local itemLink = select(2, GetItemInfo(itemId)) or ("Unknown (#%d)"):format(itemId);
109
110 self:originalSetText(itemLink);
111 end;
112
113 return widget;
114 end
115
116 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
117 end
118
119 function Widgets:ConfigItemLinkButton()
120 -- Define out custom item link button widget
121 -- This will be called as if it's an input element, we overwrite some of the related functions which are called for default input fields
122
123 local widgetType = "ConfigItemLinkButton";
124 local widgetVersion = 1;
125
126 -- Empty function for disabling functions
127 local function Dummy() end
128
129 -- Makes an instance of our ItemLinkButton widget
130 local function GetItemLinkButton()
131 local widget = AceGUI:Create("ItemLinkButton");
132 widget.type = widgetType;
133
134 -- We can only provide custom widgets for input, select and multiselect fields
135 -- Input being the simplest, we use that - however, it provides two parameters: label and text. We only need one, disable the other.
136 widget.SetLabel = Dummy;
137
138 -- SetText is called when this button is being created and contains the itemId
139 -- Forward that itemId to the ItemLinkButton
140 widget.SetText = function(self, value, ...)
141 if value and tonumber(value) then
142 self:SetItemId(tonumber(value));
143 end
144 end;
145
146 widget.OnClick = function(self, ...)
147 local option = self:GetUserData("option");
148
149 if option and option.set then
150 self:SetUserData("exec", option.set);
151 end
152 end;
153
154 return widget;
155 end
156
157 AceGUI:RegisterWidgetType(widgetType, GetItemLinkButton, widgetVersion);
158 end
159
160 function Widgets:InlineGroupWithButton()
161 -- Register InlineGroupWithButton-widget
162 -- This widget adds a button next to the header of an inline group
163 -- SetPoint doesn't seem usable within AceGUI.
164
165 local widgetType = "InlineGroupWithButton";
166 local widgetVersion = 1;
167
168 local function Constructor()
169 local widget = AceGUI:Create("InlineGroup");
170 widget.type = widgetType;
171
172 widget.MakeButton = function(self, buttonSettings)
173 if type(buttonSettings) == "table" then
174 if not self.btnQueue then
175 -- Because widgets are re-used, we don't want to recreate this button
176 self.btnQueue = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
177 self.btnQueue:SetHeight(22);
178 self.btnQueue:SetWidth(120);
179 end
180 self.btnQueue:SetText(buttonSettings.name);
181 self.btnQueue:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
182
183 -- Triggers
184 self.btnQueue:SetScript("OnClick", buttonSettings.exec);
185
186 -- Tooltip
187 self.btnQueue.tooltipTitle = buttonSettings.name;
188 self.btnQueue.tooltip = buttonSettings.desc or "";
189 self.btnQueue:SetScript("OnEnter", ShowTooltip);
190 self.btnQueue:SetScript("OnLeave", HideTooltip);
191 else
192 error("settings must be a table - usage: MakeButton(table);");
193 end
194 end;
195
196 return widget;
197 end
198
199 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
200 end