Skip to content

Commit

Permalink
Add tab support
Browse files Browse the repository at this point in the history
  • Loading branch information
xADDBx committed Nov 5, 2024
1 parent 7b858eb commit eb6d836
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 32 deletions.
2 changes: 1 addition & 1 deletion ToyBox/Classes/MainUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal static class Main {
new NamedAction("Dialog & NPCs", DialogAndNPCs.OnGUI),
new NamedAction("Saves", GameSavesBrowser.OnGUI),
new NamedAction("Achievements", AchievementsUnlocker.OnGUI),
new NamedAction("Patch Tool", PatchToolUI.OnGUI),
new NamedAction("Patch Tool", PatchToolUIManager.OnGUI),
new NamedAction("Settings", SettingsUI.OnGUI)
};
private static int partyTabID = -1;
Expand Down
6 changes: 5 additions & 1 deletion ToyBox/Classes/MainUI/PatchTool/PatchOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ public object Apply(object instance) {
break;
case PatchOperationType.ModifyUnityReference: {
throw new NotImplementedException("Modifying Unity Objects is not supported.");
}
}
#pragma warning disable CS0162 // Unreachable code detected
break;
#pragma warning restore CS0162 // Unreachable code detected
case PatchOperationType.ModifyComplex: {
var @object = field.GetValue(instance);
NestedOperation.Apply(@object);
Expand All @@ -141,7 +143,9 @@ public object Apply(object instance) {
case PatchOperationType.ModifyBlueprintReference: {
throw new NotImplementedException("Blueprint References not yet implemented");
}
#pragma warning disable CS0162 // Unreachable code detected
break;
#pragma warning restore CS0162 // Unreachable code detected
default: throw new NotImplementedException($"Unknown PatchOperation: {OperationType}");
}
return instance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
using UnityEngine;

namespace ToyBox.PatchTool;
public static class PatchToolUI {
public static PatchState CurrentState;
private static Dictionary<(object, FieldInfo), object> _editStates = new();
private static Dictionary<object, Dictionary<FieldInfo, object>> _fieldsByObject = new();
public class PatchToolTabUI {
public PatchState CurrentState;
private Dictionary<(object, FieldInfo), object> _editStates = new();
private Dictionary<object, Dictionary<FieldInfo, object>> _fieldsByObject = new();
// key: parent, containing field, object instance
private static Dictionary<(object, FieldInfo, object), bool> _toggleStates = new();
private static Dictionary<((object, FieldInfo), int), bool> _listToggleStates = new();
private static Dictionary<(object, FieldInfo), AddItemState> _addItemStates = new();
private Dictionary<(object, FieldInfo, object), bool> _toggleStates = new();
private Dictionary<((object, FieldInfo), int), bool> _listToggleStates = new();
private Dictionary<(object, FieldInfo), AddItemState> _addItemStates = new();
private static Dictionary<Type, List<Type>> _compatibleTypes = new();
private static Dictionary<Type, List<Type>> _allowedTypes = new();
private static HashSet<object> _visited = new();
private HashSet<object> _visited = new();
// private static string _target = "649ae43543fd4b47ae09a6547e67bcfc";
private static string _target = "";
private static string _pickerText = "";
public static int IndentPerLevel = 25;
internal string Target = "";
private string _pickerText = "";
public int IndentPerLevel = 25;
private static readonly HashSet<Type> _primitiveTypes = new() { typeof(string), typeof(bool), typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(float), typeof(double) };
public class AddItemState {
public Browser<Type, Type> ToAddBrowser = new(true, true, false, false) { DisplayShowAllGUI = false };
public static AddItemState Create(object parent, FieldInfo info, object @object, int index, PatchOperation wouldBePatch) {
public static AddItemState Create(object parent, FieldInfo info, object @object, int index, PatchOperation wouldBePatch, PatchToolTabUI ui) {
Type elementType = null;
Type type = info.FieldType;
if (type.IsArray) {
Expand All @@ -56,9 +56,10 @@ public static AddItemState Create(object parent, FieldInfo info, object @object,
Collection = @object,
Item = null,
IsExpanded = true,
WouldBePatch = wouldBePatch
WouldBePatch = wouldBePatch,
_ui = ui
};
_addItemStates[(parent, info)] = state;
ui._addItemStates[(parent, info)] = state;

if (!_compatibleTypes.ContainsKey(elementType)) {
(var all, var allowed) = PatchToolUtils.GetInstantiableTypes(elementType, parent);
Expand Down Expand Up @@ -86,11 +87,12 @@ public void AddItemGUI() {
}
public void Confirm(Type type) {
PatchOperation op = new(PatchOperation.PatchOperationType.ModifyCollection, Info.Name, type, null, Parent.GetType(), PatchOperation.CollectionPatchOperationType.AddAtIndex, Index);
CurrentState.AddOp(WouldBePatch.AddOperation(op));
CurrentState.CreatePatchFromState().RegisterPatch();
_addItemStates.Remove((Parent, Info));
_ui.CurrentState.AddOp(WouldBePatch.AddOperation(op));
_ui.CurrentState.CreatePatchFromState().RegisterPatch();
_ui._addItemStates.Remove((Parent, Info));
}
public object Parent;
private PatchToolTabUI _ui;
public FieldInfo Info;
public int Index;
public object Collection;
Expand All @@ -101,12 +103,12 @@ public void Confirm(Type type) {
}


public static void SetTarget(string guid) {
public void SetTarget(string guid) {
CurrentState = null;
ClearCache();
_target = guid;
Target = guid;
}
public static void OnGUI() {
public void OnGUI() {
_visited.Clear();
using (HorizontalScope()) {
Label("Enter target blueprint id", Width(200));
Expand All @@ -116,10 +118,10 @@ public static void OnGUI() {
});
}
Div();
if (CurrentState == null || CurrentState.IsDirty && !_target.IsNullOrEmpty()) {
if (CurrentState == null || CurrentState.IsDirty && !Target.IsNullOrEmpty()) {
if (Event.current.type == EventType.Layout) {
ClearCache();
var bp = ResourcesLibrary.TryGetBlueprint(_target);
var bp = ResourcesLibrary.TryGetBlueprint(Target);
if (bp != null) {
CurrentState = new(bp);
}
Expand All @@ -132,15 +134,15 @@ public static void OnGUI() {
}
}
}
public static void ClearCache() {
public void ClearCache() {
_editStates.Clear();
_fieldsByObject.Clear();
_toggleStates.Clear();
_addItemStates.Clear();
_compatibleTypes.Clear();
}

public static void NestedGUI(object o, PatchOperation wouldBePatch = null) {
public void NestedGUI(object o, PatchOperation wouldBePatch = null) {
if (_visited.Contains(o)) {
Label("Already opened on another level!".Green());
return;
Expand All @@ -165,7 +167,7 @@ public static void NestedGUI(object o, PatchOperation wouldBePatch = null) {
}
}
}
public static void FieldGUI(object parent, PatchOperation wouldBePatch, Type type, object @object, FieldInfo info) {
public void FieldGUI(object parent, PatchOperation wouldBePatch, Type type, object @object, FieldInfo info) {
if (@object == null) {
Label("Null", Width(500));
return;
Expand Down Expand Up @@ -308,7 +310,7 @@ public static void FieldGUI(object parent, PatchOperation wouldBePatch, Type typ
using (HorizontalScope()) {
Space(1220);
ActionButton("Add Item", () => {
AddItemState.Create(parent, info, @object, -1, wouldBePatch);
AddItemState.Create(parent, info, @object, -1, wouldBePatch, this);
});
}
if (_addItemStates.TryGetValue((parent, info), out var activeAddItemState)) {
Expand Down Expand Up @@ -336,7 +338,7 @@ public static void FieldGUI(object parent, PatchOperation wouldBePatch, Type typ
}
}

public static void ListItemGUI(PatchOperation wouldBePatch, object parent, FieldInfo info, object elem, int index, object collection) {
public void ListItemGUI(PatchOperation wouldBePatch, object parent, FieldInfo info, object elem, int index, object collection) {
PatchOperation tmpOp = new(PatchOperation.PatchOperationType.ModifyCollection, info.Name, null, null, parent.GetType(), PatchOperation.CollectionPatchOperationType.ModifyAtIndex, index);
PatchOperation op = wouldBePatch.AddOperation(tmpOp);
using (HorizontalScope()) {
Expand All @@ -346,11 +348,11 @@ public static void ListItemGUI(PatchOperation wouldBePatch, object parent, Field

Space(20);
ActionButton("Add Before", () => {
AddItemState.Create(parent, info, collection, index, wouldBePatch);
AddItemState.Create(parent, info, collection, index, wouldBePatch, this);
});
Space(10);
ActionButton("Add After", () => {
AddItemState.Create(parent, info, collection, index+1, wouldBePatch);
AddItemState.Create(parent, info, collection, index+1, wouldBePatch, this);
});
Space(10);
ActionButton("Remove", () => {
Expand All @@ -362,7 +364,7 @@ public static void ListItemGUI(PatchOperation wouldBePatch, object parent, Field
}
}

public static void PopulateFieldsAndObjects(object o) {
public void PopulateFieldsAndObjects(object o) {
Dictionary<FieldInfo, object> result = new();
foreach (var field in PatchToolUtils.GetFields(o.GetType())) {
result[field] = field.GetValue(o);
Expand Down
47 changes: 47 additions & 0 deletions ToyBox/Classes/MainUI/PatchTool/PatchToolUIManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using ModKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ToyBox.PatchTool;
public static class PatchToolUIManager {
private static List<PatchToolTabUI> instances = new();
private static int selectedIndex = -1;
public static void OnGUI() {
Label("Tabs".Bold(), AutoWidth());
using (HorizontalScope()) {
Space(50);
for (int i = 0; i < instances.Count; i++) {
using (HorizontalScope()) {
var tabName = instances[i].Target.IsNullOrEmpty() ? "New Tab" : instances[i].Target;
if (i == selectedIndex) {
Label($"[{tabName}]", AutoWidth());
} else {
ActionButton(tabName, () => {
selectedIndex = i;
}, AutoWidth());
}
ActionButton("Close", () => {
instances.RemoveAt(i);
if (selectedIndex >= instances.Count) {
selectedIndex = instances.Count - 1;
}
}, AutoWidth());
}
}
ActionButton("+", () => {
instances.Add(new PatchToolTabUI());
selectedIndex = instances.Count - 1;
}, AutoWidth());
}
Div();
Space(20);
if (selectedIndex >= 0 && selectedIndex < instances.Count) {
instances[selectedIndex].OnGUI();
} else {
Label("No tabs open.");
}
}
}

0 comments on commit eb6d836

Please sign in to comment.