Skip to content

Commit 07847da

Browse files
committed
Layer eye toggle wip and starting to check layout stuff
1 parent e3c23f4 commit 07847da

6 files changed

Lines changed: 154 additions & 72 deletions

File tree

Edit/Helpers/FileHelper.cs

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,23 @@ public static string GetLayoutFilePath(string layoutName)
2929
return Path.Combine(folder, $"{layoutName}.json");
3030
}
3131

32-
#region file operations
33-
34-
public static void CreateAndOpenNewLayoutFile(string layoutName)
32+
public static List<string> GetLayouts()
3533
{
36-
string basePath = GetLayoutFilePath(layoutName);
37-
string path = basePath;
38-
int counter = 1;
39-
40-
// Generate unique filename if needed
41-
while (File.Exists(path))
34+
string folder = GetLayoutsFolderPath();
35+
if (!Directory.Exists(folder))
4236
{
43-
path = GetLayoutFilePath($"{layoutName}{counter}");
44-
counter++;
37+
Log.Warn($"Layouts folder does not exist: {folder}");
38+
return [];
4539
}
4640

47-
// Get current theme and positions
48-
ResourceThemeHelper.GetActiveResourceTheme(out ResourceThemeHelper.ResourceTheme currentTheme);
49-
50-
var layoutData = new LayoutData
51-
{
52-
ResourceTheme = currentTheme,
53-
Offsets = new Dictionary<Element, Vector2>
54-
{
55-
[Element.Chat] = new Vector2(ChatHook.OffsetX, ChatHook.OffsetY),
56-
[Element.Hotbar] = new Vector2(HotbarHook.OffsetX, HotbarHook.OffsetY),
57-
[Element.Map] = new Vector2(MapHook.OffsetX, MapHook.OffsetY),
58-
[Element.InfoAccs] = new Vector2(InfoAccsHook.OffsetX, InfoAccsHook.OffsetY),
59-
[Element.ClassicLife] = new Vector2(ClassicLifeHook.OffsetX, ClassicLifeHook.OffsetY),
60-
[Element.ClassicMana] = new Vector2(ClassicManaHook.OffsetX, ClassicManaHook.OffsetY),
61-
[Element.FancyLife] = new Vector2(FancyLifeHook.OffsetX, FancyLifeHook.OffsetY),
62-
[Element.FancyMana] = new Vector2(FancyManaHook.OffsetX, FancyManaHook.OffsetY),
63-
[Element.HorizontalBars] = new Vector2(HorizontalBarsHook.OffsetX, HorizontalBarsHook.OffsetY),
64-
[Element.BarLifeText] = new Vector2(BarLifeTextHook.OffsetX, BarLifeTextHook.OffsetY),
65-
[Element.BarManaText] = new Vector2(BarManaTextHook.OffsetX, BarManaTextHook.OffsetY),
66-
[Element.Buffs] = new Vector2(BuffHook.OffsetX, BuffHook.OffsetY),
67-
[Element.Inventory] = new Vector2(InventoryHook.OffsetX, InventoryHook.OffsetY),
68-
}
69-
};
70-
71-
LayoutHelper.WriteLayoutFile(Path.GetFileNameWithoutExtension(path), layoutData);
41+
return Directory
42+
.GetFiles(folder, "*.json")
43+
.Select(Path.GetFileNameWithoutExtension)
44+
.ToList();
45+
}
7246

47+
public static void OpenFileAtPath(string path)
48+
{
7349
try
7450
{
7551
Process.Start(new ProcessStartInfo
@@ -84,21 +60,6 @@ public static void CreateAndOpenNewLayoutFile(string layoutName)
8460
}
8561
}
8662

87-
public static List<string> GetLayouts()
88-
{
89-
string folder = GetLayoutsFolderPath();
90-
if (!Directory.Exists(folder))
91-
{
92-
Log.Warn($"Layouts folder does not exist: {folder}");
93-
return [];
94-
}
95-
96-
return Directory
97-
.GetFiles(folder, "*.json")
98-
.Select(Path.GetFileNameWithoutExtension)
99-
.ToList();
100-
}
101-
10263
public static void OpenLayoutFolder()
10364
{
10465
string folder = GetLayoutsFolderPath();
@@ -122,6 +83,5 @@ public static void OpenLayoutFolder()
12283
Log.Warn($"Layouts folder does not exist: {folder}");
12384
}
12485
}
125-
#endregion
12686
}
12787
}

