Skip to content

Commit 5ffc8bf

Browse files
benni-tecbenni-tecspydon
authored
feat!: Dart SDK compatibility (#77)
# Description This PR makes this package compatible with the Dart SDK. In order to achieve this I changed 3 things: - Replace `dart:ui`'s Color with a simple ColorData class - Replaced `Rect` from `flame` with `Rectangle` from `dart:math` - Replaced `flutter_test` with `test` package The `ColorData` class is used excatly once for which I prepared a flame_tiled PR (however I need to wait until this is merged right) including conversion extensions. No `Rectangle` attribute is ever read in `flame_tiled`, evenso `flame` already contains conversion extensions! ## Checklist - [x] The title of my PR starts with a [Conventional Commit] prefix (`fix:`, `feat:`, `docs:` etc). - [x] I have read the [Contributor Guide] and followed the process outlined for submitting PRs. --> `melos run analyze` fails due to `XmlData.value` however I think this requires another issue/PR - [x] I have updated/added tests for ALL new/updated/fixed functionality. - [x] I have updated/added relevant documentation in `docs` and added dartdoc comments with `///`. - [x] I have updated/added relevant examples in `examples`. --> I don't think there are any exmaples necessary for this ## Breaking Change - [x] Yes, this is a breaking change. - [ ] No, this is *not* a breaking change. I think this should result in a minor version bump to 0.11.0 to indicate these breaking changes! In order to achieve dart comaptibilty only two things changed: - Color: Custom data class --> `flame_tiled` contains a conversion extension - Rect: Instead using `dart:math`'s Rectangle --> Use `flame`'s `toRect()` method to convert ## Related Issues - #69 --------- Co-authored-by: benni-tec <[email protected]> Co-authored-by: Lukas Klingsbo <[email protected]>
1 parent 581391c commit 5ffc8bf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+209
-126
lines changed

packages/tiled/lib/src/chunk.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import 'package:meta/meta.dart';
2+
3+
/// Basic data class holding a Color in ARGB format.
4+
/// This can be converted to dart:ui's Color using the flame_tiled package
5+
@immutable
6+
class ColorData {
7+
static int _sub(int hex, int index) => (hex >> index * 8) & 0x000000ff;
8+
9+
final int _hex;
10+
11+
int get alpha => _sub(_hex, 3);
12+
13+
int get red => _sub(_hex, 2);
14+
15+
int get green => _sub(_hex, 1);
16+
17+
int get blue => _sub(_hex, 0);
18+
19+
/// Parses the Color from an int using the lower 32-bits and tiled's format:
20+
/// 0xaarrggbb
21+
const ColorData.hex(this._hex);
22+
23+
const ColorData.rgb(int red, int green, int blue, [int alpha = 255])
24+
: assert(red >= 0 && red <= 255),
25+
assert(green >= 0 && green <= 255),
26+
assert(blue >= 0 && blue <= 255),
27+
assert(alpha >= 0 && alpha <= 255),
28+
_hex = (alpha << 3 * 8) +
29+
(red << 2 * 8) +
30+
(green << 1 * 8) +
31+
(blue << 0 * 8);
32+
33+
const ColorData.argb(int alpha, int red, int green, int blue)
34+
: assert(red >= 0 && red <= 255),
35+
assert(green >= 0 && green <= 255),
36+
assert(blue >= 0 && blue <= 255),
37+
assert(alpha >= 0 && alpha <= 255),
38+
_hex = (alpha << 3 * 8) +
39+
(red << 2 * 8) +
40+
(green << 1 * 8) +
41+
(blue << 0 * 8);
42+
43+
@override
44+
bool operator ==(Object other) {
45+
if (other is! ColorData) {
46+
return false;
47+
}
48+
return _hex == other._hex;
49+
}
50+
51+
@override
52+
int get hashCode => _hex.hashCode;
53+
}

packages/tiled/lib/src/common/enums.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
enum MapOrientation { orthogonal, isometric, staggered, hexagonal }
44

packages/tiled/lib/src/common/flips.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
part of '../../tiled.dart';
2-
31
class Flips {
42
final bool horizontally;
53
final bool vertically;

packages/tiled/lib/src/common/frame.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/common/gid.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// A [Gid], Global Tile ID is a Tiled concept to represent the tiles inside
44
/// int matrices. This wrapper is used by [Layer] and [Chunk] to provide

packages/tiled/lib/src/common/point.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/common/property.dart

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
part of '../../tiled.dart';
1+
import 'package:collection/collection.dart';
2+
import 'package:tiled/tiled.dart';
3+
import 'package:xml/xml.dart';
24

35
/// Below is Tiled's documentation about how this structure is represented
46
/// on XML files:
@@ -37,7 +39,10 @@ class Property<T> {
3739
case PropertyType.color:
3840
return ColorProperty(
3941
name: name,
40-
value: parser.getColor('value', defaults: const Color(0x00000000)),
42+
value: parser.getColor(
43+
'value',
44+
defaults: const ColorData.hex(0x00000000),
45+
),
4146
hexValue: parser.getString('value', defaults: '#00000000'),
4247
);
4348

@@ -156,7 +161,7 @@ class ObjectProperty extends Property<int> {
156161
}
157162

158163
/// [value] is the color
159-
class ColorProperty extends Property<Color> {
164+
class ColorProperty extends Property<ColorData> {
160165
final String hexValue;
161166

162167
ColorProperty({

packages/tiled/lib/src/common/tiled_image.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
part of '../../tiled.dart';
1+
import 'package:meta/meta.dart';
2+
import 'package:tiled/src/parser.dart';
23

34
/// Below is Tiled's documentation about how this structure is represented
45
/// on XML files:

packages/tiled/lib/src/editor_setting/chunk_size.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/editor_setting/editor_setting.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/editor_setting/export.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/layer.dart

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
part of '../tiled.dart';
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
4+
import 'package:archive/archive.dart';
5+
import 'package:tiled/tiled.dart';
6+
import 'package:xml/xml.dart';
27

38
/// Below is Tiled's documentation about how this structure is represented
49
/// on XML files:
@@ -77,11 +82,11 @@ abstract class Layer {
7782
/// any graphics drawn by this layer or any child layers (optional).
7883
String? tintColorHex;
7984

80-
/// [Color] that is multiplied with any graphics drawn by this layer or any
81-
/// child layers (optional).
85+
/// [ColorData] that is multiplied with any graphics drawn by this layer or
86+
/// any child layers (optional).
8287
///
8388
/// Parsed from [tintColorHex], will be null if parsing fails for any reason.
84-
Color? tintColor;
89+
ColorData? tintColor;
8590

8691
/// The opacity of the layer as a value from 0 to 1. Defaults to 1.
8792
double opacity;
@@ -420,7 +425,7 @@ class TileLayer extends Layer {
420425
}
421426

422427
class ObjectGroup extends Layer {
423-
static const defaultColor = Color.fromARGB(255, 160, 160, 164);
428+
static const defaultColor = ColorData.rgb(160, 160, 164);
424429
static const defaultColorHex = '%a0a0a4';
425430

426431
/// topdown (default) or index (indexOrder).
@@ -433,12 +438,12 @@ class ObjectGroup extends Layer {
433438
/// this group. (defaults to gray (“#a0a0a4”))
434439
String colorHex;
435440

436-
/// [Color] used to display the objects in this group.
441+
/// [ColorData] used to display the objects in this group.
437442
/// (defaults to gray (“#a0a0a4”))
438443
///
439444
/// Parsed from [colorHex], will be fallback to [defaultColor] if parsing
440445
/// fails for any reason.
441-
Color color;
446+
ColorData color;
442447

443448
ObjectGroup({
444449
required super.name,
@@ -474,11 +479,11 @@ class ImageLayer extends Layer {
474479
/// (optional).
475480
String? transparentColorHex;
476481

477-
/// [Color] to be rendered as transparent (optional).
482+
/// [ColorData] to be rendered as transparent (optional).
478483
///
479484
/// Parsed from [transparentColorHex], will be null if parsing fails for any
480485
/// reason.
481-
Color? transparentColor;
486+
ColorData? transparentColor;
482487

483488
/// Whether or not to repeat the image on the X-axis
484489
bool repeatX;

packages/tiled/lib/src/objects/text.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/objects/tiled_object.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/parser.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
part of '../tiled.dart';
1+
import 'package:tiled/tiled.dart';
2+
import 'package:xml/xml.dart';
23

34
class ParsingException implements Exception {
45
final String name;
@@ -206,7 +207,7 @@ abstract class Parser {
206207
return result;
207208
}
208209

209-
Color? getColorOrNull(String name, {Color? defaults}) {
210+
ColorData? getColorOrNull(String name, {ColorData? defaults}) {
210211
final tiledColor = getStringOrNull(name);
211212

212213
// Tiled colors are stored as either ARGB or RGB hex values, so we can
@@ -221,13 +222,13 @@ abstract class Parser {
221222
}
222223

223224
if (colorValue != null) {
224-
return Color(colorValue);
225+
return ColorData.hex(colorValue);
225226
} else {
226227
return defaults;
227228
}
228229
}
229230

230-
Color getColor(String name, {Color? defaults}) {
231+
ColorData getColor(String name, {ColorData? defaults}) {
231232
final result = getColorOrNull(name, defaults: defaults);
232233
if (result == null) {
233234
throw ParsingException(name, null, 'Missing required color field');

packages/tiled/lib/src/template.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tile_map_parser.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
part of '../tiled.dart';
1+
import 'dart:convert';
2+
3+
import 'package:tiled/tiled.dart';
4+
import 'package:xml/xml.dart';
25

36
class TileMapParser {
47
static TiledMap parseJson(String json) {

packages/tiled/lib/src/tiled_map.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
part of '../tiled.dart';
1+
import 'dart:collection';
2+
3+
import 'package:collection/collection.dart';
4+
import 'package:tiled/tiled.dart';
5+
import 'package:xml/xml.dart';
26

37
/// Below is Tiled's documentation about how this structure is represented
48
/// on XML files:
@@ -74,12 +78,12 @@ class TiledMap {
7478
/// behind all other layers (optional).
7579
String? backgroundColorHex;
7680

77-
/// [Color] to be rendered as a solid color behind all other layers
81+
/// [ColorData] to be rendered as a solid color behind all other layers
7882
/// (optional).
7983
///
8084
/// Parsed from [backgroundColorHex], will be null if parsing fails for any
8185
/// reason.
82-
Color? backgroundColor;
86+
ColorData? backgroundColor;
8387

8488
int compressionLevel;
8589

packages/tiled/lib/src/tileset/grid.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tileset/terrain.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tileset/tile.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
part of '../../tiled.dart';
1+
import 'dart:math';
2+
3+
import 'package:tiled/tiled.dart';
24

35
/// Below is Tiled's documentation about how this structure is represented
46
/// on XML files:
@@ -27,7 +29,7 @@ class Tile {
2729
List<int?> terrain;
2830

2931
TiledImage? image;
30-
Rect? imageRect;
32+
Rectangle? imageRect;
3133
Layer? objectGroup;
3234
List<Frame> animation;
3335
CustomProperties properties;
@@ -66,7 +68,7 @@ class Tile {
6668
.toList() ??
6769
[],
6870
image: parser.getSingleChildOrNullAs('image', TiledImage.parse),
69-
imageRect: Rect.fromLTWH(
71+
imageRect: Rectangle(
7072
parser.getDoubleOrNull('x') ?? 0,
7173
parser.getDoubleOrNull('y') ?? 0,
7274
parser.getDoubleOrNull('width') ?? 0,

packages/tiled/lib/src/tileset/tile_offset.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../tiled.dart';
1+
import 'package:tiled/src/parser.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tileset/tileset.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
part of '../../tiled.dart';
1+
import 'dart:math';
2+
3+
import 'package:tiled/tiled.dart';
24

35
/// Below is Tiled's documentation about how this structure is represented
46
/// on XML files:
@@ -230,7 +232,7 @@ class Tileset {
230232
final tiles = <Tile>[];
231233

232234
for (var i = 0; i < tileCount; ++i) {
233-
Rect? imageRect;
235+
Rectangle? imageRect;
234236

235237
if (columns != null &&
236238
columns != 0 &&
@@ -239,7 +241,7 @@ class Tileset {
239241
final x = (i % columns) * tileWidth;
240242
final y = i ~/ columns * tileHeight;
241243

242-
imageRect = Rect.fromLTWH(
244+
imageRect = Rectangle(
243245
x.toDouble(),
244246
y.toDouble(),
245247
tileWidth.toDouble(),

packages/tiled/lib/src/tileset/wang/wang_color.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tileset/wang/wang_set.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../../../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// Below is Tiled's documentation about how this structure is represented
44
/// on XML files:

packages/tiled/lib/src/tileset/wang/wang_tile.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
part of '../../../tiled.dart';
1+
import 'dart:typed_data';
2+
3+
import 'package:tiled/src/parser.dart';
24

35
/// Below is Tiled's documentation about how this structure is represented
46
/// on XML files:

packages/tiled/lib/src/tsx_provider.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
part of '../tiled.dart';
1+
import 'package:tiled/tiled.dart';
22

33
/// abstract class to be implemented for an external tileset data provider.
44
abstract class TsxProvider {

0 commit comments

Comments
 (0)