-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.lua
More file actions
141 lines (116 loc) · 3.87 KB
/
Settings.lua
File metadata and controls
141 lines (116 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
------------------------------------------
-- Settings Module
------------------------------------------
local MageService = MAGESERVICE
local Settings = {}
MageService.Settings = Settings
------------------------------------------
-- Default Settings
------------------------------------------
local defaultSettings = {
addonEnabled = false,
containerUIVisible = true,
containerUIPosition = {
point = "CENTER",
relativePoint = "CENTER",
xOfs = 0,
yOfs = 100
}
}
-- Table to store the character-specific settings
local characterSettings = {}
------------------------------------------
-- Saved Variables
------------------------------------------
-- Variables that will be saved between sessions
-- Will be initialized when ADDON_LOADED fires
------------------------------------------
-- Settings Functions
------------------------------------------
-- Initialize settings
function Settings.Initialize()
-- Create the saved variables if they don't exist
if MageServiceDB == nil then
MageServiceDB = {}
end
-- Get character name and realm for unique identification
local playerName = UnitName("player")
local realmName = GetRealmName()
local fullName = playerName .. "-" .. realmName
-- Initialize character settings if they don't exist
if MageServiceDB[fullName] == nil then
MageServiceDB[fullName] = {}
-- Copy default settings
for key, value in pairs(defaultSettings) do
MageServiceDB[fullName][key] = value
end
end
-- Store reference to this character's settings
characterSettings = MageServiceDB[fullName]
end
-- Get a setting value
function Settings.GetSetting(key)
-- Return default if character settings not initialized
if not characterSettings or characterSettings[key] == nil then
return defaultSettings[key]
end
return characterSettings[key]
end
-- Set a setting value
function Settings.SetSetting(key, value)
-- Ensure we have settings to write to
if not characterSettings then
Settings.Initialize()
end
-- Update the value
characterSettings[key] = value
-- Returning the value allows for method chaining
return value
end
-- Toggle a boolean setting
function Settings.ToggleSetting(key)
local currentValue = Settings.GetSetting(key)
-- Only toggle if it's a boolean value
if type(currentValue) == "boolean" then
return Settings.SetSetting(key, not currentValue)
end
return currentValue
end
------------------------------------------
-- Convenience Functions for Common Settings
------------------------------------------
-- Check if the addon is enabled
function Settings.IsAddonEnabled()
return Settings.GetSetting("addonEnabled")
end
-- Set the addon enabled state
function Settings.SetAddonEnabled(enabled)
return Settings.SetSetting("addonEnabled", enabled)
end
-- Toggle the addon enabled state
function Settings.ToggleAddonEnabled()
return Settings.ToggleSetting("addonEnabled")
end
-- Functions for Container UI settings
function Settings.IsContainerUIVisible()
return Settings.GetSetting("containerUIVisible")
end
function Settings.SetContainerUIVisible(visible)
return Settings.SetSetting("containerUIVisible", visible)
end
function Settings.GetContainerUIPosition()
return Settings.GetSetting("containerUIPosition")
end
function Settings.SetContainerUIPosition(positionTable)
return Settings.SetSetting("containerUIPosition", positionTable)
end
-- Handler for the ADDON_LOADED event
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:SetScript("OnEvent", function(self, event, addonName)
if event == "ADDON_LOADED" and addonName == "MageService" then
Settings.Initialize()
-- We only need to handle this event once
self:UnregisterEvent("ADDON_LOADED")
end
end)