Adds in SNBT Editing support
This commit is contained in:
parent
dc6dffc3ba
commit
410bde4c70
10 changed files with 107 additions and 13 deletions
|
@ -5,6 +5,7 @@ import 'package:libac_dart/nbt/NbtIo.dart';
|
|||
import 'package:libac_dart/nbt/Tag.dart';
|
||||
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
|
||||
import 'package:nbteditor/Constants.dart';
|
||||
import 'package:nbteditor/SessionData.dart';
|
||||
import 'package:nbteditor/tags/CompoundTag.dart';
|
||||
import 'package:nbteditor/tags/Tag.dart';
|
||||
|
||||
|
@ -16,7 +17,7 @@ class Editor extends StatefulWidget {
|
|||
}
|
||||
|
||||
class EditorState extends State<Editor> {
|
||||
List<Node> nodes = [CompoundTag().getNode("/")];
|
||||
//List<Node> nodes = [CompoundTag().getNode("/")];
|
||||
bool compressed = false;
|
||||
|
||||
static EditorState? _inst;
|
||||
|
@ -45,7 +46,8 @@ class EditorState extends State<Editor> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
controller = TreeViewController(children: nodes);
|
||||
controller =
|
||||
TreeViewController(children: [SessionData.ROOT_TAG.getNode("/")]);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
|
@ -68,11 +70,7 @@ class EditorState extends State<Editor> {
|
|||
leading: const Icon(Icons.add),
|
||||
onTap: () {
|
||||
setState(() {
|
||||
nodes.clear();
|
||||
|
||||
// Add a new compound tag as the root
|
||||
Tag tag = CompoundTag();
|
||||
nodes.add(TagExt.getNode("/", tag) as Node);
|
||||
SessionData.ROOT_TAG = CompoundTag();
|
||||
});
|
||||
},
|
||||
),
|
||||
|
@ -97,14 +95,24 @@ class EditorState extends State<Editor> {
|
|||
} else {
|
||||
// String!!
|
||||
CompoundTag ct = await NbtIo.read(filePath);
|
||||
nodes.clear();
|
||||
nodes.add(ct.getNode("/") as Node);
|
||||
|
||||
SessionData.ROOT_TAG = ct;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
controller = TreeViewController(children: nodes);
|
||||
controller = TreeViewController(
|
||||
children: [SessionData.ROOT_TAG.getNode("/")]);
|
||||
});
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
title: Text("R A W"),
|
||||
subtitle: Text("Edit as raw SNBT"),
|
||||
leading: Icon(Icons.edit),
|
||||
onTap: () async {
|
||||
await Navigator.pushNamed(context, "/snbt");
|
||||
setState(() {});
|
||||
},
|
||||
)
|
||||
]),
|
||||
),
|
||||
|
|
5
lib/SessionData.dart
Normal file
5
lib/SessionData.dart
Normal file
|
@ -0,0 +1,5 @@
|
|||
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
|
||||
|
||||
class SessionData {
|
||||
static CompoundTag ROOT_TAG = CompoundTag();
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:nbteditor/Editor.dart';
|
||||
import 'package:nbteditor/pages/AddPage.dart';
|
||||
import 'package:nbteditor/pages/SNBTEditor.dart';
|
||||
|
||||
void main() {
|
||||
runApp(const MainApp());
|
||||
|
@ -15,7 +16,8 @@ class MainApp extends StatelessWidget {
|
|||
theme: ThemeData.dark(),
|
||||
routes: {
|
||||
"/": (context) => const Editor(),
|
||||
"/add": (context) => AddPage()
|
||||
"/add": (context) => AddPage(),
|
||||
"/snbt": (context) => SnbtEdit()
|
||||
},
|
||||
);
|
||||
}
|
||||
|
|
66
lib/pages/SNBTEditor.dart
Normal file
66
lib/pages/SNBTEditor.dart
Normal file
|
@ -0,0 +1,66 @@
|
|||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_code_editor/flutter_code_editor.dart';
|
||||
import 'package:flutter_highlight/themes/vs.dart';
|
||||
import 'package:libac_dart/nbt/SnbtIo.dart';
|
||||
import 'package:libac_dart/nbt/impl/CompoundTag.dart';
|
||||
import 'package:nbteditor/Constants.dart';
|
||||
import 'package:nbteditor/SessionData.dart';
|
||||
|
||||
class SnbtEdit extends StatefulWidget {
|
||||
@override
|
||||
State<StatefulWidget> createState() {
|
||||
return SnbtState();
|
||||
}
|
||||
}
|
||||
|
||||
class SnbtState extends State<SnbtEdit> {
|
||||
CodeController snbt = CodeController();
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
snbt.text = SnbtIo.writeToString(SessionData.ROOT_TAG);
|
||||
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text("SNBT Editor"),
|
||||
backgroundColor: Constants.TITLEBAR_COLOR,
|
||||
),
|
||||
floatingActionButton: ElevatedButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
CompoundTag ct =
|
||||
(await SnbtIo.readFromString(snbt.text)).asCompoundTag();
|
||||
snbt.text = SnbtIo.writeToString(ct);
|
||||
|
||||
setState(() {
|
||||
SessionData.ROOT_TAG = ct;
|
||||
});
|
||||
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Successfully edited NBT data")));
|
||||
} catch (E) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||
content: Text("FATAL ERROR: Your SNBT Syntax is not valid")));
|
||||
}
|
||||
},
|
||||
child: Text("Compile"),
|
||||
),
|
||||
body: Padding(
|
||||
padding: EdgeInsets.all(8),
|
||||
child: CodeTheme(
|
||||
data: CodeThemeData(styles: vsTheme),
|
||||
child: SingleChildScrollView(
|
||||
child: CodeField(
|
||||
controller: snbt,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue