annotate Modules/DisplayInvested.lua @ 39:003de902ae64

Implemented COD mail. This loses the ability to track postage, but that will be restored next.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Mon, 19 Jul 2010 20:16:40 -0700
parents e27d13095b49
children 60ab9a4d2de1
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@38 68 ItemAuditor:RegisterFrame(promptFrame)
Asa@35 69
Asa@35 70 local window = promptFrame.frame;
Asa@35 71 local width = 345
Asa@35 72 local height = 115
Asa@35 73 window:SetWidth(width )
Asa@35 74 window:SetHeight(height )
Asa@35 75 window:SetMinResize(width, height)
Asa@35 76 window:SetMaxResize(width, height)
Asa@35 77
Asa@35 78 promptFrame:SetTitle("ItemAuditor")
Asa@35 79 promptFrame:SetStatusText("Status Here")
Asa@35 80 promptFrame:SetCallback("OnClose", function(widget) AceGUI:Release(widget); promptFrame = false end)
Asa@35 81 promptFrame:SetLayout("Flow")
Asa@35 82
Asa@35 83 promptFrame.editbox = AceGUI:Create("EditBox")
Asa@35 84 promptFrame.editbox:SetWidth(300)
Asa@35 85 promptFrame:AddChild(promptFrame.editbox)
Asa@35 86 end
Asa@35 87 promptFrame.editbox:SetCallback("OnEnterPressed", function(widget, event, text) SaveNewValue(link, type, text) end)
Asa@35 88 promptFrame:SetStatusText("Current Price: "..ItemAuditor:FormatMoney(price))
Asa@35 89 promptFrame.editbox:SetLabel(displayLink..priceDesc)
Asa@35 90 promptFrame.editbox:SetText(ItemAuditor:FormatMoney(price, "", true))
Asa@35 91
Asa@35 92 promptFrame:Show()
Asa@35 93 editBox = promptFrame.editbox
Asa@35 94 end
Asa@35 95
Asa@35 96 local function displayMoney(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@35 97 if fShow == true then
Asa@35 98 local money = data[realrow][column]
Asa@35 99 if money == nil then
Asa@35 100 cellFrame.text:SetText("None")
Asa@35 101 else
Asa@35 102 cellFrame.text:SetText(ItemAuditor:FormatMoney(data[realrow][column]))
Asa@35 103 end
Asa@35 104 end
Asa@35 105 end
Asa@35 106
Asa@28 107 local investedCols = {
Asa@28 108 { name= "Item", width = 200,
Asa@28 109 ['DoCellUpdate'] = function(rowFrame, cellFrame, data, cols, row, realrow, column, fShow, table, ...)
Asa@28 110 if fShow == true then
Asa@28 111 local _, link= strsplit("|", data[realrow][column], 2)
Asa@28 112 cellFrame.text:SetText(link)
Asa@28 113 end
Asa@28 114 end,
Asa@28 115 },
Asa@28 116 { name= "Invested Total", width = 100, align = "RIGHT",
Asa@35 117 ['DoCellUpdate'] = displayMoney,
Asa@28 118 },
Asa@35 119 { name= "Invested Each", width = 100, align = "RIGHT",
Asa@35 120 ['DoCellUpdate'] = displayMoney,
Asa@28 121 },
Asa@35 122 { name= "# Owned", width = 50, align = "RIGHT", defaultsort = "asc", },
Asa@28 123 }
Asa@28 124
Asa@28 125 local investedTable = false
Asa@28 126 local function ShowInvested(container)
Asa@28 127 if investedTable == false then
Asa@28 128 local window = container.frame
Asa@28 129 investedTable = ScrollingTable:CreateST(investedCols, 23, nil, nil, window)
Asa@28 130 investedTable.frame:SetPoint("BOTTOMLEFT",window, 10,10)
Asa@28 131 investedTable.frame:SetPoint("TOP", window, 0, -60)
Asa@28 132 investedTable.frame:SetPoint("RIGHT", window, -10,0)
Asa@28 133 investedTable:RegisterEvents({
Asa@28 134 ["OnEnter"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 135 if realrow then
Asa@28 136 local _, link= strsplit("|", data[realrow][1], 2)
Asa@28 137
Asa@28 138 GameTooltip:SetOwner(rowFrame, "ANCHOR_CURSOR")
Asa@28 139 GameTooltip:SetHyperlink(link)
Asa@28 140 GameTooltip:Show()
Asa@28 141 end
Asa@28 142 end,
Asa@28 143 ["OnLeave"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@28 144 GameTooltip:Hide()
Asa@28 145 end,
Asa@28 146 ["OnClick"] = function (rowFrame, cellFrame, data, cols, row, realrow, column, scrollingTable, ...)
Asa@35 147 if realrow ~= nil and (column == 2 or column == 3) then
Asa@35 148 -- column.text = row:CreateFontString(col:GetName().."text", "OVERLAY", "GameFontHighlightSmall");
Asa@35 149 local _, link= strsplit("|", data[realrow][1], 2)
Asa@35 150
Asa@35 151 local type=priceTypeEach
Asa@35 152 if column == 2 then
Asa@35 153 type = priceTypeTotal
Asa@35 154 end
Asa@35 155
Asa@35 156 PromptForNewPrice(link, type)
Asa@28 157 end
Asa@28 158 end,
Asa@28 159 });
Asa@28 160 end
Asa@28 161 investedTable:Show()
Asa@28 162
Asa@28 163 local width = 80
Asa@28 164 for i, data in pairs(investedCols) do
Asa@28 165 width = width + data.width
Asa@28 166 end
Asa@28 167 if container.parent then
Asa@28 168 container.parent:SetWidth(width);
Asa@28 169 end
Asa@28 170
Asa@28 171
Asa@28 172 UpdateInvestedData()
Asa@28 173 end
Asa@28 174
Asa@28 175
Asa@28 176 local function switchTab(container, event, group)
Asa@28 177 container:ReleaseChildren()
Asa@28 178
Asa@28 179 if investedTab then investedTab:Hide() end
Asa@28 180
Asa@28 181 if group == "tab_invested" then
Asa@28 182 ShowInvested(container)
Asa@28 183 end
Asa@28 184 end
Asa@28 185
Asa@28 186
Asa@28 187
Asa@28 188 displayFrame = false
Asa@28 189 local function CreateFrames()
Asa@28 190 if not displayFrame then
Asa@28 191 -- Create the frame container
Asa@28 192 displayFrame = AceGUI:Create("Frame")
Asa@38 193 ItemAuditor:RegisterFrame(displayFrame)
Asa@28 194 local window = displayFrame.frame;
Asa@28 195 displayFrame:SetTitle("ItemAuditor")
Asa@28 196 displayFrame:SetStatusText("")
Asa@28 197
Asa@28 198 displayFrame:SetLayout("Fill")
Asa@28 199
Asa@28 200 window:SetHeight(500);
Asa@28 201
Asa@28 202 local width = 80
Asa@28 203 for i, data in pairs(investedCols) do
Asa@28 204 width = width + data.width
Asa@28 205 end
Asa@28 206 window:SetWidth(width);
Asa@28 207
Asa@28 208 local tab = AceGUI:Create("TabGroup")
Asa@28 209 tab:SetLayout("Flow")
Asa@28 210 tab:SetTabs({{text="Invested", value="tab_invested"}})
Asa@28 211 tab:SetCallback("OnGroupSelected", switchTab)
Asa@28 212 tab:SelectTab("tab_invested")
Asa@28 213
Asa@28 214 displayFrame:AddChild(tab)
Asa@28 215 end
Asa@28 216 displayFrame:Show()
Asa@28 217 end
Asa@28 218
Asa@28 219
Asa@28 220
Asa@28 221
Asa@28 222
Asa@28 223 function UpdateInvestedData()
Asa@28 224 if investedTable then
Asa@28 225 tableData = {} --reset
Asa@28 226 local totalInvested = 0
Asa@28 227
Asa@28 228 local i = 1
Asa@28 229 local data
Asa@35 230 local items = ItemAuditor.db.factionrealm.items
Asa@35 231 local includedItems = {}
Asa@35 232 for safeLink in pairs(items) do
Asa@35 233 local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(safeLink)
Asa@35 234 local itemName, link = GetItemInfo(safeLink)
Asa@35 235 if investedTotal > 0 and link ~= nil then
Asa@28 236 tableData[i] = {
Asa@28 237 itemName.."|"..link,
Asa@28 238 investedTotal,
Asa@28 239 investedPerItem,
Asa@28 240 count,
Asa@28 241 }
Asa@28 242
Asa@28 243 totalInvested = totalInvested + investedTotal
Asa@28 244
Asa@28 245 i = i + 1
Asa@35 246 includedItems[safeLink] = true
Asa@35 247 end
Asa@35 248 end
Asa@35 249
Asa@35 250 local inventory = ItemAuditor:GetCurrentInventory()
Asa@35 251
Asa@35 252 for link, count in pairs(inventory.items) do
Asa@35 253 if includedItems[link] == nil then
Asa@35 254 local count = Altoholic:GetItemCount(ItemAuditor:GetIDFromLink(link))
Asa@35 255 local itemName, link = GetItemInfo(link)
Asa@35 256 tableData[i] = {
Asa@35 257 itemName.."|"..link,
Asa@35 258 0,
Asa@35 259 0,
Asa@35 260 count,
Asa@35 261 }
Asa@35 262
Asa@35 263 -- totalInvested = totalInvested + investedTotal
Asa@35 264
Asa@35 265 i = i + 1
Asa@28 266 end
Asa@28 267 end
Asa@28 268
Asa@28 269 if investedTable.frame:IsShown() then
Asa@28 270 displayFrame:SetStatusText("Total Invested: "..ItemAuditor:FormatMoney(totalInvested))
Asa@28 271 end
Asa@28 272
Asa@28 273 investedTable:SetData(tableData, true)
Asa@28 274 end
Asa@28 275 end
Asa@28 276
Asa@28 277 function ItemAuditor:CreateFrames()
Asa@28 278 CreateFrames()
Asa@28 279 end
Asa@28 280