Skip to content

Commit f4de1e1

Browse files
committed
Add GetCaption to RmlUiChoiceField and on-demand editor creation
1. Added GetCaption() method to RmlUiChoiceField - Matches Chili ChoiceField API - Returns caption for given id (or current value) - Also added captions support to init 2. Added on-demand editor creation in OpenEditor() - If editor doesn't exist when clicked, create it immediately - Handles race condition when user clicks before SB.delay executes - Falls back to registry if editor not found Fixes: - attempt to call method 'GetCaption' (a nil value) in collision_window.lua:353 - Editor not yet created: waterEditor (still initializing?)
1 parent a803d10 commit f4de1e1

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

scen_edit/view/rmlui_fields.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,15 @@ RmlUiChoiceField = RmlUiField:extends{}
113113
function RmlUiChoiceField:init(opts)
114114
self:super("init", opts)
115115
self.items = opts.items or {}
116+
self.captions = opts.captions or self.items -- Use items as captions if not provided
116117
end
117118

118119
function RmlUiChoiceField:GenerateRml()
119120
local options = ""
120-
for _, item in ipairs(self.items) do
121-
local selected = (item == self.value) and ' selected="selected"' or ''
122-
options = options .. string.format('<option value="%s"%s>%s</option>', item, selected, item)
121+
for i, item in ipairs(self.items) do
122+
local caption = self.captions[i] or tostring(item)
123+
local selected = (tostring(item) == tostring(self.value)) and ' selected="selected"' or ''
124+
options = options .. string.format('<option value="%s"%s>%s</option>', tostring(item), selected, caption)
123125
end
124126

125127
return string.format(
@@ -128,6 +130,17 @@ function RmlUiChoiceField:GenerateRml()
128130
)
129131
end
130132

133+
function RmlUiChoiceField:GetCaption(id)
134+
id = id or self.value
135+
-- Find index of id in items
136+
for i, item in ipairs(self.items) do
137+
if tostring(item) == tostring(id) then
138+
return self.captions[i]
139+
end
140+
end
141+
return nil
142+
end
143+
131144
-- Color Field
132145
RmlUiColorField = RmlUiField:extends{}
133146

scen_edit/view/view.lua

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,19 @@ end
346346
function View:OpenEditor(editorName)
347347
Log.Notice("Opening editor: " .. editorName)
348348

349-
-- Editor should be pre-created by TabbedWindow/MainWindowPanel
350-
local editor = SB.editors[editorName]
351-
if not editor then
352-
Log.Error("Editor not yet created: " .. editorName .. " (still initializing?)")
353-
return
349+
-- Create editor on-demand if not yet created by SB.delay
350+
if not SB.editors[editorName] then
351+
local editorCfg = SB.editorRegistry[editorName]
352+
if editorCfg and editorCfg.editor then
353+
Log.Notice("Creating editor on-demand: " .. editorName)
354+
SB.editors[editorName] = editorCfg.editor()
355+
else
356+
Log.Error("Editor not found in registry: " .. editorName)
357+
return
358+
end
354359
end
355360

361+
local editor = SB.editors[editorName]
356362
local editorCfg = SB.editorRegistry[editorName]
357363
local mainContent = self.mainDocument:GetElementById("main-content")
358364

0 commit comments

Comments
 (0)