Added Sign text caching

+Added the Sign.text field, assigned when the object is created
*Modified SignRenderer to use Sign.text instead of looking for the text on every render
*Sign texts are now transferred to Signal Signs when the Sign is assigned to a Signal
This commit is contained in:
izawartka 2025-07-31 22:45:52 +02:00
parent 547429e273
commit 5e8a943954
3 changed files with 33 additions and 24 deletions

View File

@ -31,13 +31,7 @@ export default function SignRenderer(props) {
function SignTextRenderer(props) {
const { object } = props;
const textSources = !!object.def.text ? Array.isArray(object.def.text) ? object.def.text : [object.def.text] : [];
let text = null;
for(const source of textSources) {
text = getSignText(object, source);
if(text) break;
}
if(!object.text) return;
const x = object.def.textOffsetX || 0;
const y = object.def.textOffsetY || 0;
@ -50,21 +44,7 @@ function SignTextRenderer(props) {
textAnchor="middle"
dominantBaseline="central"
>
{text}
{object.text}
</text>
);
}
function getSignText(object, source) {
switch(source) {
case "fun":
return object.def.textFun ? object.def.textFun(object) : null;
case "data":
return object.getPrintableSignData();
case "static":
return object.def.staticText ?? null;
default:
console.warn(`Unknown sign ${object.id} text source: ${source}`);
return null;
}
}

View File

@ -56,7 +56,7 @@ function attach(sign, signal) {
if(!attachAs || !signal.signal_elements) return;
sign.attached_skip_rendering = true;
signal.signal_elements.signs[attachAs] = true;
signal.signal_elements.signs[attachAs] = sign.text ? { text: sign.text } : true;
}
export function attachSigns(scenery) {

View File

@ -7,6 +7,7 @@ export default class Sign extends TrackObject {
type = "Sign";
data;
def;
text = null;
attached_to = null;
attached_skip_rendering = false;
@ -15,6 +16,7 @@ export default class Sign extends TrackObject {
this.data = data || null;
this.def = Sign.getDef(name, prefab_name);
this._assignText();
}
getPrintableSignData() {
@ -84,4 +86,31 @@ export default class Sign extends TrackObject {
return DefinedSigns[key];
}
_assignText() {
if(!this.def?.text) return;
const textSources = Array.isArray(this.def.text) ? this.def.text : [this.def.text];
let text = null;
for(const source of textSources) {
text = Sign.getTextFromSource(this, source);
if(text) break;
}
this.text = text;
}
static getTextFromSource(object, source) {
switch(source) {
case "fun":
return object.def.textFun ? object.def.textFun(object) : null;
case "data":
return object.getPrintableSignData();
case "static":
return object.def.staticText ?? null;
default:
console.warn(`Unknown sign ${object.id} text source: ${source}`);
return null;
}
}
}