ruen-ui/RuenUI/SwingTimers.lua

163 lines
6.5 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local _, P = ...
-- PLAYER_SWING supplies the client's own duration for each weapon, including
-- druid forms and ranged/wand attacks. No combat-log guesses or attack macros.
P.swingDockHeight = 13
P.swingCastGap = 18 -- the native cast's spell name extends below its frame
local dock, allowed, lanes
local FADE = 0.25
local colors = {{0.78, 0.64, 0.34}, {0.42, 0.67, 0.70}, {0.60, 0.72, 0.43}}
local function enabled()
return RuenUIDB and not RuenUIDB.disabled
and RuenUIDB.swingTimersEnabled ~= false and allowed and allowed()
end
local function number(value)
return not (issecretvalue and issecretvalue(value)) and type(value) == "number"
and value == value and value > 0 and value < 120
end
local function clear()
if not dock then return end
for _, lane in ipairs(lanes) do
lane.finish, lane.duration = nil, nil
lane:Hide()
end
dock.preview = nil
dock:SetScript("OnUpdate", nil)
dock:Hide()
end
P.StopSwingTimers = clear
local function paint()
if not enabled() then clear(); return end
local now, row = GetTime(), 0
for _, lane in ipairs(lanes) do
if lane.finish and now >= lane.finish + FADE then
lane.finish, lane.duration = nil, nil
end
if lane.finish then
local value = math.min(1, math.max(0, 1 - (lane.finish - now) / lane.duration))
lane:SetValue(value)
lane:SetAlpha(math.min(1, math.max(0, (lane.finish + FADE - now) / FADE)))
if lane.row ~= row then
lane:ClearAllPoints()
lane:SetPoint("TOPLEFT", dock, "TOPLEFT", 0, -row * 5)
lane:SetPoint("TOPRIGHT", dock, "TOPRIGHT", 0, -row * 5)
lane.row = row
end
lane:Show()
row = row + 1
else
lane:Hide()
end
end
if row == 0 then clear() end
end
local function start(duration, swingType)
if not enabled() or not number(duration) then return end
if issecretvalue and issecretvalue(swingType) then return end
local lane = dock.byType[swingType]
if not lane then return end
lane.duration, lane.finish = duration, GetTime() + duration
dock:Show()
paint()
-- Only runs while at least one short timer/fade is visible; zero idle tickers.
dock:SetScript("OnUpdate", paint)
end
local function equipmentChanged()
if not lanes then return end
local speed = {UnitAttackSpeed("player")}
for i, lane in ipairs(lanes) do
if not number(speed[i]) then lane.finish, lane.duration = nil, nil end
end
paint()
end
function P.InitializeSwingTimers(predicate)
allowed = predicate
if dock then return end
if not Enum.PlayerSwingType then return end
dock = CreateFrame("Frame", "RuenSwingTimers", UIParent)
dock:SetSize(208, P.swingDockHeight)
dock:SetPoint("BOTTOM", UIParent, "BOTTOM", 197)
dock:SetFrameStrata("MEDIUM")
dock:EnableMouse(false)
lanes, dock.byType = {}, {}
local types = {Enum.PlayerSwingType.MainHand, Enum.PlayerSwingType.OffHand, Enum.PlayerSwingType.Ranged}
for i, swingType in ipairs(types) do
local lane = CreateFrame("StatusBar", nil, dock)
lane:SetHeight(3)
lane:SetMinMaxValues(0, 1)
lane:SetStatusBarTexture("Interface\\Buttons\\WHITE8X8")
lane:SetStatusBarColor(unpack(colors[i]))
lane:EnableMouse(false)
local background = lane:CreateTexture(nil, "BACKGROUND")
background:SetAllPoints()
background:SetColorTexture(0.02, 0.025, 0.03, 0.75)
lane:Hide()
lanes[i], dock.byType[swingType] = lane, lane
end
dock:SetScript("OnEvent", function(_, event, a, b)
if event == "PLAYER_SWING" then
if dock.preview then clear() end
start(a, b)
if RuenUICharacter and number(a) and not (issecretvalue and issecretvalue(b)) then
RuenUICharacter.lastSwing = {duration = a, swingType = b, time = time()}
end
elseif event == "UNIT_INVENTORY_CHANGED" or event == "UNIT_ATTACK_SPEED" then
if a == "player" then equipmentChanged() end
elseif event == "WEAPON_SLOT_CHANGED" then
-- The native bar restarts an in-progress timer after a weapon swap.
local speeds = {UnitAttackSpeed("player")}
for i, lane in ipairs(lanes) do
if lane.finish then
if number(speeds[i]) then start(speeds[i], types[i])
else lane.finish, lane.duration = nil, nil end
end
end
paint()
else
clear() -- zoning, death, combat ending, or changing form
end
end)
for _, event in ipairs({"PLAYER_SWING", "PLAYER_ENTERING_WORLD", "PLAYER_DEAD",
"PLAYER_REGEN_ENABLED", "UPDATE_SHAPESHIFT_FORM", "WEAPON_SLOT_CHANGED"}) do
pcall(dock.RegisterEvent, dock, event)
end
dock:RegisterUnitEvent("UNIT_INVENTORY_CHANGED", "player")
dock:RegisterUnitEvent("UNIT_ATTACK_SPEED", "player")
clear()
end
function P.PositionSwingTimers(plan, width, place)
if not dock then return end
dock:SetWidth(width)
local point = plan.RuenSwingTimers
place(dock, point[1], UIParent, "BOTTOM", point[2], point[3])
end
function P.SwingCommand(option)
if not dock then return P.Localized("Swing timing is unavailable in this client.", "Таймеры атак недоступны в этом клиенте.") end
if option == "off" then
RuenUIDB.swingTimersEnabled = false
clear()
elseif option == "on" then
RuenUIDB.swingTimersEnabled = true
elseif option == "preview" then
if InCombatLockdown() then return P.Localized("Preview is available outside combat.", "Предпросмотр доступен вне боя.") end
if not enabled() then return P.Localized("Enable the Ruen layout and /ruen swing on first.", "Сначала включите макет Ruen и /ruen swing on.") end
clear()
for swingType in pairs(dock.byType) do start(6, swingType) end
dock.preview = true
return P.Localized("Swing preview: gold main hand, blue off hand, green ranged. Only active attacks appear during play.",
"Таймеры: золотой — основная рука, голубой — вторая, зелёный — дальний бой. В игре видны только активные атаки.")
end
return P.Localized("Swing timers: ", "Таймеры атак: ")
.. (RuenUIDB.swingTimersEnabled == false and "OFF" or "ON")
.. ". /ruen swing on | off | preview"
end