annotate Frames.lua @ 107:aa675033abdc v0.2.7-BETA

Changelog & to-do list updated.
author Zerotorescue
date Wed, 12 Jan 2011 20:09:00 +0100
parents d3fbb5676a5e
children 67bd5057ecb7
rev   line source
Zerotorescue@101 1 local addon = select(2, ...);
Zerotorescue@101 2
Zerotorescue@101 3 local function ShowTooltip(self)
Zerotorescue@101 4 -- If this function is called from a widget, self is the widget and self.frame the actual frame
Zerotorescue@101 5 local this = self.frame or self;
Zerotorescue@101 6
Zerotorescue@106 7 if not this.tooltipTitle then return; end
Zerotorescue@106 8
Zerotorescue@101 9 GameTooltip:SetOwner(this, "ANCHOR_NONE");
Zerotorescue@104 10 if this.tooltipLocation and this.tooltipLocation == "BOTTOM" then
Zerotorescue@104 11 GameTooltip:SetPoint("TOP", this, "BOTTOM");
Zerotorescue@101 12 else
Zerotorescue@101 13 GameTooltip:SetPoint("BOTTOM", this, "TOP");
Zerotorescue@101 14 end
Zerotorescue@101 15 GameTooltip:SetText(this.tooltipTitle, 1, .82, 0, 1);
Zerotorescue@101 16
Zerotorescue@101 17 if type(this.tooltip) == "string" then
Zerotorescue@101 18 GameTooltip:AddLine(this.tooltip, 1, 1, 1, 1);
Zerotorescue@101 19 end
Zerotorescue@101 20
Zerotorescue@101 21 GameTooltip:Show();
Zerotorescue@101 22 end
Zerotorescue@101 23
Zerotorescue@101 24 local function HideTooltip()
Zerotorescue@101 25 GameTooltip:Hide();
Zerotorescue@101 26 end
Zerotorescue@101 27
Zerotorescue@101 28 function addon:CreateMoverFrame(onAccept, onCancel)
Zerotorescue@101 29 local frameWidth = 400;
Zerotorescue@101 30
Zerotorescue@101 31 -- Main window
Zerotorescue@101 32 local frame = CreateFrame("Frame", "InventoriumItemMover", UIParent);
Zerotorescue@101 33 -- Hide by default
Zerotorescue@101 34 frame:Hide();
Zerotorescue@101 35 -- Center the frame (will be adjusted later)
Zerotorescue@101 36 frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0);
Zerotorescue@101 37 -- Put in front of other windows
Zerotorescue@101 38 frame:SetFrameStrata("FULLSCREEN_DIALOG");
Zerotorescue@101 39 frame:SetToplevel(true);
Zerotorescue@101 40 -- Give it a size
Zerotorescue@101 41 frame:SetWidth(frameWidth);
Zerotorescue@101 42 frame:SetHeight(175);
Zerotorescue@101 43 -- Background
Zerotorescue@101 44 frame:SetBackdrop({
Zerotorescue@101 45 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
Zerotorescue@101 46 edgeFile = "Interface\\DialogFrame\\UI-DialogBox-Border",
Zerotorescue@104 47 edgeSize = 20,
Zerotorescue@101 48 insets = {
Zerotorescue@101 49 left = 5,
Zerotorescue@101 50 right = 5,
Zerotorescue@101 51 top = 5,
Zerotorescue@101 52 bottom = 5,
Zerotorescue@101 53 },
Zerotorescue@101 54 });
Zerotorescue@101 55 frame:SetBackdropColor(0, 0, 0, .8);
Zerotorescue@101 56 -- Mouse functions
Zerotorescue@101 57 frame:EnableMouse();
Zerotorescue@101 58 frame:SetMovable(true);
Zerotorescue@101 59 frame:SetResizable(true);
Zerotorescue@101 60 frame:SetMinResize(300, 175);
Zerotorescue@101 61 -- Set event handlers
Zerotorescue@101 62 frame:SetScript("OnMouseUp", function(this) this:StopMovingOrSizing(); end);
Zerotorescue@101 63 frame:SetScript("OnShow", function(this)
Zerotorescue@104 64 if not tContains(StaticPopup_DisplayedFrames, this) then
Zerotorescue@104 65 -- We wish to display our box at a similar position as the default interface's static popups, so we literally copied the code for these popups
Zerotorescue@104 66 local lastFrame = StaticPopup_DisplayedFrames[#StaticPopup_DisplayedFrames];
Zerotorescue@104 67 if lastFrame then
Zerotorescue@104 68 this:SetPoint("TOP", lastFrame, "BOTTOM", 0, 0);
Zerotorescue@104 69 else
Zerotorescue@104 70 this:SetPoint("TOP", UIParent, "TOP", 0, -135);
Zerotorescue@104 71 end
Zerotorescue@104 72
Zerotorescue@104 73 -- Position other static popups below this
Zerotorescue@104 74 tinsert(StaticPopup_DisplayedFrames, this);
Zerotorescue@101 75 end
Zerotorescue@101 76
Zerotorescue@101 77 this:AdjustScrollTableRows();
Zerotorescue@101 78
Zerotorescue@101 79 PlaySound("OrcExploration");
Zerotorescue@101 80 end);
Zerotorescue@101 81
Zerotorescue@101 82 -- Title (AceGUI frame-widget-title used as example)
Zerotorescue@101 83 local titleBackground = frame:CreateTexture(nil, "OVERLAY");
Zerotorescue@101 84 titleBackground:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header");
Zerotorescue@101 85 titleBackground:SetTexCoord(0.31, 0.67, 0, 0.63);
Zerotorescue@101 86 titleBackground:SetPoint("TOP", 0, 12);
Zerotorescue@101 87 titleBackground:SetWidth(150);
Zerotorescue@101 88 titleBackground:SetHeight(40);
Zerotorescue@101 89
Zerotorescue@101 90 local titleBackgroundLeft = frame:CreateTexture(nil, "OVERLAY");
Zerotorescue@101 91 titleBackgroundLeft:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header");
Zerotorescue@101 92 titleBackgroundLeft:SetTexCoord(0.21, 0.31, 0, 0.63);
Zerotorescue@101 93 titleBackgroundLeft:SetPoint("RIGHT", titleBackground, "LEFT");
Zerotorescue@101 94 titleBackgroundLeft:SetWidth(30);
Zerotorescue@101 95 titleBackgroundLeft:SetHeight(40);
Zerotorescue@101 96
Zerotorescue@101 97 local titleBackgroundRight = frame:CreateTexture(nil, "OVERLAY");
Zerotorescue@101 98 titleBackgroundRight:SetTexture("Interface\\DialogFrame\\UI-DialogBox-Header");
Zerotorescue@101 99 titleBackgroundRight:SetTexCoord(0.67, 0.77, 0, 0.63);
Zerotorescue@101 100 titleBackgroundRight:SetPoint("LEFT", titleBackground, "RIGHT");
Zerotorescue@101 101 titleBackgroundRight:SetWidth(30);
Zerotorescue@101 102 titleBackgroundRight:SetHeight(40);
Zerotorescue@101 103
Zerotorescue@101 104 local frmTitle = CreateFrame("Frame", nil, frame);
Zerotorescue@101 105 frmTitle:EnableMouse(true);
Zerotorescue@101 106 frmTitle:SetScript("OnMouseDown", function(this) this:GetParent():StartMoving(); end);
Zerotorescue@101 107 frmTitle:SetScript("OnMouseUp", function(this) this:GetParent():StopMovingOrSizing(); end);
Zerotorescue@101 108 frmTitle:SetAllPoints(titleBackground);
Zerotorescue@101 109
Zerotorescue@101 110 local lblTitle = frmTitle:CreateFontString(nil, "OVERLAY", "GameFontNormal");
Zerotorescue@101 111 lblTitle:SetPoint("TOP", titleBackground, "TOP", 0, -14);
Zerotorescue@101 112 lblTitle:SetText("Inventorium Bank Refill");
Zerotorescue@101 113
Zerotorescue@101 114 -- Resizer (vertical only)
Zerotorescue@101 115 local frmResizer = CreateFrame("Frame", nil, frame);
Zerotorescue@101 116 frmResizer:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 0, -10);
Zerotorescue@101 117 frmResizer:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 0, -10);
Zerotorescue@101 118 frmResizer:SetHeight(20);
Zerotorescue@101 119 frmResizer:EnableMouse();
Zerotorescue@101 120 frmResizer:SetScript("OnMouseDown", function(this) this:GetParent():StartSizing("BOTTOM"); end);
Zerotorescue@101 121 frmResizer:SetScript("OnMouseUp", function(this) this:GetParent():StopMovingOrSizing(); end);
Zerotorescue@101 122
Zerotorescue@101 123 -- Description
Zerotorescue@101 124 local lblDescription = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
Zerotorescue@106 125 lblDescription:SetPoint("TOPLEFT", frame, "TOPLEFT", 15, -27);
Zerotorescue@104 126 lblDescription:SetWidth(frameWidth - 15 - 15); -- 10 margin left & 10 margin right
Zerotorescue@101 127 lblDescription:SetJustifyH("LEFT");
Zerotorescue@101 128 lblDescription:SetJustifyV("TOP");
Zerotorescue@101 129 lblDescription:SetText("The items listed below can be refilled from this location, do you wish to move them to your bags?");
Zerotorescue@101 130
Zerotorescue@101 131 frame.lblDescription = lblDescription;
Zerotorescue@101 132
Zerotorescue@101 133 -- Buttons
Zerotorescue@101 134 -- Move (proceed)
Zerotorescue@101 135 local btnMove = CreateFrame("Button", "$parentProceed", frame, "UIPanelButtonTemplate");
Zerotorescue@101 136 btnMove:SetHeight(21);
Zerotorescue@101 137 btnMove:SetWidth(125);
Zerotorescue@104 138 btnMove:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", 15, 11);
Zerotorescue@101 139 btnMove:SetText("Move Items");
Zerotorescue@101 140 btnMove:SetScript("OnClick", onAccept);
Zerotorescue@101 141 btnMove:SetScript("OnEnter", ShowTooltip);
Zerotorescue@101 142 btnMove:SetScript("OnLeave", HideTooltip);
Zerotorescue@106 143 btnMove.tooltipTitle = (not addon.db.profile.defaults.hideHelp and "Move Items");
Zerotorescue@106 144 btnMove.tooltip = (not addon.db.profile.defaults.hideHelp and "Start moving these items from the bank.");
Zerotorescue@101 145
Zerotorescue@101 146 frame.btnMove = btnMove;
Zerotorescue@101 147
Zerotorescue@101 148 -- Cancel
Zerotorescue@101 149 local btnCancel = CreateFrame("Button", "$parentCancel", frame, "UIPanelButtonTemplate");
Zerotorescue@101 150 btnCancel:SetHeight(21);
Zerotorescue@101 151 btnCancel:SetWidth(125);
Zerotorescue@104 152 btnCancel:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -15, 11);
Zerotorescue@101 153 btnCancel:SetText("Cancel");
Zerotorescue@101 154 btnCancel:SetScript("OnClick", onCancel);
Zerotorescue@101 155 btnCancel:SetScript("OnEnter", ShowTooltip);
Zerotorescue@101 156 btnCancel:SetScript("OnLeave", HideTooltip);
Zerotorescue@106 157 btnCancel.tooltipTitle = (not addon.db.profile.defaults.hideHelp and "Cancel");
Zerotorescue@106 158 btnCancel.tooltip = (not addon.db.profile.defaults.hideHelp and "Do not move anything and close the window.");
Zerotorescue@101 159
Zerotorescue@101 160 frame.btnCancel = btnCancel;
Zerotorescue@101 161
Zerotorescue@101 162 -- Because the scrolling table code-behind will change this element's height, we can't rely on that. Make a dummy frame which we can measure
Zerotorescue@101 163 local frmMeasureDummy = CreateFrame("Frame", nil, frame);
Zerotorescue@106 164 frmMeasureDummy:SetPoint("TOP", frame.lblDescription, "BOTTOM", 0, -18);
Zerotorescue@106 165 frmMeasureDummy:SetPoint("LEFT", frame, "LEFT", 15, 0);
Zerotorescue@104 166 frmMeasureDummy:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -15, 35);
Zerotorescue@101 167
Zerotorescue@101 168 frame.frmMeasureDummy = frmMeasureDummy;
Zerotorescue@101 169
Zerotorescue@101 170 -- Scrolling table with a list of items to be moved
Zerotorescue@106 171 local scrollTableWidth = ( frame.frmMeasureDummy:GetWidth() - 30 ); -- adjust width by the scrollbar size
Zerotorescue@101 172 local headers = {
Zerotorescue@101 173 {
Zerotorescue@101 174 ["name"] = "Item",
Zerotorescue@106 175 ["width"] = (scrollTableWidth * .60),
Zerotorescue@101 176 ["defaultsort"] = "asc",
Zerotorescue@101 177 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@106 178 local aName, _, aRarity = GetItemInfo(this:GetRow(aRow).rowData.id);
Zerotorescue@106 179 local bName, _, bRarity = GetItemInfo(this:GetRow(bRow).rowData.id);
Zerotorescue@101 180 local template = "%d%s";
Zerotorescue@101 181 aName = template:format((10 - (aRarity or 10)), (aName or ""):lower());
Zerotorescue@101 182 bName = template:format((10 - (bRarity or 10)), (bName or ""):lower());
Zerotorescue@101 183
Zerotorescue@101 184 if this.cols[column].sort == "dsc" then
Zerotorescue@101 185 return aName > bName;
Zerotorescue@101 186 else
Zerotorescue@101 187 return aName < bName;
Zerotorescue@101 188 end
Zerotorescue@101 189 end,
Zerotorescue@101 190 ["sort"] = "asc", -- when the data is set, use this column so sort the default data
Zerotorescue@106 191 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Item"),
Zerotorescue@106 192 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by item quality then item name."),
Zerotorescue@101 193 },
Zerotorescue@101 194 {
Zerotorescue@101 195 ["name"] = "Moving",
Zerotorescue@101 196 ["width"] = (scrollTableWidth * .15),
Zerotorescue@106 197 ["align"] = "RIGHT",
Zerotorescue@101 198 ["defaultsort"] = "dsc",
Zerotorescue@106 199 ["sortnext"] = 1,
Zerotorescue@106 200 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Moving"),
Zerotorescue@106 201 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the amount of movable items."),
Zerotorescue@101 202 },
Zerotorescue@101 203 {
Zerotorescue@101 204 ["name"] = "Available",
Zerotorescue@106 205 ["width"] = (scrollTableWidth * .25),
Zerotorescue@106 206 ["align"] = "RIGHT",
Zerotorescue@101 207 ["defaultsort"] = "dsc",
Zerotorescue@106 208 ["sortnext"] = 1,
Zerotorescue@106 209 ["comparesort"] = function(this, aRow, bRow, column)
Zerotorescue@106 210 local aAvailablePercent = (this:GetRow(aRow).rowData.available / this:GetRow(aRow).rowData.missing);
Zerotorescue@106 211 local bAvailablePercent = (this:GetRow(bRow).rowData.available / this:GetRow(bRow).rowData.missing);
Zerotorescue@106 212
Zerotorescue@106 213 if this.cols[column].sort == "dsc" then
Zerotorescue@106 214 return aAvailablePercent > bAvailablePercent;
Zerotorescue@106 215 else
Zerotorescue@106 216 return aAvailablePercent < bAvailablePercent;
Zerotorescue@106 217 end
Zerotorescue@106 218 end,
Zerotorescue@106 219 ["tooltipTitle"] = (not addon.db.profile.defaults.hideHelp and "Item"),
Zerotorescue@106 220 ["tooltip"] = (not addon.db.profile.defaults.hideHelp and "Click to sort the list by the availibility percentage."),
Zerotorescue@101 221 },
Zerotorescue@101 222 };
Zerotorescue@101 223
Zerotorescue@101 224 local ScrollingTable = LibStub("ScrollingTable");
Zerotorescue@101 225 local table = ScrollingTable:CreateST(headers, 3, 15, nil, frame);
Zerotorescue@106 226 -- When moving over a row, provide a tooltip for the item
Zerotorescue@101 227 table:RegisterEvents({
Zerotorescue@101 228 ["OnEnter"] = function(rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Zerotorescue@106 229 if row and realrow then
Zerotorescue@106 230 -- Data row
Zerotorescue@106 231
Zerotorescue@106 232 if data[realrow] and data[realrow].rowData and data[realrow].rowData.id then
Zerotorescue@106 233 GameTooltip:SetOwner(rowFrame, "ANCHOR_NONE");
Zerotorescue@106 234 GameTooltip:SetPoint("TOPLEFT", rowFrame, "BOTTOMLEFT");
Zerotorescue@106 235 GameTooltip:SetHyperlink(("item:%d"):format(data[realrow].rowData.id));
Zerotorescue@106 236 GameTooltip:Show();
Zerotorescue@106 237 end
Zerotorescue@106 238 else
Zerotorescue@106 239 -- Header row
Zerotorescue@106 240
Zerotorescue@106 241 if cols[column].tooltipTitle and type(cols[column].tooltipTitle) == "string" then
Zerotorescue@106 242 cellFrame.tooltipTitle = cols[column].tooltipTitle;
Zerotorescue@106 243 if cols[column].tooltip then
Zerotorescue@106 244 cellFrame.tooltip = cols[column].tooltip; -- Optional
Zerotorescue@106 245 else
Zerotorescue@106 246 cellFrame.tooltip = nil;
Zerotorescue@106 247 end
Zerotorescue@106 248
Zerotorescue@106 249 ShowTooltip(cellFrame);
Zerotorescue@106 250 end
Zerotorescue@101 251 end
Zerotorescue@101 252 end,
Zerotorescue@101 253 ["OnLeave"] = function(rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Zerotorescue@106 254 HideTooltip();
Zerotorescue@101 255 end,
Zerotorescue@101 256 });
Zerotorescue@101 257
Zerotorescue@101 258 frame.scrollTable = table;
Zerotorescue@106 259 table.frame:SetPoint("TOP", frame.lblDescription, "BOTTOM", 0, -18);
Zerotorescue@104 260 table.frame:SetPoint("LEFT", frame, "LEFT", 15, 0);
Zerotorescue@104 261 table.frame:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -15, 35);
Zerotorescue@101 262
Zerotorescue@106 263 -- Change the amount of displayed rows based on the size of the frame
Zerotorescue@106 264 frame.AdjustScrollTableRows = function(this)
Zerotorescue@106 265 local newRows = math.floor(( this.frmMeasureDummy:GetHeight() - 5 ) / 15);
Zerotorescue@106 266 newRows = (newRows < 3 and 3) or newRows;
Zerotorescue@106 267
Zerotorescue@106 268 this.scrollTable:SetDisplayRows(newRows, 15);
Zerotorescue@106 269 end;
Zerotorescue@106 270 frame:SetScript("OnSizeChanged", frame.AdjustScrollTableRows);
Zerotorescue@101 271 end
Zerotorescue@101 272
Zerotorescue@101 273 function addon:SetMoverFrameData(data)
Zerotorescue@101 274 InventoriumItemMover.scrollTable:SetData(data);
Zerotorescue@101 275
Zerotorescue@101 276 InventoriumItemMover:Show();
Zerotorescue@101 277 end