Skip to content

Commit 7b58b89

Browse files
committed
Add RTT (Render-to-Texture) support for RmlUi GridView
This enables 3D model previews for units/features without buildpics in RmlUi mode, using OpenGL texture rendering. RTT Implementation: - Updated _UpdateRmlUiGrid() to handle both file paths and texture IDs * String paths: regular image files (adjusted for LuaUI/ prefix) * Number IDs: OpenGL textures using ":t{id}" format * Table objects: extract texture ID from texID/id fields - item.SetImage() now triggers grid refresh when texture changes - item.Invalidate() triggers grid refresh for RTT updates - Added throttling to prevent excessive DOM updates (max 10 FPS) * Schedules delayed update if called too frequently * Prevents performance issues from rapid RTT invalidations The existing RTT rendering code (gl.CreateTexture, gl.RenderToTexture) works identically in both modes since it's pure OpenGL. The only change needed was to support texture ID references in RmlUi <img> tags. Assumes RmlUi supports Spring's ":t<textureID>" format for OpenGL texture references in image src attributes.
1 parent 5605e26 commit 7b58b89

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

scen_edit/view/grid_view.lua

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ function GridView:NewItem(tbl)
262262
self.layoutPanel:AddChild(item)
263263
else
264264
-- RmlUi mode - create simple table item
265+
local gridView = self -- Capture reference to parent grid
265266
item = {
266267
name = 'grid_item' .. tostring(__gridItemCounter),
267268
tooltip = tbl.tooltip,
@@ -271,10 +272,19 @@ function GridView:NewItem(tbl)
271272
-- Add wrapper method for setting images
272273
item.SetImage = function(self, imagePath)
273274
self.image = imagePath
275+
-- Trigger grid update when image changes (for RTT updates)
276+
if gridView then
277+
gridView:_UpdateRmlUiGrid()
278+
end
274279
end
275280

276281
-- Stub methods that might be called
277-
item.Invalidate = function() end
282+
item.Invalidate = function()
283+
-- Trigger grid refresh when item is invalidated (e.g., RTT texture updated)
284+
if gridView then
285+
gridView:_UpdateRmlUiGrid()
286+
end
287+
end
278288
item.IsInView = function() return true end
279289
item.AddChild = function() end
280290
item.SetChildLayer = function() end
@@ -403,6 +413,25 @@ function GridView:_UpdateRmlUiGrid()
403413
return
404414
end
405415

416+
-- Throttle updates to avoid excessive DOM manipulation (e.g., from RTT invalidations)
417+
-- Maximum 10 updates per second
418+
local now = Spring.GetTimer()
419+
if self._lastUpdateTime then
420+
local elapsed = Spring.DiffTimers(now, self._lastUpdateTime)
421+
if elapsed < 0.1 then
422+
-- Too soon, schedule delayed update
423+
if not self._updateScheduled then
424+
self._updateScheduled = true
425+
SB.delay(function()
426+
self._updateScheduled = false
427+
self:_UpdateRmlUiGrid()
428+
end)
429+
end
430+
return
431+
end
432+
end
433+
self._lastUpdateTime = now
434+
406435
-- Update the RmlUi DOM to reflect current grid state
407436
-- This will be called whenever the grid needs to refresh
408437
if not SB.view or not SB.view.mainDocument then
@@ -427,10 +456,22 @@ function GridView:_UpdateRmlUiGrid()
427456
-- Add image if present
428457
if item.image then
429458
local imagePath = item.image
430-
if imagePath:sub(1, 6) == "LuaUI/" then
431-
imagePath = "../../../" .. imagePath
459+
-- Handle both file paths (strings) and texture IDs (numbers/tables)
460+
if type(imagePath) == "string" then
461+
-- Regular file path
462+
if imagePath:sub(1, 6) == "LuaUI/" then
463+
imagePath = "../../../" .. imagePath
464+
end
465+
html = html .. string.format('<img src="%s" class="grid-item-image"/>', imagePath)
466+
elseif type(imagePath) == "number" then
467+
-- OpenGL texture ID - RmlUi should support this directly
468+
-- Format: :t<textureID> for Spring texture references
469+
html = html .. string.format('<img src=":t%d" class="grid-item-image"/>', imagePath)
470+
else
471+
-- Texture table/object - try to extract ID or convert to string
472+
local texId = imagePath.texID or imagePath.id or tostring(imagePath)
473+
html = html .. string.format('<img src=":t%s" class="grid-item-image"/>', tostring(texId))
432474
end
433-
html = html .. string.format('<img src="%s" class="grid-item-image"/>', imagePath)
434475
end
435476

436477
-- Add caption if present

0 commit comments

Comments
 (0)