Skip to content

Commit 784a45b

Browse files
committed
Edit panels UI are almost done
1 parent 66c37a3 commit 784a45b

11 files changed

Lines changed: 484 additions & 87 deletions

File tree

Assets/DarkPanel.png

356 Bytes
Loading

Common/Integrations/DragonLens/DragonLensEditTool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override void DrawIcon(SpriteBatch spriteBatch, Rectangle position)
4545
//if (tex.Width > position.Width || tex.Height > position.Height)
4646
//scale = tex.Width > tex.Height ? position.Width / tex.Width : position.Height / tex.Height;
4747

48-
scale = 1.4f;
48+
scale = 1.0f;
4949
Vector2 pos = new(position.Center.X + 0, position.Center.Y);
5050

5151
spriteBatch.Draw(tex, pos, null, Color.White, 0, tex.Size() / 2f, scale, 0, 0);

Edit/UI/EditPanel.cs

Lines changed: 103 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,102 +3,123 @@
33
using Terraria.GameContent.UI.Elements;
44
using Terraria.ModLoader;
55
using Terraria.UI;
6+
using UICustomizer.Edit.UI;
67
using UICustomizer.EditMode.System;
78

8-
namespace UICustomizer.EditMode.UI
9+
namespace UICustomizer.EditMode.UI;
10+
public sealed class EditPanel : UIPanel
911
{
10-
public class EditPanel : UIPanel
12+
private enum Subpanel { None, Positions, Layouts, Settings }
13+
14+
private readonly IconOptionButton _btnPositions;
15+
private readonly IconOptionButton _btnLayouts;
16+
private readonly IconOptionButton _btnSettings;
17+
18+
private readonly PositionsPanel _positions;
19+
private readonly LayoutsPanel _layouts;
20+
private readonly SettingsPanel _settings;
21+
22+
private Subpanel _active;
23+
24+
private const int IconSize = 40;
25+
private static readonly Point SubPanelPos = new(70, 0);
26+
private static readonly Point SubPanelSize = new(300, 300);
27+
28+
public EditPanel()
1129
{
12-
private UIPanel positionsPanel;
13-
private UIPanel layoutsPanel;
14-
private UIPanel settingsPanel;
30+
Width.Set(50, 0);
31+
Top.Set(785, 0);
32+
Left.Set(20, 0);
33+
Height.Set(6 + IconSize + 6 + IconSize + 6 + IconSize + 6, 0);
34+
SetPadding(0);
1535

16-
public EditPanel()
17-
{
18-
Width.Set(60, 0);
19-
Height.Set(200, 0);
20-
Top.Set(120, 0);
21-
Left.Set(40, 0);
22-
23-
// Base colors (dark blue like Journey Mode UI)
24-
BorderColor = new Color(89, 116, 213, 255) * 0.9f;
25-
BackgroundColor = new Color(73, 94, 171) * 0.9f;
26-
27-
SetPadding(6);
28-
29-
// Create 3 icon buttons
30-
var positionsButton = CreateIconButton(Ass.P.Value, 0, TogglePositions);
31-
var layoutsButton = CreateIconButton(Ass.O.Value, 40, ToggleLayouts);
32-
var settingsButton = CreateIconButton(Ass.S.Value, 80, ToggleSettings);
33-
34-
Append(positionsButton);
35-
Append(layoutsButton);
36-
Append(settingsButton);
37-
38-
// Create sub-panels (hidden initially)
39-
positionsPanel = CreateSubPanel("I am a positions panel");
40-
layoutsPanel = CreateSubPanel("I am a layouts panel");
41-
settingsPanel = CreateSubPanel("I am a settings panel");
42-
}
36+
BorderColor = new Color(89, 116, 213) * 0.9f;
37+
BackgroundColor = new Color(73, 94, 171) * 0.9f;
4338

44-
private UIImage CreateIconButton(Texture2D tex, float topOffset, UIElement.MouseEvent onClick)
45-
{
46-
var button = new UIImage(tex)
47-
{
48-
Width = new StyleDimension(32, 0),
49-
Height = new StyleDimension(32, 0),
50-
Top = new StyleDimension(topOffset, 0),
51-
HAlign = 0.5f
52-
};
53-
button.OnLeftClick += onClick;
54-
return button;
55-
}
39+
_btnPositions = new IconOptionButton(Ass.P, "Positions", 0);
40+
_btnLayouts = new IconOptionButton(Ass.O, "Layouts", 1);
41+
_btnSettings = new IconOptionButton(Ass.S, "Settings", 2);
5642

57-
private UIPanel CreateSubPanel(string labelText)
58-
{
59-
var panel = new UIPanel
60-
{
61-
Width = new StyleDimension(200, 0),
62-
Height = new StyleDimension(120, 0),
63-
Top = new StyleDimension(20, 0),
64-
Left = new StyleDimension(70, 0),
65-
BorderColor = new Color(89, 116, 213, 255) * 0.9f,
66-
BackgroundColor = new Color(73, 94, 171) * 0.9f
67-
};
68-
69-
var text = new UIText(labelText)
70-
{
71-
HAlign = 0.5f,
72-
VAlign = 0.5f
73-
};
74-
75-
panel.Append(text);
76-
panel.Remove(); // start hidden
77-
Append(panel);
78-
return panel;
79-
}
43+
_btnPositions.OnLeftClick += (_, _) => Toggle(Subpanel.Positions);
44+
_btnLayouts.OnLeftClick += (_, _) => Toggle(Subpanel.Layouts);
45+
_btnSettings.OnLeftClick += (_, _) => Toggle(Subpanel.Settings);
8046

81-
private void TogglePositions(UIMouseEvent evt, UIElement listeningElement) => TogglePanel(positionsPanel);
82-
private void ToggleLayouts(UIMouseEvent evt, UIElement listeningElement) => TogglePanel(layoutsPanel);
83-
private void ToggleSettings(UIMouseEvent evt, UIElement listeningElement) => TogglePanel(settingsPanel);
47+
Append(_btnPositions);
48+
Append(_btnLayouts);
49+
Append(_btnSettings);
8450

85-
private void TogglePanel(UIPanel panel)
86-
{
87-
// Hide all first
88-
positionsPanel.Remove();
89-
layoutsPanel.Remove();
90-
settingsPanel.Remove();
51+
_positions = new PositionsPanel();
52+
_layouts = new LayoutsPanel();
53+
_settings = new SettingsPanel();
54+
55+
_active = Subpanel.None;
56+
CloseActive();
57+
}
58+
59+
private void CloseActive()
60+
{
61+
_positions.Remove();
62+
_layouts.Remove();
63+
_settings.Remove();
64+
65+
_btnPositions.SetSelected(false);
66+
_btnLayouts.SetSelected(false);
67+
_btnSettings.SetSelected(false);
9168

92-
// Show selected
93-
Append(panel);
69+
_active = Subpanel.None;
70+
}
71+
72+
private void Toggle(Subpanel target)
73+
{
74+
if (_active == target) { CloseActive(); return; }
75+
76+
_positions.Remove();
77+
_layouts.Remove();
78+
_settings.Remove();
79+
_btnPositions.SetSelected(false);
80+
_btnLayouts.SetSelected(false);
81+
_btnSettings.SetSelected(false);
82+
83+
UIPanel panelToShow = null;
84+
switch (target)
85+
{
86+
case Subpanel.Positions: panelToShow = _positions; _btnPositions.SetSelected(true); break;
87+
case Subpanel.Layouts: panelToShow = _layouts; _btnLayouts.SetSelected(true); break;
88+
case Subpanel.Settings: panelToShow = _settings; _btnSettings.SetSelected(true); break;
9489
}
9590

96-
public override void Draw(SpriteBatch spriteBatch)
91+
if (panelToShow != null)
9792
{
9893
var sys = ModContent.GetInstance<EditSystem>();
99-
if (!sys.Enabled) return;
94+
sys.editState.Append(panelToShow);
10095

101-
base.Draw(spriteBatch);
96+
Vector2 SubPanelPos = new(70, 600);
97+
Vector2 SubPanelSize = new(285, 390);
98+
99+
panelToShow.Left.Set(SubPanelPos.X, 0f);
100+
panelToShow.Top.Set(SubPanelPos.Y, 0f);
101+
panelToShow.Width.Set(SubPanelSize.X, 0f);
102+
panelToShow.Height.Set(SubPanelSize.Y, 0f);
103+
_active = target;
102104
}
103105
}
104-
}
106+
107+
public override void LeftClick(UIMouseEvent evt)
108+
{
109+
var sys = ModContent.GetInstance<EditSystem>();
110+
if (!sys.Enabled)
111+
return;
112+
113+
114+
base.LeftClick(evt);
115+
}
116+
117+
public override void Draw(SpriteBatch spriteBatch)
118+
{
119+
var sys = ModContent.GetInstance<EditSystem>();
120+
if (!sys.Enabled)
121+
return;
122+
123+
base.Draw(spriteBatch);
124+
}
125+
}

