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

Merged
merged 14 commits into from
Dec 28, 2024
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/chunk.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
53 changes: 53 additions & 0 deletions packages/tiled/lib/src/common/color.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import 'package:meta/meta.dart';

/// Basic data class holding a Color in ARGB format.
/// This can be converted to dart:ui's Color using the flame_tiled package
@immutable
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
Contributor 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 _hex;

int get alpha => _sub(_hex, 3);

int get red => _sub(_hex, 2);

int get green => _sub(_hex, 1);

int get blue => _sub(_hex, 0);

/// Parses the Color from an int using the lower 32-bits and tiled's format:
/// 0xaarrggbb
const ColorData.hex(this._hex);

const ColorData.rgb(int red, int green, int blue, [int alpha = 255])
: assert(red >= 0 && red <= 255),
assert(green >= 0 && green <= 255),
assert(blue >= 0 && blue <= 255),
assert(alpha >= 0 && alpha <= 255),
_hex = (alpha << 3 * 8) +
(red << 2 * 8) +
(green << 1 * 8) +
(blue << 0 * 8);

const ColorData.argb(int alpha, int red, int green, int blue)
: assert(red >= 0 && red <= 255),
assert(green >= 0 && green <= 255),
assert(blue >= 0 && blue <= 255),
assert(alpha >= 0 && alpha <= 255),
_hex = (alpha << 3 * 8) +
(red << 2 * 8) +
(green << 1 * 8) +
(blue << 0 * 8);

@override
bool operator ==(Object other) {
if (other is! ColorData) {
return false;
}
return _hex == other._hex;
}

@override
int get hashCode => _hex.hashCode;
}
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/enums.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

enum MapOrientation { orthogonal, isometric, staggered, hexagonal }

Expand Down
2 changes: 0 additions & 2 deletions packages/tiled/lib/src/common/flips.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
part of '../../tiled.dart';

class Flips {
final bool horizontally;
final bool vertically;
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/frame.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/gid.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// A [Gid], Global Tile ID is a Tiled concept to represent the tiles inside
/// int matrices. This wrapper is used by [Layer] and [Chunk] to provide
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/common/point.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
11 changes: 8 additions & 3 deletions packages/tiled/lib/src/common/property.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../../tiled.dart';
import 'package:collection/collection.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down Expand Up @@ -37,7 +39,10 @@ class Property<T> {
case PropertyType.color:
return ColorProperty(
name: name,
value: parser.getColor('value', defaults: const Color(0x00000000)),
value: parser.getColor(
'value',
defaults: const ColorData.hex(0x00000000),
),
hexValue: parser.getString('value', defaults: '#00000000'),
);

Expand Down Expand Up @@ -156,7 +161,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
3 changes: 2 additions & 1 deletion packages/tiled/lib/src/common/tiled_image.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
part of '../../tiled.dart';
import 'package:meta/meta.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/editor_setting/chunk_size.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/editor_setting/editor_setting.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/editor_setting/export.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
23 changes: 14 additions & 9 deletions packages/tiled/lib/src/layer.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
part of '../tiled.dart';
import 'dart:convert';
import 'dart:typed_data';

import 'package:archive/archive.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down Expand Up @@ -77,11 +82,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
/// child layers (optional).
/// [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 +425,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);
static const defaultColorHex = '%a0a0a4';

/// topdown (default) or index (indexOrder).
Expand All @@ -433,12 +438,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({
required super.name,
Expand Down Expand Up @@ -474,11 +479,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
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/objects/text.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/objects/tiled_object.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
9 changes: 5 additions & 4 deletions packages/tiled/lib/src/parser.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
part of '../tiled.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

class ParsingException implements Exception {
final String name;
Expand Down Expand Up @@ -206,7 +207,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 @@ -221,13 +222,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
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/template.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
5 changes: 4 additions & 1 deletion packages/tiled/lib/src/tile_map_parser.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
part of '../tiled.dart';
import 'dart:convert';

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

class TileMapParser {
static TiledMap parseJson(String json) {
Expand Down
10 changes: 7 additions & 3 deletions packages/tiled/lib/src/tiled_map.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
part of '../tiled.dart';
import 'dart:collection';

import 'package:collection/collection.dart';
import 'package:tiled/tiled.dart';
import 'package:xml/xml.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down Expand Up @@ -74,12 +78,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
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/grid.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/terrain.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
8 changes: 5 additions & 3 deletions packages/tiled/lib/src/tileset/tile.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../../tiled.dart';
import 'dart:math';

import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down Expand Up @@ -27,7 +29,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 +68,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
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/tile_offset.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../tiled.dart';
import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
8 changes: 5 additions & 3 deletions packages/tiled/lib/src/tileset/tileset.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../../tiled.dart';
import 'dart:math';

import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down Expand Up @@ -230,7 +232,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 +241,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/src/tileset/wang/wang_color.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tileset/wang/wang_set.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../../../tiled.dart';
import 'package:tiled/tiled.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
4 changes: 3 additions & 1 deletion packages/tiled/lib/src/tileset/wang/wang_tile.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
part of '../../../tiled.dart';
import 'dart:typed_data';

import 'package:tiled/src/parser.dart';

/// Below is Tiled's documentation about how this structure is represented
/// on XML files:
Expand Down
2 changes: 1 addition & 1 deletion packages/tiled/lib/src/tsx_provider.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
part of '../tiled.dart';
import 'package:tiled/tiled.dart';

/// abstract class to be implemented for an external tileset data provider.
abstract class TsxProvider {
Expand Down
Loading
Loading