mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-01 19:12:31 +08:00
Merge branch 'develop' into erick.remove-svg-package
This commit is contained in:
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
## [next]
|
## [next]
|
||||||
- Outsourcing SVG support to an external package
|
- Outsourcing SVG support to an external package
|
||||||
|
- Adding MemoryCache class
|
||||||
|
- Fixing games crashes on Web
|
||||||
|
- Update tiled dependency to 0.6.0 (objects' properties are now double)
|
||||||
|
|
||||||
## 0.23.0
|
## 0.23.0
|
||||||
- Add Joystick Component
|
- Add Joystick Component
|
||||||
|
|||||||
@ -48,7 +48,7 @@ class AndroidComponent extends SpriteComponent with Resizable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MyGame extends BaseGame {
|
class MyGame extends BaseGame {
|
||||||
final fpsTextConfig = const TextConfig(color: const Color(0xFFFFFFFF));
|
final fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool debugMode() => true;
|
bool debugMode() => true;
|
||||||
|
|||||||
@ -16,8 +16,7 @@ void main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MyGame extends BaseGame with TapDetector {
|
class MyGame extends BaseGame with TapDetector {
|
||||||
final TextConfig fpsTextConfig =
|
final TextConfig fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||||
const TextConfig(color: const Color(0xFFFFFFFF));
|
|
||||||
|
|
||||||
final paint = Paint()..color = const Color(0xFFE5E5E5E5);
|
final paint = Paint()..color = const Color(0xFFE5E5E5E5);
|
||||||
final List<String> _animations = ["Stand", "Wave", "Jump", "Dance"];
|
final List<String> _animations = ["Stand", "Wave", "Jump", "Dance"];
|
||||||
|
|||||||
@ -43,7 +43,7 @@ class MyGame extends BaseGame {
|
|||||||
final Random rnd = Random();
|
final Random rnd = Random();
|
||||||
final StepTween steppedTween = StepTween(begin: 0, end: 5);
|
final StepTween steppedTween = StepTween(begin: 0, end: 5);
|
||||||
final trafficLight = TrafficLightComponent();
|
final trafficLight = TrafficLightComponent();
|
||||||
final TextConfig fpsTextConfig = const TextConfig(
|
final TextConfig fpsTextConfig = TextConfig(
|
||||||
color: const Color(0xFFFFFFFF),
|
color: const Color(0xFFFFFFFF),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -19,8 +19,7 @@ TextConfig tiny = regular.withFontSize(12.0);
|
|||||||
|
|
||||||
class MyTextBox extends TextBoxComponent {
|
class MyTextBox extends TextBoxComponent {
|
||||||
MyTextBox(String text)
|
MyTextBox(String text)
|
||||||
: super(text,
|
: super(text, config: tiny, boxConfig: TextBoxConfig(timePerChar: 0.05));
|
||||||
config: tiny, boxConfig: const TextBoxConfig(timePerChar: 0.05));
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void drawBackground(Canvas c) {
|
void drawBackground(Canvas c) {
|
||||||
|
|||||||
@ -33,8 +33,7 @@ class GameWidget extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class RenderedTimeComponent extends TimerComponent {
|
class RenderedTimeComponent extends TimerComponent {
|
||||||
final TextConfig textConfig =
|
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||||
const TextConfig(color: const Color(0xFFFFFFFF));
|
|
||||||
|
|
||||||
RenderedTimeComponent(Timer timer) : super(timer);
|
RenderedTimeComponent(Timer timer) : super(timer);
|
||||||
|
|
||||||
@ -58,8 +57,7 @@ class MyBaseGame extends BaseGame with TapDetector, DoubleTapDetector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class MyGame extends Game with TapDetector {
|
class MyGame extends Game with TapDetector {
|
||||||
final TextConfig textConfig =
|
final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF));
|
||||||
const TextConfig(color: const Color(0xFFFFFFFF));
|
|
||||||
Timer countdown;
|
Timer countdown;
|
||||||
Timer interval;
|
Timer interval;
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,7 @@ class TextBoxConfig {
|
|||||||
final double timePerChar;
|
final double timePerChar;
|
||||||
final double dismissDelay;
|
final double dismissDelay;
|
||||||
|
|
||||||
const TextBoxConfig({
|
TextBoxConfig({
|
||||||
this.maxWidth = 200.0,
|
this.maxWidth = 200.0,
|
||||||
this.margin = 8.0,
|
this.margin = 8.0,
|
||||||
this.timePerChar = 0.0,
|
this.timePerChar = 0.0,
|
||||||
@ -46,11 +46,13 @@ class TextBoxComponent extends PositionComponent with Resizable {
|
|||||||
|
|
||||||
TextBoxConfig get boxConfig => _boxConfig;
|
TextBoxConfig get boxConfig => _boxConfig;
|
||||||
|
|
||||||
TextBoxComponent(String text,
|
TextBoxComponent(
|
||||||
{TextConfig config = const TextConfig(),
|
String text, {
|
||||||
TextBoxConfig boxConfig = const TextBoxConfig()}) {
|
TextConfig config,
|
||||||
_boxConfig = boxConfig;
|
TextBoxConfig boxConfig,
|
||||||
_config = config;
|
}) {
|
||||||
|
_boxConfig = boxConfig ?? TextBoxConfig();
|
||||||
|
_config = config ?? TextConfig();
|
||||||
_text = text;
|
_text = text;
|
||||||
_lines = [];
|
_lines = [];
|
||||||
text.split(' ').forEach((word) {
|
text.split(' ').forEach((word) {
|
||||||
|
|||||||
@ -27,8 +27,8 @@ class TextComponent extends PositionComponent {
|
|||||||
_updateBox();
|
_updateBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
TextComponent(this._text, {TextConfig config = const TextConfig()}) {
|
TextComponent(this._text, {TextConfig config}) {
|
||||||
_config = config;
|
_config = config ?? TextConfig();
|
||||||
_updateBox();
|
_updateBox();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
27
lib/memory_cache.dart
Normal file
27
lib/memory_cache.dart
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import 'dart:collection';
|
||||||
|
|
||||||
|
/// Simple class to cache values on the cache
|
||||||
|
///
|
||||||
|
class MemoryCache<K, V> {
|
||||||
|
final LinkedHashMap<K, V> _cache = LinkedHashMap();
|
||||||
|
final int cacheSize;
|
||||||
|
|
||||||
|
MemoryCache({this.cacheSize = 10});
|
||||||
|
|
||||||
|
void setValue(K key, V value) {
|
||||||
|
if (!_cache.containsKey(key)) {
|
||||||
|
_cache[key] = value;
|
||||||
|
|
||||||
|
while (_cache.length > cacheSize) {
|
||||||
|
final k = _cache.keys.first;
|
||||||
|
_cache.remove(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
V getValue(K key) => _cache[key];
|
||||||
|
|
||||||
|
bool containsKey(K key) => _cache.containsKey(key);
|
||||||
|
|
||||||
|
int get size => _cache.length;
|
||||||
|
}
|
||||||
@ -4,6 +4,7 @@ import 'package:flutter/material.dart' as material;
|
|||||||
|
|
||||||
import 'position.dart';
|
import 'position.dart';
|
||||||
import 'anchor.dart';
|
import 'anchor.dart';
|
||||||
|
import 'memory_cache.dart';
|
||||||
|
|
||||||
/// A Text Config contains all typographical information required to render texts; i.e., font size and color, family, etc.
|
/// A Text Config contains all typographical information required to render texts; i.e., font size and color, family, etc.
|
||||||
///
|
///
|
||||||
@ -52,10 +53,13 @@ class TextConfig {
|
|||||||
/// For proper fonts of languages like Hebrew or Arabic, replace this with [TextDirection.rtl].
|
/// For proper fonts of languages like Hebrew or Arabic, replace this with [TextDirection.rtl].
|
||||||
final TextDirection textDirection;
|
final TextDirection textDirection;
|
||||||
|
|
||||||
|
final MemoryCache _textPainterCache =
|
||||||
|
MemoryCache<String, material.TextPainter>();
|
||||||
|
|
||||||
/// Creates a constant [TextConfig] with sensible defaults.
|
/// Creates a constant [TextConfig] with sensible defaults.
|
||||||
///
|
///
|
||||||
/// Every parameter can be specified.
|
/// Every parameter can be specified.
|
||||||
const TextConfig({
|
TextConfig({
|
||||||
this.fontSize = 24.0,
|
this.fontSize = 24.0,
|
||||||
this.color = const Color(0xFF000000),
|
this.color = const Color(0xFF000000),
|
||||||
this.fontFamily = 'Arial',
|
this.fontFamily = 'Arial',
|
||||||
@ -93,22 +97,26 @@ class TextConfig {
|
|||||||
/// However, you probably want to use the [render] method witch already renders for you considering the anchor.
|
/// However, you probably want to use the [render] method witch already renders for you considering the anchor.
|
||||||
/// That way, you don't need to perform the math for yourself.
|
/// That way, you don't need to perform the math for yourself.
|
||||||
material.TextPainter toTextPainter(String text) {
|
material.TextPainter toTextPainter(String text) {
|
||||||
final material.TextStyle style = material.TextStyle(
|
if (!_textPainterCache.containsKey(text)) {
|
||||||
color: color,
|
final material.TextStyle style = material.TextStyle(
|
||||||
fontSize: fontSize,
|
color: color,
|
||||||
fontFamily: fontFamily,
|
fontSize: fontSize,
|
||||||
);
|
fontFamily: fontFamily,
|
||||||
final material.TextSpan span = material.TextSpan(
|
);
|
||||||
style: style,
|
final material.TextSpan span = material.TextSpan(
|
||||||
text: text,
|
style: style,
|
||||||
);
|
text: text,
|
||||||
final material.TextPainter tp = material.TextPainter(
|
);
|
||||||
text: span,
|
final material.TextPainter tp = material.TextPainter(
|
||||||
textAlign: textAlign,
|
text: span,
|
||||||
textDirection: textDirection,
|
textAlign: textAlign,
|
||||||
);
|
textDirection: textDirection,
|
||||||
tp.layout();
|
);
|
||||||
return tp;
|
tp.layout();
|
||||||
|
|
||||||
|
_textPainterCache.setValue(text, tp);
|
||||||
|
}
|
||||||
|
return _textPainterCache.getValue(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new [TextConfig] changing only the [fontSize].
|
/// Creates a new [TextConfig] changing only the [fontSize].
|
||||||
|
|||||||
@ -11,7 +11,7 @@ dependencies:
|
|||||||
path_provider: ^1.6.0
|
path_provider: ^1.6.0
|
||||||
box2d_flame: ^0.4.6
|
box2d_flame: ^0.4.6
|
||||||
synchronized: ^2.1.0
|
synchronized: ^2.1.0
|
||||||
tiled: ^0.5.0
|
tiled: ^0.6.0
|
||||||
convert: ^2.0.1
|
convert: ^2.0.1
|
||||||
flare_flutter: ^2.0.1
|
flare_flutter: ^2.0.1
|
||||||
meta: ^1.1.8
|
meta: ^1.1.8
|
||||||
|
|||||||
31
test/memory_cache_test.dart
Normal file
31
test/memory_cache_test.dart
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
import 'package:flame/memory_cache.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
group('MemoryCache', () {
|
||||||
|
test('basic cache addition', () {
|
||||||
|
final cache = MemoryCache<int, String>();
|
||||||
|
cache.setValue(0, 'bla');
|
||||||
|
expect(cache.getValue(0), 'bla');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('contains key', () {
|
||||||
|
final cache = MemoryCache<int, String>();
|
||||||
|
cache.setValue(0, 'bla');
|
||||||
|
expect(cache.containsKey(0), true);
|
||||||
|
expect(cache.containsKey(1), false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('cache size', () {
|
||||||
|
final cache = MemoryCache<int, String>(cacheSize: 1);
|
||||||
|
cache.setValue(0, 'bla');
|
||||||
|
cache.setValue(1, 'ble');
|
||||||
|
expect(cache.containsKey(0), false);
|
||||||
|
expect(cache.containsKey(1), true);
|
||||||
|
expect(cache.getValue(0), null);
|
||||||
|
expect(cache.getValue(1), 'ble');
|
||||||
|
expect(cache.size, 1);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user