AltSystem/Core.lua

158 lines
5.7 KiB
Lua
Executable file

-- AltSystem Core
-- Addon initialization, slash command registration, and TRP3 integration.
AltSystem = AltSystem or {}
AltSystem.State = {
selectedSkillIndex = 1,
selectedSkillName = nil, -- skill name used to restore selection across sessions
selectedItemIndex = 1, -- 1 = No item
selectedDefenseIndex = 1, -- 1 = No Armor
shieldEnabled = false,
petSummonEnabled = false,
announceEnabled = false,
announceChannelIndex = 1, -- 1 = Emote, 2 = Party, 3 = Raid, 4 = Guild
rollType = "attack", -- "attack" or "defense"
announceOptionIndex = 1, -- 1 = Self Roll, 2+ = channel from AnnounceChannels
rollLog = {}, -- array of { text = "...", timestamp = time() }, max 100, not persisted
}
-- Channel definitions for announcing rolls
AltSystem.AnnounceChannels = {
{ name = "Emote (/e)", channel = "EMOTE" },
{ name = "Party (/p)", channel = "PARTY" },
{ name = "Raid (/ra)", channel = "RAID" },
{ name = "Guild (/g)", channel = "GUILD" },
}
-- Initialization on ADDON_LOADED
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, arg1)
if event == "ADDON_LOADED" and arg1 == "AltSystem" then
AltSystem:Init()
AltSystem:RegisterTRP3Module()
end
end)
function AltSystem:Init()
-- Load saved settings
AltSystemDB = AltSystemDB or {}
if AltSystemDB.petSummonEnabled ~= nil then
AltSystem.State.petSummonEnabled = AltSystemDB.petSummonEnabled
end
if AltSystemDB.shieldEnabled ~= nil then
AltSystem.State.shieldEnabled = AltSystemDB.shieldEnabled
end
if AltSystemDB.selectedItemIndex then
AltSystem.State.selectedItemIndex = AltSystemDB.selectedItemIndex
end
if AltSystemDB.selectedDefenseIndex then
AltSystem.State.selectedDefenseIndex = AltSystemDB.selectedDefenseIndex
end
if AltSystemDB.selectedSkillName then
AltSystem.State.selectedSkillName = AltSystemDB.selectedSkillName
end
if AltSystemDB.rollType then
AltSystem.State.rollType = AltSystemDB.rollType
end
-- Migrate from old announce settings or load new announceOptionIndex
if AltSystemDB.announceOptionIndex then
AltSystem.State.announceOptionIndex = AltSystemDB.announceOptionIndex
elseif AltSystemDB.announceEnabled ~= nil then
-- Backwards compat: derive from old announceEnabled + announceChannelIndex
if AltSystemDB.announceEnabled and AltSystemDB.announceChannelIndex then
AltSystem.State.announceOptionIndex = AltSystemDB.announceChannelIndex + 1
else
AltSystem.State.announceOptionIndex = 1
end
end
-- Derive announceEnabled and announceChannelIndex from announceOptionIndex
if AltSystem.State.announceOptionIndex > 1 then
AltSystem.State.announceEnabled = true
AltSystem.State.announceChannelIndex = AltSystem.State.announceOptionIndex - 1
else
AltSystem.State.announceEnabled = false
AltSystem.State.announceChannelIndex = 1
end
-- Register slash command /altsystem
SLASH_ALTSYSTEM1 = "/altsystem"
SlashCmdList["ALTSYSTEM"] = function()
AltSystem:ToggleWindow()
end
-- Save settings on logout
local saveFrame = CreateFrame("Frame")
saveFrame:RegisterEvent("PLAYER_LOGOUT")
saveFrame:SetScript("OnEvent", function()
AltSystemDB = AltSystemDB or {}
AltSystemDB.announceOptionIndex = AltSystem.State.announceOptionIndex
AltSystemDB.petSummonEnabled = AltSystem.State.petSummonEnabled
AltSystemDB.shieldEnabled = AltSystem.State.shieldEnabled
AltSystemDB.selectedItemIndex = AltSystem.State.selectedItemIndex
AltSystemDB.selectedDefenseIndex = AltSystem.State.selectedDefenseIndex
AltSystemDB.selectedSkillName = AltSystem.State.selectedSkillName
AltSystemDB.rollType = AltSystem.State.rollType
end)
-- Build the main frame now that saved variables are loaded
AltSystem:CreateMainFrame()
print("|cff00ccffAltSystem|r loaded. Type /altsystem to open.")
end
function AltSystem:ToggleWindow()
if AltSystem.MainFrame then
if AltSystem.MainFrame:IsShown() then
AltSystem.MainFrame:Hide()
else
AltSystem.MainFrame:Show()
end
end
end
-- Register as a TRP3 module so our onStart runs in the correct lifecycle phase
-- (after the toolbar module is initialized, but before WORKFLOW_ON_FINISH locks it)
function AltSystem:RegisterTRP3Module()
if not TRP3_API or not TRP3_API.module then
return
end
local MODULE_STRUCTURE = {
["name"] = "AltSystem",
["description"] = "AltSystem rolling window toolbar button",
["version"] = 1.000,
["id"] = "altsystem_module",
["onStart"] = function()
AltSystem:RegisterTRP3Button()
end,
["minVersion"] = 3,
["requiredDeps"] = {
{"trp3_tool_bar", 1},
},
}
TRP3_API.module.registerModule(MODULE_STRUCTURE)
end
-- TRP3 toolbar button registration (called from the TRP3 module onStart)
function AltSystem:RegisterTRP3Button()
if TRP3_API and TRP3_API.toolbar then
local toolbarButton = {
id = "altsystem_toolbar_btn",
icon = "INV_Misc_Dice_01",
configText = "AltSystem",
tooltip = "AltSystem",
tooltipSub = "Open the AltSystem rolling window",
onClick = function(_, _, button)
if button == "LeftButton" then
AltSystem:ToggleWindow()
end
end,
visible = 1,
}
TRP3_API.toolbar.toolbarAddButton(toolbarButton)
end
end