Files

336 lines
16 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Barbershop window in the native Blizzard panel style for the server's
-- TW_BARBERSHOP service (Octo Turtle_Barbershop.lua contract). The server owns
-- the session, the preview on the character, the price and the purchase; this
-- file only shows its state and sends the original requests. Nothing is
-- computed or invented locally: option counts are the Octo client's table.
WoWHybridUI = WoWHybridUI or {};
local state = WoWHybridUI;
local shop = state.barberShop or {};
state.barberShop = shop;
shop.prefix = "TW_BARBERSHOP";
local DELIMITER = ";"; -- Octo Globals.lua ADDON_MSG_FIELD_DELIMITER
local STYLE, COLOR, FACIAL = 1, 2, 3;
local REQUEST = {[STYLE] = "C2S_PREVIEW_STYLE", [COLOR] = "C2S_PREVIEW_COLOR", [FACIAL] = "C2S_PREVIEW_FACIAL_HAIR"};
-- Options per race file name, {male, female} (Turtle_Barbershop.lua Customizations).
shop.counts = {
[STYLE] = {Human = {17, 26}, Dwarf = {17, 19}, Gnome = {12, 12}, NightElf = {12, 12}, BloodElf = {16, 20},
Orc = {13, 14}, Troll = {10, 10}, Tauren = {13, 12}, Scourge = {16, 17}, Goblin = {14, 17}},
[COLOR] = {Human = {10, 10}, Dwarf = {10, 10}, Gnome = {10, 10}, NightElf = {10, 10}, BloodElf = {16, 16},
Orc = {8, 8}, Troll = {10, 10}, Tauren = {9, 9}, Scourge = {11, 11}, Goblin = {5, 5}},
[FACIAL] = {Human = {9, 7}, Dwarf = {21, 6}, Gnome = {12, 7}, NightElf = {14, 10}, BloodElf = {11, 11},
Orc = {11, 7}, Troll = {14, 10}, Tauren = {9, 7}, Scourge = {17, 8}, Goblin = {10, 5}},
};
local ERRORS = {
"You are not currently in a barbershop session.",
"You do not have enough money.",
"You are already in a barbershop session.",
"Selected style and color combination is not available to you.",
"A system error occurred. Please try again.",
};
local session = shop.session or {active = false, cost = 0, old = {1, 1, 1}, selected = {1, 1, 1}};
shop.session = session;
local function Public(value)
if issecretvalue and issecretvalue(value) then return nil; end
return value;
end
function shop.Identity()
local _, race = UnitRace("player");
local sex = Public(UnitSex("player"));
race = Public(race);
return type(race) == "string" and race or nil, (sex == 2 or sex == 3) and sex - 1 or nil;
end
function shop.Total(kind, race, sex)
local row = shop.counts[kind] and shop.counts[kind][race];
return row and row[sex] or nil;
end
-- Row labels follow the original per-race wording.
function shop.Label(kind, race, sex)
if kind == STYLE then return race == "Tauren" and "Horn Style" or "Hair Style"; end
if kind == COLOR then return race == "Tauren" and "Horn Color" or "Hair Color"; end
local label = "Facial Hair";
if sex == 2 then
if race == "NightElf" then label = "Markings";
elseif race == "Gnome" or race == "BloodElf" then label = "Earrings";
elseif race == "Tauren" then label = "Hair";
else label = "Piercings"; end
end
if race == "Scourge" or race == "Goblin" then label = "Features"; end
if race == "Troll" then label = "Tusks"; end
return label;
end
function shop.Changed()
for kind = STYLE, FACIAL do
if session.selected[kind] ~= session.old[kind] then return true; end
end
return false;
end
function shop.Parse(message)
if type(message) ~= "string" or message == "" then return nil; end
local fields = {};
for field in (message .. DELIMITER):gmatch("(.-)" .. DELIMITER) do fields[#fields + 1] = field; end
local kind = table.remove(fields, 1);
return kind, fields;
end
function shop.Send(payload)
if not C_ChatInfo or type(C_ChatInfo.SendAddonMessage) ~= "function" then return false; end
local name = Public(UnitName("player"));
if type(name) ~= "string" then return false; end
-- Qualified native self-service envelope; the gateway keeps the server's GUILD route.
local result = C_ChatInfo.SendAddonMessage(shop.prefix, payload, "WHISPER", name);
if result == 0 or result == true then return true; end
if UIErrorsFrame then UIErrorsFrame:AddMessage("Could not send the barbershop request. Try again.", 1, 0.1, 0.1); end
return false;
end
-- The server answers every preview request with the resulting selection and price.
function shop.Step(kind, direction)
if not session.active or not REQUEST[kind] then return false; end
local race, sex = shop.Identity();
local total = shop.Total(kind, race, sex);
local index = session.selected[kind] + direction;
if total then
if index < 1 then index = total; elseif index > total then index = 1; end
elseif index < 1 then
return false;
end
return shop.Send(REQUEST[kind] .. DELIMITER .. (index - 1));
end
function shop.Purchase() return session.active and shop.Send("C2S_PURCHASE"); end
function shop.Reset()
if not session.active or not shop.Send("C2S_RESET") then return false; end
for kind = STYLE, FACIAL do session.selected[kind] = session.old[kind]; end
return true;
end
function shop.Close()
if session.active then return shop.Send("C2S_CLOSE"); end
return false;
end
-- ---- window --------------------------------------------------------------
local frame;
local function Sound(key)
if SOUNDKIT and SOUNDKIT[key] and PlaySound then PlaySound(SOUNDKIT[key]); end
end
local function MoneyText(amount)
if type(GetMoneyString) == "function" then return GetMoneyString(amount, true); end
if C_CurrencyInfo and C_CurrencyInfo.GetCoinTextureString then return C_CurrencyInfo.GetCoinTextureString(amount); end
return string.format("%dg %ds %dc", math.floor(amount / 10000), math.floor(amount / 100) % 100, amount % 100);
end
local function ShowPlayer(model)
model:SetUnit("player");
-- Show the head without the helm in the preview only; the real helm setting is untouched.
if model.UndressSlot then pcall(model.UndressSlot, model, 1); end
if model.SetPortraitZoom then pcall(model.SetPortraitZoom, model, 0.55); end
end
local function ArrowButton(parent, direction)
local button = CreateFrame("Button", nil, parent);
button:SetSize(32, 32);
local art = "Interface\\Buttons\\UI-SpellbookIcon-" .. (direction < 0 and "PrevPage" or "NextPage");
button:SetNormalTexture(art .. "-Up");
button:SetPushedTexture(art .. "-Down");
button:SetDisabledTexture(art .. "-Disabled");
button:SetHighlightTexture("Interface\\Buttons\\UI-Common-MouseHilight", "ADD");
return button;
end
local function PanelButton(parent, text, width)
local button = CreateFrame("Button", nil, parent, "UIPanelButtonTemplate");
button:SetSize(width, 22);
button:SetText(text);
return button;
end
local function Build()
if frame then return frame; end
frame = CreateFrame("Frame", "WoWHybridBarberShopFrame", UIParent, "ButtonFrameTemplate");
frame:Hide();
frame:SetSize(338, 468);
frame:SetPoint("RIGHT", UIParent, "RIGHT", -120, 0);
frame:SetToplevel(true);
frame:EnableMouse(true);
frame:SetMovable(true);
frame:SetClampedToScreen(true);
frame:RegisterForDrag("LeftButton");
frame:SetScript("OnDragStart", frame.StartMoving);
frame:SetScript("OnDragStop", frame.StopMovingOrSizing);
if frame.SetTitle then frame:SetTitle(BARBERSHOP or "Barbershop");
elseif frame.TitleText then frame.TitleText:SetText(BARBERSHOP or "Barbershop"); end
-- Keep addon keys on the visible window. Inserting a handler into the sorted
-- game-menu list marks the stock casting callback read on every Escape.
local function makeKeyListener(consumeEscape)
local listener = CreateFrame("Frame", nil, frame, "InsecureKeyboardInputPropagatorTemplate")
listener:Hide()
listener:EnableKeyboard(true)
listener:SetScript("OnKeyDown", function(self, key)
local dismiss = (key == "ESCAPE" or GetBindingFromClick(key) == "TOGGLEGAMEMENU")
and not StaticPopup_IsAnyDialogShown() and not GetCurrentKeyBoardFocus()
and not (GameMenuFrame and GameMenuFrame:IsShown())
and not (EditModeManagerFrame and EditModeManagerFrame:IsShown())
and not SpellIsTargeting() and not (PlayerCastingBarFrame and PlayerCastingBarFrame:IsVisible())
if consumeEscape and not InCombatLockdown() then self:SetPropagateKeyboardInput(not dismiss) end
if dismiss then frame:Hide() end
end)
return listener
end
local escapeKeys = makeKeyListener(true)
local combatKeys = makeKeyListener(false)
local function armEscapeKeys()
local combat = not not InCombatLockdown()
if not combat then escapeKeys:SetPropagateKeyboardInput(true) end
escapeKeys:SetShown(not combat)
-- This listener keeps the template's fixed pass-through during lockdown.
combatKeys:SetShown(combat)
end
escapeKeys:RegisterEvent("PLAYER_REGEN_DISABLED")
escapeKeys:RegisterEvent("PLAYER_REGEN_ENABLED")
escapeKeys:SetScript("OnEvent", function() if frame:IsShown() then armEscapeKeys() end end)
local inset = frame.Inset or frame;
local model = CreateFrame("DressUpModel", nil, inset);
model:SetPoint("TOPLEFT", inset, "TOPLEFT", 4, -4);
model:SetPoint("TOPRIGHT", inset, "TOPRIGHT", -4, -4);
model:SetHeight(230);
model:EnableMouse(true);
model:SetScript("OnMouseDown", function(self) self.dragX = GetCursorPosition(); end);
model:SetScript("OnMouseUp", function(self) self.dragX = nil; end);
model:SetScript("OnUpdate", function(self)
if not self.dragX then return; end
local x = GetCursorPosition();
self:SetFacing(self:GetFacing() + (x - self.dragX) / 120);
self.dragX = x;
end);
frame.Model = model;
frame.Rows = {};
for kind = STYLE, FACIAL do
local row = CreateFrame("Frame", nil, inset);
row:SetSize(300, 32);
row:SetPoint("TOP", model, "BOTTOM", 0, -6 - (kind - 1) * 36);
row.Previous = ArrowButton(row, -1);
row.Previous:SetPoint("LEFT", row, "LEFT", 8, 0);
row.Next = ArrowButton(row, 1);
row.Next:SetPoint("RIGHT", row, "RIGHT", -8, 0);
row.Label = row:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
row.Label:SetPoint("CENTER");
row.Previous:SetScript("OnClick", function() Sound("IG_MAINMENU_OPTION_CHECKBOX_ON"); shop.Step(kind, -1); end);
row.Next:SetScript("OnClick", function() Sound("IG_MAINMENU_OPTION_CHECKBOX_ON"); shop.Step(kind, 1); end);
frame.Rows[kind] = row;
end
frame.CostLabel = inset:CreateFontString(nil, "ARTWORK", "GameFontNormal");
frame.CostLabel:SetPoint("BOTTOMLEFT", inset, "BOTTOMLEFT", 18, 12);
frame.CostLabel:SetText(COSTS_LABEL or "Cost:");
frame.Cost = inset:CreateFontString(nil, "ARTWORK", "GameFontHighlight");
frame.Cost:SetPoint("BOTTOMRIGHT", inset, "BOTTOMRIGHT", -18, 12);
frame.Cancel = PanelButton(frame, CANCEL or "Cancel", 90);
frame.Cancel:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", -8, 4);
frame.Reset = PanelButton(frame, RESET or "Reset", 90);
frame.Reset:SetPoint("RIGHT", frame.Cancel, "LEFT", -4, 0);
frame.Okay = PanelButton(frame, OKAY or "Okay", 90);
frame.Okay:SetPoint("RIGHT", frame.Reset, "LEFT", -4, 0);
frame.Okay:SetScript("OnClick", function() shop.Purchase(); end);
frame.Reset:SetScript("OnClick", function()
Sound("IG_MAINMENU_OPTION_CHECKBOX_ON");
shop.Reset();
shop.Refresh();
end);
frame.Cancel:SetScript("OnClick", function() frame:Hide(); end);
frame:SetScript("OnShow", function(self)
armEscapeKeys()
if self.SetPortraitToUnit then self:SetPortraitToUnit("player"); end
ShowPlayer(self.Model);
Sound("IG_CHARACTER_INFO_OPEN");
end);
frame:SetScript("OnHide", function()
Sound("IG_CHARACTER_INFO_CLOSE");
-- Closing the window by any route ends the server session, as in the original.
-- The session is treated as ended at once, so a lost reply cannot block the next visit.
if session.active then shop.Close(); session.active = false; session.cost = 0; end
end);
return frame;
end
function shop.Refresh()
if not frame then return; end
local race, sex = shop.Identity();
for kind = STYLE, FACIAL do
local total = shop.Total(kind, race, sex);
local text = shop.Label(kind, race, sex) .. " " .. session.selected[kind];
if total then text = text .. "/" .. total; end
frame.Rows[kind].Label:SetText(text);
end
local changed = shop.Changed();
frame.Okay:SetEnabled(changed);
frame.Reset:SetEnabled(changed);
local money = Public(GetMoney());
local text = MoneyText(session.cost);
if type(money) == "number" and money < session.cost and RED_FONT_COLOR and RED_FONT_COLOR.WrapTextInColorCode then
text = RED_FONT_COLOR:WrapTextInColorCode(text);
end
frame.Cost:SetText(text);
end
-- ---- server messages -----------------------------------------------------
local function Index(value) return (tonumber(value) or 0) + 1; end -- wire indices are 0-based
function shop.Handle(message)
local kind, fields = shop.Parse(message);
if kind == "S2C_OPEN_BARBER" then
-- cost;style;available;color;available;facial;available (the lists are unused, as in the original)
if session.active then return; end
session.cost = tonumber(fields[1]) or 0;
for option = STYLE, FACIAL do
session.old[option] = Index(fields[option * 2]);
session.selected[option] = session.old[option];
end
session.active = true;
Build():Show();
shop.Refresh();
elseif kind == "S2C_UPDATE_PREVIEW" then
if #fields < 4 then return; end
session.cost = tonumber(fields[1]) or 0;
for option = STYLE, FACIAL do session.selected[option] = Index(fields[option + 1]); end
shop.Refresh();
elseif kind == "S2C_PURCHASE_RESULT" then
if #fields < 4 or fields[1] ~= "1" then return; end
session.cost = 0;
for option = STYLE, FACIAL do session.old[option] = session.selected[option]; end
shop.Refresh();
Sound("MONEY_FRAME_OPEN");
elseif kind == "S2C_ERROR" then
local code = tonumber(fields[1]);
local text = code and ERRORS[code];
if text and UIErrorsFrame then UIErrorsFrame:AddMessage(text, 1, 0.1, 0.1); end
-- A session left over from before an interface reload has no window: end it so the next visit opens.
if code == 3 and not session.active then shop.Send("C2S_CLOSE"); end
elseif kind == "S2C_CLOSE_BARBER" then
session.active = false;
session.cost = 0;
if frame then frame:Hide(); end
end
end
local function RefreshModel()
if frame and frame:IsShown() then ShowPlayer(frame.Model); end
end
local events = CreateFrame("Frame");
events:RegisterEvent("CHAT_MSG_ADDON");
events:RegisterEvent("PLAYER_MONEY");
events:RegisterEvent("UNIT_MODEL_CHANGED");
events:SetScript("OnEvent", function(_, event, first, message, distribution)
if event == "CHAT_MSG_ADDON" then
if first == shop.prefix and distribution == "GUILD" then shop.Handle(message); end
elseif event == "PLAYER_MONEY" then
if session.active then shop.Refresh(); end
elseif event == "UNIT_MODEL_CHANGED" and first == "player" and session.active then
-- The server applies each preview to the character; redraw once the model has reloaded.
if C_Timer and C_Timer.After then C_Timer.After(0.6, RefreshModel); else RefreshModel(); end
end
end);
if C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix then C_ChatInfo.RegisterAddonMessagePrefix(shop.prefix); end