Skip to content

Commit 695c7d0

Browse files
committed
Fix button compatibility layer - don't override Chili Button globally
The previous implementation globally overrode Button and TabbedPanelButton, breaking all Chili UI code. This caused crashes in links.lua trying to call AutoDispose() on the fake Button object. New approach: - DON'T override Button or TabbedPanelButton globally - Keep Chili buttons working normally - During Editor:_FinalizeRmlUi(), convert Chili buttons to RmlUi buttons - This happens only when an editor finalizes in RmlUi mode Changes: - rmlui_field_compat.lua: - Remove global Button and TabbedPanelButton overrides - Keep RmlUiButton and RmlUiTabbedPanelButton as conversion targets - Updated RmlUiTabbedPanelButton to accept caption/image directly - editor.lua: - Add ConvertChiliButtonToRmlUi() helper function - Detect Chili TabbedPanelButton by SetPressedState + children - Detect Chili Button by caption + OnClick - Extract caption/image from Chili button children - Convert during _FinalizeRmlUi() instead of at creation time - Handle buttons in field.ctrl.children for AddControl() buttons Now editors create Chili buttons as normal, and they get converted to RmlUi buttons only when rendering in RmlUi mode. Chili UI remains intact.
1 parent 3574790 commit 695c7d0

File tree

2 files changed

+81
-51
lines changed

2 files changed

+81
-51
lines changed

scen_edit/view/editor.lua

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -724,23 +724,70 @@ end
724724
-- We load these fields last as they might be/contain subclasses of editor view
725725
SB.IncludeDir(Path.Join(SB.DIRS.SRC, 'view/fields'))
726726

727+
-- Helper function to convert Chili button to RmlUi button
728+
local function ConvertChiliButtonToRmlUi(chiliButton)
729+
if not chiliButton then
730+
return nil
731+
end
732+
733+
-- Check if it's a TabbedPanelButton (has SetPressedState and children with images/labels)
734+
if chiliButton.SetPressedState and chiliButton.children then
735+
-- Extract caption and image from children
736+
local caption = ""
737+
local image = nil
738+
for _, child in pairs(chiliButton.children) do
739+
if type(child) == "table" then
740+
if child.classname == "label" and child.caption then
741+
caption = child.caption
742+
elseif child.classname == "image" and child.file then
743+
image = child.file
744+
end
745+
end
746+
end
747+
748+
-- Create RmlUi TabbedPanelButton
749+
return RmlUiTabbedPanelButton({
750+
x = chiliButton.x,
751+
y = chiliButton.y,
752+
tooltip = chiliButton.tooltip or "",
753+
OnClick = chiliButton.OnClick or {},
754+
caption = caption,
755+
image = image,
756+
})
757+
758+
-- Check if it's a regular Button (has caption and OnClick)
759+
elseif chiliButton.caption and chiliButton.OnClick then
760+
return RmlUiButton({
761+
caption = chiliButton.caption,
762+
OnClick = chiliButton.OnClick or {},
763+
width = chiliButton.width,
764+
height = chiliButton.height,
765+
tooltip = chiliButton.tooltip,
766+
})
767+
end
768+
769+
return nil
770+
end
771+
727772
-- RmlUi-specific finalization
728773
function Editor:_FinalizeRmlUi(children, opts)
729774
children = children or {}
730775
opts = opts or {}
731776

732-
-- Extract action buttons from children (TabbedPanelButton instances)
777+
-- Convert Chili buttons to RmlUi buttons
733778
self.actionButtons = {}
734779
self.regularButtons = {}
735780

736781
for _, child in ipairs(children) do
737782
if child and type(child) == "table" then
738-
-- Check if it's an RmlUi button
739-
if child.GenerateRml and child.OnClick then
740-
if child.id and child.id:match("^action%-btn%-") then
741-
table.insert(self.actionButtons, child)
742-
elseif child.id and child.id:match("^btn%-") then
743-
table.insert(self.regularButtons, child)
783+
local rmlButton = ConvertChiliButtonToRmlUi(child)
784+
if rmlButton then
785+
-- TabbedPanelButtons become action buttons
786+
if rmlButton.id and rmlButton.id:match("^action%-btn%-") then
787+
table.insert(self.actionButtons, rmlButton)
788+
-- Regular buttons
789+
elseif rmlButton.id and rmlButton.id:match("^btn%-") then
790+
table.insert(self.regularButtons, rmlButton)
744791
end
745792
end
746793
end
@@ -765,11 +812,27 @@ function Editor:_FinalizeRmlUi(children, opts)
765812
if field.GenerateRml then
766813
fieldsHtml = fieldsHtml .. field:GenerateRml()
767814
else
768-
-- Check if it's a control with a button
769-
if field.ctrl and field.ctrl.GenerateRml then
770-
fieldsHtml = fieldsHtml .. field.ctrl:GenerateRml()
815+
-- Check if it's a control with a button inside
816+
if field.ctrl then
817+
-- Try to convert the ctrl's children to buttons
818+
local ctrlHtml = ''
819+
if field.ctrl.children then
820+
for _, btnChild in pairs(field.ctrl.children) do
821+
local rmlBtn = ConvertChiliButtonToRmlUi(btnChild)
822+
if rmlBtn then
823+
ctrlHtml = ctrlHtml .. rmlBtn:GenerateRml()
824+
table.insert(self.regularButtons, rmlBtn)
825+
end
826+
end
827+
end
828+
if ctrlHtml ~= '' then
829+
fieldsHtml = fieldsHtml .. ctrlHtml
830+
else
831+
-- Fallback for other controls
832+
fieldsHtml = fieldsHtml .. '<div class="field-separator"></div>'
833+
end
771834
else
772-
-- Fallback for other controls
835+
-- Fallback for fields without ctrl
773836
fieldsHtml = fieldsHtml .. '<div class="field-separator"></div>'
774837
end
775838
end

scen_edit/view/rmlui_field_compat.lua

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ AssetPickerWindow = RmlUiAssetPickerWindow
4141
ColorPickerWindow = RmlUiColorPickerWindow
4242
MaterialPickerWindow = RmlUiMaterialPickerWindow
4343

44-
-- Button Compatibility Layer
45-
-- Create button objects that can generate RML
44+
-- Button classes for RmlUi mode
45+
-- These are created by Editor:_FinalizeRmlUi() when converting Chili buttons to RmlUi
4646

4747
-- Counter for auto-generating button IDs
4848
local _BUTTON_INDEX = 0
4949

50-
-- RmlUi Button (used for regular Button:New{} calls via AddControl)
50+
-- RmlUi Button (converted from Chili Button:New{} during finalization)
5151
RmlUiButton = LCS.class{}
5252

5353
function RmlUiButton:init(opts)
@@ -67,15 +67,7 @@ function RmlUiButton:GenerateRml()
6767
)
6868
end
6969

70-
-- Override Button table for RmlUi mode
71-
Button = {
72-
New = function(opts)
73-
return RmlUiButton(opts)
74-
end
75-
}
76-
77-
-- TabbedPanelButton for action buttons (e.g., "Add", "Set", "Smooth")
78-
-- These store their caption, image, tooltip and click handler
70+
-- RmlUi TabbedPanelButton (converted from Chili TabbedPanelButton during finalization)
7971
RmlUiTabbedPanelButton = LCS.class{}
8072

8173
function RmlUiTabbedPanelButton:init(opts)
@@ -86,19 +78,9 @@ function RmlUiTabbedPanelButton:init(opts)
8678
self.tooltip = opts.tooltip or ""
8779
self.OnClick = opts.OnClick or {}
8880
self.pressed = false
89-
90-
-- Extract caption and image from children
91-
self.caption = ""
92-
self.image = nil
93-
if opts.children then
94-
for _, child in ipairs(opts.children) do
95-
if child.caption then
96-
self.caption = child.caption
97-
elseif child.file then
98-
self.image = child.file
99-
end
100-
end
101-
end
81+
self.caption = opts.caption or ""
82+
self.image = opts.image
83+
self.children = opts.children
10284
end
10385

10486
function RmlUiTabbedPanelButton:SetPressedState(pressed)
@@ -129,19 +111,4 @@ function RmlUiTabbedPanelButton:GenerateRml()
129111
return html
130112
end
131113

132-
-- Helper functions to match Chili API
133-
function TabbedPanelButton(opts)
134-
return RmlUiTabbedPanelButton(opts)
135-
end
136-
137-
function TabbedPanelImage(opts)
138-
-- Just return the opts, they'll be extracted by TabbedPanelButton
139-
return opts
140-
end
141-
142-
function TabbedPanelLabel(opts)
143-
-- Just return the opts, they'll be extracted by TabbedPanelButton
144-
return opts
145-
end
146-
147114
Log.Notice("RmlUi field compatibility layer loaded - original API maintained")

0 commit comments

Comments
 (0)