Skip to content

Commit

Permalink
Fix default state validation for lists and color labels properly
Browse files Browse the repository at this point in the history
- Fix GetDefaultState to properly handle lists (this also saves saving
  of bunch of default list values to build XMLs)
- Fix control state checks for non-boolean type config options
- Fix coloring of config controls with separate labels
- Improve GetDefaultState performance

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
  • Loading branch information
deathbeam committed Feb 27, 2023
1 parent 7127369 commit 347e750
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/Classes/ConfigTab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ local ConfigTabClass = newClass("ConfigTab", "UndoHandler", "ControlHost", "Cont

self.input = { }
self.placeholder = { }
self.defaultState = { }

self.enemyLevel = 1

Expand Down Expand Up @@ -236,8 +237,10 @@ local ConfigTabClass = newClass("ConfigTab", "UndoHandler", "ControlHost", "Cont
if varData.tooltipFunc then
control.tooltipFunc = varData.tooltipFunc
end
local labelControl = control
if varData.label and varData.type ~= "check" then
t_insert(self.controls, new("LabelControl", {"RIGHT",control,"LEFT"}, -4, 0, 0, DrawStringWidth(14, "VAR", varData.label) > 228 and 12 or 14, "^7"..varData.label))
labelControl = new("LabelControl", {"RIGHT",control,"LEFT"}, -4, 0, 0, DrawStringWidth(14, "VAR", varData.label) > 228 and 12 or 14, "^7"..varData.label)
t_insert(self.controls, labelControl)
end
if varData.var then
self.input[varData.var] = varData.defaultState
Expand All @@ -249,19 +252,34 @@ local ConfigTabClass = newClass("ConfigTab", "UndoHandler", "ControlHost", "Cont
self.input[varData.var] = varData.list[varData.defaultIndex].val
control.selIndex = varData.defaultIndex
end
if varData.type == "check" then
self.defaultState[varData.var] = varData.defaultState or false
elseif varData.type == "count" or varData.type == "integer" or varData.type == "countAllowZero" then
self.defaultState[varData.var] = varData.defaultState or 0
elseif varData.type == "list" then
self.defaultState[varData.var] = varData.list[varData.defaultIndex or 1].val
elseif varData.type == "text" then
self.defaultState[varData.var] = varData.defaultState or ""
else
self.defaultState[varData.var] = varData.defaultState
end
end

if not varData.hideIfInvalid then
local innerShown = control.shown
control.shown = function()
local shown = type(innerShown) == "boolean" and innerShown or innerShown()
return not shown and control.state ~= self:GetDefaultState(varData.var, type(control.state)) or shown
local cur = self.input[varData.var]
local def = self:GetDefaultState(varData.var, type(cur))
return not shown and cur ~= nil and cur ~= def or shown
end
local innerLabel = control.label
control.label = function()
local innerLabel = labelControl.label
labelControl.label = function()
local shown = type(innerShown) == "boolean" and innerShown or innerShown()
if not shown and control.state ~= self:GetDefaultState(varData.var, type(control.state)) then
return "^1"..innerLabel
local cur = self.input[varData.var]
local def = self:GetDefaultState(varData.var, type(cur))
if not shown and cur ~= nil and cur ~= def then
return "^1"..StripEscapes(innerLabel)
end
return innerLabel
end
Expand All @@ -279,7 +297,9 @@ local ConfigTabClass = newClass("ConfigTab", "UndoHandler", "ControlHost", "Cont
end

local shown = type(innerShown) == "boolean" and innerShown or innerShown()
if not shown and control.state ~= self:GetDefaultState(varData.var, type(control.state)) then
local cur = self.input[varData.var]
local def = self:GetDefaultState(varData.var, type(cur))
if not shown and cur ~= nil and cur ~= def then
tooltip:AddLine(14, "^1This config option is conditional with missing source and is invalid.")
end
end
Expand Down Expand Up @@ -339,21 +359,16 @@ function ConfigTabClass:GetDefaultState(var, varType)
return self.placeholder[var]
end

for i = 1, #varList do
if varList[i].var == var then
if varType == "number" then
return varList[i].defaultState or 0
elseif varType == "boolean" then
return varList[i].defaultState == true
else
return varList[i].defaultState
end
end
if self.defaultState[var] ~= nil then
return self.defaultState[var]
end

if varType == "number" then
return 0
elseif varType == "boolean" then
return false
elseif varType == "string" then
return ""
else
return nil
end
Expand Down

0 comments on commit 347e750

Please sign in to comment.