Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Dart SDK compatibility #77

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/tiled/lib/src/common/color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
part of tiled;

/// Basic data class holding a Color in ARGB format.
/// This can be converted to dart:ui's Color using the flame_tiled package
class ColorData {
static int _sub(int hex, int index) => (hex >> index * 8) & 0x000000ff;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw this in the other PR; is there a mix up between the two? If so; my comment there is valid here as well: why not store the "hex" as one value and do bit shifting in setters?

This is what Color was doing before and so the pattern would match and the memory footprint should be the same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right that would be more efficient! This change does belong here, but I based the PR‘s branch on this one so I don‘t have to integrate the new types later!


final int red;
final int green;
final int blue;
final int alpha;

/// Parses the Color from an int using the lower 32-bits and tiled's format: 0xaarrggbb
ColorData.hex(int hex)
: alpha = _sub(hex, 3),
red = _sub(hex, 2),
green = _sub(hex, 1),
blue = _sub(hex, 0);

const ColorData.rgb(this.red, this.green, this.blue, [this.alpha = 255])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"rgba"? and why deviate from "argb"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t really know, it just felt more natural. For most colors the alpha value is just 0xff so it can have a default value at the end. CSS also puts it at the end #rrggbbaa, I believe for the same reason.

This constructor is meant for users and not used internally, so I think this way is a better experience. However I‘m happy to change it!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I was trying to stick with the dart:ui color for familiarity.

: assert(red >= 0 && red <= 255),
assert(green >= 0 && green <= 255),
assert(blue >= 0 && blue <= 255),
assert(alpha >= 0 && alpha <= 255);
}
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/common/property.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Property<T> {
case PropertyType.color:
return ColorProperty(
name: name,
value: parser.getColor('value', defaults: const Color(0x00000000)),
value: parser.getColor('value', defaults: ColorData.hex(0x00000000)),
hexValue: parser.getString('value', defaults: '#00000000'),
);

Expand Down Expand Up @@ -156,7 +156,7 @@ class ObjectProperty extends Property<int> {
}

/// [value] is the color
class ColorProperty extends Property<Color> {
class ColorProperty extends Property<ColorData> {
final String hexValue;

ColorProperty({
Expand Down
14 changes: 7 additions & 7 deletions packages/tiled/lib/src/layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ abstract class Layer {
/// any graphics drawn by this layer or any child layers (optional).
String? tintColorHex;

/// [Color] that is multiplied with any graphics drawn by this layer or any
/// [ColorData] that is multiplied with any graphics drawn by this layer or any
/// child layers (optional).
///
/// Parsed from [tintColorHex], will be null if parsing fails for any reason.
Color? tintColor;
ColorData? tintColor;

/// The opacity of the layer as a value from 0 to 1. Defaults to 1.
double opacity;
Expand Down Expand Up @@ -420,7 +420,7 @@ class TileLayer extends Layer {
}

class ObjectGroup extends Layer {
static const defaultColor = Color.fromARGB(255, 160, 160, 164);
static const defaultColor = ColorData.rgb(160, 160, 164, 255);
static const defaultColorHex = '%a0a0a4';

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

/// [Color] used to display the objects in this group.
/// [ColorData] used to display the objects in this group.
/// (defaults to gray (“#a0a0a4”))
///
/// Parsed from [colorHex], will be fallback to [defaultColor] if parsing
/// fails for any reason.
Color color;
ColorData color;

ObjectGroup({
super.id,
Expand Down Expand Up @@ -474,11 +474,11 @@ class ImageLayer extends Layer {
/// (optional).
String? transparentColorHex;

/// [Color] to be rendered as transparent (optional).
/// [ColorData] to be rendered as transparent (optional).
///
/// Parsed from [transparentColorHex], will be null if parsing fails for any
/// reason.
Color? transparentColor;
ColorData? transparentColor;

/// Whether or not to repeat the image on the X-axis
bool repeatX;
Expand Down
6 changes: 3 additions & 3 deletions packages/tiled/lib/src/parser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ abstract class Parser {
return result;
}

Color? getColorOrNull(String name, {Color? defaults}) {
ColorData? getColorOrNull(String name, {ColorData? defaults}) {
final tiledColor = getStringOrNull(name);

// Tiled colors are stored as either ARGB or RGB hex values, so we can
Expand All @@ -212,13 +212,13 @@ abstract class Parser {
}

if (colorValue != null) {
return Color(colorValue);
return ColorData.hex(colorValue);
} else {
return defaults;
}
}

Color getColor(String name, {Color? defaults}) {
ColorData getColor(String name, {ColorData? defaults}) {
final result = getColorOrNull(name, defaults: defaults);
if (result == null) {
throw ParsingException(name, null, 'Missing required color field');
Expand Down
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ class TiledMap {
/// behind all other layers (optional).
String? backgroundColorHex;

/// [Color] to be rendered as a solid color behind all other layers
/// [ColorData] to be rendered as a solid color behind all other layers
/// (optional).
///
/// Parsed from [backgroundColorHex], will be null if parsing fails for any
/// reason.
Color? backgroundColor;
ColorData? backgroundColor;

int compressionLevel;

Expand Down
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/tileset/tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Tile {
List<int?> terrain;

TiledImage? image;
Rect? imageRect;
Rectangle? imageRect;
Layer? objectGroup;
List<Frame> animation;
CustomProperties properties;
Expand Down Expand Up @@ -66,7 +66,7 @@ class Tile {
.toList() ??
[],
image: parser.getSingleChildOrNullAs('image', TiledImage.parse),
imageRect: Rect.fromLTWH(
imageRect: Rectangle(
parser.getDoubleOrNull('x') ?? 0,
parser.getDoubleOrNull('y') ?? 0,
parser.getDoubleOrNull('width') ?? 0,
Expand Down
4 changes: 2 additions & 2 deletions packages/tiled/lib/src/tileset/tileset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Tileset {
final tiles = <Tile>[];

for (var i = 0; i < tileCount; ++i) {
Rect? imageRect;
Rectangle? imageRect;

if (columns != null &&
columns != 0 &&
Expand All @@ -239,7 +239,7 @@ class Tileset {
final x = (i % columns) * tileWidth;
final y = i ~/ columns * tileHeight;

imageRect = Rect.fromLTWH(
imageRect = Rectangle(
x.toDouble(),
y.toDouble(),
tileWidth.toDouble(),
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/tiled.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:collection';
import 'dart:convert';
import 'dart:math' show Rectangle;
import 'dart:typed_data';
import 'dart:ui';

import 'package:archive/archive.dart';
import 'package:collection/collection.dart';
Expand All @@ -19,6 +18,7 @@ part 'src/common/gid.dart';
part 'src/common/point.dart';
part 'src/common/property.dart';
part 'src/common/tiled_image.dart';
part 'src/common/color.dart';
part 'src/editor_setting/chunk_size.dart';
part 'src/editor_setting/editor_setting.dart';
part 'src/editor_setting/export.dart';
Expand Down
7 changes: 2 additions & 5 deletions packages/tiled/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ environment:
dependencies:
archive: ^3.3.0
collection: ^1.16.0
flutter:
sdk: flutter
meta: ^1.7.0
xml: ^6.1.0

dev_dependencies:
dartdoc: ^6.0.1
flame_lint: ^0.2.0
flutter_test:
sdk: flutter
flame_lint: ^1.1.1
test: ^1.24.8
25 changes: 25 additions & 0 deletions packages/tiled/test/color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:math';

import 'package:test/expect.dart';
import 'package:test/scaffolding.dart';
import 'package:tiled/tiled.dart';

void main() {
group("ColorData.hex", () {
test("parse", () {
final random = Random();
final red = random.nextInt(256);
final green = random.nextInt(256);
final blue = random.nextInt(256);
final alpha = random.nextInt(256);

final hex = alpha << 24 | red << 16 | green << 8 | blue << 0;
final data = ColorData.hex(hex);

expect(data.alpha, equals(alpha));
expect(data.red, equals(red));
expect(data.green, equals(green));
expect(data.blue, equals(blue));
});
});
}
2 changes: 1 addition & 1 deletion packages/tiled/test/complexmap_infinite_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/image_layer_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:tiled/tiled.dart';
import 'package:test/test.dart';

void main() {
late TiledMap map;
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/isometric_staggered_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/isometric_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
5 changes: 2 additions & 3 deletions packages/tiled/test/layer_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down Expand Up @@ -75,7 +74,7 @@ void main() {
expect(layer.tintColorHex, equals('#ffaabb'));
expect(
layer.tintColor,
equals(Color(int.parse('0xffffaabb'))),
equals(ColorData.hex(int.parse('0xffffaabb'))),
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/map_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
5 changes: 2 additions & 3 deletions packages/tiled/test/object_group_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:io';
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down Expand Up @@ -31,7 +30,7 @@ void main() {
expect(objectGroup.colorHex, equals('#555500'));
expect(
objectGroup.color,
equals(Color(int.parse('0xff555500'))),
equals(ColorData.hex(int.parse('0xff555500'))),
);
});

Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/overflow_bug_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/parser_compare_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
11 changes: 5 additions & 6 deletions packages/tiled/test/parser_test.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import 'dart:io';
import 'dart:math' show Rectangle;
import 'dart:ui';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

Expand Down Expand Up @@ -44,7 +43,7 @@ void main() {
expect(map.backgroundColorHex, equals('#ccddaaff'));
expect(
map.backgroundColor,
equals(Color(int.parse('0xccddaaff'))),
equals(ColorData.hex(int.parse('0xccddaaff'))),
);
});

Expand Down Expand Up @@ -89,7 +88,7 @@ void main() {
);
expect(
properties.getValue<String>('multiline string'),
equals('Hello,\nWorld'),
equals('Hello,\r\nWorld'),
);
expect(
properties.getValue<int>('integer property'),
Expand All @@ -100,8 +99,8 @@ void main() {
equals('#00112233'),
);
expect(
properties.getValue<Color>('color property'),
equals(const Color(0x00112233)),
properties.getValue<ColorData>('color property'),
equals(ColorData.hex(0x00112233)),
);
expect(
properties.getValue<double>('float property'),
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/test/rectangle_map_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:io';

import 'package:flutter_test/flutter_test.dart';
import 'package:test/test.dart';
import 'package:tiled/tiled.dart';

void main() {
Expand Down
Loading