From e2657a3bb5229dad240951c3b7cd4d56ee0caa12 Mon Sep 17 00:00:00 2001 From: zontreck Date: Mon, 22 Jul 2024 03:19:23 -0700 Subject: [PATCH] Implement adding tags --- Icons/PNG/{Integer.png => Int.png} | Bin Icons/PNG/{IntegerArray.png => IntArray.png} | Bin lib/pages/AddPage.dart | 151 +++++++++++++++++-- lib/pages/EditValue.dart | 4 +- lib/tags/ByteArrayTag.dart | 2 +- lib/tags/ByteTag.dart | 2 +- lib/tags/CompoundTag.dart | 2 +- lib/tags/DoubleTag.dart | 2 +- lib/tags/FloatTag.dart | 2 +- lib/tags/IntArrayTag.dart | 2 +- lib/tags/IntTag.dart | 2 +- lib/tags/ListTag.dart | 2 +- lib/tags/LongArrayTag.dart | 2 +- lib/tags/LongTag.dart | 2 +- lib/tags/ShortTag.dart | 2 +- lib/tags/StringTag.dart | 2 +- lib/tags/Tag.dart | 11 +- pubspec.yaml | 4 +- 18 files changed, 168 insertions(+), 26 deletions(-) rename Icons/PNG/{Integer.png => Int.png} (100%) rename Icons/PNG/{IntegerArray.png => IntArray.png} (100%) diff --git a/Icons/PNG/Integer.png b/Icons/PNG/Int.png similarity index 100% rename from Icons/PNG/Integer.png rename to Icons/PNG/Int.png diff --git a/Icons/PNG/IntegerArray.png b/Icons/PNG/IntArray.png similarity index 100% rename from Icons/PNG/IntegerArray.png rename to Icons/PNG/IntArray.png diff --git a/lib/pages/AddPage.dart b/lib/pages/AddPage.dart index 53530d8..e7b8588 100644 --- a/lib/pages/AddPage.dart +++ b/lib/pages/AddPage.dart @@ -1,6 +1,15 @@ import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; import 'package:libac_dart/nbt/Tag.dart'; +import 'package:libac_dart/nbt/impl/ByteArrayTag.dart'; +import 'package:libac_dart/nbt/impl/IntArrayTag.dart'; +import 'package:libac_dart/nbt/impl/ListTag.dart'; +import 'package:libac_dart/nbt/impl/LongArrayTag.dart'; import 'package:nbteditor/Constants.dart'; +import 'package:nbteditor/pages/EditValue.dart'; +import 'package:nbteditor/pages/RenamePrompt.dart'; + +import '../tags/Tag.dart'; class AddPage extends StatefulWidget { const AddPage({super.key}); @@ -10,26 +19,146 @@ class AddPage extends StatefulWidget { } class AddState extends State { + List allowedTagTypes = []; + bool canAddAnyType = false; + bool isList = false; + bool isArray = false; + String newTagName = ""; + dynamic val; + Tag? ParentTag; + bool singleShot = true; + + Function? didChangeState; + @override void didChangeDependencies() { var args = ModalRoute.of(context)!.settings.arguments as AddElementArgs; + + canAddAnyType = args.allowAllTagTypes; + isList = args.tag.getTagType() == TagType.List; + isArray = args.tag.getTagType() == TagType.ByteArray || + args.tag.getTagType() == TagType.IntArray || + args.tag.getTagType() == TagType.LongArray; + + if (isList || isArray) { + allowedTagTypes = args.allowedTagTypes; + } + + if (canAddAnyType) { + allowedTagTypes.clear(); + for (TagType type in TagType.values) { + if (type == TagType.End) { + continue; + } else { + allowedTagTypes.add(type); + } + } + } + + didChangeState = args.didChangeState; + ParentTag = args.tag; + } + + Future processTagType(TagType type) async { + if (!(isArray || isList)) { + var reply = await showDialog( + context: context, + routeSettings: RouteSettings(arguments: ""), + builder: (builder) { + return RenamePrompt(); + }); + newTagName = reply as String; + } + var nTag = Tag.makeTagOfType(type); + bool hasValue = true; + + if (type == TagType.List || + type == TagType.ByteArray || + type == TagType.LongArray || + type == TagType.IntArray || + type == TagType.Compound) { + hasValue = false; + } else { + nTag.setKey(newTagName); + var reply = await showDialog( + context: context, + routeSettings: RouteSettings(arguments: nTag), + builder: (builder) { + return EditValuePrompt(); + }); + val = reply; + } + + if (isArray) { + switch (ParentTag!.getTagType()) { + case TagType.LongArray: + { + LongArrayTag LAT = ParentTag! as LongArrayTag; + LAT.value.add(val); + break; + } + case TagType.IntArray: + { + IntArrayTag IAT = ParentTag! as IntArrayTag; + IAT.value.add(val); + break; + } + + case TagType.ByteArray: + { + ByteArrayTag BAT = ParentTag! as ByteArrayTag; + BAT.value.add(val); + break; + } + default: + { + break; + } + } + } else { + if (hasValue) nTag.setValue(val); + + if (isList) { + ListTag lst = ParentTag! as ListTag; + lst.add(nTag); + } else { + ParentTag!.asCompoundTag().put(newTagName, nTag); + } + } + + didChangeState!.call(); + Navigator.pop(context); } @override Widget build(BuildContext context) { + if (isList || isArray && !canAddAnyType && singleShot) { + singleShot = false; + + Future.delayed(Duration(seconds: 2), () { + processTagType(allowedTagTypes[0]); + }); + } + return Scaffold( appBar: AppBar( - title: const Text("Add Tag"), + title: const Text("Add Tag - Select Tag Type"), backgroundColor: Constants.TITLEBAR_COLOR, ), - body: const Padding( - padding: EdgeInsets.all(8), - child: SingleChildScrollView( - child: Column( - children: [], - ), - ), - ), + body: Padding( + padding: EdgeInsets.all(8), + child: ListView.builder( + itemBuilder: (builder, index) { + return ListTile( + title: Text(allowedTagTypes[index].name), + leading: TagExt.getTagIcon(allowedTagTypes[index]), + onTap: () async { + processTagType(allowedTagTypes[index]); + }, + ); + }, + itemCount: allowedTagTypes.length, + )), ); } } @@ -38,6 +167,7 @@ class AddElementArgs { Tag tag; bool allowAllTagTypes; bool isArray; + Function didChangeState; List allowedTagTypes; @@ -45,5 +175,6 @@ class AddElementArgs { {required this.tag, required this.allowAllTagTypes, required this.isArray, - this.allowedTagTypes = const []}); + this.allowedTagTypes = const [], + required this.didChangeState}); } diff --git a/lib/pages/EditValue.dart b/lib/pages/EditValue.dart index 2b383f8..4260256 100644 --- a/lib/pages/EditValue.dart +++ b/lib/pages/EditValue.dart @@ -17,7 +17,9 @@ class EditValueState extends State { @override void didChangeDependencies() { - var args = ModalRoute.of(context)!.settings.arguments!; + var args = ModalRoute.of(context)!.settings.arguments; + + if (args == null) return; if (args is Tag) { Tag tag = args as Tag; diff --git a/lib/tags/ByteArrayTag.dart b/lib/tags/ByteArrayTag.dart index 1727f60..42fd739 100644 --- a/lib/tags/ByteArrayTag.dart +++ b/lib/tags/ByteArrayTag.dart @@ -22,7 +22,7 @@ extension ByteArrayTagExt on ByteArrayTag { Widget render(BuildContext context, Function didChangeState) { return ListTile( title: Text("TAG_ByteArray (${getKey()})"), - leading: const Image(image: AssetImage("Icons/PNG/ByteArray.png")), + leading: TagExt.getTagIcon(getTagType()), subtitle: TagExt.getElementDescriptor("${value.length} entries"), trailing: TagExt.getElementButtons( true, canBeNamed(this), false, this, context, didChangeState), diff --git a/lib/tags/ByteTag.dart b/lib/tags/ByteTag.dart index cc97f12..a45985b 100644 --- a/lib/tags/ByteTag.dart +++ b/lib/tags/ByteTag.dart @@ -14,7 +14,7 @@ extension ByteTagExt on ByteTag { subtitle: TagExt.getElementDescriptor( "$value", ), - leading: const Image(image: AssetImage("Icons/PNG/Byte.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/CompoundTag.dart b/lib/tags/CompoundTag.dart index 940102b..211d1bb 100644 --- a/lib/tags/CompoundTag.dart +++ b/lib/tags/CompoundTag.dart @@ -10,7 +10,7 @@ extension CompoundTagExt on CompoundTag { subtitle: TagExt.getElementDescriptor( "${value.length} tag${value.length > 1 ? "s" : ""}", ), - leading: const Image(image: AssetImage("Icons/PNG/Compound.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( true, canBeNamed(this), false, this, context, didChangeState), ); diff --git a/lib/tags/DoubleTag.dart b/lib/tags/DoubleTag.dart index da8d61b..bd9b359 100644 --- a/lib/tags/DoubleTag.dart +++ b/lib/tags/DoubleTag.dart @@ -12,7 +12,7 @@ extension DoubleTagExt on DoubleTag { return ListTile( title: Text("TAG_Double (${getKey()})"), subtitle: TagExt.getElementDescriptor("$value"), - leading: const Image(image: AssetImage("Icons/PNG/Double.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/FloatTag.dart b/lib/tags/FloatTag.dart index 1d16760..e8659fa 100644 --- a/lib/tags/FloatTag.dart +++ b/lib/tags/FloatTag.dart @@ -13,7 +13,7 @@ extension FloatTagExt on FloatTag { return ListTile( title: Text("TAG_Float (${getKey()})"), subtitle: TagExt.getElementDescriptor("$value"), - leading: const Image(image: AssetImage("Icons/PNG/Float.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/IntArrayTag.dart b/lib/tags/IntArrayTag.dart index 82d4a56..dbff13a 100644 --- a/lib/tags/IntArrayTag.dart +++ b/lib/tags/IntArrayTag.dart @@ -24,7 +24,7 @@ extension IntArrayTagExt on IntArrayTag { return ListTile( title: Text("TAG_IntArray (${getKey()})"), subtitle: TagExt.getElementDescriptor("${value.length} entries"), - leading: const Image(image: AssetImage("Icons/PNG/IntegerArray.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( true, canBeNamed(this), false, this, context, didChangeState), ); diff --git a/lib/tags/IntTag.dart b/lib/tags/IntTag.dart index d786c18..5fe830c 100644 --- a/lib/tags/IntTag.dart +++ b/lib/tags/IntTag.dart @@ -13,7 +13,7 @@ extension IntTagExt on IntTag { return ListTile( title: Text("TAG_Int (${getKey()})"), subtitle: TagExt.getElementDescriptor("$value"), - leading: const Image(image: AssetImage("Icons/PNG/Integer.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/ListTag.dart b/lib/tags/ListTag.dart index 39c65d8..1adb520 100644 --- a/lib/tags/ListTag.dart +++ b/lib/tags/ListTag.dart @@ -24,7 +24,7 @@ extension ListTagExt on ListTag { return ListTile( title: Text("TAG_List (${getKey()}) ($type)"), subtitle: TagExt.getElementDescriptor("${value.length} entries"), - leading: const Image(image: AssetImage("Icons/PNG/List.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( true, canBeNamed(this), false, this, context, didChangeState), ); diff --git a/lib/tags/LongArrayTag.dart b/lib/tags/LongArrayTag.dart index 02867f6..18cdb53 100644 --- a/lib/tags/LongArrayTag.dart +++ b/lib/tags/LongArrayTag.dart @@ -24,7 +24,7 @@ extension LongArrayTagExt on LongArrayTag { return ListTile( title: Text("TAG_LongArray (${getKey()})"), subtitle: TagExt.getElementDescriptor("${value.length} entries"), - leading: const Image(image: AssetImage("Icons/PNG/LongArray.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( true, canBeNamed(this), false, this, context, didChangeState), ); diff --git a/lib/tags/LongTag.dart b/lib/tags/LongTag.dart index df0691b..9525090 100644 --- a/lib/tags/LongTag.dart +++ b/lib/tags/LongTag.dart @@ -13,7 +13,7 @@ extension LongTagExt on LongTag { return ListTile( title: Text("TAG_Long (${getKey()})"), subtitle: TagExt.getElementDescriptor("$value"), - leading: const Image(image: AssetImage("Icons/PNG/Long.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/ShortTag.dart b/lib/tags/ShortTag.dart index cc0874c..0bc2012 100644 --- a/lib/tags/ShortTag.dart +++ b/lib/tags/ShortTag.dart @@ -13,7 +13,7 @@ extension ShortTagExt on ShortTag { return ListTile( title: Text("TAG_Short (${getKey()})"), subtitle: TagExt.getElementDescriptor("$value"), - leading: const Image(image: AssetImage("Icons/PNG/Short.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/StringTag.dart b/lib/tags/StringTag.dart index b30b54c..7d1acf6 100644 --- a/lib/tags/StringTag.dart +++ b/lib/tags/StringTag.dart @@ -13,7 +13,7 @@ extension StringTagExt on StringTag { return ListTile( title: Text("TAG_String (${getKey()})"), subtitle: TagExt.getElementDescriptor(value), - leading: const Image(image: AssetImage("Icons/PNG/String.png")), + leading: TagExt.getTagIcon(getTagType()), trailing: TagExt.getElementButtons( false, canBeNamed(this), true, this, context, didChangeState), ); diff --git a/lib/tags/Tag.dart b/lib/tags/Tag.dart index e2ec320..8983e29 100644 --- a/lib/tags/Tag.dart +++ b/lib/tags/Tag.dart @@ -190,7 +190,8 @@ class TagExt { tag: tag, allowAllTagTypes: allowAllTagTypes, isArray: isArray, - allowedTagTypes: allowedTypes)); + allowedTagTypes: allowedTypes, + didChangeState: didChangeState)); }, icon: Icon(Icons.add)), if (isNamed) @@ -237,6 +238,14 @@ class TagExt { static Widget getElementDescriptor(String descript) { return Text(descript); } + + static String getTagIconPath(TagType type) { + return "Icons/PNG/${type.name}.png"; + } + + static Widget getTagIcon(TagType type) { + return Image(image: AssetImage(getTagIconPath(type))); + } } bool canBeNamed(Tag tag) { diff --git a/pubspec.yaml b/pubspec.yaml index ec26a68..de112a8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,8 +33,8 @@ flutter: - "Icons/PNG/Compound.png" - "Icons/PNG/Double.png" - "Icons/PNG/Float.png" - - "Icons/PNG/Integer.png" - - "Icons/PNG/IntegerArray.png" + - "Icons/PNG/Int.png" + - "Icons/PNG/IntArray.png" - "Icons/PNG/List.png" - "Icons/PNG/Long.png" - "Icons/PNG/LongArray.png"