sort nodes
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
namespace NodeBeautify
|
||||
{
|
||||
internal static class Configs
|
||||
{
|
||||
/// <summary>
|
||||
/// Round to the cloest multiples of this value
|
||||
/// </summary>
|
||||
public const int GRID_SIZE = 10;
|
||||
|
||||
/// <summary>
|
||||
/// NodeType, (Width, Height)
|
||||
/// </summary>
|
||||
public static readonly Dictionary<string, (float, float)> FIXED_SIZE = new(){
|
||||
{ "CLIPTextEncode", (400.0f, 160.0f) },
|
||||
{ "PreviewImage", (420.0f, 420.0f) }
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using static NodeBeautify.Configs;
|
||||
|
||||
namespace NodeBeautify
|
||||
{
|
||||
internal static class Functions
|
||||
{
|
||||
private static float Round(JToken value) => (float)value - ((float)value % GRID_SIZE);
|
||||
|
||||
internal static void FixNodes(JObject data)
|
||||
{
|
||||
var nodes = (JArray)data["nodes"];
|
||||
|
||||
// Doing try-catch cause somehow the value can be either array or dictionary...
|
||||
foreach (JObject node in nodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
JArray pos = (JArray)node["pos"];
|
||||
pos[0] = Round(pos[0]);
|
||||
pos[1] = Round(pos[1]);
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
JObject pos = (JObject)node["pos"];
|
||||
pos["0"] = Round(pos["0"]);
|
||||
pos["1"] = Round(pos["1"]);
|
||||
}
|
||||
|
||||
string key = (string)node["type"];
|
||||
|
||||
try
|
||||
{
|
||||
JObject size = (JObject)node["size"];
|
||||
|
||||
if (FIXED_SIZE.ContainsKey(key))
|
||||
{
|
||||
size["0"] = FIXED_SIZE[key].Item1;
|
||||
size["1"] = FIXED_SIZE[key].Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
size["0"] = Round(size["0"]);
|
||||
size["1"] = Round(size["1"]);
|
||||
}
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
JArray size = (JArray)node["size"];
|
||||
|
||||
if (FIXED_SIZE.ContainsKey(key))
|
||||
{
|
||||
size[0] = FIXED_SIZE[key].Item1;
|
||||
size[1] = FIXED_SIZE[key].Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
size[0] = Round(size[0]);
|
||||
size[1] = Round(size[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static void FixGroups(JObject data)
|
||||
{
|
||||
var groups = (JArray)data["groups"];
|
||||
|
||||
foreach (JObject group in groups)
|
||||
{
|
||||
JArray bound = (JArray)group["bounding"];
|
||||
for (int edge = 0; edge < 4; edge++)
|
||||
bound[edge] = Round(bound[edge]);
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SortNode(JObject data)
|
||||
{
|
||||
var nodes = (JArray)data["nodes"];
|
||||
var sortedNodes = nodes.OrderBy(obj => (int)obj["id"]).ToArray();
|
||||
(data["nodes"] as JArray).ReplaceAll(sortedNodes);
|
||||
}
|
||||
}
|
||||
}
|
||||
+5
-88
@@ -5,22 +5,9 @@ namespace NodeBeautify
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Round to the cloest multiples of this value
|
||||
/// </summary>
|
||||
private const int GRID_SIZE = 20;
|
||||
|
||||
/// <summary>
|
||||
/// NodeType, (Width, Height)
|
||||
/// </summary>
|
||||
private static readonly Dictionary<string, (float, float)> FIXED_SIZE = new(){
|
||||
{
|
||||
"CLIPTextEncode", (400.0f, 150.0f)
|
||||
}
|
||||
};
|
||||
|
||||
private static bool hasError;
|
||||
static void Main(string[] args)
|
||||
|
||||
internal static void Main(string[] args)
|
||||
{
|
||||
hasError = false;
|
||||
|
||||
@@ -55,77 +42,6 @@ namespace NodeBeautify
|
||||
Process(path);
|
||||
}
|
||||
|
||||
private static float Round(JToken value)
|
||||
{
|
||||
return (float)value - ((float)value % GRID_SIZE);
|
||||
}
|
||||
|
||||
private static void FixNodes(JObject data)
|
||||
{
|
||||
var nodes = (JArray)data["nodes"];
|
||||
|
||||
// Doing try-catch cause somehow the value can be either array or dictionary...
|
||||
foreach (JObject node in nodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
JArray pos = (JArray)node["pos"];
|
||||
pos[0] = Round(pos[0]);
|
||||
pos[1] = Round(pos[1]);
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
JObject pos = (JObject)node["pos"];
|
||||
pos["0"] = Round(pos["0"]);
|
||||
pos["1"] = Round(pos["1"]);
|
||||
}
|
||||
|
||||
string key = (string)node["type"];
|
||||
|
||||
try
|
||||
{
|
||||
JObject size = (JObject)node["size"];
|
||||
|
||||
if (FIXED_SIZE.ContainsKey(key))
|
||||
{
|
||||
size["0"] = FIXED_SIZE[key].Item1;
|
||||
size["1"] = FIXED_SIZE[key].Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
size["0"] = Round(size["0"]);
|
||||
size["1"] = Round(size["1"]);
|
||||
}
|
||||
}
|
||||
catch (InvalidCastException)
|
||||
{
|
||||
JArray size = (JArray)node["size"];
|
||||
if (FIXED_SIZE.ContainsKey(key))
|
||||
{
|
||||
size[0] = FIXED_SIZE[key].Item1;
|
||||
size[1] = FIXED_SIZE[key].Item2;
|
||||
}
|
||||
else
|
||||
{
|
||||
size[0] = Round(size[0]);
|
||||
size[1] = Round(size[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void FixGroups(JObject data)
|
||||
{
|
||||
var groups = (JArray)data["groups"];
|
||||
|
||||
foreach (JObject group in groups)
|
||||
{
|
||||
JArray bound = (JArray)group["bounding"];
|
||||
for (int edge = 0; edge < 4; edge++)
|
||||
bound[edge] = Round(bound[edge]);
|
||||
}
|
||||
}
|
||||
|
||||
private static void Process(string path)
|
||||
{
|
||||
string jsonContent = File.ReadAllText(path).Trim();
|
||||
@@ -133,8 +49,9 @@ namespace NodeBeautify
|
||||
|
||||
try
|
||||
{
|
||||
FixNodes(data);
|
||||
FixGroups(data);
|
||||
Functions.FixNodes(data);
|
||||
Functions.FixGroups(data);
|
||||
Functions.SortNode(data);
|
||||
File.WriteAllText(path, JsonConvert.SerializeObject(data));
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
|
||||
Reference in New Issue
Block a user