annotate Widgets.lua @ 73:6216b754350d

Default size for the summary window increase to 700 pixels (from 650). Cached item name and rarity no longer have a default and will be nil if the value is unknown. Cached item name, rarity and link can now be updated when the itemlink element is build. Item element now gets the item table passed rather than just the item id. If the item link is already known, this prevent an additional GetItemInfo call.
author Zerotorescue
date Fri, 24 Dec 2010 16:10:20 +0100
parents fee06221176f
children 8d11fc88ecab
rev   line source
Zerotorescue@62 1 local addon = select(2, ...);
Zerotorescue@62 2 local Widgets = addon:NewModule("Widgets");
Zerotorescue@62 3
Zerotorescue@62 4 local AceGUI = LibStub("AceGUI-3.0");
Zerotorescue@62 5
Zerotorescue@62 6 function Widgets:ItemLinkButton()
Zerotorescue@62 7 --[[
Zerotorescue@62 8 [ ItemLinkButton ]
Zerotorescue@62 9 This custom widget has to show an icon with the item link next to it.
Zerotorescue@62 10 Upon hover it must show the item tooltip.
Zerotorescue@62 11 Upon click it must execute the function provided through user data.
Zerotorescue@62 12
Zerotorescue@62 13 UserData: itemId, onClickEvent
Zerotorescue@62 14
Zerotorescue@62 15 OnEnter: tooltip show
Zerotorescue@62 16 OnLeave: tooltip hide
Zerotorescue@62 17 OnClick: UserData.onClickEvent
Zerotorescue@62 18 ]]
Zerotorescue@62 19
Zerotorescue@62 20 local widgetType = "ItemLinkButton";
Zerotorescue@62 21 local widgetVersion = 1;
Zerotorescue@62 22
Zerotorescue@62 23 local function Constructor()
Zerotorescue@62 24 local widget = AceGUI:Create("InteractiveLabel");
Zerotorescue@62 25 widget.type = widgetType;
Zerotorescue@62 26
Zerotorescue@62 27 -- We overwrite the OnAcquire as we want to set our callbacks even
Zerotorescue@62 28 -- when the widget is re-used from the widget pool
Zerotorescue@62 29 widget.originalOnAcquire = widget.OnAcquire;
Zerotorescue@62 30 widget.OnAcquire = function(self, ...)
Zerotorescue@62 31
Zerotorescue@62 32
Zerotorescue@62 33 -- We overwrite the setcallback because we don't want anything else
Zerotorescue@62 34 -- to overwrite our OnEnter, OnLeave and OnClick events
Zerotorescue@62 35 -- which would be done by the AceConfigDialog after a widget gets re-used
Zerotorescue@62 36 if not self.originalSetCallBack then
Zerotorescue@62 37 self.originalSetCallBack = self.SetCallback;
Zerotorescue@62 38 self.SetCallback = function(this, event, func, ...)
Zerotorescue@62 39 if event == "OnEnter" or event == "OnLeave" or event == "OnClick" then
Zerotorescue@62 40 -- Don't allow overwriting of these events
Zerotorescue@62 41 return;
Zerotorescue@62 42 elseif event == "CustomOnEnter" then
Zerotorescue@62 43 return this.originalSetCallBack(this, "OnEnter", func, ...);
Zerotorescue@62 44 elseif event == "CustomOnLeave" then
Zerotorescue@62 45 return this.originalSetCallBack(this, "OnLeave", func, ...);
Zerotorescue@62 46 elseif event == "CustomOnClick" then
Zerotorescue@62 47 return this.originalSetCallBack(this, "OnClick", func, ...);
Zerotorescue@62 48 else
Zerotorescue@62 49 return this.originalSetCallBack(this, event, func, ...);
Zerotorescue@62 50 end
Zerotorescue@62 51 end;
Zerotorescue@62 52 end
Zerotorescue@62 53
Zerotorescue@62 54
Zerotorescue@62 55
Zerotorescue@62 56 -- Set our own events, since we disabled the normal event-names, we'll call them our custom versions
Zerotorescue@62 57 self:SetCallback("CustomOnEnter", function(this)
Zerotorescue@62 58 local itemId = this:GetUserData("itemId");
Zerotorescue@62 59
Zerotorescue@62 60 if itemId then
Zerotorescue@62 61 GameTooltip:SetOwner(this.frame, "ANCHOR_TOPRIGHT");
Zerotorescue@62 62 GameTooltip:SetHyperlink(("item:%d"):format(itemId));
Zerotorescue@62 63 GameTooltip:Show();
Zerotorescue@62 64 end
Zerotorescue@62 65 end);
Zerotorescue@62 66 self:SetCallback("CustomOnLeave", function(this)
Zerotorescue@62 67 GameTooltip:Hide();
Zerotorescue@62 68 end);
Zerotorescue@62 69 self:SetCallback("CustomOnClick", function(this, ...)
Zerotorescue@62 70 -- Below is used in child widgets to prepare for onclick
Zerotorescue@62 71 if this.OnClick then
Zerotorescue@62 72 this.OnClick(this, ...);
Zerotorescue@62 73 end
Zerotorescue@62 74
Zerotorescue@62 75 local func = this:GetUserData("exec");
Zerotorescue@62 76 local itemId = this:GetUserData("itemId");
Zerotorescue@62 77
Zerotorescue@62 78 if func then
Zerotorescue@62 79 -- If this is a config option we will need the group id
Zerotorescue@62 80 local path = this:GetUserData("path");
Zerotorescue@62 81 local groupId = (path and path[2]) or nil;
Zerotorescue@62 82
Zerotorescue@62 83 func(groupId, itemId, ...);
Zerotorescue@62 84 end
Zerotorescue@62 85 end);
Zerotorescue@62 86
Zerotorescue@62 87
Zerotorescue@62 88
Zerotorescue@62 89 -- Then also do whatever it wanted to do
Zerotorescue@62 90 self.originalOnAcquire(self, ...);
Zerotorescue@62 91 end;
Zerotorescue@62 92
Zerotorescue@62 93 -- Remember the original SetText as this might get overwritten by the config-widget
Zerotorescue@62 94 widget.originalSetText = widget.SetText;
Zerotorescue@62 95
Zerotorescue@62 96 widget.SetItemId = function(self, itemId)
Zerotorescue@62 97 self:SetUserData("itemId", itemId);
Zerotorescue@62 98
Zerotorescue@62 99 -- Put the icon in front of it
Zerotorescue@62 100 self:SetImage(GetItemIcon(itemId));
Zerotorescue@62 101 -- Standardize the size
Zerotorescue@62 102 self:SetImageSize(16, 16);
Zerotorescue@62 103
Zerotorescue@62 104 -- Make readable font
Zerotorescue@62 105 self:SetFontObject(GameFontHighlight);
Zerotorescue@62 106
Zerotorescue@62 107 -- We don't want to set the itemId as text, but rather the item link, so get that.
Zerotorescue@62 108 local itemLink = select(2, GetItemInfo(itemId)) or ("Unknown (#%d)"):format(itemId);
Zerotorescue@62 109
Zerotorescue@62 110 self:originalSetText(itemLink);
Zerotorescue@62 111 end;
Zerotorescue@73 112 widget.SetItem = function(self, item)
Zerotorescue@73 113 self:SetUserData("itemId", item.id);
Zerotorescue@73 114
Zerotorescue@73 115 -- Put the icon in front of it
Zerotorescue@73 116 self:SetImage(GetItemIcon(item.id));
Zerotorescue@73 117 -- Standardize the size
Zerotorescue@73 118 self:SetImageSize(16, 16);
Zerotorescue@73 119
Zerotorescue@73 120 -- Make readable font
Zerotorescue@73 121 self:SetFontObject(GameFontHighlight);
Zerotorescue@73 122
Zerotorescue@73 123 if not item.link then
Zerotorescue@73 124 -- If no item link was cached, give it another try
Zerotorescue@73 125
Zerotorescue@73 126 local itemName, itemLink, itemRarity = GetItemInfo(item.id);
Zerotorescue@73 127
Zerotorescue@73 128 -- item is a table, so passed by reference, so updatable
Zerotorescue@73 129 item.name = itemName;
Zerotorescue@73 130 item.link = itemLink;
Zerotorescue@73 131 item.rarity = itemRarity;
Zerotorescue@73 132 end
Zerotorescue@73 133
Zerotorescue@73 134 -- We don't want to set the itemId as text, but rather the item link, so get that.
Zerotorescue@73 135 local itemLink = item.link or ("Unknown (#%d)"):format(itemId);
Zerotorescue@73 136
Zerotorescue@73 137 self:originalSetText(itemLink);
Zerotorescue@73 138 end;
Zerotorescue@62 139
Zerotorescue@62 140 return widget;
Zerotorescue@62 141 end
Zerotorescue@62 142
Zerotorescue@62 143 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 144 end
Zerotorescue@62 145
Zerotorescue@62 146 function Widgets:ConfigItemLinkButton()
Zerotorescue@62 147 -- Define out custom item link button widget
Zerotorescue@62 148 -- 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
Zerotorescue@62 149
Zerotorescue@62 150 local widgetType = "ConfigItemLinkButton";
Zerotorescue@62 151 local widgetVersion = 1;
Zerotorescue@62 152
Zerotorescue@62 153 -- Empty function for disabling functions
Zerotorescue@62 154 local function Dummy() end
Zerotorescue@62 155
Zerotorescue@62 156 -- Makes an instance of our ItemLinkButton widget
Zerotorescue@62 157 local function GetItemLinkButton()
Zerotorescue@62 158 local widget = AceGUI:Create("ItemLinkButton");
Zerotorescue@62 159 widget.type = widgetType;
Zerotorescue@62 160
Zerotorescue@62 161 -- We can only provide custom widgets for input, select and multiselect fields
Zerotorescue@62 162 -- Input being the simplest, we use that - however, it provides two parameters: label and text. We only need one, disable the other.
Zerotorescue@62 163 widget.SetLabel = Dummy;
Zerotorescue@62 164
Zerotorescue@62 165 -- SetText is called when this button is being created and contains the itemId
Zerotorescue@62 166 -- Forward that itemId to the ItemLinkButton
Zerotorescue@62 167 widget.SetText = function(self, value, ...)
Zerotorescue@62 168 if value and tonumber(value) then
Zerotorescue@62 169 self:SetItemId(tonumber(value));
Zerotorescue@62 170 end
Zerotorescue@62 171 end;
Zerotorescue@62 172
Zerotorescue@62 173 widget.OnClick = function(self, ...)
Zerotorescue@62 174 local option = self:GetUserData("option");
Zerotorescue@62 175
Zerotorescue@62 176 if option and option.set then
Zerotorescue@62 177 self:SetUserData("exec", option.set);
Zerotorescue@62 178 end
Zerotorescue@62 179 end;
Zerotorescue@62 180
Zerotorescue@62 181 return widget;
Zerotorescue@62 182 end
Zerotorescue@62 183
Zerotorescue@62 184 AceGUI:RegisterWidgetType(widgetType, GetItemLinkButton, widgetVersion);
Zerotorescue@62 185 end
Zerotorescue@62 186
Zerotorescue@62 187 function Widgets:InlineGroupWithButton()
Zerotorescue@62 188 -- Register InlineGroupWithButton-widget
Zerotorescue@62 189 -- This widget adds a button next to the header of an inline group
Zerotorescue@62 190 -- SetPoint doesn't seem usable within AceGUI.
Zerotorescue@62 191
Zerotorescue@62 192 local widgetType = "InlineGroupWithButton";
Zerotorescue@62 193 local widgetVersion = 1;
Zerotorescue@62 194
Zerotorescue@62 195 local function Constructor()
Zerotorescue@62 196 local widget = AceGUI:Create("InlineGroup");
Zerotorescue@62 197 widget.type = widgetType;
Zerotorescue@62 198
Zerotorescue@62 199 widget.MakeButton = function(self, buttonSettings)
Zerotorescue@62 200 if type(buttonSettings) == "table" then
Zerotorescue@62 201 if not self.btnQueue then
Zerotorescue@62 202 -- Because widgets are re-used, we don't want to recreate this button
Zerotorescue@62 203 self.btnQueue = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
Zerotorescue@62 204 self.btnQueue:SetHeight(22);
Zerotorescue@62 205 self.btnQueue:SetWidth(120);
Zerotorescue@62 206 end
Zerotorescue@62 207 self.btnQueue:SetText(buttonSettings.name);
Zerotorescue@62 208 self.btnQueue:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
Zerotorescue@62 209
Zerotorescue@62 210 -- Triggers
Zerotorescue@62 211 self.btnQueue:SetScript("OnClick", buttonSettings.exec);
Zerotorescue@62 212
Zerotorescue@62 213 -- Tooltip
Zerotorescue@62 214 self.btnQueue.tooltipTitle = buttonSettings.name;
Zerotorescue@62 215 self.btnQueue.tooltip = buttonSettings.desc or "";
Zerotorescue@62 216 self.btnQueue:SetScript("OnEnter", ShowTooltip);
Zerotorescue@62 217 self.btnQueue:SetScript("OnLeave", HideTooltip);
Zerotorescue@62 218 else
Zerotorescue@62 219 error("settings must be a table - usage: MakeButton(table);");
Zerotorescue@62 220 end
Zerotorescue@62 221 end;
Zerotorescue@62 222
Zerotorescue@62 223 return widget;
Zerotorescue@62 224 end
Zerotorescue@62 225
Zerotorescue@62 226 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 227 end