Skip to content

Commit

Permalink
feat(setting): added individual options for info statistics in display
Browse files Browse the repository at this point in the history
  • Loading branch information
exochron committed Nov 28, 2023
1 parent ecd691d commit 1df4544
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 91 deletions.
2 changes: 1 addition & 1 deletion DevZone/Roadmap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

### Next
- screenshot generator
- quick filters in display
- black filter inaccurate
- personal favorites rework

Expand Down
17 changes: 11 additions & 6 deletions Settings/Handling.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ local L = ADDON.L
--region setting handler
local callbacks, defaults, uiLabels, behaviourLabels = {}, {}, {}, {}
function ADDON:RegisterUISetting(key, default, label, func, options)
callbacks[key] = function(flag)
ADDON.settings.ui[key] = flag
local isMultiOptions = type(options) == "table" and type(default) == "table"
callbacks[key] = function(flag, subKey, ...)
if isMultiOptions and subKey then
ADDON.settings.ui[key][subKey] = flag
else
ADDON.settings.ui[key] = flag
end
if func then
func(flag)
func(flag, subKey, ...)
end
end
defaults[key] = default
table.insert(uiLabels, { key, label, options })
table.insert(uiLabels, { key, label, options, isMultiOptions })
end
function ADDON:RegisterBehaviourSetting(key, default, label, func)
callbacks[key] = function(flag)
Expand All @@ -24,9 +29,9 @@ function ADDON:RegisterBehaviourSetting(key, default, label, func)
defaults[key] = default
table.insert(behaviourLabels, { key, label })
end
function ADDON:ApplySetting(key, value)
function ADDON:ApplySetting(key, ...)
if callbacks[key] then
callbacks[key](value)
callbacks[key](...)
end
end
function ADDON:ResetSettings()
Expand Down
42 changes: 30 additions & 12 deletions Settings/OptionsPanel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local function BuildCheckBox(parent, text)

return button
end
local function BuildDropDown(parent, text, options)
local function BuildDropDown(parent, text, options, isMultiOptions)
local group = AceGUI:Create("SimpleGroup")
group:SetLayout("Flow")
group:SetFullWidth(true)
Expand All @@ -29,6 +29,7 @@ local function BuildDropDown(parent, text, options)
group:AddChild(label)

local dropdown = AceGUI:Create("Dropdown")
dropdown:SetMultiselect(isMultiOptions)
for option, optionLabel in pairs(options) do
dropdown:AddItem(option, optionLabel)
end
Expand Down Expand Up @@ -61,10 +62,10 @@ local function BuildMainFrame(uiLabels, behaviourLabels)
BuildHeading(frame, UIOPTIONS_MENU)

