mirror of
https://github.com/zontreck/NBTEditor
synced 2024-11-21 05:25:54 -07:00
Implement adding tags
This commit is contained in:
parent
8b9acb7847
commit
e2657a3bb5
18 changed files with 168 additions and 26 deletions
Before Width: | Height: | Size: 1.4 KiB After Width: | Height: | Size: 1.4 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
|
@ -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<AddPage> {
|
||||
List<TagType> 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<void> 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<TagType> 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});
|
||||
}
|
||||
|
|
|
@ -17,7 +17,9 @@ class EditValueState extends State<EditValuePrompt> {
|
|||
|
||||
@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;
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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),
|
||||
);
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue