Skip to content

Commit 8377803

Browse files
committed
Fix AddControl to be mode-aware and prevent Chili/RmlUi mixing
- _AddControl now returns RmlUi placeholder in RmlUi mode instead of creating Chili controls - Updated _FinalizeRmlUi and _FinalizeRmlUiNew to handle RmlUi placeholders - Added type check to ConvertChiliButtonToRmlUi to handle non-table values - Fixes crash when EditorButton used in AddControl (RmlUi buttons being added to Chili controls)
1 parent 8595402 commit 8377803

File tree

1 file changed

+49
-18
lines changed

1 file changed

+49
-18
lines changed

scen_edit/view/editor.lua

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -473,12 +473,20 @@ function Editor:AddControl(name, children)
473473
end
474474

475475
function Editor:_AddControl(name, children)
476+
-- In RmlUi mode, don't create Chili controls
477+
if SB.view and SB.view.useRmlUi then
478+
-- Just store the children for later use in _FinalizeRmlUi
479+
table.insert(self.fieldOrder, name)
480+
-- Return a dummy object that holds the children
481+
return { children = children, isRmlUiPlaceholder = true }
482+
end
483+
484+
-- Chili mode - create actual Control
476485
local ctrl = Control:New {
477486
autosize = true,
478487
padding = {0, 0, 0, 0},
479488
children = children
480489
}
481-
-- Only add to stackPanel in Chili mode
482490
if self.stackPanel then
483491
self.stackPanel:AddChild(ctrl)
484492
end
@@ -754,7 +762,7 @@ SB.IncludeDir(Path.Join(SB.DIRS.SRC, 'view/fields'))
754762

755763
-- Helper function to convert Chili button to RmlUi button
756764
local function ConvertChiliButtonToRmlUi(chiliButton)
757-
if not chiliButton then
765+
if not chiliButton or type(chiliButton) ~= "table" then
758766
return nil
759767
end
760768

@@ -842,22 +850,33 @@ function Editor:_FinalizeRmlUi(children, opts)
842850
else
843851
-- Check if it's a control with a button inside
844852
if field.ctrl then
845-
-- Try to convert the ctrl's children to buttons
846-
local ctrlHtml = ''
847-
if field.ctrl.children then
848-
for _, btnChild in pairs(field.ctrl.children) do
849-
local rmlBtn = ConvertChiliButtonToRmlUi(btnChild)
850-
if rmlBtn then
851-
ctrlHtml = ctrlHtml .. rmlBtn:GenerateRml()
853+
-- Check if this is an RmlUi placeholder (from new EditorButton API)
854+
if field.ctrl.isRmlUiPlaceholder and field.ctrl.children then
855+
-- Children are already RmlUi buttons, use them directly
856+
for _, rmlBtn in ipairs(field.ctrl.children) do
857+
if rmlBtn and rmlBtn.GenerateRml then
858+
fieldsHtml = fieldsHtml .. rmlBtn:GenerateRml()
852859
table.insert(self.regularButtons, rmlBtn)
853860
end
854861
end
855-
end
856-
if ctrlHtml ~= '' then
857-
fieldsHtml = fieldsHtml .. ctrlHtml
858862
else
859-
-- Fallback for other controls
860-
fieldsHtml = fieldsHtml .. '<div class="field-separator"></div>'
863+
-- Old path: try to convert Chili buttons to RmlUi buttons
864+
local ctrlHtml = ''
865+
if field.ctrl.children then
866+
for _, btnChild in pairs(field.ctrl.children) do
867+
local rmlBtn = ConvertChiliButtonToRmlUi(btnChild)
868+
if rmlBtn then
869+
ctrlHtml = ctrlHtml .. rmlBtn:GenerateRml()
870+
table.insert(self.regularButtons, rmlBtn)
871+
end
872+
end
873+
end
874+
if ctrlHtml ~= '' then
875+
fieldsHtml = fieldsHtml .. ctrlHtml
876+
else
877+
-- Fallback for other controls
878+
fieldsHtml = fieldsHtml .. '<div class="field-separator"></div>'
879+
end
861880
end
862881
else
863882
-- Fallback for fields without ctrl
@@ -897,10 +916,22 @@ function Editor:_FinalizeRmlUiNew(layout, opts)
897916
if field.GenerateRml then
898917
fieldsHtml = fieldsHtml .. field:GenerateRml()
899918
else
900-
-- Check if it's a control with a button inside
901-
if field.ctrl and field.ctrl.GenerateRml then
902-
fieldsHtml = fieldsHtml .. field.ctrl:GenerateRml()
903-
table.insert(self.regularButtons, field.ctrl)
919+
-- Check if it's a control with buttons inside
920+
if field.ctrl then
921+
-- Check if this is an RmlUi placeholder (from new EditorButton API)
922+
if field.ctrl.isRmlUiPlaceholder and field.ctrl.children then
923+
-- Children are already RmlUi buttons, use them directly
924+
for _, rmlBtn in ipairs(field.ctrl.children) do
925+
if rmlBtn and rmlBtn.GenerateRml then
926+
fieldsHtml = fieldsHtml .. rmlBtn:GenerateRml()
927+
table.insert(self.regularButtons, rmlBtn)
928+
end
929+
end
930+
elseif field.ctrl.GenerateRml then
931+
-- Single button with GenerateRml method
932+
fieldsHtml = fieldsHtml .. field.ctrl:GenerateRml()
933+
table.insert(self.regularButtons, field.ctrl)
934+
end
904935
end
905936
end
906937
end

0 commit comments

Comments
 (0)