annotate Modules/Widgets.lua @ 136:396c2960d54d

Queueing = queuing. :p
author Zerotorescue
date Tue, 18 Jan 2011 00:39:40 +0100
parents 3bec0ea44607
children
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@74 58 local itemData = this:GetUserData("ItemData");
Zerotorescue@62 59
Zerotorescue@74 60 if itemData then
Zerotorescue@62 61 GameTooltip:SetOwner(this.frame, "ANCHOR_TOPRIGHT");
Zerotorescue@74 62 GameTooltip:SetHyperlink(("item:%d"):format(itemData.id));
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@74 76 local itemData = this:GetUserData("ItemData");
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@74 83 func(groupId, itemData, ...);
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@74 96 widget.SetItem = function(self, item)
Zerotorescue@74 97 self:SetUserData("ItemData", item);
Zerotorescue@62 98
Zerotorescue@62 99 -- Put the icon in front of it
Zerotorescue@74 100 self:SetImage(item.icon);
Zerotorescue@73 101 -- Standardize the size
Zerotorescue@73 102 self:SetImageSize(16, 16);
Zerotorescue@73 103
Zerotorescue@73 104 -- Make readable font
Zerotorescue@73 105 self:SetFontObject(GameFontHighlight);
Zerotorescue@73 106
Zerotorescue@73 107 if not item.link then
Zerotorescue@73 108 -- If no item link was cached, give it another try
Zerotorescue@73 109
Zerotorescue@73 110 local itemName, itemLink, itemRarity = GetItemInfo(item.id);
Zerotorescue@73 111
Zerotorescue@73 112 -- item is a table, so passed by reference, so updatable
Zerotorescue@73 113 item.name = itemName;
Zerotorescue@73 114 item.link = itemLink;
Zerotorescue@73 115 item.rarity = itemRarity;
Zerotorescue@73 116 end
Zerotorescue@73 117
Zerotorescue@73 118 -- We don't want to set the itemId as text, but rather the item link, so get that.
Zerotorescue@74 119 local itemLink = item.link or ("Unknown (#%d)"):format(item.id);
Zerotorescue@73 120
Zerotorescue@73 121 self:originalSetText(itemLink);
Zerotorescue@73 122 end;
Zerotorescue@62 123
Zerotorescue@62 124 return widget;
Zerotorescue@62 125 end
Zerotorescue@62 126
Zerotorescue@62 127 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 128 end
Zerotorescue@62 129
Zerotorescue@62 130 function Widgets:ConfigItemLinkButton()
Zerotorescue@62 131 -- Define out custom item link button widget
Zerotorescue@62 132 -- 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 133
Zerotorescue@62 134 local widgetType = "ConfigItemLinkButton";
Zerotorescue@62 135 local widgetVersion = 1;
Zerotorescue@62 136
Zerotorescue@62 137 -- Empty function for disabling functions
Zerotorescue@62 138 local function Dummy() end
Zerotorescue@62 139
Zerotorescue@62 140 -- Makes an instance of our ItemLinkButton widget
Zerotorescue@62 141 local function GetItemLinkButton()
Zerotorescue@62 142 local widget = AceGUI:Create("ItemLinkButton");
Zerotorescue@62 143 widget.type = widgetType;
Zerotorescue@62 144
Zerotorescue@62 145 -- We can only provide custom widgets for input, select and multiselect fields
Zerotorescue@62 146 -- Input being the simplest, we use that - however, it provides two parameters: label and text. We only need one, disable the other.
Zerotorescue@62 147 widget.SetLabel = Dummy;
Zerotorescue@62 148
Zerotorescue@62 149 -- SetText is called when this button is being created and contains the itemId
Zerotorescue@62 150 -- Forward that itemId to the ItemLinkButton
Zerotorescue@62 151 widget.SetText = function(self, value, ...)
Zerotorescue@62 152 if value and tonumber(value) then
Zerotorescue@74 153 local newItemData = addon.ItemData:New(tonumber(value));
Zerotorescue@74 154
Zerotorescue@74 155 self:SetItem(newItemData);
Zerotorescue@62 156 end
Zerotorescue@62 157 end;
Zerotorescue@62 158
Zerotorescue@62 159 widget.OnClick = function(self, ...)
Zerotorescue@62 160 local option = self:GetUserData("option");
Zerotorescue@62 161
Zerotorescue@62 162 if option and option.set then
Zerotorescue@62 163 self:SetUserData("exec", option.set);
Zerotorescue@62 164 end
Zerotorescue@62 165 end;
Zerotorescue@62 166
Zerotorescue@62 167 return widget;
Zerotorescue@62 168 end
Zerotorescue@62 169
Zerotorescue@62 170 AceGUI:RegisterWidgetType(widgetType, GetItemLinkButton, widgetVersion);
Zerotorescue@62 171 end
Zerotorescue@62 172
Zerotorescue@62 173 function Widgets:InlineGroupWithButton()
Zerotorescue@62 174 -- Register InlineGroupWithButton-widget
Zerotorescue@62 175 -- This widget adds a button next to the header of an inline group
Zerotorescue@62 176 -- SetPoint doesn't seem usable within AceGUI.
Zerotorescue@62 177
Zerotorescue@62 178 local widgetType = "InlineGroupWithButton";
Zerotorescue@62 179 local widgetVersion = 1;
Zerotorescue@62 180
Zerotorescue@62 181 local function Constructor()
Zerotorescue@62 182 local widget = AceGUI:Create("InlineGroup");
Zerotorescue@62 183 widget.type = widgetType;
Zerotorescue@62 184
Zerotorescue@62 185 widget.MakeButton = function(self, buttonSettings)
Zerotorescue@62 186 if type(buttonSettings) == "table" then
Zerotorescue@62 187 if not self.btnQueue then
Zerotorescue@62 188 -- Because widgets are re-used, we don't want to recreate this button
Zerotorescue@62 189 self.btnQueue = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
Zerotorescue@62 190 self.btnQueue:SetHeight(22);
Zerotorescue@62 191 self.btnQueue:SetWidth(120);
Zerotorescue@62 192 end
Zerotorescue@62 193 self.btnQueue:SetText(buttonSettings.name);
Zerotorescue@62 194 self.btnQueue:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
Zerotorescue@62 195
Zerotorescue@62 196 -- Triggers
Zerotorescue@62 197 self.btnQueue:SetScript("OnClick", buttonSettings.exec);
Zerotorescue@62 198
Zerotorescue@62 199 -- Tooltip
Zerotorescue@62 200 self.btnQueue.tooltipTitle = buttonSettings.name;
Zerotorescue@62 201 self.btnQueue.tooltip = buttonSettings.desc or "";
Zerotorescue@62 202 self.btnQueue:SetScript("OnEnter", ShowTooltip);
Zerotorescue@62 203 self.btnQueue:SetScript("OnLeave", HideTooltip);
Zerotorescue@62 204 else
Zerotorescue@62 205 error("settings must be a table - usage: MakeButton(table);");
Zerotorescue@62 206 end
Zerotorescue@62 207 end;
Zerotorescue@62 208
Zerotorescue@62 209 return widget;
Zerotorescue@62 210 end
Zerotorescue@62 211
Zerotorescue@62 212 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 213 end