Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: Extract TextRendererFactory (#2680)
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: #2663 As a first step to break down that huge PR, this extracts TextRendererFactory in preparation of getting rid of TextRenderer entirely. This is technically a breaking change but should have no effect for users, unless you they have a custom Renderer *and* are using the registry (which I honestly doubt). But if you are: * just import TextRendererFactory and use the createDefault method from there instead
- Loading branch information
1 parent
1a64443
commit eeb6749
Showing
5 changed files
with
30 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import 'package:flame/text.dart'; | ||
|
||
class TextRendererFactory { | ||
/// A registry containing default providers for every [TextRenderer] subclass; | ||
/// used by [createDefault] to create default parameter values. | ||
/// | ||
/// If you add a new [TextRenderer] child, you can register it by adding it, | ||
/// together with a provider lambda, to this map. | ||
static Map<Type, TextRenderer Function()> defaultRegistry = { | ||
TextRenderer: TextPaint.new, | ||
TextPaint: TextPaint.new, | ||
}; | ||
|
||
/// Given a generic type [T], creates a default renderer of that type. | ||
static T createDefault<T extends TextRenderer>() { | ||
final creator = defaultRegistry[T]; | ||
if (creator != null) { | ||
return creator() as T; | ||
} else { | ||
throw 'Unknown implementation of TextRenderer: $T. Please register it ' | ||
'under [TextRendererFactory.defaultRegistry].'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters