Skip to content

Commit

Permalink
v0.16, Quick Tweak tool, UI Tweak tool
Browse files Browse the repository at this point in the history
  • Loading branch information
JavidPack committed Aug 8, 2021
1 parent 3a745c5 commit 9b6800f
Show file tree
Hide file tree
Showing 9 changed files with 1,140 additions and 59 deletions.
27 changes: 27 additions & 0 deletions ModdersToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using ModdersToolkit.Tools;
using ReLogic.OS;
using System;
using System.Collections.Generic;
using Terraria;
using Terraria.ID;
Expand Down Expand Up @@ -51,6 +52,7 @@ public override void Load() {
// Not ready yet tools.Add(new Tools.Loot.LootTool());
AddTool(new Tools.UIPlayground.UIPlaygroundTool());
AddTool(new Tools.Backgrounds.BackgroundsTool());
AddTool(new Tools.QuickTweak.QuickTweakTool());
AddTool(new Tools.Miscellaneous.MiscellaneousTool());

tools.ForEach(tool => tool.Initialize());
Expand Down Expand Up @@ -226,6 +228,31 @@ internal void DrawUpdateToggles() {
}
}

// v0.16 - add ("AddTweakSimple", object)
// - object - all instance fields will be displayed
// - Type - all static fields
// - FieldInfo - just 1 static field?
// - (object, FieldInfo)/ValueTuple<object, FieldInfo> - Only specified instance field.
// nested objects?? pass in depth?
// get range from field itself? [Range(2f, 3f)]
public override object Call(params object[] args) {
try {
string messageType = args[0] as string;
switch (messageType) {
case "AddTweakSimple":
Tools.QuickTweak.QuickTweakTool.Call(args);
return "Success";
default:
Logger.Warn("Unknown Message type: " + messageType);
return "Failure";
}
}
catch (Exception e) {
Logger.Warn("Call Error: " + e.StackTrace + e.Message);
}
return "Failure";
}

public Tool LastVisibleTool { get; private set; }

public static ModdersToolkit Instance { get; private set; }
Expand Down
42 changes: 42 additions & 0 deletions Tools/QuickTweak/QuickTweakCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Reflection;
using Terraria.ModLoader;

namespace ModdersToolkit.Tools.QuickTweak
{
class QuickTweakCommand : ModCommand
{
public override string Command => "tweak";

public override string Description => "Adds a static field to the Quick Tweak menu";

public override string Usage => "/tweak className fieldName";

public override CommandType Type => CommandType.Chat;

public override void Action(CommandCaller caller, string input, string[] args) {
try {
string className = args[0];
string fieldName = args[1];

foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
foreach (var type in assembly.GetTypes()) {
if(type.Name == className) {
foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic)) {
if(field.Name == fieldName) {
QuickTweakTool.AddTweak(field, $"{className}.{fieldName}");
caller.Reply($"Quick tweak found and added for {className}.{fieldName}");
return;
}
}
}
}
}
caller.Reply($"Static field {className}.{fieldName} not found");
}
catch (Exception) {
throw;
}
}
}
}
108 changes: 108 additions & 0 deletions Tools/QuickTweak/QuickTweakTool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Terraria;
using Terraria.UI;

namespace ModdersToolkit.Tools.QuickTweak
{
class TweakEntry
{
public object item;
public string label;
//public Func<string> label;

public TweakEntry(object item, string label) {
this.item = item;
this.label = label;
}
}

public class QuickTweakTool : Tool
{
internal static QuickTweakUI QuickTweakUI;
internal static List<TweakEntry> tweaks = new List<TweakEntry>();

internal static bool showPrivateFields = false;

public override void Initialize() {
ToggleTooltip = "Click to toggle Quick Tweak Tool";
}

public override void ClientInitialize() {
Interface = new UserInterface();

QuickTweakUI = new QuickTweakUI(Interface);
QuickTweakUI.Activate();

Interface.SetState(QuickTweakUI);
}

public override void ClientTerminate() {
Interface = null;

QuickTweakUI?.Deactivate();
QuickTweakUI = null;
}

public override void UIDraw() {
if (Visible) {
QuickTweakUI.Draw(Main.spriteBatch);
}
}

internal static void Call(object[] args) {
// Pass in object instance -> all fields shown
// Pass in just fieldinfo -> static field
// pass in static class somehow? Pass in Type?
// Pass in ()=> annonomous functions.
// --> View only usage? () => Main.LocalPlayer.Center.X, ModPLayer bool, etc.
// check RangeAttribute automatically somehow? pass in range?
// Pass in object "stringFieldName"

string label = "";
if (args.Length >= 3 && args[2] is string _label)
label = _label;

//Type inputType = args[1].GetType();
//if (inputType.IsGenericType) {
// var openType = inputType.GetGenericTypeDefinition();
// if (openType == typeof(ValueTuple<,>)) {

// //object myTuple = ((object)instance, new object());

// if (typeof(object).IsAssignableFrom(inputType.GenericTypeArguments[0]) && typeof(FieldInfo).IsAssignableFrom(inputType.GenericTypeArguments[1])){
// var tuple = (ValueTuple <object, string>)args[1];
// var result = new ValueTuple<object, string>();
// tweaks.Add(new TweakEntry(args[1], label));
// }
// }
//}

tweaks.Add(new TweakEntry(args[1], label));
}

internal static void AddTweak(FieldInfo field, string label) {
tweaks.Add(new TweakEntry(field, label));
QuickTweakUI.updateNeeded = true;
}

public static void AddTweak(object field, string label) {
tweaks.Add(new TweakEntry(field, label));
QuickTweakUI.updateNeeded = true;
}

internal static void AddTweak(ValueTuple<object, FieldInfo> tuple) {
tweaks.Add(new TweakEntry(tuple, ""));
QuickTweakUI.updateNeeded = true;
}

internal static void AddTweakUIElement(UIElement uIElement) {
tweaks.Add(new TweakEntry(uIElement, ""));
QuickTweakUI.updateNeeded = true;
}
}
}
Loading

0 comments on commit 9b6800f

Please sign in to comment.