Skip to content

Commit

Permalink
fix: image loading (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo authored Jan 27, 2024
1 parent 09e5b9c commit 9113879
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:fire_atlas_editor/store/store.dart';
import 'package:fire_atlas_editor/widgets/button.dart';
import 'package:fire_atlas_editor/widgets/image_selection_container.dart';
import 'package:fire_atlas_editor/widgets/text.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart';
import 'package:slices/slices.dart';

Expand All @@ -17,6 +16,7 @@ class ChangeImageModal extends StatefulWidget {

class _ChangeImageModalState extends State<ChangeImageModal> {
String? _imageData;
String? _imageName;

@override
Widget build(BuildContext ctx) {
Expand All @@ -28,10 +28,11 @@ class _ChangeImageModalState extends State<ChangeImageModal> {
child: ImageSelectionContainer(
margin: const EdgeInsets.all(30),
imageData: _imageData,
onSelectImage: (imageData) {
Flame.images.clearCache();
imageName: _imageName,
onSelectImage: (imageName, imageData) {
setState(() {
_imageData = imageData;
_imageName = imageName;
});
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ConcatImageModal extends StatefulWidget {

class _ConcatImageModalState extends State<ConcatImageModal> {
String? _imageData;
String? _imageName;
Vector2? _placement;
Rect? _selection;

Expand All @@ -40,9 +41,11 @@ class _ConcatImageModalState extends State<ConcatImageModal> {
right: 10,
),
imageData: _imageData,
onSelectImage: (imageData) {
imageName: _imageName,
onSelectImage: (imageName, imageData) {
setState(() {
_imageData = imageData;
_imageName = imageName;
});
},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class SelectionList extends StatelessWidget {
);
})
.toList()
.cast<Widget>()
.cast<Widget>(),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:fire_atlas_editor/widgets/button.dart';
import 'package:fire_atlas_editor/widgets/image_selection_container.dart';
import 'package:fire_atlas_editor/widgets/input_text_row.dart';
import 'package:fire_atlas_editor/widgets/text.dart';
import 'package:flame/flame.dart';
import 'package:flutter/material.dart' hide Image;
import 'package:slices/slices.dart';

Expand All @@ -25,6 +24,7 @@ class AtlasOptionsContainer extends StatefulWidget {

class _AtlasOptionsContainerState extends State<AtlasOptionsContainer> {
String? _imageData;
String? _imageName;
late final TextEditingController atlasNameController;
late final TextEditingController tileWidthController;
late final TextEditingController tileHeightController;
Expand Down Expand Up @@ -163,10 +163,11 @@ class _AtlasOptionsContainerState extends State<AtlasOptionsContainer> {
flex: 5,
child: ImageSelectionContainer(
imageData: _imageData,
onSelectImage: (imageData) {
Flame.images.clearCache();
imageName: _imageName,
onSelectImage: (imageName, imageData) {
setState(() {
_imageData = imageData;
_imageName = imageName;
});
},
),
Expand Down
1 change: 0 additions & 1 deletion fire_atlas_editor/lib/services/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Future<String> concatenateImages(
) async {
final original = await Flame.images.fromBase64('original', originalData);
final newImage = await Flame.images.fromBase64('newImage', newImageData);
Flame.images.clearCache();

final recorder = PictureRecorder();
final canvas = Canvas(recorder);
Expand Down
4 changes: 2 additions & 2 deletions fire_atlas_editor/lib/services/storage/desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ class FireAtlasStorage extends FireAtlasStorageApi {
}

@override
Future<String> selectFile() async {
Future<(String, String)> selectFile() async {
final file = await _selectDialog();
final bytes = await file.readAsBytes();
return base64Encode(bytes);
return (file.name, base64Encode(bytes));
}

Future<XFile> _selectDialog([XTypeGroup? typeGroup]) async {
Expand Down
2 changes: 1 addition & 1 deletion fire_atlas_editor/lib/services/storage/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class FireAtlasStorageApi {
Future<void> rememberProject(LoadedProjectEntry entry);
Future<String> selectNewProjectPath(FireAtlas atlas);
Future<LoadedProjectEntry> selectProject();
Future<String> selectFile();
Future<(String, String)> selectFile();
Future<void> exportFile(List<int> bytes, String fileName);
Future<void> setConfig(String key, String value);
Future<String> getConfig(String key, String defaultValue);
Expand Down
2 changes: 1 addition & 1 deletion fire_atlas_editor/lib/services/storage/unsupported.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class FireAtlasStorage extends FireAtlasStorageApi {
}

@override
Future<String> selectFile() {
Future<(String, String)> selectFile() {
throw 'Unsupported';
}

Expand Down
8 changes: 4 additions & 4 deletions fire_atlas_editor/lib/services/storage/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class FireAtlasStorage extends FireAtlasStorageApi {
@override
Future<LoadedProjectEntry> selectProject() async {
final fileData = await selectFile();
final base64 = fileData.substring(fileData.indexOf(',') + 1);
final base64 = fileData.$2.substring(fileData.$2.indexOf(',') + 1);
final atlas = _readBase64Project(base64);

return LoadedProjectEntry(
Expand All @@ -70,8 +70,8 @@ class FireAtlasStorage extends FireAtlasStorageApi {
}

@override
Future<String> selectFile() {
final completer = Completer<String>();
Future<(String, String)> selectFile() {
final completer = Completer<(String, String)>();
final uploadInput = FileUploadInputElement();
uploadInput.click();

Expand All @@ -85,7 +85,7 @@ class FireAtlasStorage extends FireAtlasStorageApi {
reader.onLoadEnd.listen((e) {
final result = reader.result;
if (result != null) {
completer.complete(result as String);
completer.complete((file.name, result as String));
}
});
reader.readAsDataUrl(file);
Expand Down
2 changes: 2 additions & 0 deletions fire_atlas_editor/lib/store/actions/atlas_actions.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:fire_atlas_editor/services/storage/storage.dart';
import 'package:fire_atlas_editor/store/actions/editor_actions.dart';
import 'package:fire_atlas_editor/store/store.dart';
import 'package:flame/flame.dart';
import 'package:flame_fire_atlas/flame_fire_atlas.dart';
import 'package:flutter/rendering.dart';
import 'package:slices/slices.dart';
Expand Down Expand Up @@ -48,6 +49,7 @@ class UpdateAtlasImageAction extends AsyncSlicesAction<FireAtlasState> {
if (state.currentAtlas != null) {
final atlas = state.currentAtlas!;

Flame.images.clearCache();
atlas.imageData = imageData;
await atlas.loadImage(clearImageData: false);

Expand Down
16 changes: 8 additions & 8 deletions fire_atlas_editor/lib/widgets/image_selection_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import 'package:flame/flame.dart';
import 'package:flame/sprite.dart';
import 'package:flutter/material.dart' hide Image;

typedef OnSelectImage = void Function(String);
typedef OnSelectImage = void Function(String, String);

class ImageSelectionContainer extends StatelessWidget {
final String? imageData;
final String? imageName;
final OnSelectImage onSelectImage;
final EdgeInsets margin;

const ImageSelectionContainer({
required this.onSelectImage,
this.imageData,
this.imageName,
this.margin = const EdgeInsets.only(
left: 30,
right: 2.5,
Expand All @@ -34,13 +36,11 @@ class ImageSelectionContainer extends StatelessWidget {
Expanded(
child: FContainer(
margin: margin,
child: imageData != null
child: imageData != null && imageName != null
? FutureBuilder<Image>(
// TODO(erick): image name
future: Flame.images.fromBase64('', imageData!),
future: Flame.images.fromBase64(imageName!, imageData!),
builder: (ctx, snapshot) {
if (snapshot.hasData) {
Flame.images.clearCache();
return SizedBox(
width: 200,
child: SimpleSpriteWidget(
Expand All @@ -61,10 +61,10 @@ class ImageSelectionContainer extends StatelessWidget {
label: 'Select image',
onSelect: () async {
final storage = FireAtlasStorage();
final imgDataUrl = await storage.selectFile();
onSelectImage(imgDataUrl);
final file = await storage.selectFile();
onSelectImage(file.$1, file.$2);
},
)
),
],
);
}
Expand Down
42 changes: 25 additions & 17 deletions fire_atlas_editor/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ packages:
dependency: transitive
description:
name: collection
sha256: "4a07be6cb69c84d677a6c3096fcf960cc3285a8330b4603e0d463d15d9bd934c"
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.dev"
source: hosted
version: "1.17.1"
version: "1.18.0"
convert:
dependency: transitive
description:
Expand Down Expand Up @@ -236,26 +236,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "6501fbd55da300384b768785b83e5ce66991266cec21af89ab9ae7f5ce1c4cbb"
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
source: hosted
version: "0.12.15"
version: "0.12.16"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: d92141dc6fe1dad30722f9aa826c7fbc896d021d792f80678280601aff8cf724
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
source: hosted
version: "0.2.0"
version: "0.5.0"
meta:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
ordered_set:
dependency: transitive
description:
Expand Down Expand Up @@ -401,26 +401,26 @@ packages:
dependency: transitive
description:
name: source_span
sha256: dd904f795d4b4f3b870833847c461801f6750a9fa8e61ea5ac53f9422b31f250
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
source: hosted
version: "1.9.1"
version: "1.10.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.dev"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "2.1.2"
string_scanner:
dependency: transitive
description:
Expand All @@ -441,10 +441,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: eb6ac1540b26de412b3403a163d919ba86f6a973fe6cc50ae3541b80092fdcfb
sha256: "5c2f730018264d276c20e4f1503fd1308dfbbae39ec8ee63c5236311ac06954b"
url: "https://pub.dev"
source: hosted
version: "0.5.1"
version: "0.6.1"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -525,6 +525,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
url: "https://pub.dev"
source: hosted
version: "0.3.0"
win32:
dependency: transitive
description:
Expand All @@ -542,5 +550,5 @@ packages:
source: hosted
version: "0.2.0+3"
sdks:
dart: ">=3.0.0 <4.0.0"
dart: ">=3.2.0-194.0.dev <4.0.0"
flutter: ">=3.3.0"

0 comments on commit 9113879

Please sign in to comment.