view Summary.lua @ 0:c6ff7ba0e8f6

Reasonably functional now. Cleaning up some stuff which might have to be reverted.
author Zerotorescue
date Thu, 07 Oct 2010 17:17:43 +0200
parents
children 9fff13c08f99
line wrap: on
line source
local addon = LibStub("AceAddon-3.0"):GetAddon("Inventory");
local mod = addon:NewModule("Summary", "AceEvent-3.0");

local AceGUI = LibStub("AceGUI-3.0");

function mod:OnEnable()
	self:RegisterWidgets();
	
	self:BuildMain();
	self:Build();
end

function mod:RegisterWidgets()
	-- Register InlineGroupWithButton-widget
	-- This widget adds a button next to the header of an inline group
	-- SetPoint doesn't seem usable within AceGUI.
	
	local widgetType = "InlineGroupWithButton";
	local widgetVersion = 1;

	local function Constructor() 
	    local widget = AceGUI:Create("InlineGroup");
	    widget.type = widgetType;
	    
	    widget.MakeButton = function(self, buttonSettings)
			if type(buttonSettings) == "table" then
				local button = CreateFrame("Button", nil, self.frame, "UIPanelButtonTemplate");
				button:SetText(buttonSettings.name);
				button:SetHeight(22);
				button:SetWidth(120);
				button:SetPoint("TOPRIGHT", self.frame, "TOPRIGHT", -10, 5);
				button:SetScript("OnClick", buttonSettings.exec);
				button.tooltipTitle = buttonSettings.name;
				button.tooltip = buttonSettings.desc or "";
				button:SetScript("OnEnter", function(self)
					GameTooltip:SetOwner(self, "ANCHOR_NONE")
					GameTooltip:SetPoint("BOTTOM", self, "TOP")
					GameTooltip:SetText(self.tooltipTitle, 1, .82, 0, 1)
					
					if type(self.tooltip) == "string" then
						GameTooltip:AddLine(self.tooltip, 1, 1, 1, 1);
					end
					
					GameTooltip:Show();
				end);
				button:SetScript("OnLeave", function(self)
					GameTooltip:Hide();
				end);
			else
				error("settings must be a table - usage: MakeButton(table);");
			end
	    end
		
	    return widget;
	end
    
	AceGUI:RegisterWidgetType(widgetType, Constructor, widgetVersion);
end

function mod:BuildMain()
	local frame = AceGUI:Create("Frame");
	frame:SetTitle("Inventory Summary");
	frame:SetLayout("Fill");
	
	local scrollFrame = AceGUI:Create("ScrollFrame");
	scrollFrame:SetLayout("Flow");
	
	frame:AddChild(scrollFrame);
	
	mod.scrollFrame = scrollFrame;
end

local temp = {};

local sortMethod = "item";
local sortDirectory = "ASC";
local function ReSort(subject)
	if sortMethod == subject then
		sortDirectory = (sortDirectory == "ASC" and "DESC") or "ASC";
	else
		sortDirectory = "ASC";
	end
	sortMethod = subject;
	
	mod:Build();
end