Edit/UI/EditToggleButton.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using Microsoft.Xna.Framework.Graphics;
22
using Terraria;
3+
using Terraria.GameContent;
34
using Terraria.GameContent.UI.Elements;
45
using Terraria.ID;
56
using Terraria.ModLoader;
7+
using Terraria.UI;
68
using UICustomizer.EditMode.System;
79

810
namespace UICustomizer.EditMode.UI
@@ -15,10 +17,13 @@ public EditToggleButton() : base(Ass.EditorIcon)
1517
ImageScale = 1f;
1618
Top.Set(260, 0);
1719
Left.Set(Main.GameMode == GameModeID.Creative ? 65 : 30, 0);
20+
}
1821

19-
// Click action
22+
public override void LeftClick(UIMouseEvent evt)
23+
{
24+
base.LeftClick(evt);
2025
var sys = ModContent.GetInstance<EditSystem>();
21-
OnLeftClick += (_, _) => sys.Toggle();
26+
sys.Toggle();
2227
}
2328

2429
public override void Draw(SpriteBatch sb)
@@ -27,7 +32,7 @@ public override void Draw(SpriteBatch sb)
2732

2833
base.Draw(sb);
2934

30-
Rectangle r = new(76, 270, 28, 28);
35+
Rectangle r = new(74, 268, 28, 28);
3136
//sb.Draw(TextureAssets.MagicPixel.Value, r, Color.White*0.5f);
3237
bool hover = r.Contains(Main.MouseScreen.ToPoint());
3338

Edit/UI/IconOptionButton.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Reflection;
3+
using Microsoft.Xna.Framework.Graphics;
4+
using ReLogic.Content;
5+
using Terraria;
6+
using Terraria.GameContent.UI.Elements;
7+
8+
namespace UICustomizer.Edit.UI
9+
{
10+
internal class IconOptionButton : UIColoredImageButton
11+
{
12+
private string name;
13+
14+
public IconOptionButton(Asset<Texture2D> texture, string name, int index) : base(texture, isSmall: false)
15+
{
16+
this.name = name;
17+
Width.Set(40, 0);
18+
Height.Set(40, 0);
19+
HAlign = 0.5f;
20+
21+
Top.Set(46 * index+6,0);
22+
}
23+
24+
public override void Draw(SpriteBatch spriteBatch)
25+
{
26+
_backPanelTexture = Ass.DarkPanel;
27+
base.Draw(spriteBatch);
28+
29+
if (IsMouseHovering)
30+
{
31+
Main.hoverItemName = $"{name}";
32+
}
33+
}
34+
}
35+
}

Edit/UI/Subpanels/LayoutsPanel.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Terraria.GameContent.UI.Elements;
5+
using Terraria.ModLoader.UI;
6+
using Terraria.UI;
7+
using UICustomizer.EditMode.Helpers;
8+
9+
namespace UICustomizer.Edit.UI;
10+
internal sealed class LayoutsPanel : UIPanel
11+
{
12+
public LayoutsPanel()
13+
{
14+
BorderColor = new Color(89, 116, 213) * 0.9f;
15+
BackgroundColor = new Color(73, 94, 171) * 0.9f;
16+
17+
Width.Set(360, 0);
18+
Height.Set(260, 0);
19+
Left.Set(70, 0);
20+
Top.Set(0, 0);
21+
SetPadding(8);
22+
23+
Build();
24+
}
25+
26+
private void Build()
27+
{
28+
RemoveAllChildren();
29+
30+
float y = 4;
31+
32+
// Active layout
33+
Append(MakeButton("Use Active", "Switch to Active layout", () =>
34+
{
35+
TryCall(() => LayoutHelper.ApplyLayout("Active"));
36+
TryCall(() => LayoutHelper.CurrentLayoutName = "Active");
37+
TryCall(() => LayoutHelper.SaveLastLayout());
38+
}, y));
39+
y += 28;
40+
41+
// Presets
42+
Append(new UIText("Presets", 0.9f) { Top = { Pixels = y }, Left = { Pixels = 6 } });
43+
y += 20;
44+
45+
var names = TryGetLayouts() ?? new List<string>();
46+
foreach (var name in names.Where(n => !string.Equals(n, "Active", StringComparison.OrdinalIgnoreCase)))
47+
{
48+
Append(MakeButton(name, "Apply this layout", () =>
49+
{
50+
TryCall(() => LayoutHelper.ApplyLayout(name));
51+
TryCall(() => LayoutHelper.CurrentLayoutName = name);
52+
TryCall(() => LayoutHelper.SaveLastLayout());
53+
}, y));
54+
55+
y += 26;
56+
}
57+
58+
y += 6;
59+
Append(new UIText("Options", 0.9f) { Top = { Pixels = y }, Left = { Pixels = 6 } });
60+
y += 20;
61+
62+
Append(MakeButton("Open layout folder", "Open layout folder", () => TryCall(FileHelper.OpenLayoutFolder), y)); y += 26;
63+
Append(MakeButton("Save as new layout", "Create a new layout file", () => TryCall(() => FileHelper.CreateAndOpenNewLayoutFile("MyCustomLayout")), y)); y += 26;
64+
Append(MakeButton("Remove all layouts", "Delete all layouts", () => TryCall(FileHelper.DeleteAllLayouts), y)); y += 26;
65+
}
66+
67+
private UIElement MakeButton(string text, string tip, Action onClick, float y)
68+
{
69+
var btn = new UIPanel
70+
{
71+
Width = { Percent = 1f, Pixels = -12 },
72+
Height = { Pixels = 22 },
73+
Top = { Pixels = y },
74+
Left = { Pixels = 6 },
75+
BackgroundColor = new Color(60, 80, 150) * 0.9f,
76+
BorderColor = new Color(40, 55, 110)
77+
};
78+
btn.SetPadding(0);
79+
btn.Append(new UIText(text, 0.85f) { HAlign = 0.5f, VAlign = 0.5f });
80+
btn.OnLeftClick += (_, _) => onClick();
81+
btn.OnMouseOver += (_, _) =>
82+
{
83+
UICommon.TooltipMouseText(tip);
84+
btn.BackgroundColor = new Color(80, 100, 170) * 0.95f;
85+
};
86+
btn.OnMouseOut += (_, _) => btn.BackgroundColor = new Color(60, 80, 150) * 0.9f;
87+
return btn;
88+
}
89+
90+
private static void TryCall(Action a)
91+
{
92+
try { a?.Invoke(); } catch { /* no-op if helpers not present */ }
93+
}
94+
private static List<string> TryGetLayouts()
95+
{
96+
try { return FileHelper.GetLayouts().ToList(); } catch { return null; }
97+
}
98+
}

0 commit comments

Comments
 (0)