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: Adding way to configure a layer paint in flame tiled #2851

Merged
merged 5 commits into from
Nov 14, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ abstract class RenderableLayer<T extends Layer> {
required CameraComponent? camera,
required Map<Tile, TileFrames> animationFrames,
required TiledAtlas atlas,
required Paint Function(double opacity) layerPaintFactory,
FilterQuality? filterQuality,
bool? ignoreFlip,
Images? images,
Expand All @@ -55,6 +56,7 @@ abstract class RenderableLayer<T extends Layer> {
atlas: atlas.clone(),
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
layerPaintFactory: layerPaintFactory,
);
} else if (layer is ImageLayer) {
return FlameImageLayer.load(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class HexagonalTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
required super.layerPaintFactory,
super.filterQuality,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class IsometricTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
required super.layerPaintFactory,
super.filterQuality,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class OrthogonalTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
required super.layerPaintFactory,
super.filterQuality,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class StaggeredTileLayer extends FlameTileLayer {
required super.tiledAtlas,
required super.animationFrames,
required super.ignoreFlip,
required super.layerPaintFactory,
super.filterQuality,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import 'package:meta/meta.dart';
/// {@endtemplate}
@internal
abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
late final _layerPaint = Paint()
..color = Color.fromRGBO(255, 255, 255, opacity);
late final _layerPaint = layerPaintFactory(opacity);
final TiledAtlas tiledAtlas;
late List<List<MutableRSTransform?>> transforms;
final animations = <TileAnimation>[];
final Map<Tile, TileFrames> animationFrames;
final bool ignoreFlip;
Paint Function(double opacity) layerPaintFactory;

FlameTileLayer({
required super.layer,
Expand All @@ -50,6 +50,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
required this.tiledAtlas,
required this.animationFrames,
required this.ignoreFlip,
required this.layerPaintFactory,
super.filterQuality,
});

Expand All @@ -61,6 +62,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
required Vector2 destTileSize,
required Map<Tile, TileFrames> animationFrames,
required TiledAtlas atlas,
required Paint Function(double opacity) layerPaintFactory,
FilterQuality? filterQuality,
bool? ignoreFlip,
}) {
Expand All @@ -81,6 +83,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
animationFrames: animationFrames,
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
layerPaintFactory: layerPaintFactory,
);
case MapOrientation.staggered:
return StaggeredTileLayer(
Expand All @@ -92,6 +95,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
animationFrames: animationFrames,
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
layerPaintFactory: layerPaintFactory,
);
case MapOrientation.hexagonal:
return HexagonalTileLayer(
Expand All @@ -103,6 +107,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
animationFrames: animationFrames,
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
layerPaintFactory: layerPaintFactory,
);
case MapOrientation.orthogonal:
return OrthogonalTileLayer(
Expand All @@ -114,6 +119,7 @@ abstract class FlameTileLayer extends RenderableLayer<TileLayer> {
animationFrames: animationFrames,
ignoreFlip: ignoreFlip,
filterQuality: filterQuality,
layerPaintFactory: layerPaintFactory,
);
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/flame_tiled/lib/src/renderable_tile_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import 'package:flutter/painting.dart';
import 'package:flutter/services.dart';
import 'package:tiled/tiled.dart';

Paint _defaultLayerPaintFactory(double opacity) =>
Paint()..color = Color.fromRGBO(255, 255, 255, opacity);

/// {@template _renderable_tiled_map}
/// This is a wrapper over Tiled's [TiledMap] which can be rendered to a
/// canvas.
Expand Down Expand Up @@ -212,6 +215,7 @@ class RenderableTiledMap {
AssetBundle? bundle,
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
}) async {
final contents =
await (bundle ?? Flame.bundle).loadString('$prefix$fileName');
Expand All @@ -227,6 +231,7 @@ class RenderableTiledMap {
bundle: bundle,
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory ?? _defaultLayerPaintFactory,
);
}

Expand All @@ -247,6 +252,7 @@ class RenderableTiledMap {
AssetBundle? bundle,
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
}) async {
final map = await TiledMap.fromString(
contents,
Expand All @@ -263,6 +269,7 @@ class RenderableTiledMap {
bundle: bundle,
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory ?? _defaultLayerPaintFactory,
);
}

Expand All @@ -280,6 +287,7 @@ class RenderableTiledMap {
AssetBundle? bundle,
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
}) async {
// We're not going to load animation frames that are never referenced; but
// we do supply the common cache for all layers in this map, and maintain
Expand Down Expand Up @@ -307,6 +315,7 @@ class RenderableTiledMap {
),
ignoreFlip: ignoreFlip,
images: images,
layerPaintFactory: layerPaintFactory ?? _defaultLayerPaintFactory,
);

return RenderableTiledMap(
Expand All @@ -326,6 +335,7 @@ class RenderableTiledMap {
CameraComponent? camera,
Map<Tile, TileFrames> animationFrames, {
required TiledAtlas atlas,
required Paint Function(double opacity) layerPaintFactory,
bool? ignoreFlip,
Images? images,
}) {
Expand All @@ -342,6 +352,7 @@ class RenderableTiledMap {
atlas: atlas,
ignoreFlip: ignoreFlip,
images: images,
layerPaintFactory: layerPaintFactory,
);

if (layer is Group && renderableLayer is GroupLayer) {
Expand All @@ -355,6 +366,7 @@ class RenderableTiledMap {
atlas: atlas,
ignoreFlip: ignoreFlip,
images: images,
layerPaintFactory: layerPaintFactory,
);
}

Expand Down
4 changes: 3 additions & 1 deletion packages/flame_tiled/lib/src/tile_atlas.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ class TiledAtlas {
),
]);

final emptyPaint = Paint();
final emptyPaint = Paint()
..isAntiAlias = false
..filterQuality = FilterQuality.none;
for (final entry in mappedImageList) {
final tiledImage = entry.$2;
final tileImageSource = entry.$1;
Expand Down
2 changes: 2 additions & 0 deletions packages/flame_tiled/lib/src/tiled_component.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class TiledComponent<T extends FlameGame> extends PositionComponent
Images? images,
bool Function(Tileset)? tsxPackingFilter,
bool useAtlas = true,
Paint Function(double opacity)? layerPaintFactory,
}) async {
return TiledComponent(
await RenderableTiledMap.fromFile(
Expand All @@ -128,6 +129,7 @@ class TiledComponent<T extends FlameGame> extends PositionComponent
images: images,
tsxPackingFilter: tsxPackingFilter,
useAtlas: useAtlas,
layerPaintFactory: layerPaintFactory,
),
priority: priority,
);
Expand Down