Edit/Helpers/LayoutData.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
using System.Collections.Generic;
22
using Newtonsoft.Json.Converters;
3-
using static UICustomizer.Edit.Helpers.ResourceThemeHelper;
3+
using static UICustomizer.Edit.Helpers.ElementHelper;
44
using static UICustomizer.Edit.Helpers.MapThemeHelper;
5+
using static UICustomizer.Edit.Helpers.ResourceThemeHelper;
56

67
namespace UICustomizer.Edit.Helpers
78
{
@@ -10,7 +11,7 @@ public class LayoutData
1011
/// <summary>
1112
/// Holds all the positions (offsets) of UI elements.
1213
/// </summary>
13-
public Dictionary<ElementHelper.Element, Vector2> Offsets { get; set; } = [];
14+
public Dictionary<Element, Vector2> Offsets { get; set; } = [];
1415

1516
/// <summary>
1617
/// The life and mana theme for the layout. Uses StringEnumConverter to ensure string representation in JSON as opposed to integer values.

Edit/Helpers/LayoutHelper.cs

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ namespace UICustomizer.Edit.Helpers
1313
{
1414
public static class LayoutHelper
1515
{
16-
public static string CurrentLayoutName { get; set; } = "Vanilla";
17-
1816
#region save layouts
1917

2018
public static void WriteLayoutFile(string layoutName, LayoutData data)
@@ -49,6 +47,48 @@ public static void WriteLayoutFile(string layoutName, LayoutData data)
4947
}
5048
}
5149

50+
public static void SaveCurrentAs(string layoutName, bool setAsActiveInConfig = true)
51+
{
52+
if (string.IsNullOrWhiteSpace(layoutName))
53+
layoutName = "CustomLayout";
54+
55+
GetActiveResourceTheme(out ResourceTheme currentTheme);
56+
GetActiveMapTheme(out MapTheme mapTheme);
57+
58+
var data = new LayoutData
59+
{
60+
ResourceTheme = currentTheme,
61+
MapTheme = mapTheme,
62+
Offsets = new Dictionary<Element, Vector2>
63+
{
64+
[Element.Chat] = new Vector2(ChatHook.OffsetX, ChatHook.OffsetY),
65+
[Element.Hotbar] = new Vector2(HotbarHook.OffsetX, HotbarHook.OffsetY),
66+
[Element.Map] = new Vector2(MapHook.OffsetX, MapHook.OffsetY),
67+
[Element.InfoAccs] = new Vector2(InfoAccsHook.OffsetX, InfoAccsHook.OffsetY),
68+
[Element.ClassicLife] = new Vector2(ClassicLifeHook.OffsetX, ClassicLifeHook.OffsetY),
69+
[Element.ClassicMana] = new Vector2(ClassicManaHook.OffsetX, ClassicManaHook.OffsetY),
70+
[Element.FancyLife] = new Vector2(FancyLifeHook.OffsetX, FancyLifeHook.OffsetY),
71+
[Element.FancyMana] = new Vector2(FancyManaHook.OffsetX, FancyManaHook.OffsetY),
72+
[Element.HorizontalBars] = new Vector2(HorizontalBarsHook.OffsetX, HorizontalBarsHook.OffsetY),
73+
[Element.BarLifeText] = new Vector2(BarLifeTextHook.OffsetX, BarLifeTextHook.OffsetY),
74+
[Element.BarManaText] = new Vector2(BarManaTextHook.OffsetX, BarManaTextHook.OffsetY),
75+
[Element.Buffs] = new Vector2(BuffHook.OffsetX, BuffHook.OffsetY),
76+
[Element.Inventory] = new Vector2(InventoryHook.OffsetX, InventoryHook.OffsetY)
77+
}
78+
};
79+
80+
WriteLayoutFile(layoutName, data);
81+
82+
if (setAsActiveInConfig)
83+
{
84+
var cfg = ModContent.GetInstance<Config>();
85+
cfg.Layout = layoutName;
86+
Terraria.ModLoader.Config.ConfigManager.Save(cfg);
87+
}
88+
89+
Log.Info($"Saved layout '{layoutName}' with resource theme '{currentTheme}' and map theme '{mapTheme}'.");
90+
}
91+
5292
#endregion
5393

5494
#region load layouts
@@ -97,6 +137,7 @@ public static void ApplyLayout(string layoutName)
97137
Log.Error($"Error applying layout '{layoutName}': {ex.Message}");
98138
}
99139
}
140+
100141
public static void TryApplyLayoutFromConfig()
101142
{
102143
var cfg = ModContent.GetInstance<Config>();
@@ -117,18 +158,25 @@ public static void TryApplyLayoutFromConfig()
117158

118159
#endregion
119160

120-
public static void SaveCurrentAs(string layoutName, bool setAsActiveInConfig = true)
161+
public static void CreateAndOpenNewLayoutFile(string layoutName)
121162
{
122-
if (string.IsNullOrWhiteSpace(layoutName))
123-
layoutName = "CustomLayout";
163+
string basePath = FileHelper.GetLayoutFilePath(layoutName);
164+
string path = basePath;
165+
int counter = 1;
124166

167+
// Generate unique filename if needed
168+
while (File.Exists(path))
169+
{
170+
path = FileHelper.GetLayoutFilePath($"{layoutName}{counter}");
171+
counter++;
172+
}
173+
174+
// Get current theme and positions
125175
GetActiveResourceTheme(out ResourceTheme currentTheme);
126-
GetActiveMapTheme(out MapTheme mapTheme);
127176

128-
var data = new LayoutData
177+
var layoutData = new LayoutData
129178
{
130179
ResourceTheme = currentTheme,
131-
MapTheme = mapTheme,
132180
Offsets = new Dictionary<Element, Vector2>
133181
{
134182
[Element.Chat] = new Vector2(ChatHook.OffsetX, ChatHook.OffsetY),
@@ -143,20 +191,13 @@ public static void SaveCurrentAs(string layoutName, bool setAsActiveInConfig = t
143191
[Element.BarLifeText] = new Vector2(BarLifeTextHook.OffsetX, BarLifeTextHook.OffsetY),
144192
[Element.BarManaText] = new Vector2(BarManaTextHook.OffsetX, BarManaTextHook.OffsetY),
145193
[Element.Buffs] = new Vector2(BuffHook.OffsetX, BuffHook.OffsetY),
146-
[Element.Inventory] = new Vector2(InventoryHook.OffsetX, InventoryHook.OffsetY)
194+
[Element.Inventory] = new Vector2(InventoryHook.OffsetX, InventoryHook.OffsetY),
147195
}
148196
};
149197

150-
WriteLayoutFile(layoutName, data);
198+
LayoutHelper.WriteLayoutFile(Path.GetFileNameWithoutExtension(path), layoutData);
151199

152-
if (setAsActiveInConfig)
153-
{
154-
var cfg = ModContent.GetInstance<Config>();
155-
cfg.Layout = layoutName;
156-
Terraria.ModLoader.Config.ConfigManager.Save(cfg);
157-
}
158-
159-
Log.Info($"Saved layout '{layoutName}' with resource theme '{currentTheme}' and map theme '{mapTheme}'.");
200+
FileHelper.OpenFileAtPath(path);
160201
}
161202

162203
private static void ApplyPositions(Dictionary<Element, Vector2> positions)

Edit/System/EditState.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,34 @@ public static void DrawHitboxOutlineAndText(SpriteBatch sb, Rectangle rect, Elem
172172
return; // Exit early for this element if no mapping for eye toggle
173173
}
174174

175+
// Draw eye toggle (only if mapping was found)
176+
if (EditorFlags.ShowLayerToggle)
177+
{
178+
if (LayerSystem.LayerStates == null)
179+
{
180+
return;
181+
}
182+
183+
bool isCurrentlyVisible = LayerSystem.LayerStates.TryGetValue(interfaceLayerName, out bool currentState) ? currentState : true;
184+
Rectangle eyeRect = new(rect.X - Ass.Inventory_Tick_On.Width(), rect.Y, Ass.Inventory_Tick_On.Width(), Ass.Inventory_Tick_On.Height());
185+
186+
if (eyeRect.Contains(Main.mouseX, Main.mouseY))
187+
{
188+
sb.Draw(Ass.Inventory_Tick_On.Value, eyeRect, Color.White); // Draw hover icon
189+
if (Main.mouseLeft && Main.mouseLeftRelease)
190+
{
191+
isCurrentlyVisible = !isCurrentlyVisible;
192+
// Use interfaceLayerName (from mapping) as the key for LayerSystem.LayerStates
193+
LayerSystem.LayerStates[interfaceLayerName] = isCurrentlyVisible;
194+
Main.mouseLeftRelease = false;
195+
}
196+
}
197+
else // Only draw the normal eye if not hovering
198+
{
199+
sb.Draw(isCurrentlyVisible ? Ass.Inventory_Tick_On.Value : Ass.Inventory_Tick_Off.Value, eyeRect, Color.White);
200+
}
201+
}
202+
175203
// Draw names of the UI elements
176204
// Use interfaceLayerName if available and ShowNames is true, otherwise use element.ToString()
177205
if (EditorFlags.ShowNames)

Edit/System/EditSystem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public override void OnWorldLoad()
3131
editState = new();
3232
ui.SetState(editState);
3333

34+
ExampleLayouts.CreateAllExampleLayouts();
3435
LayoutHelper.TryApplyLayoutFromConfig();
3536
}
3637

Edit/System/LayerSystem.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Collections.Generic;
2+
using Terraria;
3+
using Terraria.ModLoader;
4+
using Terraria.UI;
5+
6+
namespace UICustomizer.Edit.System;
7+
8+
public class LayerSystem : ModSystem
9+
{
10+
public static readonly Dictionary<string, bool> LayerStates = [];
11+
12+
// UI components
13+
private UserInterface userInterface;
14+
15+
public override void OnWorldLoad()
16+
{
17+
base.OnWorldLoad();
18+
userInterface = new UserInterface();
19+
}
20+
public override void UpdateUI(GameTime gameTime)
21+
{
22+
userInterface?.Update(gameTime);
23+
}
24+
25+
public override void ModifyInterfaceLayers(List<GameInterfaceLayer> layers)
26+
{
27+
// build dictionary the first time or when new layers appear
28+
foreach (var l in layers)
29+
if (!LayerStates.ContainsKey(l.Name))
30+
LayerStates[l.Name] = true; // default ON
31+
32+
// apply user choices (never crash if something disappeared)
33+
foreach (var l in layers)
34+
if (LayerStates.TryGetValue(l.Name, out bool show) && !show)
35+
l.Active = false;
36+
37+
// Main overlay
38+
int mouseText = layers.FindIndex(l => l.Name == "Vanilla: Mouse Text");
39+
if (mouseText != -1)
40+
{
41+
layers.Insert(mouseText, new LegacyGameInterfaceLayer(
42+
"UICustomizer: LayerSystem",
43+
() =>
44+
{
45+
userInterface?.Draw(Main.spriteBatch, new GameTime());
46+
return true;
47+
},
48+
InterfaceScaleType.UI));
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)