Files

188 lines
8.4 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- OctoRadio (Gitea #5): Octo's in-game radio (Booty Bay Pirate Radio) on the Forever client.
-- Stations come from the Octo client (Stations.lua, generated by ops/octo_radio_stations.py). The gateway
-- Backend streams the station itself over the local "OctoRadio" add-on channel:
-- "P <n>" tune in, "S" off, "V <0-100>" volume, "Q" status -> reply "T;<state>;<n>;<Artist - Title>"
-- (state 0 off, 1 connecting, 2 playing, 3 offline). Only post-hooks and own frames: no Blizzard globals.
local ADDON = ...
local PREFIX = "OctoRadio"
local L = {
title = "Booty Bay Pirate Radio", tag = "[Radio]", tuning = "Tuning in to %s...", off = "Radio off.",
nowPlaying = "Now Playing", isOff = "Radio is off", fetching = "Fetching track...", offline = "Station offline",
offButton = "Off", hint = "Click to choose a station", volume = "Volume",
}
local DB
local state, activeCmd, title = 0, nil, ""
local menu, button
local buttons = {}
local function send(text)
C_ChatInfo.SendAddonMessage(PREFIX, text, "WHISPER", (UnitName("player")))
end
local function alert(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff39bdf3" .. L.tag .. "|r " .. msg)
end
local function stationName(cmd)
for _, s in ipairs(OctoRadioStations) do if s.cmd == cmd then return s.name end end
end
-- Menu -----------------------------------------------------------------------------------------------
local MENU_W, MARGIN = 200, 10
local function refresh()
if not menu then return end
for _, b in ipairs(buttons) do
local lit = (b.cmd == nil and activeCmd == nil) or (b.cmd ~= nil and b.cmd == activeCmd)
if lit then b:LockHighlight() else b:UnlockHighlight() end
end
local line, sub
if not activeCmd then line, sub = L.isOff, ""
elseif state == 3 then line, sub = L.offline, stationName(activeCmd) or ""
elseif state ~= 2 or title == "" then line, sub = L.fetching, stationName(activeCmd) or ""
else
local artist, song = title:match("^(.-) %- (.+)$")
line, sub = song or title, artist or ""
end
menu.track:SetText(line)
menu.artist:SetText(sub)
end
local function tune(cmd)
activeCmd, state, title = cmd, 1, ""
send("P " .. cmd)
alert(L.tuning:format(stationName(cmd) or cmd))
DB.station = cmd
refresh()
end
local function turnOff()
activeCmd, state, title = nil, 0, ""
send("S")
alert(L.off)
DB.station = nil
refresh()
end
local function makeMenu()
menu = CreateFrame("Frame", "OctoRadioMenu", UIParent, "BackdropTemplate")
menu:SetFrameStrata("DIALOG")
menu:SetClampedToScreen(true)
menu:SetBackdrop({ bgFile = "Interface\\DialogFrame\\UI-DialogBox-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border", tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 } })
menu:EnableMouse(true)
menu:Hide()
local head = menu:CreateFontString(nil, "OVERLAY", "GameFontNormal")
head:SetPoint("TOP", 0, -MARGIN); head:SetText(L.title)
-- volume
local vol = menu:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
vol:SetPoint("TOPLEFT", MARGIN, -32); vol:SetText(L.volume)
local slider = CreateFrame("Slider", nil, menu, "BackdropTemplate")
slider:SetOrientation("HORIZONTAL"); slider:SetSize(MENU_W - 2 * MARGIN - 50, 15)
slider:SetPoint("LEFT", vol, "RIGHT", 8, 0)
slider:SetBackdrop({ bgFile = "Interface\\Buttons\\UI-SliderBar-Background",
edgeFile = "Interface\\Buttons\\UI-SliderBar-Border", tile = true, tileSize = 8, edgeSize = 8,
insets = { left = 3, right = 3, top = 6, bottom = 6 } })
slider:SetThumbTexture("Interface\\Buttons\\UI-SliderBar-Button-Horizontal")
slider:SetMinMaxValues(0, 100); slider:SetValueStep(5); slider:SetObeyStepOnDrag(true)
slider:SetValue(DB.volume)
slider:SetScript("OnValueChanged", function(_, v)
v = math.floor(v + 0.5)
if v ~= DB.volume then DB.volume = v; send("V " .. v) end
end)
-- now playing
local np = menu:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
np:SetPoint("TOP", 0, -56); np:SetText(L.nowPlaying)
menu.track = menu:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
menu.track:SetPoint("TOP", np, "BOTTOM", 0, -3); menu.track:SetWidth(MENU_W - 2 * MARGIN)
menu.track:SetMaxLines(2); menu.track:SetWordWrap(true)
menu.artist = menu:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
menu.artist:SetPoint("TOP", menu.track, "BOTTOM", 0, -2); menu.artist:SetWidth(MENU_W - 2 * MARGIN)
-- stations + off
local y = -112
local function add(text, cmd)
local b = CreateFrame("Button", nil, menu, "UIPanelButtonTemplate")
b:SetSize(MENU_W - 2 * MARGIN, 22); b:SetPoint("TOP", 0, y); b:SetText(text)
b:GetFontString():SetWidth(MENU_W - 2 * MARGIN - 16); b:GetFontString():SetWordWrap(false)
b.cmd = cmd
b:SetScript("OnClick", function() if cmd then tune(cmd) else turnOff() end end)
buttons[#buttons + 1] = b
y = y - 25
end
for _, s in ipairs(OctoRadioStations) do add(s.name, s.cmd) end
add(L.offButton, nil)
menu:SetSize(MENU_W, -y + MARGIN - 3)
-- close when the mouse has been away for a moment, like a drop-down
local away = 0
menu:SetScript("OnUpdate", function(self, elapsed)
if self:IsMouseOver() or (button and button:IsMouseOver()) then away = 0 else away = away + elapsed end
if away > 2 then self:Hide() end
end)
menu:SetScript("OnShow", function() away = 0; send("Q") end)
end
local function toggleMenu()
if not menu then makeMenu() end
if menu:IsShown() then menu:Hide() return end
menu:ClearAllPoints()
menu:SetPoint("TOPRIGHT", button, "BOTTOMLEFT", 10, 10)
refresh()
menu:Show()
end
-- Minimap button (OctoMinimapButtons collects it into its panel) -------------------------------------
local function makeButton()
button = CreateFrame("Button", "OctoRadioMinimapButton", Minimap)
button:SetSize(31, 31)
button:SetFrameStrata("MEDIUM")
button:SetFrameLevel(Minimap:GetFrameLevel() + 8)
button:SetPoint("TOPLEFT", Minimap, "TOPLEFT", 52 - 80 * math.cos(math.rad(DB.angle)), 80 * math.sin(math.rad(DB.angle)) - 52)
local icon = button:CreateTexture(nil, "BACKGROUND")
icon:SetSize(20, 20); icon:SetPoint("TOPLEFT", 7, -5)
icon:SetTexture("Interface\\Icons\\INV_Gizmo_GoblinBoomBox_01")
local border = button:CreateTexture(nil, "OVERLAY")
border:SetSize(53, 53); border:SetPoint("TOPLEFT")
border:SetTexture("Interface\\Minimap\\MiniMap-TrackingBorder")
button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
button:RegisterForClicks("LeftButtonUp")
button:SetScript("OnClick", toggleMenu)
button:SetScript("OnEnter", function(self)
GameTooltip:SetOwner(self, "ANCHOR_LEFT")
GameTooltip:SetText(L.title)
if activeCmd then GameTooltip:AddLine(stationName(activeCmd), 1, 1, 1) end
if activeCmd and state == 2 and title ~= "" then GameTooltip:AddLine(title, 0.8, 0.8, 0.8, true) end
GameTooltip:AddLine(L.hint, 0.5, 0.5, 0.5)
GameTooltip:Show()
end)
button:SetScript("OnLeave", function() GameTooltip:Hide() end)
end
-- Status from the Backend ----------------------------------------------------------------------------
local poll = CreateFrame("Frame")
poll.t = 0
poll:SetScript("OnUpdate", function(self, elapsed)
self.t = self.t + elapsed
if self.t < 3 then return end
self.t = 0
if activeCmd or (menu and menu:IsShown()) then send("Q") end
end)
local events = CreateFrame("Frame")
events:RegisterEvent("ADDON_LOADED")
events:RegisterEvent("PLAYER_LOGIN")
events:RegisterEvent("CHAT_MSG_ADDON")
events:SetScript("OnEvent", function(_, event, a1, a2)
if event == "ADDON_LOADED" and a1 == ADDON then
OctoRadioDB = OctoRadioDB or {}
DB = OctoRadioDB
DB.volume = DB.volume or 70
DB.angle = DB.angle or 200
C_ChatInfo.RegisterAddonMessagePrefix(PREFIX)
elseif event == "PLAYER_LOGIN" then
makeButton()
send("V " .. DB.volume)
elseif event == "CHAT_MSG_ADDON" and a1 == PREFIX then
local s, cmd, t = a2:match("^T;(%d);(%d+);(.*)$")
if not s then return end
state, title = tonumber(s), t
cmd = tonumber(cmd)
activeCmd = cmd ~= 0 and cmd or nil
refresh()
end
end)