Skip to content

Commit 6cda099

Browse files
committed
generic stackable params
1 parent 80976fd commit 6cda099

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

src/BuiltinExtensions/GridGenerator/GridGeneratorExtension.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1+
using System;
2+
using System.IO;
3+
using System.Collections.Generic;
4+
using System.Linq;
15
using FreneticUtilities.FreneticExtensions;
26
using FreneticUtilities.FreneticToolkit;
37
using Newtonsoft.Json.Linq;
48
using SwarmUI.Accounts;
59
using SwarmUI.Core;
610
using SwarmUI.Text2Image;
11+
using SwarmUI.Media;
712
using SwarmUI.Utils;
813
using SwarmUI.WebAPI;
9-
using System.IO;
1014
using System.Net.WebSockets;
1115
using SixLabors.Fonts;
1216
using SixLabors.ImageSharp;
1317
using SixLabors.ImageSharp.Processing;
1418
using SixLabors.ImageSharp.Drawing.Processing;
19+
1520
using static SwarmUI.Builtin_GridGeneratorExtension.GridGenCore;
1621
using Image = SwarmUI.Utils.Image;
1722
using ISImage = SixLabors.ImageSharp.Image;
1823
using ISImageRGBA = SixLabors.ImageSharp.Image<SixLabors.ImageSharp.PixelFormats.Rgba32>;
19-
using SwarmUI.Media;
2024

2125
namespace SwarmUI.Builtin_GridGeneratorExtension;
2226

@@ -29,6 +33,15 @@ public class GridGeneratorExtension : Extension
2933
public static PermInfo PermReadGrids = Permissions.Register(new("gridgen_read_grids", "[Grid Generator] Read Grids", "Allows the user to read their list of saved grids.", PermissionDefault.USER, Permissions.GroupUser));
3034
public static PermInfo PermSaveGrids = Permissions.Register(new("gridgen_save_grids", "[Grid Generator] Save Grids", "Allows the user to save new custom grids to their list of saved grids.", PermissionDefault.USER, Permissions.GroupUser));
3135

36+
/// <summary>Set of parameter IDs that should be comma-stackable in multiple grid axes.</summary>
37+
public static Dictionary<string, T2IParamType> CommaStackableParameters = [];
38+
39+
/// <summary>Marks a parameter type as being stackable with comma separation in multiple grid axes.</summary>
40+
public static void MakeStackable(T2IParamType type)
41+
{
42+
CommaStackableParameters[type.ID] = type;
43+
}
44+
3245
public override void OnPreInit()
3346
{
3447
ScriptFiles.Add("Assets/grid_gen.js");
@@ -90,9 +103,9 @@ public override void OnPreInit()
90103
(call.LocalData as GridCallData).Additions.Add(val);
91104
return true;
92105
}
93-
else if (cleaned == PresetsParameter.Type.ID)
106+
else if (CommaStackableParameters.TryGetValue(cleaned, out T2IParamType type))
94107
{
95-
(call.LocalData as GridCallData).Presets.Add(val);
108+
(call.LocalData as GridCallData).CommaStackable.GetOrCreate(type, () => []).Add(val);
96109
return true;
97110
}
98111
else if (cleaned == "width" || cleaned == "outwidth")
@@ -135,14 +148,18 @@ public override void OnPreInit()
135148
string prompt = param.InternalSet.Get(T2IParamTypes.Prompt, "") + " " + data.Additions.JoinString(" ");
136149
param.InternalSet.Set(T2IParamTypes.Prompt, prompt.Trim());
137150
}
138-
if (data.Presets.Any())
151+
foreach ((T2IParamType key, List<string> vals) in data.CommaStackable)
139152
{
140-
string presets = data.Presets.JoinString(",");
141-
if (param.InternalSet.TryGet(PresetsParameter, out string existing))
153+
string joined = vals.JoinString(",");
154+
if (param.TryGetRaw(key, out object existing))
142155
{
143-
presets += $",{existing}";
156+
if (existing is List<string> strs)
157+
{
158+
existing = strs.JoinString(",");
159+
}
160+
joined = $"{existing},{joined}";
144161
}
145-
param.InternalSet.Set(PresetsParameter, presets);
162+
param.Set(key, joined);
146163
}
147164
};
148165
GridRunnerPreRunHook = (runner) =>
@@ -278,6 +295,11 @@ public override void OnInit()
278295
API.RegisterAPICall(GridGenDeleteData, true, PermSaveGrids);
279296
API.RegisterAPICall(GridGenGetData, false, PermReadGrids);
280297
API.RegisterAPICall(GridGenListData, false, PermReadGrids);
298+
MakeStackable(PresetsParameter.Type);
299+
MakeStackable(T2IParamTypes.Loras.Type);
300+
MakeStackable(T2IParamTypes.LoraWeights.Type);
301+
MakeStackable(T2IParamTypes.LoraTencWeights.Type);
302+
MakeStackable(T2IParamTypes.LoraSectionConfinement.Type);
281303
}
282304

283305
public async Task<JObject> GridGenSaveData(Session session, string gridName, bool isPublic, JObject rawData)
@@ -340,7 +362,7 @@ public class GridCallData
340362

341363
public List<string> Additions = [];
342364

343-
public List<string> Presets = [];
365+
public Dictionary<T2IParamType, List<string>> CommaStackable = [];
344366
}
345367

346368
public class SwarmUIGridData

src/Text2Image/T2IParamTypes.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -174,10 +174,9 @@ public static T2IParamType FromNet(JObject data)
174174

175175
/// <summary>Helper class to easily read T2I Parameters.</summary>
176176
/// <typeparam name="T">The C# datatype of the parameter.</typeparam>
177-
public class T2IRegisteredParam<T>
177+
/// <param name="Type">The underlying type data.</param>
178+
public record class T2IRegisteredParam<T>(T2IParamType Type)
178179
{
179-
/// <summary>The underlying type data.</summary>
180-
public T2IParamType Type;
181180
}
182181

183182
/// <summary>Represents a group of parameters.</summary>
@@ -265,7 +264,7 @@ public static T2IRegisteredParam<T> Register<T>(T2IParamType type)
265264
}
266265
Types.Add(type.ID, type);
267266
LanguagesHelper.AppendSetInternal(type.Name, type.Description);
268-
return new T2IRegisteredParam<T>() { Type = type };
267+
return new T2IRegisteredParam<T>(type);
269268
}
270269

271270
/// <summary>Type-name cleaner.</summary>

0 commit comments

Comments
 (0)