Skip to content

Commit

Permalink
Fix #4, handle deletes and additions of new layouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mctalian committed Aug 18, 2024
1 parent db5bbe1 commit d7af290
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 34 deletions.
1 change: 1 addition & 0 deletions .pkgmeta
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ externals:
Libs/AceGUI-3.0: https://repos.wowace.com/wow/ace3/trunk/AceGUI-3.0
Libs/AceConfig-3.0: https://repos.wowace.com/wow/ace3/trunk/AceConfig-3.0
Libs/AceTimer-3.0: https://repos.wowace.com/wow/ace3/trunk/AceTimer-3.0
Libs/AceHook-3.0: https://repos.wowace.com/wow/ace3/trunk/AceHook-3.0

ignore:
- README.md
67 changes: 33 additions & 34 deletions Core.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local addonName = "DeviceLayoutPreset"
DLP = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0")
DLP = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "AceHook-3.0")

local options = {
name = addonName,
Expand All @@ -25,6 +25,9 @@ local options = {
type = "select",
name = "Preset to Load",
desc = "The Edit Mode preset to load when logging in on this device.",
values = function()
return DLP:GetLayouts()
end,
get = "GetPreset",
set = "SetPreset",
order = -1
Expand All @@ -51,55 +54,51 @@ function DLP:OnInitialize()
self.db.global.migratedFromProfile = true
end
LibStub("AceConfig-3.0"):RegisterOptionsTable(addonName, options)
self.initialized = false
self:RegisterEvent("PLAYER_ENTERING_WORLD")
self:InitializeOptions()
self:RegisterEvent("EDIT_MODE_LAYOUTS_UPDATED")
self:SecureHook(C_EditMode, "OnLayoutDeleted", OnLayoutDeleted)
self:SecureHook(C_EditMode, "OnLayoutAdded", OnLayoutAdded)
self:RegisterChatCommand("dlp", "SlashCommand")
self:RegisterChatCommand("devicelayoutpreset", "SlashCommand")
self:RegisterChatCommand("deviceLayoutPreset", "SlashCommand")
end

function DLP:PLAYER_ENTERING_WORLD(event, isLogin, isReload)
if not self.initialized then
self:CheckForEditMode()
function DLP:EDIT_MODE_LAYOUTS_UPDATED(_, layoutInfo)
local desired = self.db.global.presetIndexOnLogin
layouts = EditModeManagerFrame:GetLayouts()
if layoutInfo.activeLayout == desired then
self:Print("Have a fun session!")
elseif desired <= 0 or desired > table.getn(layouts) then
self:Print("Visit the addon options (/dlp) to select the Edit Mode preset for this device.")
self.db.global.presetIndexOnLogin = 0
else
EditModeManagerFrame:SelectLayout(desired)
self:Print("Successfully loaded your device layout: \"" .. layouts[desired].layoutName .. "\" - Have a fun session!")
end
self:UnregisterEvent("EDIT_MODE_LAYOUTS_UPDATED")
end

local attempts = 0
function DLP:CheckForEditMode()
if EditModeManagerFrame and EditModeManagerFrame.GetLayouts and EditModeManagerFrame:CanEnterEditMode() then
self:PopulateOptions()
else
if attempts <= 30 then
attempts = attempts + 1
-- Keep checking until it's available
self:ScheduleTimer("CheckForEditMode", 1)
else
self:Print("EditModeManagerFrame:GetLayouts was unavailable for > 30 seconds, could not retrieve list of layours")
self:Print("Please report this issue @ github: McTalian/DeviceLayoutPreset")
end
function DLP:OnLayoutDeleted(deletedIndex)
if deletedIndex == self.db.global.presetIndexOnLogin then
self:Print("Visit the addon options (/dlp) to select a new preset for this device.")
self.db.global.presetIndexOnLogin = 0
end
end

function DLP:PopulateOptions()
function DLP:OnLayoutAdded()
self:Print("New layout detected! Visit the addon options (/dlp) to change your preset to your new layout.")
end

function DLP:GetLayouts()
layouts = EditModeManagerFrame:GetLayouts()
self:InitializeOptions()
if self.db.global.presetIndexOnLogin == 0 or self.db.global.presetIndexOnLogin > table.getn(layouts) then
self:Print("Visit the addon options (/dlp) to select the Edit Mode preset for this device")
self.db.global.presetIndexOnLogin = 0
else
self:Print("Configured to select Edit Mode preset \"" .. layouts[self.db.global.presetIndexOnLogin].layoutName .. "\" on this device")
end
if self.db.global.presetIndexOnLogin > 0 then
EditModeManagerFrame:SelectLayout(self.db.global.presetIndexOnLogin)
local values = {}
for i, l in ipairs(layouts) do
values[i] = l.layoutName
end
self.initialized = true
return values
end

function DLP:InitializeOptions()
options.args.preset.values = {}
for i, l in ipairs(layouts) do
options.args.preset.values[i] = l.layoutName
end
if self.optionsFrame == nil then
self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions(addonName, addonName)
end
Expand Down
1 change: 1 addition & 0 deletions embeds.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<Include file="Libs\AceGUI-3.0\AceGUI-3.0.xml"/>
<Include file="Libs\AceConfig-3.0\AceConfig-3.0.xml"/>
<Include file="Libs\AceTimer-3.0\AceTimer-3.0.xml"/>
<Include file="Libs\AceHook-3.0\AceHook-3.0.xml"/>
</Ui>

0 comments on commit d7af290

Please sign in to comment.