From 9f9aa7362a6dc7c605d774c2dc4842294f6813d3 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:24:34 -0300 Subject: [PATCH 1/9] Adding MemoryCache class --- CHANGELOG.md | 1 + lib/memory_cache.dart | 28 ++++++++++++++++++++++++++++ test/memory_cache_test.dart | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 lib/memory_cache.dart create mode 100644 test/memory_cache_test.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 38fe2a8d0..22d201c78 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ## [next] + - Adding MemoryCache class ## 0.23.0 - Add Joystick Component diff --git a/lib/memory_cache.dart b/lib/memory_cache.dart new file mode 100644 index 000000000..f311e7de3 --- /dev/null +++ b/lib/memory_cache.dart @@ -0,0 +1,28 @@ + +/// Simple class to cache values on the cache +/// +class MemoryCache { + final Map _cache = {}; + final List _addedOrder = []; + final int cacheSize; + + MemoryCache({ this.cacheSize = 10 }); + + void setValue(K key, V value) { + if (!_cache.containsKey(key)) { + _cache[key] = value; + _addedOrder.add(key); + + while (_addedOrder.length > cacheSize) { + final k = _addedOrder.removeAt(0); + _cache.remove(k); + } + } + } + + V getValue(K key) => _cache[key]; + + bool containsKey(K key) => _cache.containsKey(key); + + int get size => _cache.length; +} diff --git a/test/memory_cache_test.dart b/test/memory_cache_test.dart new file mode 100644 index 000000000..780ab33cc --- /dev/null +++ b/test/memory_cache_test.dart @@ -0,0 +1,32 @@ +import 'package:test/test.dart'; + +import 'package:flame/memory_cache.dart'; + +void main() { + group('MemoryCache', () { + test('basic cache addition', () { + final cache = MemoryCache(); + cache.setValue(0, 'bla'); + expect(cache.getValue(0), 'bla'); + }); + + test('contains key', () { + final cache = MemoryCache(); + cache.setValue(0, 'bla'); + expect(cache.containsKey(0), true); + expect(cache.containsKey(1), false); + }); + + test('cache size', () { + final cache = MemoryCache(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); + }); + }); +} + From c60e039f0603d17de0ae7a2951e5c979e7499efc Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:36:47 -0300 Subject: [PATCH 2/9] Fixing game crashes on web --- CHANGELOG.md | 1 + lib/components/text_box_component.dart | 13 ++++---- lib/components/text_component.dart | 4 +-- lib/text_config.dart | 41 +++++++++++++++----------- 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 22d201c78..a7107b375 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## [next] - Adding MemoryCache class + - Fixing games crashes on Web ## 0.23.0 - Add Joystick Component diff --git a/lib/components/text_box_component.dart b/lib/components/text_box_component.dart index c2180f29a..24171ce39 100644 --- a/lib/components/text_box_component.dart +++ b/lib/components/text_box_component.dart @@ -15,7 +15,7 @@ class TextBoxConfig { final double timePerChar; final double dismissDelay; - const TextBoxConfig({ + TextBoxConfig({ this.maxWidth = 200.0, this.margin = 8.0, this.timePerChar = 0.0, @@ -46,11 +46,12 @@ class TextBoxComponent extends PositionComponent with Resizable { TextBoxConfig get boxConfig => _boxConfig; - TextBoxComponent(String text, - {TextConfig config = const TextConfig(), - TextBoxConfig boxConfig = const TextBoxConfig()}) { - _boxConfig = boxConfig; - _config = config; + TextBoxComponent(String text, { + TextConfig config, + TextBoxConfig boxConfig, + }) { + _boxConfig = boxConfig ?? TextBoxConfig(); + _config = config ?? TextConfig(); _text = text; _lines = []; text.split(' ').forEach((word) { diff --git a/lib/components/text_component.dart b/lib/components/text_component.dart index e21b732ad..4a7702b12 100644 --- a/lib/components/text_component.dart +++ b/lib/components/text_component.dart @@ -27,8 +27,8 @@ class TextComponent extends PositionComponent { _updateBox(); } - TextComponent(this._text, {TextConfig config = const TextConfig()}) { - _config = config; + TextComponent(this._text, {TextConfig config }) { + _config = config ?? TextConfig(); _updateBox(); } diff --git a/lib/text_config.dart b/lib/text_config.dart index 9f9bdcfa8..268a4e8e8 100644 --- a/lib/text_config.dart +++ b/lib/text_config.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart' as material; import 'position.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. /// @@ -52,10 +53,12 @@ class TextConfig { /// For proper fonts of languages like Hebrew or Arabic, replace this with [TextDirection.rtl]. final TextDirection textDirection; + final MemoryCache _textPainterCache = MemoryCache(); + /// Creates a constant [TextConfig] with sensible defaults. /// /// Every parameter can be specified. - const TextConfig({ + TextConfig({ this.fontSize = 24.0, this.color = const Color(0xFF000000), this.fontFamily = 'Arial', @@ -93,22 +96,26 @@ class TextConfig { /// 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. material.TextPainter toTextPainter(String text) { - final material.TextStyle style = material.TextStyle( - color: color, - fontSize: fontSize, - fontFamily: fontFamily, - ); - final material.TextSpan span = material.TextSpan( - style: style, - text: text, - ); - final material.TextPainter tp = material.TextPainter( - text: span, - textAlign: textAlign, - textDirection: textDirection, - ); - tp.layout(); - return tp; + if (!_textPainterCache.containsKey(text)) { + final material.TextStyle style = material.TextStyle( + color: color, + fontSize: fontSize, + fontFamily: fontFamily, + ); + final material.TextSpan span = material.TextSpan( + style: style, + text: text, + ); + final material.TextPainter tp = material.TextPainter( + text: span, + textAlign: textAlign, + textDirection: textDirection, + ); + tp.layout(); + + _textPainterCache.setValue(text, tp); + } + return _textPainterCache.getValue(text); } /// Creates a new [TextConfig] changing only the [fontSize]. From 2f694c48144015e732ecce77e4ae3612aad21666 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:43:02 -0300 Subject: [PATCH 3/9] fixing example --- doc/examples/text/lib/main.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/examples/text/lib/main.dart b/doc/examples/text/lib/main.dart index c10090dee..ab0b85432 100644 --- a/doc/examples/text/lib/main.dart +++ b/doc/examples/text/lib/main.dart @@ -20,7 +20,7 @@ TextConfig tiny = regular.withFontSize(12.0); class MyTextBox extends TextBoxComponent { MyTextBox(String text) : super(text, - config: tiny, boxConfig: const TextBoxConfig(timePerChar: 0.05)); + config: tiny, boxConfig: TextBoxConfig(timePerChar: 0.05)); @override void drawBackground(Canvas c) { From 32df868b5ba56212c2bb34762be9f91d569b42d9 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:46:08 -0300 Subject: [PATCH 4/9] linting --- doc/examples/text/lib/main.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/doc/examples/text/lib/main.dart b/doc/examples/text/lib/main.dart index ab0b85432..037c8df67 100644 --- a/doc/examples/text/lib/main.dart +++ b/doc/examples/text/lib/main.dart @@ -19,8 +19,7 @@ TextConfig tiny = regular.withFontSize(12.0); class MyTextBox extends TextBoxComponent { MyTextBox(String text) - : super(text, - config: tiny, boxConfig: TextBoxConfig(timePerChar: 0.05)); + : super(text, config: tiny, boxConfig: TextBoxConfig(timePerChar: 0.05)); @override void drawBackground(Canvas c) { From 629fbb72476308bd4bf770fb219e9ecbb8fd187b Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:49:56 -0300 Subject: [PATCH 5/9] linting --- lib/components/text_box_component.dart | 3 ++- lib/components/text_component.dart | 2 +- lib/memory_cache.dart | 5 ++--- lib/text_config.dart | 19 ++++++++++--------- test/memory_cache_test.dart | 1 - 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/components/text_box_component.dart b/lib/components/text_box_component.dart index 24171ce39..02e9be5e8 100644 --- a/lib/components/text_box_component.dart +++ b/lib/components/text_box_component.dart @@ -46,7 +46,8 @@ class TextBoxComponent extends PositionComponent with Resizable { TextBoxConfig get boxConfig => _boxConfig; - TextBoxComponent(String text, { + TextBoxComponent( + String text, { TextConfig config, TextBoxConfig boxConfig, }) { diff --git a/lib/components/text_component.dart b/lib/components/text_component.dart index 4a7702b12..4e52ca28b 100644 --- a/lib/components/text_component.dart +++ b/lib/components/text_component.dart @@ -27,7 +27,7 @@ class TextComponent extends PositionComponent { _updateBox(); } - TextComponent(this._text, {TextConfig config }) { + TextComponent(this._text, {TextConfig config}) { _config = config ?? TextConfig(); _updateBox(); } diff --git a/lib/memory_cache.dart b/lib/memory_cache.dart index f311e7de3..dd31b6e05 100644 --- a/lib/memory_cache.dart +++ b/lib/memory_cache.dart @@ -1,12 +1,11 @@ - /// Simple class to cache values on the cache /// -class MemoryCache { +class MemoryCache { final Map _cache = {}; final List _addedOrder = []; final int cacheSize; - MemoryCache({ this.cacheSize = 10 }); + MemoryCache({this.cacheSize = 10}); void setValue(K key, V value) { if (!_cache.containsKey(key)) { diff --git a/lib/text_config.dart b/lib/text_config.dart index 268a4e8e8..088b62805 100644 --- a/lib/text_config.dart +++ b/lib/text_config.dart @@ -53,7 +53,8 @@ class TextConfig { /// For proper fonts of languages like Hebrew or Arabic, replace this with [TextDirection.rtl]. final TextDirection textDirection; - final MemoryCache _textPainterCache = MemoryCache(); + final MemoryCache _textPainterCache = + MemoryCache(); /// Creates a constant [TextConfig] with sensible defaults. /// @@ -98,18 +99,18 @@ class TextConfig { material.TextPainter toTextPainter(String text) { if (!_textPainterCache.containsKey(text)) { final material.TextStyle style = material.TextStyle( - color: color, - fontSize: fontSize, - fontFamily: fontFamily, + color: color, + fontSize: fontSize, + fontFamily: fontFamily, ); final material.TextSpan span = material.TextSpan( - style: style, - text: text, + style: style, + text: text, ); final material.TextPainter tp = material.TextPainter( - text: span, - textAlign: textAlign, - textDirection: textDirection, + text: span, + textAlign: textAlign, + textDirection: textDirection, ); tp.layout(); diff --git a/test/memory_cache_test.dart b/test/memory_cache_test.dart index 780ab33cc..bf4cf6473 100644 --- a/test/memory_cache_test.dart +++ b/test/memory_cache_test.dart @@ -29,4 +29,3 @@ void main() { }); }); } - From 861918c8234613d93c8cbd3b3545294cb2b55f77 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Tue, 30 Jun 2020 23:56:24 -0300 Subject: [PATCH 6/9] Linting --- doc/examples/debug/lib/main.dart | 2 +- doc/examples/flare/lib/main.dart | 3 +-- doc/examples/particles/lib/main.dart | 2 +- doc/examples/timer/lib/main.dart | 6 ++---- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/doc/examples/debug/lib/main.dart b/doc/examples/debug/lib/main.dart index 032987a90..6de04c9ba 100644 --- a/doc/examples/debug/lib/main.dart +++ b/doc/examples/debug/lib/main.dart @@ -49,7 +49,7 @@ class AndroidComponent extends SvgComponent with Resizable { } class MyGame extends BaseGame { - final fpsTextConfig = const TextConfig(color: const Color(0xFFFFFFFF)); + final fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF)); @override bool debugMode() => true; diff --git a/doc/examples/flare/lib/main.dart b/doc/examples/flare/lib/main.dart index 36310713f..af5dda162 100644 --- a/doc/examples/flare/lib/main.dart +++ b/doc/examples/flare/lib/main.dart @@ -16,8 +16,7 @@ void main() { } class MyGame extends BaseGame with TapDetector { - final TextConfig fpsTextConfig = - const TextConfig(color: const Color(0xFFFFFFFF)); + final TextConfig fpsTextConfig = TextConfig(color: const Color(0xFFFFFFFF)); final paint = Paint()..color = const Color(0xFFE5E5E5E5); final List _animations = ["Stand", "Wave", "Jump", "Dance"]; diff --git a/doc/examples/particles/lib/main.dart b/doc/examples/particles/lib/main.dart index aa59a502c..4f5608ab7 100644 --- a/doc/examples/particles/lib/main.dart +++ b/doc/examples/particles/lib/main.dart @@ -43,7 +43,7 @@ class MyGame extends BaseGame { final Random rnd = Random(); final StepTween steppedTween = StepTween(begin: 0, end: 5); final trafficLight = TrafficLightComponent(); - final TextConfig fpsTextConfig = const TextConfig( + final TextConfig fpsTextConfig = TextConfig( color: const Color(0xFFFFFFFF), ); diff --git a/doc/examples/timer/lib/main.dart b/doc/examples/timer/lib/main.dart index dcb8e7d25..0e1dad562 100644 --- a/doc/examples/timer/lib/main.dart +++ b/doc/examples/timer/lib/main.dart @@ -33,8 +33,7 @@ class GameWidget extends StatelessWidget { } class RenderedTimeComponent extends TimerComponent { - final TextConfig textConfig = - const TextConfig(color: const Color(0xFFFFFFFF)); + final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF)); RenderedTimeComponent(Timer timer) : super(timer); @@ -58,8 +57,7 @@ class MyBaseGame extends BaseGame with TapDetector, DoubleTapDetector { } class MyGame extends Game with TapDetector { - final TextConfig textConfig = - const TextConfig(color: const Color(0xFFFFFFFF)); + final TextConfig textConfig = TextConfig(color: const Color(0xFFFFFFFF)); Timer countdown; Timer interval; From a8796a56051621236e3aec4761205f78e4285684 Mon Sep 17 00:00:00 2001 From: Erick Zanardo Date: Wed, 1 Jul 2020 11:44:21 -0300 Subject: [PATCH 7/9] PR suggestion --- lib/memory_cache.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/memory_cache.dart b/lib/memory_cache.dart index dd31b6e05..4c5d46e7a 100644 --- a/lib/memory_cache.dart +++ b/lib/memory_cache.dart @@ -1,8 +1,9 @@ +import 'dart:collection'; + /// Simple class to cache values on the cache /// class MemoryCache { - final Map _cache = {}; - final List _addedOrder = []; + final LinkedHashMap _cache = LinkedHashMap(); final int cacheSize; MemoryCache({this.cacheSize = 10}); @@ -10,10 +11,9 @@ class MemoryCache { void setValue(K key, V value) { if (!_cache.containsKey(key)) { _cache[key] = value; - _addedOrder.add(key); - while (_addedOrder.length > cacheSize) { - final k = _addedOrder.removeAt(0); + while (_cache.length > cacheSize) { + final k = _cache.keys.first; _cache.remove(k); } } From 0e794e8f88327c4c6ed4113dd47cb306b748316a Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Thu, 2 Jul 2020 11:40:16 -0400 Subject: [PATCH 8/9] Update tiled dependency --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 0ebcfbba0..9a97cd5d5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,7 @@ dependencies: path_provider: ^1.6.0 box2d_flame: ^0.4.6 synchronized: ^2.1.0 - tiled: ^0.5.0 + tiled: ^0.6.0 convert: ^2.0.1 flutter_svg: ^0.18.0 flare_flutter: ^2.0.1 From 84240462ccbc07c2b0285f4edee48e08fedd26ea Mon Sep 17 00:00:00 2001 From: Luan Nico Date: Thu, 2 Jul 2020 11:40:45 -0400 Subject: [PATCH 9/9] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38fe2a8d0..7e7c7bac5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ## [next] +- Update tiled dependency to 0.6.0 (objects' properties are now double) ## 0.23.0 - Add Joystick Component