174 lines
8.7 KiB
Lua
174 lines
8.7 KiB
Lua
-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
|
|
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
|
|
-- "Send materials to chat" for the native profession window (owner request: same as Octo's AdvancedTradeSkillWindow2).
|
|
-- ATSW2 behaviour reproduced: the selected recipe's materials are expanded through every recipe of the open profession that
|
|
-- produces an intermediate reagent (base materials only, counts multiplied, divided by the intermediate's minimum yield),
|
|
-- previewed in a confirmation popup naming the target, then sent as one header line + one line per material to the chat
|
|
-- line that is open/active (say, party, guild, raid, yell, whisper, channel, or a WIM whisper window).
|
|
-- Entry points: a "Materials to Chat" button on the recipe panel and /mats. Never sends without the confirmation click.
|
|
WoWHybridUI = WoWHybridUI or {};
|
|
local state = WoWHybridUI;
|
|
local L = {
|
|
header = "Crafting of %s require the following reagents:",
|
|
button = "Materials to Chat",
|
|
tooltip = "Send the materials needed to craft this item to the open chat line (like ATSW).",
|
|
noRecipe = "Select a recipe first.",
|
|
to = " to %s",
|
|
};
|
|
|
|
local function ItemName(itemID)
|
|
local name = C_Item and C_Item.GetItemNameByID and C_Item.GetItemNameByID(itemID);
|
|
if not name and GetItemInfo then name = GetItemInfo(itemID); end
|
|
return name or ("item:" .. itemID);
|
|
end
|
|
|
|
local function BasicReagents(recipeID)
|
|
local schematic = C_TradeSkillUI.GetRecipeSchematic(recipeID, false);
|
|
local list = {};
|
|
if not schematic or not schematic.reagentSlotSchematics then return list, schematic; end
|
|
for _, slot in ipairs(schematic.reagentSlotSchematics) do
|
|
local basic = (Enum and Enum.CraftingReagentType and slot.reagentType == Enum.CraftingReagentType.Basic) or slot.reagentType == nil or slot.reagentType == 1;
|
|
local reagent = slot.reagents and slot.reagents[1];
|
|
if basic and reagent and reagent.itemID and (slot.quantityRequired or 0) > 0 then
|
|
list[#list + 1] = { itemID = reagent.itemID, quantity = slot.quantityRequired };
|
|
end
|
|
end
|
|
return list, schematic;
|
|
end
|
|
|
|
-- Map intermediate products to learned recipes of the open profession (ATSW: "GetPositionFromGame(Name)").
|
|
local function ProducerIndex()
|
|
local index = {};
|
|
local ids = C_TradeSkillUI.GetAllRecipeIDs and C_TradeSkillUI.GetAllRecipeIDs() or {};
|
|
for _, id in ipairs(ids) do
|
|
local info = C_TradeSkillUI.GetRecipeInfo(id);
|
|
if info and info.learned then
|
|
local schematic = C_TradeSkillUI.GetRecipeSchematic(id, false);
|
|
local out = schematic and schematic.outputItemID;
|
|
if out and out ~= 0 and not index[out] then
|
|
index[out] = { recipeID = id, yield = math.max(1, schematic.quantityMin or 1) };
|
|
end
|
|
end
|
|
end
|
|
return index;
|
|
end
|
|
|
|
-- Returns ordered { {itemID, quantity} } of base materials for `amount` crafts of recipeID.
|
|
function state.ExpandMaterials(recipeID, amount, producers, totals, order, depth)
|
|
producers = producers or ProducerIndex(); totals = totals or {}; order = order or {}; depth = depth or 0;
|
|
for _, r in ipairs((BasicReagents(recipeID))) do
|
|
local need = r.quantity * amount;
|
|
local p = producers[r.itemID];
|
|
if p and depth < 6 and p.recipeID ~= recipeID then
|
|
state.ExpandMaterials(p.recipeID, math.ceil(need / p.yield), producers, totals, order, depth + 1);
|
|
else
|
|
if not totals[r.itemID] then totals[r.itemID] = 0; order[#order + 1] = r.itemID; end
|
|
totals[r.itemID] = totals[r.itemID] + need;
|
|
end
|
|
end
|
|
local result = {};
|
|
for _, id in ipairs(order) do result[#result + 1] = { itemID = id, quantity = totals[id] }; end
|
|
return result;
|
|
end
|
|
|
|
-- Chat target: WIM whisper box, else the active (or sticky) chat edit box type.
|
|
function state.MaterialsChatTarget()
|
|
if WIM_EditBoxInFocus and WIM_EditBoxInFocus.GetParent then
|
|
local user = WIM_EditBoxInFocus:GetParent().theUser;
|
|
if user then return "WHISPER", user, user; end
|
|
end
|
|
local active = ChatFrameUtil and ChatFrameUtil.GetActiveWindow or ChatEdit_GetActiveWindow;
|
|
local lastActive = ChatFrameUtil and ChatFrameUtil.GetLastActiveWindow or ChatEdit_GetLastActiveWindow;
|
|
local box = (active and active()) or (lastActive and lastActive()) or (DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.editBox);
|
|
local chatType = box and box:GetAttribute("chatType") or "SAY";
|
|
local language = box and box.languageID;
|
|
if chatType == "BN_WHISPER" then
|
|
return nil, nil, "Select a character whisper to send materials.";
|
|
elseif chatType == "WHISPER" then
|
|
local who = box:GetAttribute("tellTarget");
|
|
if not who or who == "" then return nil, nil, "Select a whisper recipient."; end
|
|
return chatType, who, who, language;
|
|
elseif chatType == "CHANNEL" then
|
|
local target = box:GetAttribute("channelTarget");
|
|
local id, name;
|
|
if target then id, name = GetChannelName(target); end
|
|
if not id or id <= 0 or not name then return nil, nil, "Select a joined chat channel."; end
|
|
return chatType, id, string.format("[%d. %s]", id, name), language;
|
|
end
|
|
return chatType, nil, _G[chatType] or chatType, language;
|
|
end
|
|
|
|
local pending;
|
|
StaticPopupDialogs["WOWHYBRID_SAY_REAGENTS"] = {
|
|
text = "%s", button1 = YES or "Yes", button2 = NO or "No",
|
|
OnAccept = function()
|
|
if not pending then return; end
|
|
local message = pending;
|
|
pending = nil;
|
|
local send = C_ChatInfo and C_ChatInfo.SendChatMessage or SendChatMessage;
|
|
for _, line in ipairs(message.lines) do send(line, message.chatType, message.language, message.target); end
|
|
end,
|
|
OnCancel = function() pending = nil; end,
|
|
timeout = 0, whileDead = 1, hideOnEscape = 1, showAlert = 1, preferredIndex = 3,
|
|
};
|
|
|
|
function state.BuildMaterialsMessage(recipeID)
|
|
local link = C_TradeSkillUI.GetRecipeItemLink and C_TradeSkillUI.GetRecipeItemLink(recipeID);
|
|
if not link then
|
|
local info = C_TradeSkillUI.GetRecipeInfo(recipeID); link = info and info.name or ("recipe " .. recipeID);
|
|
end
|
|
local lines = { string.format(L.header, link) };
|
|
for _, m in ipairs(state.ExpandMaterials(recipeID, 1)) do
|
|
local line = ItemName(m.itemID);
|
|
if m.quantity > 1 then line = line .. " (" .. m.quantity .. ")"; end
|
|
lines[#lines + 1] = line;
|
|
end
|
|
return lines;
|
|
end
|
|
|
|
function state.SayMaterials(recipeID)
|
|
if not recipeID then UIErrorsFrame:AddMessage(L.noRecipe, 1, 0.2, 0.2); return; end
|
|
local chatType, target, label, language = state.MaterialsChatTarget();
|
|
if not chatType then UIErrorsFrame:AddMessage(label, 1, 0.2, 0.2); return; end
|
|
local lines = state.BuildMaterialsMessage(recipeID);
|
|
pending = { lines = lines, chatType = chatType, target = target, language = language };
|
|
StaticPopup_Show("WOWHYBRID_SAY_REAGENTS", (L.button .. string.format(L.to, tostring(label))) .. "\n\n" .. table.concat(lines, "\n"));
|
|
end
|
|
|
|
local function SelectedRecipeID()
|
|
local form = ProfessionsFrame and ProfessionsFrame.CraftingPage and ProfessionsFrame.CraftingPage.SchematicForm;
|
|
local info = form and form.GetRecipeInfo and form:GetRecipeInfo();
|
|
return info and info.recipeID;
|
|
end
|
|
|
|
local function InstallMaterialsButton()
|
|
local form = ProfessionsFrame and ProfessionsFrame.CraftingPage and ProfessionsFrame.CraftingPage.SchematicForm;
|
|
if not form or state.materialsButton then return form ~= nil; end
|
|
if InCombatLockdown() then state.materialsPending = true; return false; end
|
|
local button = CreateFrame("Button", nil, form, "UIPanelButtonTemplate");
|
|
button:SetSize(140, 22);
|
|
button:SetPoint("BOTTOMLEFT", form, "BOTTOMLEFT", 10, 8);
|
|
button:SetText(L.button);
|
|
button:SetScript("OnClick", function() state.SayMaterials(SelectedRecipeID()); end);
|
|
button:SetScript("OnEnter", function(self)
|
|
GameTooltip:SetOwner(self, "ANCHOR_TOP"); GameTooltip:SetText(L.button);
|
|
local chatType, _, label = state.MaterialsChatTarget();
|
|
GameTooltip:AddLine(chatType and (L.tooltip .. string.format(L.to, tostring(label))) or label, 1, 1, 1, true); GameTooltip:Show();
|
|
end);
|
|
button:SetScript("OnLeave", function() GameTooltip:Hide(); end);
|
|
state.materialsButton = button;
|
|
state.materialsPending = nil;
|
|
return true;
|
|
end
|
|
|
|
SLASH_WOWHYBRIDMATS1 = "/mats";
|
|
SlashCmdList["WOWHYBRIDMATS"] = function() state.SayMaterials(SelectedRecipeID()); end;
|
|
|
|
local events = CreateFrame("Frame");
|
|
events:RegisterEvent("ADDON_LOADED");
|
|
events:RegisterEvent("PLAYER_REGEN_ENABLED");
|
|
events:SetScript("OnEvent", function(_, event)
|
|
if InstallMaterialsButton() then events:UnregisterEvent("ADDON_LOADED"); end
|
|
end);
|
|
InstallMaterialsButton();
|