function mod:Build()
	mod.scrollFrame:ReleaseChildren();
	
	for groupName, values in pairs(addon.db.global.groups) do
		if values.items then
			-- Get group settings
			local stockRequired = values.minimumStock or addon.db.global.defaults.minimumStock;
			local showWhenBelow = (values.summaryThresholdShow or addon.db.global.defaults.summaryThresholdShow);
			
			-- Make group container
			local iGroup = AceGUI:Create("InlineGroupWithButton");
			iGroup:SetTitle(groupName);
			iGroup:SetFullWidth(true);
			iGroup:SetLayout("Flow");
			iGroup:MakeButton({
				name = "Queue",
				desc = "Queue all items in this group.",
				exec = function()
					print(groupName);
				end,
			});
			
			-- Headers
			-- Itemlink
			local lblItem = AceGUI:Create("InteractiveLabel");
			lblItem:SetText("|cfffed000Item|r");
			lblItem:SetFontObject(GameFontHighlight);
			lblItem:SetRelativeWidth(0.7);
			lblItem:SetCallback("OnClick", function() ReSort("item"); end);
			
			iGroup:AddChild(lblItem);
			
			-- Current quantity
			local lblQuantity = AceGUI:Create("InteractiveLabel");
			lblQuantity:SetText("|cfffed000Cur.|r");
			lblQuantity:SetFontObject(GameFontHighlight);
			lblQuantity:SetRelativeWidth(0.149);
			lblQuantity:SetCallback("OnClick", function() ReSort("current"); end);
			
			iGroup:AddChild(lblQuantity);
			
			-- Required stock
			local lblStockRequired = AceGUI:Create("InteractiveLabel");
			lblStockRequired:SetText("|cfffed000Req.|r");
			lblStockRequired:SetFontObject(GameFontHighlight);
			lblStockRequired:SetRelativeWidth(0.149);
			lblStockRequired:SetCallback("OnClick", function() ReSort("percentage"); end);
			
			iGroup:AddChild(lblStockRequired);
			
			-- Sort item list
			for itemId in pairs(values.items) do
				local itemName, itemLink, itemRarity = GetItemInfo(itemId);
				
				table.insert(temp, {
					id = itemId,
					name = itemName,
					link = itemLink,
					rarity = itemRarity,
					count = addon:GetItemCount(itemId),
				});
			end
			
			local sortNameFormat = "%d%s"; --rarity .. name
			table.sort(temp, function(a, b)
				if sortMethod == "item" then
					if sortDirectory == "ASC" then
						return sortNameFormat:format((7 - a.rarity), a.name):upper() < sortNameFormat:format((7 - b.rarity), b.name):upper();
					else
						return sortNameFormat:format((7 - a.rarity), a.name):upper() > sortNameFormat:format((7 - b.rarity), b.name):upper();
					end
				elseif sortMethod == "current" then
					if sortDirectory == "ASC" then
						return a.count < b.count;
					else
						return a.count > b.count;
					end
				elseif sortMethod == "percentage" then
					if sortDirectory == "ASC" then
						return ( a.count / stockRequired ) < ( b.count / stockRequired );
					else
						return ( a.count / stockRequired ) > ( b.count / stockRequired );
					end
				end
			end);
			
			-- Show stuff
			for _, item in pairs(temp) do
				if ( item.count / stockRequired ) < showWhenBelow then
					local btnItemLink = AceGUI:Create("ItemLinkButton");
					btnItemLink:SetText(item.id);
					btnItemLink:SetRelativeWidth(0.7);
					btnItemLink:SetCallback("OnEnter", function() end);
					
					iGroup:AddChild(btnItemLink);
					
					-- Current quantity
					local lblQuantity = AceGUI:Create("Label");
					lblQuantity:SetText(self:ColorCode(item.count, stockRequired));
					lblQuantity:SetRelativeWidth(0.149);
					
					iGroup:AddChild(lblQuantity);
					
					-- Required stock
					local lblStockRequired = AceGUI:Create("Label");
					lblStockRequired:SetText(stockRequired);
					lblStockRequired:SetRelativeWidth(0.149);
					
					iGroup:AddChild(lblStockRequired);
				end
			end
			
			-- We no longer need this, so forget about it
			table.wipe(temp);
	
			mod.scrollFrame:AddChild(iGroup);
		end
	end
end

function mod:ColorCode(num, required)
	local percentage = ( num / required );
	
	if percentage >= addon.db.global.defaults.colors.green then
		return ("|cff00ff00%d|r"):format(num);
	elseif percentage >= addon.db.global.defaults.colors.yellow then
		return ("|cffffff00%d|r"):format(num);
	elseif percentage >= addon.db.global.defaults.colors.orange then
		return ("|cffff9933%d|r"):format(num);
	elseif percentage >= addon.db.global.defaults.colors.red then
		return ("|cffff0000%d|r"):format(num);
	end
end

function mod:NumberFormat(num)
	local formatted = string.gsub(num, "(%d)(%d%d%d)$", "%1,%2", 1);
	
	while true do
		formatted, matches = string.gsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1);
		
		if matches == 0 then
			break;
		end
	end
	
	return formatted;
end