Skip to content

Commit

Permalink
Allow setting background on create
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Sep 7, 2024
1 parent b1d6fc0 commit 5f0878d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 111 deletions.
15 changes: 14 additions & 1 deletion api/lib/src/models/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class QuokkaData extends ArchiveData<QuokkaData> {
GameTable getTableOrDefault([String name = '']) =>
getTable(name) ?? GameTable();

QuokkaData setTable(GameTable table, String name) => setAsset(
QuokkaData setTable(GameTable table, [String name = '']) => setAsset(
'$kGameTablePath/$name.json',
utf8.encode(table.toJson()),
);
Expand Down Expand Up @@ -213,6 +213,13 @@ class QuokkaData extends ArchiveData<QuokkaData> {
final metadata = getMetadataOrDefault().toJson();
return sha512256.convert(utf8.encode(metadata));
}

TranslationsStore getTranslationsStore(
{required String Function() getLocale}) =>
TranslationsStore(
translations: getAllTranslations(),
getLocale: getLocale,
);
}

final class PackItem<T> {
Expand Down Expand Up @@ -252,4 +259,10 @@ final class PackItem<T> {

String get namespace => location.namespace;
String get id => location.id;

PackItem<E> withItem<E>(E backgroundTranslation) => PackItem(
pack: pack,
location: location,
item: backgroundTranslation,
);
}
6 changes: 2 additions & 4 deletions app/lib/helpers/asset.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ class GameAssetManager extends AssetManager {
final key = file.pathWithoutLeadingSlash;
final pack = file.data!;
_loadedPacks[key] = pack;
_loadedTranslations[key] = TranslationsStore(
translations: pack.getAllTranslations(),
getLocale: () => currentLocale,
);
_loadedTranslations[key] =
pack.getTranslationsStore(getLocale: () => currentLocale);
} catch (_) {}
}
}
Expand Down
72 changes: 61 additions & 11 deletions app/lib/pages/home/create.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class _CreateDialogState extends State<CreateDialog>
late final Future<List<FileSystemFile<QuokkaData>>> _packsFuture;

String? _selectedTemplate;
PackItem<BackgroundTranslation>? _background;
List<String>? _selectedPacks;

bool _infoView = false;
Expand Down Expand Up @@ -113,16 +114,24 @@ class _CreateDialogState extends State<CreateDialog>
_CustomCreateView(
packsFuture: _packsFuture,
selectedPacksId: _selectedPacks,
onPacksSelected: (value) =>
setState(() => _selectedPacks = value),
onPacksSelected: (value) => setState(() {
_selectedPacks = value;
if (_background != null &&
!_selectedPacks!
.contains(_background!.namespace)) {
_background = null;
}
}),
),
ListView(
children: [
ListTile(
title:
Text(AppLocalizations.of(context).background),
subtitle:
Text(AppLocalizations.of(context).comingSoon),
subtitle: _background == null
? null
: Text(_background!.item.name),
onTap: _showBackgroundPicker,
),
],
)
Expand Down Expand Up @@ -294,13 +303,17 @@ class _CreateDialogState extends State<CreateDialog>
(await _packsFuture).map((e) => e.path).toList(),
),
);
template = template.setFileMetadata(
FileMetadata(
name: name,
description: description,
type: FileType.game,
),
);
template = template
.setFileMetadata(
FileMetadata(
name: name,
description: description,
type: FileType.game,
),
)
.setTable(GameTable(
background: _background?.location,
));
await _worldSystem.createFile(name, template);

if (context.mounted) {
Expand All @@ -314,6 +327,43 @@ class _CreateDialogState extends State<CreateDialog>
],
);
}

Future<void> _showBackgroundPicker() async {
List<PackItem<BackgroundDefinition>> backgrounds = [];
final packs =
_selectedPacks ?? (await _packsFuture).map((e) => e.path).toList();
for (final name in packs) {
final pack = await _fileSystem.getPack(name);
if (pack == null) continue;
final backgroundItems = pack.getBackgroundItems(name);
backgrounds.addAll(backgroundItems);
}
if (!mounted) return;
showLeapBottomSheet(
context: context,
titleBuilder: (context) => Text(AppLocalizations.of(context).background),
childrenBuilder: (context) => backgrounds
.sorted((a, b) => b.item.priority.compareTo(a.item.priority))
.map((entry) {
final translations = entry.pack.getTranslationsStore(
getLocale: () => Localizations.localeOf(context).languageCode);
final translation = translations.getBackgroundTranslation(entry.id);
return ListTile(
title: Text(translation.name),
subtitle: translation.description == null
? null
: Text(translation.description!),
onTap: () {
setState(() {
_background = entry.withItem(translation);
});
Navigator.of(context).pop();
},
selected: _background?.location == entry.location,
);
}).toList(),
);
}
}

class _CustomCreateView extends StatelessWidget {
Expand Down
95 changes: 0 additions & 95 deletions app/lib/widgets/loading.dart

This file was deleted.

0 comments on commit 5f0878d

Please sign in to comment.