-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refacto: rewrite VisibleIf Feat: manage LogicalOperator #11
Open
mqueb
wants to merge
6
commits into
AtilioA:devel
Choose a base branch
from
mqueb:isVisibleRefacto
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9023e20
Refacto: rewrite VisibleIf
mqueb 71b04ee
better conditionOperator validation
mqueb a60fa68
add isVisible operator
mqueb 7dfcaba
remove debug print
mqueb cba35f9
updateSchema with isVisible
mqueb dbdf80c
move visibility to IMGUIVisibilityManager
mqueb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
Mod Configuration Menu/Mods/BG3MCM/ScriptExtender/Lua/Client/IMGUIVisibilityManager.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
---@class IMGUIVisibilityManager: MetaClass | ||
IMGUIVisibilityManager = _Class:Create("IMGUIVisibilityManager", nil, { | ||
visibilityTriggers = {}, | ||
uiElementByName = {}, | ||
operators = { | ||
["=="] = function(a, b) return a == b end, | ||
["!="] = function(a, b) return a ~= b end, | ||
["<="] = function(a, b) return a <= b end, | ||
[">="] = function(a, b) return a >= b end, | ||
["<"] = function(a, b) return a < b end, | ||
[">"] = function(a, b) return a > b end, | ||
["isVisible"] = function(a, b) return a == b end | ||
} | ||
}) | ||
|
||
|
||
--- | ||
function IMGUIVisibilityManager:manageVisibleIf(modGUID, elementInfo, uiElement) | ||
if elementInfo.VisibleIf and elementInfo.VisibleIf.Conditions then | ||
if self:EvaluateVisibleIf(modGUID, elementInfo.VisibleIf, elementInfo.Name or elementInfo.SectionName or elementInfo.TabName ) then -- change for generic Name ... | ||
self.visibilityTriggers[modGUID] = self.visibilityTriggers[modGUID] or {} | ||
self.visibilityTriggers[modGUID][uiElement] = elementInfo.VisibleIf | ||
end | ||
end | ||
|
||
self.uiElementByName[modGUID] = self.uiElementByName[modGUID] or {} | ||
self.uiElementByName[modGUID][elementInfo.TabId or elementInfo.SectionId or elementInfo.Id] = uiElement -- change for generic Id ... | ||
end | ||
|
||
|
||
function IMGUIVisibilityManager:EvaluateVisibleIf(modGUID, visibleIf, elementName) | ||
local valid = true | ||
-- control LogicalOperator is valid if present | ||
if visibleIf.LogicalOperator ~= nil and visibleIf.LogicalOperator ~= "or" and visibleIf.LogicalOperator ~= "and" then | ||
MCMWarn(0, | ||
"Invalid LogicalOperator passed by mod '" .. | ||
Ext.Mod.GetMod(modGUID).Info.Name .. | ||
"' for visibility condition of '"..elementName.."'. Please contact " .. | ||
Ext.Mod.GetMod(modGUID).Info.Author .. " about this issue.") | ||
valid = false | ||
end | ||
|
||
-- Control Conditions | ||
for _, condition in ipairs(visibleIf.Conditions) do | ||
if condition.SettingId == nil then | ||
MCMWarn(0, | ||
"Invalid condition (no settingId) passed by mod '" .. | ||
Ext.Mod.GetMod(modGUID).Info.Name .. | ||
"' for visibility condition of '"..elementName.."'. Please contact " .. | ||
Ext.Mod.GetMod(modGUID).Info.Author .. " about this issue.") | ||
valid = false | ||
end | ||
if condition.Operator == nil then | ||
MCMWarn(0, | ||
"Invalid condition (no operator) passed by mod '" .. | ||
Ext.Mod.GetMod(modGUID).Info.Name .. | ||
"' for visibility condition of '"..elementName.."'. Please contact " .. | ||
Ext.Mod.GetMod(modGUID).Info.Author .. " about this issue.") | ||
valid = false | ||
else | ||
if self.operators[condition.Operator] == nil then | ||
MCMWarn(0, | ||
"Invalid condition (invalid operator: "..condition.Operator..") passed by mod '" .. | ||
Ext.Mod.GetMod(modGUID).Info.Name .. | ||
"' for visibility condition of '"..elementName.."'. Please contact " .. | ||
Ext.Mod.GetMod(modGUID).Info.Author .. " about this issue.") | ||
valid = false | ||
end | ||
end | ||
if condition.ExpectedValue == nil then | ||
MCMWarn(0, | ||
"Invalid condition (no triggerValue) passed by mod '" .. | ||
Ext.Mod.GetMod(modGUID).Info.Name .. | ||
"' for visibility condition of '"..elementName.."'. Please contact " .. | ||
Ext.Mod.GetMod(modGUID).Info.Author .. " about this issue.") | ||
valid = false | ||
end | ||
end | ||
|
||
return valid | ||
end | ||
|
||
function IMGUIVisibilityManager:UpdateAllVisibility() | ||
for _, modGUID in ipairs(self.visibilityTriggers) do | ||
for _, uiElement in pairs(self.visibilityTriggers[modGUID]) do | ||
IMGUIVisibilityManager:UpdateVisibilityOfUiElement(modGUID, uiElement, false) | ||
end | ||
end | ||
end | ||
|
||
|
||
-- TODO: this should be refactored to use OOP or at least be more modular, however I've wasted too much time on this already with Lua's nonsense, so I'm stashing and leaving it as is | ||
function IMGUIVisibilityManager:UpdateVisibility_SettingChanged(modGUID, settingId) | ||
if not modGUID or not settingId then | ||
return | ||
end | ||
|
||
local visibilityTriggers = self.visibilityTriggers[modGUID] or {} | ||
for uiElement, visibleIf in pairs(visibilityTriggers) do | ||
for _, condition in ipairs(visibleIf.Conditions) do | ||
if condition.SettingId == settingId then | ||
self:UpdateVisibilityOfUiElement(modGUID, uiElement, true) | ||
break | ||
end | ||
end | ||
end | ||
end | ||
|
||
function IMGUIVisibilityManager:UpdateVisibility_GroupVisibilityChanged(modGUID, uiElementChanged) | ||
local visibilityTriggers = self.visibilityTriggers[modGUID] or {} | ||
for uiElement, visibleIf in pairs(visibilityTriggers) do | ||
for _, condition in ipairs(visibleIf.Conditions) do | ||
if condition.Operator == "isVisible" and self.uiElementByName[modGUID][condition.SettingId] == uiElementChanged then | ||
self:UpdateVisibilityOfUiElement(modGUID, uiElement, true) | ||
break | ||
end | ||
end | ||
end | ||
end | ||
|
||
|
||
function IMGUIVisibilityManager:UpdateVisibilityOfUiElement(modGUID, uiElement, transmitVisibilityChanged) | ||
local visibilityTriggers = self.visibilityTriggers[modGUID] or {} | ||
local visibleIf = visibilityTriggers[uiElement] | ||
local logicalOperator = visibleIf.LogicalOperator or "and" | ||
local visible = true | ||
if logicalOperator == "or" then | ||
visible = false | ||
end | ||
|
||
for _, condition in ipairs(visibleIf.Conditions) do | ||
local settingIdTriggering = condition.SettingId | ||
local operator = condition.Operator | ||
local triggerValue = condition.ExpectedValue | ||
|
||
local value = nil | ||
if operator == "isVisible" then | ||
value = self.uiElementByName[modGUID][settingIdTriggering].Visible | ||
else | ||
value = MCMClientState:GetClientStateValue(settingIdTriggering, modGUID) | ||
end | ||
|
||
local strValue, strTrigger = tostring(value), tostring(triggerValue) | ||
local numValue, numTrigger = tonumber(value), tonumber(triggerValue) | ||
|
||
local v = nil | ||
if operator == "==" or operator == "!=" or operator == "isVisible" then | ||
v = self.operators[operator](strValue, strTrigger) | ||
elseif numValue ~= nil and numTrigger ~= nil then | ||
v = self.operators[operator](numValue, numTrigger) | ||
else | ||
MCMWarn(0, | ||
"Something go wrong, contact MCM support.") -- should never happen (trigegr is not empty at that point, value from widget too) | ||
v = false | ||
end | ||
|
||
if v and (logicalOperator == "or") then | ||
visible = true | ||
break | ||
elseif (not v) and (logicalOperator == "and") then | ||
visible = false | ||
break | ||
end | ||
end | ||
local visibilityChanged = (uiElement.Visible ~= visible) | ||
if transmitVisibilityChanged and visibilityChanged then | ||
uiElement.Visible = visible | ||
self:UpdateVisibility_GroupVisibilityChanged(modGUID, uiElement) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be local function in
manageVisibleIf