mirror of
				https://github.com/flame-engine/flame.git
				synced 2025-11-01 01:18:38 +08:00 
			
		
		
		
	 1a64443cca
			
		
	
	1a64443cca
	
	
	
		
			
			This is part of my ongoing effort to simplify the text rendering pipeline. My ultimate goal is to: * get rid of renders * rename formatters to renderers * make the interface complies to both All details are specified here: https://github.com/flame-engine/flame/pull/2663 As a first step to break down that huge PR, this makes a small change to TextElements to make them more useful This PR will: ### rename render -> draw draw becomes the "internal", underlying impl, raw method, that just draws the element w/ any custom options ### add a new render method that takes in more options this does not need to be extended by every impl. this is for end users and accepts parameters like position and anchor to be more in line with the renderer interface This is technically a breaking change but should have no effect for users, unless you are creating your own custom `TextElement`s. In that case, to migrate: * rename your `render` method to `draw`
		
			
				
	
	
		
			57 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Dart
		
	
	
	
	
	
| import 'dart:ui';
 | |
| 
 | |
| import 'package:flame/components.dart';
 | |
| import 'package:flame/text.dart';
 | |
| import 'package:flame_test/flame_test.dart';
 | |
| import 'package:flutter_test/flutter_test.dart';
 | |
| 
 | |
| void main() {
 | |
|   group('DebugTextFormatter', () {
 | |
|     testGolden(
 | |
|       'Render debug text',
 | |
|       (game) async {
 | |
|         game.add(
 | |
|           TextElementsComponent([
 | |
|             DebugTextFormatter().format('one two  three')..translate(5, 5),
 | |
|             DebugTextFormatter().format(' x ')..translate(5, 25),
 | |
|             DebugTextFormatter().format('  ')..translate(5, 45),
 | |
|             DebugTextFormatter().format('')..translate(25, 45),
 | |
|             DebugTextFormatter(color: const Color(0xFFFF88AA))
 | |
|                 .format('Flame Engine')
 | |
|               ..translate(5, 65),
 | |
|             DebugTextFormatter(fontWeight: FontWeight.bold).format('Blue Fire')
 | |
|               ..translate(5, 85),
 | |
|             DebugTextFormatter(fontWeight: FontWeight.w900).format('Blue Fire')
 | |
|               ..translate(5, 105),
 | |
|             DebugTextFormatter(fontStyle: FontStyle.italic).format('Blue Fire')
 | |
|               ..translate(5, 125),
 | |
|             DebugTextFormatter(
 | |
|               fontWeight: FontWeight.bold,
 | |
|               fontStyle: FontStyle.italic,
 | |
|               color: const Color(0xFF0088FF),
 | |
|             ).format('a b c d e f g h i')
 | |
|               ..translate(5, 145),
 | |
|             DebugTextFormatter(fontSize: 10).format('www.flame-engine.org')
 | |
|               ..translate(5, 165),
 | |
|           ]),
 | |
|         );
 | |
|       },
 | |
|       goldenFile: 'golden_debug_text.png',
 | |
|       size: Vector2(300, 200),
 | |
|     );
 | |
|   });
 | |
| }
 | |
| 
 | |
| class TextElementsComponent extends PositionComponent {
 | |
|   TextElementsComponent(this.elements);
 | |
| 
 | |
|   final List<TextElement> elements;
 | |
| 
 | |
|   @override
 | |
|   void render(Canvas canvas) {
 | |
|     for (final element in elements) {
 | |
|       element.draw(canvas);
 | |
|     }
 | |
|   }
 | |
| }
 |