Skip to content

Commit

Permalink
feat: Adding a quick auto replace image
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo committed Oct 9, 2024
1 parent 2d06004 commit 1888091
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ChangeImageModal extends StatefulWidget {

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

@override
Expand All @@ -29,9 +30,14 @@ class _ChangeImageModalState extends State<ChangeImageModal> {
margin: const EdgeInsets.all(30),
imageData: _imageData,
imageName: _imageName,
onSelectImage: (imageName, imageData) {
onSelectImage: ({
required imageName,
required imagePath,
required imageData,
}) {
setState(() {
_imageData = imageData;
_imagePath = imagePath;
_imageName = imageName;
});
},
Expand All @@ -56,6 +62,7 @@ class _ChangeImageModalState extends State<ChangeImageModal> {
store.dispatchAsync(
UpdateAtlasImageAction(
imageData: _imageData!,
imagePath: _imagePath!,
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ class _ConcatImageModalState extends State<ConcatImageModal> {
),
imageData: _imageData,
imageName: _imageName,
onSelectImage: (imageName, imageData) {
onSelectImage: ({
required imageName,
required imagePath,
required imageData,
}) {
setState(() {
_imageData = imageData;
_imageName = imageName;
Expand Down
17 changes: 16 additions & 1 deletion fire_atlas_editor/lib/screens/editor_screen/widgets/toolbar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,20 @@ import 'package:slices/slices.dart';

class _ToolbarSlice extends Equatable {
final FireAtlas? currentAtlas;
final String? lastUsedImage;
final bool hasChanges;

_ToolbarSlice.fromState(FireAtlasState state)
: currentAtlas = state.currentAtlas,
lastUsedImage = state.loadedProject.value?.lastUsedImage,
hasChanges = state.hasChanges;

@override
List<Object?> get props => [currentAtlas?.id, hasChanges];
List<Object?> get props => [
currentAtlas?.id,
lastUsedImage,
hasChanges,
];
}

class Toolbar extends StatelessWidget {
Expand Down Expand Up @@ -91,6 +97,15 @@ class Toolbar extends StatelessWidget {
},
tooltip: 'Update base image',
),
if (!kIsWeb)
FIconButton(
disabled: slice.lastUsedImage == null,
iconData: Icons.fireplace,
onPress: () {
store.dispatchAsync(QuickReplaceImageAction());
},
tooltip: 'Quick replace base image',
),
FIconButton(
iconData: Icons.add_photo_alternate,
onPress: () {
Expand Down
10 changes: 2 additions & 8 deletions fire_atlas_editor/lib/screens/open_screen/open_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class _OpenScreenState extends State<OpenScreen> {
onConfirm: (
String atlasName,
String imageData,
String imagePath,
double tileWidth,
double tileHeight,
) async {
Expand All @@ -82,6 +83,7 @@ class _OpenScreenState extends State<OpenScreen> {
CreateAtlasAction(
id: atlasName,
imageData: imageData,
imagePath: imagePath,
tileWidth: tileWidth,
tileHeight: tileHeight,
),
Expand Down Expand Up @@ -155,14 +157,6 @@ class _OpenScreenState extends State<OpenScreen> {
Expanded(
child: Image.asset('assets/Logo.png'),
),
if (!kIsWeb && Platform.isMacOS)
Container(
width: 300,
child: _Buttons(
importAtlas: _importAtlas,
newAtlas: _newAtlas,
),
),
],
),
FContainer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:slices/slices.dart';

class AtlasOptionsContainer extends StatefulWidget {
final void Function() onCancel;
final Function(String, String, double, double) onConfirm;
final Function(String, String, String, double, double) onConfirm;

const AtlasOptionsContainer({
required this.onCancel,
Expand All @@ -25,6 +25,8 @@ class AtlasOptionsContainer extends StatefulWidget {
class _AtlasOptionsContainerState extends State<AtlasOptionsContainer> {
String? _imageData;
String? _imageName;
String? _imagePath;

late final TextEditingController atlasNameController;
late final TextEditingController tileWidthController;
late final TextEditingController tileHeightController;
Expand Down Expand Up @@ -114,6 +116,7 @@ class _AtlasOptionsContainerState extends State<AtlasOptionsContainer> {
widget.onConfirm(
atlasName,
_imageData!,
_imagePath!,
double.parse(tileWidthRaw),
double.parse(tileHeightRaw),
);
Expand Down Expand Up @@ -164,9 +167,14 @@ class _AtlasOptionsContainerState extends State<AtlasOptionsContainer> {
child: ImageSelectionContainer(
imageData: _imageData,
imageName: _imageName,
onSelectImage: (imageName, imageData) {
onSelectImage: ({
required imageName,
required imagePath,
required imageData,
}) {
setState(() {
_imageData = imageData;
_imagePath = imagePath;
_imageName = imageName;
});
},
Expand Down
43 changes: 39 additions & 4 deletions fire_atlas_editor/lib/services/storage/desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class FireAtlasStorage extends FireAtlasStorageApi {
final file = File(path);
final raw = await file.readAsBytes();
final atlas = FireAtlas.deserializeBytes(raw);
return LoadedProjectEntry(path, atlas);
final lastUsedImage = await getProjectLastImageFile(atlas.id);
return LoadedProjectEntry(path, atlas, lastUsedImage);
}

@override
Expand Down Expand Up @@ -52,13 +53,43 @@ class FireAtlasStorage extends FireAtlasStorageApi {
prefs.setString('PROJECT_${entry.project.id}', path);
}

@override
Future<void> rememberProjectImageFile(
String projectId,
String imagePath,
) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('LAST_IMAGE_$projectId', imagePath);
}

@override
Future<String?> getProjectLastImageFile(String projectId) async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString('LAST_IMAGE_$projectId');
}

@override
Future<LoadedProjectEntry> selectProject() async {
const typeGroup = XTypeGroup(label: 'fire atlas', extensions: ['fa']);
final file = await _selectDialog(typeGroup);
final bytes = await file.readAsBytes();
final atlas = FireAtlas.deserializeBytes(bytes);
return LoadedProjectEntry(file.path, atlas);

final lastImage = await getProjectLastImageFile(atlas.id);

return LoadedProjectEntry(
file.path,
atlas,
lastImage,
);
}

@override
Future<String> readImageData(String path) async {
final file = File(path);
final bytes = await file.readAsBytes();

return base64Encode(bytes);
}

@override
Expand All @@ -71,10 +102,14 @@ class FireAtlasStorage extends FireAtlasStorageApi {
}

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

Future<XFile> _selectDialog([XTypeGroup? typeGroup]) async {
Expand Down
8 changes: 7 additions & 1 deletion fire_atlas_editor/lib/services/storage/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ abstract class FireAtlasStorageApi {
Future<void> saveProject(LoadedProjectEntry entry);
Future<List<LastProjectEntry>> lastUsedProjects();
Future<void> rememberProject(LoadedProjectEntry entry);
Future<void> rememberProjectImageFile(
String projectId,
String imageFile,
);
Future<String?> getProjectLastImageFile(String projectId);
Future<String> selectNewProjectPath(FireAtlas atlas);
Future<LoadedProjectEntry> selectProject();
Future<(String, String)> selectFile();
Future<(String, String, String)> selectFile();
Future<String> readImageData(String path);
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, String)> selectFile() {
Future<(String, String, String)> selectFile() {
throw 'Unsupported';
}

Expand Down
26 changes: 23 additions & 3 deletions fire_atlas_editor/lib/services/storage/web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,23 @@ class FireAtlasStorage extends FireAtlasStorageApi {
return LoadedProjectEntry(
'ATLAS_${atlas.id}',
atlas,
null,
);
}

@override
Future<String> readImageData(String path) async {
throw 'Unsupported';
}

@override
Future<String> selectNewProjectPath(FireAtlas atlas) async {
return 'ATLAS_${atlas.id}';
}

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

Expand All @@ -85,7 +91,13 @@ class FireAtlasStorage extends FireAtlasStorageApi {
reader.onLoadEnd.listen((e) {
final result = reader.result;
if (result != null) {
completer.complete((file.name, result as String));
completer.complete(
(
file.name,
file.relativePath ?? file.name,
result as String,
),
);
}
});
reader.readAsDataUrl(file);
Expand Down Expand Up @@ -123,4 +135,12 @@ class FireAtlasStorage extends FireAtlasStorageApi {

return FireAtlas.deserializeBytes(jsonRaw);
}

@override
Future<void> rememberProjectImageFile(
String projectId,
String imageFile,
) async {
// Noop on web
}
}
Loading

0 comments on commit 1888091

Please sign in to comment.