for _, labelData in ipairs(uiLabels) do
local setting, label, options = labelData[1], labelData[2], labelData[3]
if (ADDON.settings.ui[setting] ~= nil) then
local setting, label, options, isMultiOptions = labelData[1], labelData[2], labelData[3], labelData[4]
if ADDON.settings.ui[setting] ~= nil then
if options then
frame.checks[setting] = BuildDropDown(frame, label, options)
frame.checks[setting] = BuildDropDown(frame, label, options, isMultiOptions)
else
frame.checks[setting] = BuildCheckBox(frame, label)
end
Expand Down Expand Up @@ -163,28 +164,45 @@ ADDON.Events:RegisterCallback("OnLogin", function()
local group = BuildMainFrame(uiLabels, behaviourLabels)
group:SetCallback("refresh", function(frame)
for _, labelData in ipairs(uiLabels) do
local setting = labelData[1]
if (frame.checks[setting]) then
frame.checks[setting]:SetValue(ADDON.settings.ui[setting])
local setting, isMultiOptions = labelData[1], labelData[4]
local check = frame.checks[setting]
if check then
if isMultiOptions then
for subSetting, value in pairs(ADDON.settings.ui[setting]) do
check:SetItemValue(subSetting, value)
end
else
check:SetValue(ADDON.settings.ui[setting])
end
end
end
for _, labelData in ipairs(behaviourLabels) do
local setting = labelData[1]
if (frame.checks[setting]) then
if frame.checks[setting] then
frame.checks[setting]:SetValue(ADDON.settings[setting])
end
end
end)
group:SetCallback("okay", function(frame)
for _, labelData in ipairs(uiLabels) do
local setting = labelData[1]
if (frame.checks[setting]) then
ADDON:ApplySetting(setting, frame.checks[setting]:GetValue())
local setting, isMultiOptions = labelData[1], labelData[4]
if frame.checks[setting] then
if isMultiOptions then
local values = {}
for _, widget in pairs(frame.checks[setting].pullout.items) do
if widget.userdata and widget.userdata.value then
values[widget.userdata.value] = widget:GetValue()
end
end
ADDON:ApplySetting(setting, values)
else
ADDON:ApplySetting(setting, frame.checks[setting]:GetValue())
end
end
end
for _, labelData in ipairs(behaviourLabels) do
local setting = labelData[1]
if (frame.checks[setting]) then
if frame.checks[setting] then
ADDON:ApplySetting(setting, frame.checks[setting]:GetValue())
end
end
Expand Down
10 changes: 9 additions & 1 deletion Settings/Settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ local function PrepareDefaults()
showShopButton = false,
compactMountList = true,
unlockDisplayCamera = true,
showStatistics = true,
displayStatistics = {
CustomizationCount = true,
UsedCount = true,
TravelTime = true,
TravelDistance = true,
LearnedDate = true,
Rarity = true,
Family = true,
},
enableCursorKeys = true,
moveEquipmentSlot = true,
previewButton = true,
Expand Down
160 changes: 93 additions & 67 deletions UI/DisplayStatistics.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@ local ADDON_NAME, ADDON = ...
local tooltip
local LibMountsRarity

ADDON:RegisterUISetting('showStatistics', true, ADDON.L.SETTING_SHOW_STATISTICS, function()
if ADDON.initialized then
MountJournal_UpdateMountDisplay()
do
local options = {
CustomizationCount = ADDON.L["STATS_TIP_CUSTOMIZATION_COUNT_HEAD"],
UsedCount = ADDON.L["STATS_TIP_USAGE_COUNT_HEAD"],
TravelTime = ADDON.L["STATS_TIP_TRAVEL_TIME_HEAD"],
TravelDistance = ADDON.L["STATS_TIP_TRAVEL_DISTANCE_HEAD"],
LearnedDate = ADDON.L["STATS_TIP_LEARNED_DATE_HEAD"],
Rarity = ADDON.L["STATS_TIP_RARITY_HEAD"],
Family = ADDON.L["Family"],
}
if WOW_PROJECT_ID ~= WOW_PROJECT_MAINLINE then
options.Rarity = nil
end
end)
local defaults = CopyTable(options)
for key in pairs(defaults) do
defaults[key] = true
end
ADDON:RegisterUISetting('displayStatistics',
defaults,
ADDON.L.SETTING_SHOW_STATISTICS,
function()
if ADDON.initialized then
MountJournal_UpdateMountDisplay()
end
end,
options
)
end


local function buildStat(container, iconTexture, tooltipHead, tooltipText)
local item = CreateFrame("frame", nil, container)
Expand Down Expand Up @@ -99,6 +123,17 @@ local function setupContainer()
L["Family"]
)

container.Items = {
container.CustomizationCount,
container.UsedCount,
container.TravelTime,
container.TravelDistance,
container.LearnedDate,
container.Rarity,
container.Family1,
container.Family2,
}

container:Hide()

return container
Expand Down Expand Up @@ -167,71 +202,67 @@ local function buildFamilyStrings(mountId)
end

local function updateContainer(mountId, container)
local displayed = {}
local settings = ADDON.settings.ui.displayStatistics

container.CustomizationCount:SetShown(ADDON.DB.Customization[mountId] ~= nil)
if ADDON.DB.Customization[mountId] ~= nil then
container.CustomizationCount:SetShown(settings.CustomizationCount and ADDON.DB.Customization[mountId] ~= nil)
if container.CustomizationCount:IsShown() then
container.CustomizationCount.Text:SetText(parseCustomization(ADDON.DB.Customization[mountId]))
displayed[#displayed + 1] = container.CustomizationCount
end

local useCount, lastUseTime, travelTime, travelDistance, learnedTime = ADDON:GetMountStatistics(mountId)
if useCount ~= nil then
container.UsedCount:SetShown(useCount > 0)
if useCount > 0 then
container.UsedCount.Text:SetText(useCount .. ' x')
displayed[#displayed + 1] = container.UsedCount
end

container.TravelTime:SetShown(travelTime > 0)
if travelTime > 0 then
container.TravelTime.Text:SetText(SecondsToClock(travelTime, true))
displayed[#displayed + 1] = container.TravelTime
end
container.UsedCount:SetShown(settings.UsedCount and useCount > 0)
if container.UsedCount:IsShown() then
container.UsedCount.Text:SetText(useCount .. ' x')
end

container.TravelDistance:SetShown(travelDistance > 0)
if travelDistance > 0 then
container.TravelDistance.Text:SetText(formatDistance(travelDistance))
displayed[#displayed + 1] = container.TravelDistance
end
container.TravelTime:SetShown(settings.TravelTime and travelTime > 0)
if container.TravelTime:IsShown() then
container.TravelTime.Text:SetText(SecondsToClock(travelTime, true))
end

container.LearnedDate:SetShown(learnedTime)
if learnedTime then
local data = date('*t', learnedTime)
container.LearnedDate.Text:SetText(FormatShortDate(data.day, data.month, data.year))
displayed[#displayed + 1] = container.LearnedDate
end
container.TravelDistance:SetShown(settings.TravelDistance and travelDistance > 0)
if container.TravelDistance:IsShown() then
container.TravelDistance.Text:SetText(formatDistance(travelDistance))
end

container.Rarity:SetShown(false)
if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE then
container.LearnedDate:SetShown(settings.LearnedDate and learnedTime ~= nil)
if container.LearnedDate:IsShown() then
local data = date('*t', learnedTime)
container.LearnedDate.Text:SetText(FormatShortDate(data.day, data.month, data.year))
end

container.Rarity:Hide()
if WOW_PROJECT_ID == WOW_PROJECT_MAINLINE and settings.Rarity then
LibMountsRarity = LibMountsRarity or LibStub("MountsRarity-2.0")
local rarity = LibMountsRarity:GetRarityByID(mountId)
if rarity ~= nil then
container.Rarity:SetShown(true)
container.Rarity:Show()
container.Rarity.Text:SetText(string.format("%.2f %%", rarity))
displayed[#displayed + 1] = container.Rarity
end
end

local mountFamily1, mountFamily2 = buildFamilyStrings(mountId)
container.Family1:SetShown(mountFamily1 ~= nil)
if mountFamily1 then
container.Family1.Text:SetText(mountFamily1)
displayed[#displayed + 1] = container.Family1
end
container.Family2:SetShown(mountFamily2 ~= nil)
if mountFamily2 then
container.Family2.Text:SetText(mountFamily2)
displayed[#displayed + 1] = container.Family2
container.Family1:Hide()
container.Family2:Hide()
if settings.Family then
local mountFamily1, mountFamily2 = buildFamilyStrings(mountId)
container.Family1:SetShown(mountFamily1 ~= nil)
if mountFamily1 then
container.Family1.Text:SetText(mountFamily1)
end
container.Family2:SetShown(mountFamily2 ~= nil)
if mountFamily2 then
container.Family2.Text:SetText(mountFamily2)
end
end

if #displayed > 0 then
local maxWidth = MountJournal.MountDisplay:GetWidth() - 52
local rowWidth = 0
local rowLead
local rowCount = 0
for i, stat in ipairs(displayed) do
local maxWidth = MountJournal.MountDisplay:GetWidth() - 52
local rowWidth = 0
local rowLead
local rowPrevious
local rowCount = 0
for _, stat in ipairs(container.Items) do
if stat:IsShown() then
stat:SetWidth(stat.Text:GetUnboundedStringWidth() + 14)
stat:ClearAllPoints()
rowWidth = rowWidth + 7 + stat:GetWidth()
Expand All @@ -240,34 +271,29 @@ local function updateContainer(mountId, container)
rowCount = rowCount + 1
rowLead = stat
else
stat:SetPoint("LEFT", displayed[i - 1], "RIGHT", 7, 0)
stat:SetPoint("LEFT", rowPrevious, "RIGHT", 7, 0)
end
rowLead:SetPoint("TOPLEFT", container, "TOP", -(rowWidth / 2), (rowCount - 1) * -12)
rowPrevious = stat
end
container:SetHeight(12 * rowCount)
end

container:SetShown(#displayed > 0)
container:SetHeight(12 * rowCount)
container:SetShown(rowCount > 0)
end

ADDON.Events:RegisterCallback("loadUI", function()
local container

local callback = function()
if ADDON.settings.ui.showStatistics then
if not container then
container = setupContainer()
end

local selectedMount = ADDON.Api:GetSelected()
if selectedMount then
updateContainer(selectedMount, container)
else
container:Hide();
end
if not container then
container = setupContainer()
end

elseif container then
container:Hide()
local selectedMount = ADDON.Api:GetSelected()
if selectedMount then
updateContainer(selectedMount, container)
else
container:Hide();
end
end
ADDON.Events:RegisterCallback("OnUpdateMountDisplay", callback, ADDON_NAME .. 'DisplayStatistics')
Expand Down
15 changes: 11 additions & 4 deletions UI/SettingsDropDown.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,28 @@ local function InitializeDropDown(self, level)
elseif level == 2 then
local uiLabels, _ = ADDON:GetSettingLabels()
for _, labelData in ipairs(uiLabels) do
local setting, _, options = labelData[1], labelData[2], labelData[3]
local setting, options, isMultiSelect = labelData[1], labelData[3], labelData[4]
if ADDON.settings.ui[setting] ~= nil and options and UIDROPDOWNMENU_MENU_VALUE == setting then
for option, label in pairs(options) do
local button = {
keepShownOnClick = true,
isNotRadio = false,
isNotRadio = isMultiSelect,
hasArrow = false,
text = label,
notCheckable = false,
checked = function()
if isMultiSelect then
return ADDON.settings.ui[setting][option]
end
return ADDON.settings.ui[setting] == option
end,
value = option,
func = function()
ADDON:ApplySetting(setting, option)
func = function(_, _, _, checked)
if isMultiSelect then
ADDON:ApplySetting(setting, checked, option)
else
ADDON:ApplySetting(setting, option)
end
UIDropDownMenu_Refresh(self, nil, level)
end,
}
Expand Down

0 comments on commit 1df4544

Please sign in to comment.