annotate 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
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@62 112
Zerotorescue@62 113 return widget;
Zerotorescue@62 114 end
Zerotorescue@62 115
Zerotorescue@62 116 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 117 end
Zerotorescue@62 118
Zerotorescue@62 119 function Widgets:ConfigItemLinkButton()
Zerotorescue@62 120 -- Define out custom item link button widget
Zerotorescue@62 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
Zerotorescue@62 122
Zerotorescue@62 123 local widgetType = "ConfigItemLinkButton";
Zerotorescue@62 124 local widgetVersion = 1;
Zerotorescue@62 125
Zerotorescue@62 126 -- Empty function for disabling functions
Zerotorescue@62 127 local function Dummy() end
Zerotorescue@62 128
Zerotorescue@62 129 -- Makes an instance of our ItemLinkButton widget
Zerotorescue@62 130 local function GetItemLinkButton()
Zerotorescue@62 131 local widget = AceGUI:Create("ItemLinkButton");
Zerotorescue@62 132 widget.type = widgetType;
Zerotorescue@62 133
Zerotorescue@62 134 -- We can only provide custom widgets for input, select and multiselect fields
Zerotorescue@62 135 -- Input being the simplest, we use that - however, it provides two parameters: label and text. We only need one, disable the other.
Zerotorescue@62 136 widget.SetLabel = Dummy;
Zerotorescue@62 137
Zerotorescue@62 138 -- SetText is called when this button is being created and contains the itemId
Zerotorescue@62 139 -- Forward that itemId to the ItemLinkButton
Zerotorescue@62 140 widget.SetText = function(self, value, ...)
Zerotorescue@62 141 if value and tonumber(value) then
Zerotorescue@62 142 self:SetItemId(tonumber(value));
Zerotorescue@62 143 end
Zerotorescue@62 144 end;
Zerotorescue@62 145
Zerotorescue@62 146 widget.OnClick = function(self, ...)
Zerotorescue@62 147 local option = self:GetUserData("option");
Zerotorescue@62 148
Zerotorescue@62 149 if option and option.set then
Zerotorescue@62 150 self:SetUserData("exec", option.set);
Zerotorescue@62 151 end
Zerotorescue@62 152 end;
Zerotorescue@62 153
Zerotorescue@62 154 return widget;
Zerotorescue@62 155 end
Zerotorescue@62 156
Zerotorescue@62 157 AceGUI:RegisterWidgetType(widgetType, GetItemLinkButton, widgetVersion);
Zerotorescue@62 158 end
Zerotorescue@62 159
Zerotorescue@62 160 function Widgets:InlineGroupWithButton()
Zerotorescue@62 161 -- Register InlineGroupWithButton-widget
Zerotorescue@62 162 -- This widget adds a button next to the header of an inline group
Zerotorescue@62 163 -- SetPoint doesn't seem usable within AceGUI.
Zerotorescue@62 164
Zerotorescue@62 165 local widgetType = "InlineGroupWithButton";
Zerotorescue@62 166 local widgetVersion = 1;
Zerotorescue@62 167
Zerotorescue@62 168 local function Constructor()
Zerotorescue@62 169 local widget = AceGUI:Create("InlineGroup");
Zerotorescue@62 170 widget.type = widgetType;
Zerotorescue@62 171
Zerotorescue@62 172 widget.MakeButton = function(self, buttonSettings)
Zerotorescue@62 173 if type(buttonSettings) == "table" then
Zerotorescue@62 174 if not self.btnQueue then
Zerotorescue@62 175 -- Because widgets are re-used, we don't want to recreate this button
Zerotorescue@62 176 self.btnQueue = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
Zerotorescue@62 177 self.btnQueue:SetHeight(22);
Zerotorescue@62 178 self.btnQueue:SetWidth(120);
Zerotorescue@62 179 end
Zerotorescue@62 180 self.btnQueue:SetText(buttonSettings.name);
Zerotorescue@62 181 self.btnQueue:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
Zerotorescue@62 182
Zerotorescue@62 183 -- Triggers
Zerotorescue@62 184 self.btnQueue:SetScript("OnClick", buttonSettings.exec);
Zerotorescue@62 185
Zerotorescue@62 186 -- Tooltip
Zerotorescue@62 187 self.btnQueue.tooltipTitle = buttonSettings.name;
Zerotorescue@62 188 self.btnQueue.tooltip = buttonSettings.desc or "";
Zerotorescue@62 189 self.btnQueue:SetScript("OnEnter", ShowTooltip);
Zerotorescue@62 190 self.btnQueue:SetScript("OnLeave", HideTooltip);
Zerotorescue@62 191 else
Zerotorescue@62 192 error("settings must be a table - usage: MakeButton(table);");
Zerotorescue@62 193 end
Zerotorescue@62 194 end;
Zerotorescue@62 195
Zerotorescue@62 196 return widget;
Zerotorescue@62 197 end
Zerotorescue@62 198
Zerotorescue@62 199 AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
Zerotorescue@62 200 end