Implement adding tags
This commit is contained in:
parent
8b9acb7847
commit
e2657a3bb5
18 changed files with 168 additions and 26 deletions
|
@ -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});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue