view SecureMenu.lua @ 12:2273a56762cb

Manage -partially- multiple recipes for the same item adn track the number of items made by a recipe
author contrebasse
date Sun, 03 Apr 2011 00:25:08 +0200
parents 2716889648bd
children f7b23db7bcc6
line wrap: on
line source
local addonName, A = ...

-- Create the menu frame
local MenuFrame = CreateFrame("Frame","ReagentMaker_ExternalFrame",UIParent) --, "ReagentMakerDropDownMenu"); -- Needs a global name ?
MenuFrame:Hide()
MenuFrame:SetSize(192,256)
MenuFrame:SetFrameStrata("DIALOG")
MenuFrame:EnableMouse(true)
MenuFrame:SetPoint("CENTER")
tinsert(UISpecialFrames,"ReagentMaker_ExternalFrame") -- make it closable with escape

MenuFrame:SetScript("OnEvent",function(self,event,...)
	if event == "TRADE_SKILL_CLOSE" or event == "PLAYER_REGEN_DISABLED" then
		MenuFrame:Hide()
	end
end)
MenuFrame:RegisterEvent("TRADE_SKILL_CLOSE")
MenuFrame:RegisterEvent("PLAYER_REGEN_ENABLED")
A.MenuFrame = MenuFrame

-- Background adaptable vertically
local bg_top = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_top:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_top:SetSize(192,80)
bg_top:SetPoint("TOP")
bg_top:SetTexCoord(0,192/256,0,80/256)
local bg_bot = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_bot:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_bot:SetSize(192,16)
bg_bot:SetPoint("BOTTOM")
bg_bot:SetTexCoord(0,192/256,240/256,1)
local bg_mid = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,0)
bg_mid:SetTexture("Interface\\LootFrame\\UI-LootPanel")
bg_mid:SetWidth(192)
bg_mid:SetPoint("TOP",bg_top,"BOTTOM")
bg_mid:SetPoint("BOTTOM",bg_bot,"TOP")
bg_mid:SetTexCoord(0,192/256,80/256,240/256)

-- Bouton de fermeture
local CloseButton = CreateFrame("Button",nil,MenuFrame,"UIPanelCloseButton");
CloseButton:SetPoint("TOPRIGHT",0,-10)

-- Main icon
local itemIcon = MenuFrame:CreateTexture(nil,"BACKGROUND",nil,-1)
itemIcon:SetSize(64,64)
itemIcon:SetPoint("TOPLEFT",8,-4)

-- Title
local TitleText = MenuFrame:CreateFontString(nil,"ARTWORK","GameFontHighlight")
TitleText:SetSize(92,14)
TitleText:SetPoint("RIGHT",CloseButton,"LEFT",4,1)

local MENU_ENTRY_HEIGHT = 41
local MENU_ENTRY_WIDTH = 147
local MENU_ENTRY_ICON_RATIO = 40/48

local numActiveEntries = 0
local menuEntries = {}

-- Button hovering
local function btnEntered(btn)
	--[[
	-- Index of the reagent in the recipe, taken from the button name
	local reagentRecipeIndex = A.buttonNumber(btn)

	-- ID of the reagent we want to craft
	local reagentID = A.link2ID(GetTradeSkillReagentItemLink(GetTradeSkillSelectionIndex(), reagentRecipeIndex))

	-- Continue only if the reagent is known
	if not reagentID or not A.data[reagentID] then return end
	--]]

	if btn.numMakable>0 then
		btn.textureHighlight:Show()
	end

	GameTooltip:SetOwner(btn,"ANCHOR_TOPLEFT")
	GameTooltip:SetHyperlink(btn.reagentLink)
	GameTooltip:Show()
	local link, tradeLink = GetSpellLink(btn.spellID)
	A.tooltipRecipe:SetOwner(GameTooltip)
	A.tooltipRecipe:SetHyperlink(link)
	A.tooltipRecipe:Show()
	A.tooltipRecipe:ClearAllPoints()
	A.tooltipRecipe:SetPoint("BOTTOMLEFT",GameTooltip,"BOTTOMRIGHT")
end
local function btnLeft(btn)
	btn.textureHighlight:Hide()
	GameTooltip:Hide()
	A.tooltipRecipe:Hide()
end
local function createMenuEntry()
	local btn = CreateFrame("Button", nil, MenuFrame, "SecureActionButtonTemplate")
	table.insert(menuEntries,btn)

	btn:Hide()
	btn:SetSize(MENU_ENTRY_WIDTH,MENU_ENTRY_HEIGHT)
	btn:SetFrameStrata("DIALOG")

	-- Set its position
	if #menuEntries>1 then
		btn:SetPoint("TOP",menuEntries[#menuEntries-1],"BOTTOM",0,-2)
	else
		btn:SetPoint("TOPLEFT",MenuFrame,"TOPLEFT",24,-79)
	end

	local icon = btn:CreateTexture(nil,"BACKGROUND")
	icon:SetPoint("TOPLEFT")
	icon:SetSize(39,39)
	btn.icon = icon

	local itemNameBG = btn:CreateTexture(nil,"BACKGROUND")
	itemNameBG:SetTexture("Interface\\QuestFrame\\UI-QuestItemNameFrame")
	itemNameBG:SetSize(128,64)
	itemNameBG:SetPoint("LEFT",icon,"RIGHT",-10,0)

	local itemName = btn:CreateFontString(nil,"BACKGROUND","GameFontHighlight")
	itemName:SetSize(90,36)
	itemName:SetPoint("LEFT",itemNameBG,"LEFT",15,0)
	itemName:SetJustifyH("LEFT")
	itemName:SetWordWrap(true)
	itemName:SetNonSpaceWrap(false)
	btn.itemName = itemName

	local textureHighlight = btn:CreateTexture(nil,"BORDER")
	textureHighlight:Hide()
	textureHighlight:SetTexture("Interface\\BUTTONS\\CheckButtonHilight")
	textureHighlight:SetBlendMode("ADD")
	textureHighlight:SetAllPoints(icon)
	btn.textureHighlight = textureHighlight

	local countDetail = btn:CreateFontString(nil,"ARTWORK","NumberFontNormal")
	countDetail:SetPoint("BOTTOMRIGHT",icon,"BOTTOMRIGHT",-1,1)
	countDetail:SetJustifyH("RIGHT")
	countDetail:SetJustifyV("BOTTOM")
	btn.countDetail = countDetail

	local resultNumber = btn:CreateFontString(nil,"ARTWORK","NumberFontNormal")
	resultNumber:SetPoint("TOPLEFT",icon,"TOPLEFT",1,-3)
	resultNumber:SetJustifyH("LEFT")
	resultNumber:SetJustifyV("TOP")
	resultNumber:SetFont("Fonts\\ARIALN.TTF", 12, "OUTLINE")
	btn.resultNumber = resultNumber

	btn:HookScript("OnEnter", btnEntered)
	btn:HookScript("OnLeave", btnLeft)

	return btn
end


local function menuCraftItem()
	action(itemID,reagentIndex,IsShiftKeyDown())
end

local function updateCounts()
	local anyMakable
	for i=1,numActiveEntries do
		btn = menuEntries[i]
		local itemCount = GetItemCount(btn.reagentID)

		local numMakable = math.floor(itemCount/(btn.reagentsForOneRecipe or 1))
		btn.countDetail:SetText(itemCount.."/"..(btn.reagentsForOneRecipe or 1))

		if numMakable>0 then
			anyMakable = true
			btn.countDetail:SetTextColor(1, 1, 1, 1)
			btn.icon:SetVertexColor(1,1,1);
			btn.itemName:SetTextColor(1,1,1,1)
		else
			-- Do not disable the button, to be able to show the tooltip
			btn.countDetail:SetTextColor(1, 0.1, 0.1, 1)
			btn.icon:SetVertexColor(0.5, 0.5, 0.5)
			btn.itemName:SetTextColor(1,1,1,0.5)
		end

		btn.numMakable = numMakable
	end

	local r,g,b = TitleText:GetTextColor()
	if anyMakable then
		itemIcon:SetVertexColor(1,1,1)
		TitleText:SetTextColor(r,g,b,1)
	else
		itemIcon:SetVertexColor(0.5, 0.5, 0.5)
		TitleText:SetTextColor(r,g,b,0.7)
	end
end

local function menuAddItem(action,itemID,reagent,spellID)
	local btn
	-- Create a button only if necessary
	if numActiveEntries >= #menuEntries then
		btn = createMenuEntry()
	else
		btn = menuEntries[numActiveEntries+1]
	end

	-- Set text and icon
	local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(reagent[1])
	if name then
		btn.itemName:SetText(name)
	else
		A.DEBUG("No item name : "..reagent[1])
		return
	end

	if texture then
		btn.icon:SetTexture(texture)
	else
		A.DEBUG("No item texture : "..reagent[1])
		return
	end

	-- Set chance to have the item or the number of items created
	if reagent[3] then
		if reagent[3]<1 then
			btn.resultNumber:SetText((reagent[3]*100).."%")
		elseif reagent[4] and reagent[3]~=reagent[4] then
			btn.resultNumber:SetText(math.min(reagent[3],reagent[4]).."-"..math.max(reagent[3],reagent[4]))
		elseif reagent[3]>1 then
			btn.resultNumber:SetText(reagent[3])
		end
	end

	-- Save params
	btn.itemID = itemID
	btn.reagentID = reagent[1]
	btn.reagentLink = link
	btn.reagentsForOneRecipe = reagent[2]
	btn.spellID = spellID

	-- Set action
	if type(action)=="function" then
		btn:SetScript("PreClick",action)
		btn:SetAttribute("type", nil)
		btn:SetAttribute("macrotext", nil)
	else --if type(action)=="string" then
		btn:SetScript("PreClick",nil)
		btn:SetAttribute("type", "macro")
		btn:SetAttribute("macrotext", action..name)
	end -- if

	btn:Show()

	-- Increase the entry number
	numActiveEntries = numActiveEntries + 1
end -- function

-- Fill the window and open it
function A.externalCraftWindow(itemID)
	-- Do not open during combat
	if InCombatLockdown() then return end

	-- Save the tradeskill
	A.currentTradeSkill = GetTradeSkillLine()

	-- Close the previous menu
	MenuFrame:Hide()
	for i=1,numActiveEntries do
		menuEntries[i]:Hide()
	end
	numActiveEntries = 0

	-- Fill the info of the reagent to make
	local name, link, quality, iLevel, reqLevel, class, subclass, maxStack, equipSlot, texture, vendorPrice = GetItemInfo(itemID)
	SetPortraitToTexture(itemIcon, texture)
	TitleText:SetText(name)
	local color = ITEM_QUALITY_COLORS[quality]
	TitleText:SetTextColor(color.r, color.g, color.b)

	-- Loop over the available recipes
	for _,reagent in ipairs(A.data[itemID]) do
			if A.data[itemID].spell then
				-- Special spell
				menuAddItem(A.data[itemID].spell,itemID,reagent,A.data[itemID].spellID)
			else
				-- Standard tradeskill spell UNTESTED
				menuAddItem(A.craft,itemID,reagent)
			end -- if
	end -- for

	MenuFrame:SetHeight(89 + numActiveEntries*(MENU_ENTRY_HEIGHT+2))

	MenuFrame:ClearAllPoints()
	MenuFrame:SetPoint("TOPLEFT",TradeSkillFrame,"TOPRIGHT",-2,14)

	updateCounts()

	MenuFrame:Show()
end