annotate Modules/DisplayInvested.lua @ 35:aaa716c93fb2 v0.1.1

Added the ability to change the price of an item. You can click the Total Invested or the Invested Each to change the value.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sun, 18 Jul 2010 16:43:03 -0700
parents e6ddeb9f9994
children e27d13095b49
rev   line source
Asa@28 1 local addonName, addonTable = ...;
Asa@28 2 local ItemAuditor = _G[addonName]
Asa@28 3
Asa@28 4 local AceGUI = LibStub("AceGUI-3.0")
Asa@28 5 local ScrollingTable = LibStub("ScrollingTable")
Asa@28 6
Asa@35 7 local priceTypeEach = 1
Asa@35 8 local priceTypeTotal = 2
Asa@35 9
Asa@35 10 local promptFrame = false
Asa@35 11
Asa@35 12 -- Copied from QuickAuctions
Asa@35 13 local function validateMoney(value)
Asa@35 14 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@35 15 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@35 16 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@35 17
Asa@35 18 if( not gold and not silver and not copper ) then
Asa@35 19 return false;
Asa@35 20 -- return L["Invalid monney format entered, should be \"#g#s#c\", \"25g4s50c\" is 25 gold, 4 silver, 50 copper."]
Asa@35 21 end
Asa@35 22
Asa@35 23 return true
Asa@35 24 end
Asa@35 25
Asa@35 26 -- Copied from QuickAuctions
Asa@35 27 local function parseMoney(value)
Asa@35 28 local gold = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)g|r") or string.match(value, "([0-9]+)g"))
Asa@35 29 local silver = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)s|r") or string.match(value, "([0-9]+)s"))
Asa@35 30 local copper = tonumber(string.match(value, "([0-9]+)|c([0-9a-fA-F]+)c|r") or string.match(value, "([0-9]+)c"))
Asa@35 31
Asa@35 32 -- Convert it all into copper
Asa@35 33 return (copper or 0) + ((gold or 0) * COPPER_PER_GOLD) + ((silver or 0) * COPPER_PER_SILVER)
Asa@35 34
Asa@35 35 end
Asa@35 36
Asa@35 37 local function SaveNewValue(link, type, text)
Asa@35 38 if not validateMoney(text) then
Asa@35 39 error("Invalid value")
Asa@35 40 end
Asa@35 41 local investedTotal, investedPerItem, numOwned = ItemAuditor:GetItemCost(link)
Asa@35 42 local newValue=parseMoney(text)
Asa@35 43
Asa@35 44 if type == priceTypeEach then
Asa@35 45 newValue = newValue * numOwned
Asa@35 46 end
Asa@35 47
Asa@35 48 ItemAuditor:SaveValue(link, newValue-investedTotal, 0)
Asa@35 49 -- ItemAuditor:SaveValue(link, newValue, 0)
Asa@35 50
Asa@35 51 promptFrame:Hide()
Asa@35 52 end
Asa@35 53
Asa@35 54
Asa@35 55 local function PromptForNewPrice(link, type)
Asa@35 56 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(link)
Asa@35 57 local itemName, displayLink = GetItemInfo(link)
Asa@35 58 local priceDesc = " Invested Each:"
Asa@35 59 local price = investedPerItem
Asa@35 60
Asa@35 61 if type == priceTypeTotal then
Asa@35 62 priceDesc = " Invested Total:"
Asa@35 63 price = investedTotal
Asa@35 64 end
Asa@35 65
Asa@35 66 if not promptFrame then
Asa@35 67 promptFrame = AceGUI:Create("Frame")
Asa@35 68
Asa@35 69 local window = promptFrame.frame;
Asa@35 70 local width = 345
Asa@35 71 local height = 115
Asa@35 72 window:SetWidth(width )
Asa@35 73 window:SetHeight(height )
Asa@35 74 window:SetMinResize(width, height)
Asa@35 75 window:SetMaxResize(width, height)
Asa@35 76
Asa@35 77 promptFrame:SetTitle("ItemAuditor")
Asa@35 78 promptFrame:SetStatusText("Status Here")
Asa@35 79 promptFrame:SetCallback("OnClose", function(widget) AceGUI:Release(widget); promptFrame = false end)
Asa@35 80 promptFrame:SetLayout("Flow")
Asa@35 81
Asa@35 82 promptFrame.editbox = AceGUI:Create("EditBox")
Asa@35 83 promptFrame.editbox:SetWidth(300)
Asa@35 84 promptFrame:AddChild(promptFrame.editbox)
Asa@35 85 end
Asa@35 86 promptFrame.editbox:SetCallback("OnEnterPressed", function(widget, event, text) SaveNewValue(link, type, text) end)
Asa@35 87 promptFrame:SetStatusText("Current Price: "..ItemAuditor:FormatMoney(price))
Asa@35 88 promptFrame.editbox:SetLabel(displayLink..priceDesc)
Asa@35 89 promptFrame.editbox:SetText(ItemAuditor:FormatMoney(price, "", true))
Asa@35 90
Asa@35 91 promptFrame:Show()
Asa@35 92 editBox = promptFrame.editbox
Asa@35 93 end
Asa@35 94
Asa@35 95 local function displayMoney(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@35 96 if fShow == true then
Asa@35 97 local money = data[realrow][column]
Asa@35 98 if money == nil then
Asa@35 99 cellFrame.text:SetText("None")
Asa@35 100 else
Asa@35 101 cellFrame.text:SetText(ItemAuditor:FormatMoney(data[realrow][column]))
Asa@35 102 end
Asa@35 103 end
Asa@35 104 end
Asa@35 105
Asa@28 106 local investedCols = {
Asa@28 107 { name= "Item", width = 200,
Asa@28 108 ['DoCellUpdate'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@28 109 if fShow == true then
Asa@28 110 local _, link= strsplit("|", data[realrow][column], 2)
Asa@28 111 cellFrame.text:SetText(link)
Asa@28 112 end
Asa@28 113 end,
Asa@28 114 },
Asa@28 115 { name= "Invested Total", width = 100, align = "RIGHT",
Asa@35 116 ['DoCellUpdate'] = displayMoney,
Asa@28 117 },
Asa@35 118 { name= "Invested Each", width = 100, align = "RIGHT",
Asa@35 119 ['DoCellUpdate'] = displayMoney,
Asa@28 120 },
Asa@35 121 { name= "# Owned", width = 50, align = "RIGHT", defaultsort = "asc", },
Asa@28 122 }
Asa@28 123
Asa@28 124 local investedTable = false
Asa@28 125 local function ShowInvested(container)
Asa@28 126 if investedTable == false then
Asa@28 127 local window = container.frame
Asa@28 128 investedTable = ScrollingTable:CreateST(investedCols, 23, nil, nil, window)
Asa@28 129 investedTable.frame:SetPoint("BOTTOMLEFT",window, 10,10)
Asa@28 130 investedTable.frame:SetPoint("TOP", window, 0, -60)
Asa@28 131 investedTable.frame:SetPoint("RIGHT", window, -10,0)
Asa@28 132 investedTable:RegisterEvents({
Asa@28 133 ["OnEnter"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 134 if realrow then
Asa@28 135 local _, link= strsplit("|", data[realrow][1], 2)
Asa@28 136
Asa@28 137 GameTooltip:SetOwner(rowFrame, "ANCHOR_CURSOR")
Asa@28 138 GameTooltip:SetHyperlink(link)
Asa@28 139 GameTooltip:Show()
Asa@28 140 end
Asa@28 141 end,
Asa@28 142 ["OnLeave"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 143 GameTooltip:Hide()
Asa@28 144 end,
Asa@28 145 ["OnClick"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@35 146 if realrow ~= nil and (column == 2 or column == 3) then
Asa@35 147 -- column.text = row:CreateFontString(col:GetName().."text", "OVERLAY", "GameFontHighlightSmall");
Asa@35 148 local _, link= strsplit("|", data[realrow][1], 2)
Asa@35 149
Asa@35 150 local type=priceTypeEach
Asa@35 151 if column == 2 then
Asa@35 152 type = priceTypeTotal
Asa@35 153 end
Asa@35 154
Asa@35 155 PromptForNewPrice(link, type)
Asa@28 156 end
Asa@28 157 end,
Asa@28 158 });
Asa@28 159 end
Asa@28 160 investedTable:Show()
Asa@28 161
Asa@28 162 local width = 80
Asa@28 163 for i, data in pairs(investedCols) do
Asa@28 164 width = width + data.width
Asa@28 165 end
Asa@28 166 if container.parent then
Asa@28 167 container.parent:SetWidth(width);
Asa@28 168 end
Asa@28 169
Asa@28 170
Asa@28 171 UpdateInvestedData()
Asa@28 172 end
Asa@28 173
Asa@28 174
Asa@28 175 local function switchTab(container, event, group)
Asa@28 176 container:ReleaseChildren()
Asa@28 177
Asa@28 178 if investedTab then investedTab:Hide() end
Asa@28 179
Asa@28 180 if group == "tab_invested" then
Asa@28 181 ShowInvested(container)
Asa@28 182 end
Asa@28 183 end
Asa@28 184
Asa@28 185
Asa@28 186
Asa@28 187 displayFrame = false
Asa@28 188 local function CreateFrames()
Asa@28 189 if not displayFrame then
Asa@28 190 -- Create the frame container
Asa@28 191 displayFrame = AceGUI:Create("Frame")
Asa@28 192 local window = displayFrame.frame;
Asa@28 193 displayFrame:SetTitle("ItemAuditor")
Asa@28 194 displayFrame:SetStatusText("")
Asa@28 195
Asa@28 196 displayFrame:SetLayout("Fill")
Asa@28 197
Asa@28 198 window:SetHeight(500);
Asa@28 199
Asa@28 200 local width = 80
Asa@28 201 for i, data in pairs(investedCols) do
Asa@28 202 width = width + data.width
Asa@28 203 end
Asa@28 204 window:SetWidth(width);
Asa@28 205
Asa@28 206 local tab = AceGUI:Create("TabGroup")
Asa@28 207 tab:SetLayout("Flow")
Asa@28 208 tab:SetTabs({{text="Invested", value="tab_invested"}})
Asa@28 209 tab:SetCallback("OnGroupSelected", switchTab)
Asa@28 210 tab:SelectTab("tab_invested")
Asa@28 211
Asa@28 212 displayFrame:AddChild(tab)
Asa@28 213 end
Asa@28 214 displayFrame:Show()
Asa@28 215 end
Asa@28 216
Asa@28 217
Asa@28 218
Asa@28 219
Asa@28 220
Asa@28 221 function UpdateInvestedData()
Asa@28 222 if investedTable then
Asa@28 223 tableData = {} --reset
Asa@28 224 local totalInvested = 0
Asa@28 225
Asa@28 226 local i = 1
Asa@28 227 local data
Asa@35 228 local items = ItemAuditor.db.factionrealm.items
Asa@35 229 local includedItems = {}
Asa@35 230 for safeLink in pairs(items) do
Asa@35 231 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(safeLink)
Asa@35 232 local itemName, link = GetItemInfo(safeLink)
Asa@35 233 if investedTotal > 0 and link ~= nil then
Asa@28 234 tableData[i] = {
Asa@28 235 itemName.."|"..link,
Asa@28 236 investedTotal,
Asa@28 237 investedPerItem,
Asa@28 238 count,
Asa@28 239 }
Asa@28 240
Asa@28 241 totalInvested = totalInvested + investedTotal
Asa@28 242
Asa@28 243 i = i + 1
Asa@35 244 includedItems[safeLink] = true
Asa@35 245 end
Asa@35 246 end
Asa@35 247
Asa@35 248 local inventory = ItemAuditor:GetCurrentInventory()
Asa@35 249
Asa@35 250 for link, count in pairs(inventory.items) do
Asa@35 251 if includedItems[link] == nil then
Asa@35 252 local count = Altoholic:GetItemCount(ItemAuditor:GetIDFromLink(link))
Asa@35 253 local itemName, link = GetItemInfo(link)
Asa@35 254 tableData[i] = {
Asa@35 255 itemName.."|"..link,
Asa@35 256 0,
Asa@35 257 0,
Asa@35 258 count,
Asa@35 259 }
Asa@35 260
Asa@35 261 -- totalInvested = totalInvested + investedTotal
Asa@35 262
Asa@35 263 i = i + 1
Asa@28 264 end
Asa@28 265 end
Asa@28 266
Asa@28 267 if investedTable.frame:IsShown() then
Asa@28 268 displayFrame:SetStatusText("Total Invested: "..ItemAuditor:FormatMoney(totalInvested))
Asa@28 269 end
Asa@28 270
Asa@28 271 investedTable:SetData(tableData, true)
Asa@28 272 end
Asa@28 273 end
Asa@28 274
Asa@28 275 function ItemAuditor:CreateFrames()
Asa@28 276 CreateFrames()
Asa@28 277 end
Asa@28 278