Fix text alpha updates during fade

This commit is contained in:
gem
2026-06-09 12:06:58 +08:00
parent 5e6a4877f4
commit 409942b4af
2 changed files with 73 additions and 13 deletions

View File

@@ -67,7 +67,12 @@ class RuntimeComponent extends PositionComponent
double get renderAlpha => _runtimeAlpha ?? _node.alpha; double get renderAlpha => _runtimeAlpha ?? _node.alpha;
void setRuntimeAlpha(double value) { void setRuntimeAlpha(double value) {
_runtimeAlpha = value.clamp(0, 1).toDouble(); final next = value.clamp(0, 1).toDouble();
if (_runtimeAlpha == next) {
return;
}
_runtimeAlpha = next;
_syncTextStyle(_node);
} }
void setParentScroll({ void setParentScroll({
@@ -961,21 +966,11 @@ class RuntimeComponent extends PositionComponent
} }
final text = label ?? ''; final text = label ?? '';
final color = _textColor(node);
final style = TextStyle(
color: composeRuntimeColorAlpha(color, renderAlpha),
fontSize: node.fontSize ?? 18,
fontWeight: node.type == RuntimeNodeType.button
? FontWeight.w600
: FontWeight.normal,
shadows: _textShadows(node),
);
final component = _textComponent; final component = _textComponent;
if (component == null) { if (component == null) {
_textComponent = TextComponent( _textComponent = TextComponent(
text: text, text: text,
textRenderer: TextPaint(style: style), textRenderer: TextPaint(style: _textStyle(node)),
anchor: _textAnchor(node), anchor: _textAnchor(node),
position: _textPosition(node), position: _textPosition(node),
priority: priority + 1, priority: priority + 1,
@@ -986,10 +981,30 @@ class RuntimeComponent extends PositionComponent
component component
..text = text ..text = text
..textRenderer = TextPaint(style: style)
..anchor = _textAnchor(node) ..anchor = _textAnchor(node)
..position = _textPosition(node) ..position = _textPosition(node)
..priority = priority + 1; ..priority = priority + 1;
_syncTextStyle(node);
}
void _syncTextStyle(RuntimeNode node) {
final component = _textComponent;
if (component == null) {
return;
}
component.textRenderer = TextPaint(style: _textStyle(node));
}
TextStyle _textStyle(RuntimeNode node) {
final color = _textColor(node);
return TextStyle(
color: composeRuntimeColorAlpha(color, renderAlpha),
fontSize: node.fontSize ?? 18,
fontWeight: node.type == RuntimeNodeType.button
? FontWeight.w600
: FontWeight.normal,
shadows: _textShadows(node),
);
} }
@override @override

View File

@@ -138,6 +138,51 @@ void main() {
); );
}); });
test('updates text alpha style without rebuilding text component', () {
final component = RuntimeComponent(
node: const RuntimeNode(
id: 'text',
type: RuntimeNodeType.text,
text: 'Fade me',
alpha: 0,
color: Color(0xffffffff),
),
resources: GameResourceManager(),
onNodeTap: (_, __) {},
);
final text = component.children.whereType<TextComponent>().single;
expect(((text.textRenderer as TextPaint).style.color!).a, 0);
component.setRuntimeAlpha(1);
final updatedText = component.children.whereType<TextComponent>().single;
expect(identical(updatedText, text), isTrue);
expect(((updatedText.textRenderer as TextPaint).style.color!).a, 1);
});
test('updates button text alpha style without rebuilding text component', () {
final component = RuntimeComponent(
node: const RuntimeNode(
id: 'button',
type: RuntimeNodeType.button,
text: 'Fade me',
alpha: 0,
),
resources: GameResourceManager(),
onNodeTap: (_, __) {},
);
final text = component.children.whereType<TextComponent>().single;
expect(((text.textRenderer as TextPaint).style.color!).a, 0);
component.setRuntimeAlpha(1);
final updatedText = component.children.whereType<TextComponent>().single;
expect(identical(updatedText, text), isTrue);
expect(((updatedText.textRenderer as TextPaint).style.color!).a, 1);
});
test('applies text shadow style', () { test('applies text shadow style', () {
final component = RuntimeComponent( final component = RuntimeComponent(
node: const RuntimeNode( node: const RuntimeNode(