-
Notifications
You must be signed in to change notification settings - Fork 554
Expand file tree
/
Copy pathChipSaveMenu.cs
More file actions
197 lines (164 loc) · 6.99 KB
/
ChipSaveMenu.cs
File metadata and controls
197 lines (164 loc) · 6.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using DLS.Description;
using DLS.Game;
using DLS.SaveSystem;
using Seb.Types;
using Seb.Vis;
using Seb.Vis.UI;
using UnityEngine;
using Random = System.Random;
namespace DLS.Graphics
{
public static class ChipSaveMenu
{
public const string MaxLengthChipName = "MY VERY LONG CHIP NAME";
const int CancelButtonIndex = 0;
const int CustomizeButtonIndex = 1;
const int SaveButtonIndex = 2;
const int SaveAsButtonIndex = 3;
static readonly UIHandle ID_ChipNameField = new("SaveMenu_ChipNameField");
static readonly Func<string, bool> chipNameValidator = ValidateChipNameInput;
static readonly Random rng = new();
public static SubChipInstance ActiveCustomizeChip;
static SubChipInstance CustomizeStateBeforeEnteringCustomizeMenu;
static readonly string[] CancelSaveButtonNames =
{
"CANCEL", "CUSTOMIZE", "SAVE"
};
static readonly string[] CancelRenameSaveButtonNames =
{
"CANCEL", "CUSTOMIZE", "RENAME", "SAVE AS"
};
static readonly bool[] ButtonGroupInteractStates = { true, true, true, true };
public static ChipDescription ActiveCustomizeDescription => ActiveCustomizeChip.Description;
public static void OnMenuOpened()
{
ActiveCustomizeChip ??= CreateCustomizationState();
InitUIFromDescription(ActiveCustomizeChip.Description);
}
public static (Vector2 size, float pad) GetTextInputSize()
{
const float textPad = 2;
InputFieldTheme inputTheme = DrawSettings.ActiveUITheme.ChipNameInputField;
Vector2 inputFieldSize = UI.CalculateTextSize(MaxLengthChipName, inputTheme.fontSize, inputTheme.font) + new Vector2(textPad * 2, 3);
return (inputFieldSize, textPad);
}
public static void DrawMenu()
{
MenuHelper.DrawBackgroundOverlay();
DrawSettings.UIThemeDLS theme = DrawSettings.ActiveUITheme;
InputFieldTheme inputTheme = DrawSettings.ActiveUITheme.ChipNameInputField;
InputFieldState inputFieldState;
using (UI.BeginBoundsScope(true))
{
Draw.ID panelID = UI.ReservePanel();
// -- Chip name input field --
(Vector2 inputFieldSize, float inputFieldTextPad) = GetTextInputSize();
inputFieldState = UI.InputField(ID_ChipNameField, inputTheme, new Vector2(50, 33), inputFieldSize, "Name", Anchor.Centre, inputFieldTextPad, chipNameValidator, true);
Vector2 buttonTopLeft = UI.PrevBounds.BottomLeft + Vector2.down * (DrawSettings.DefaultButtonSpacing * 2);
bool renaming = Project.ActiveProject.ChipHasBeenSavedBefore && !ChipDescription.NameMatch(inputFieldState.text, Project.ActiveProject.ViewedChip.LastSavedDescription.Name);
bool saveButtonEnabled = IsValidSaveName(inputFieldState.text);
ButtonGroupInteractStates[SaveButtonIndex] = saveButtonEnabled;
ButtonGroupInteractStates[SaveAsButtonIndex] = saveButtonEnabled;
string[] buttonGroupNames = renaming ? CancelRenameSaveButtonNames : CancelSaveButtonNames;
int buttonIndex = UI.HorizontalButtonGroup(buttonGroupNames, ButtonGroupInteractStates, theme.ButtonTheme, buttonTopLeft, UI.PrevBounds.Width, DrawSettings.DefaultButtonSpacing, 0, Anchor.TopLeft);
bool confirmShortcut = !renaming && KeyboardShortcuts.ConfirmShortcutTriggered;
if (buttonIndex == CancelButtonIndex || KeyboardShortcuts.CancelShortcutTriggered)
{
Cancel();
}
else if (buttonIndex == CustomizeButtonIndex)
{
OpenCustomizationMenu();
}
else if (buttonIndex == SaveButtonIndex || confirmShortcut)
{
Save(renaming ? Project.SaveMode.Rename : Project.SaveMode.Normal);
}
else if (buttonIndex == SaveAsButtonIndex)
{
Save(Project.SaveMode.SaveAs);
}
Bounds2D uiBounds = UI.GetCurrentBoundsScope();
MenuHelper.DrawReservedMenuPanel(panelID, uiBounds);
// Update customization state
if (ActiveCustomizeChip != null)
{
string newName = inputFieldState.text;
if (ActiveCustomizeDescription.Name != newName)
{
ActiveCustomizeDescription.Name = newName;
Vector2 minChipSize = SubChipInstance.CalculateMinChipSize(ActiveCustomizeDescription.InputPins, ActiveCustomizeDescription.OutputPins, newName, NameAlignment.Centre);
Vector2 chipSizeNew = Vector2.Max(minChipSize, ActiveCustomizeDescription.Size);
ActiveCustomizeDescription.Size = chipSizeNew;
}
}
}
}
// Create a subchip instance based on the current dev chip (we need a subchip instance to be able to draw a preview of the chip in the customization menu)
// The description on this subchip holds potential customizations, such as name changes, resizing, colour etc.
static SubChipInstance CreateCustomizationState()
{
ChipDescription desc = DescriptionCreator.CreateChipDescription(Project.ActiveProject.ViewedChip);
return CreatePreviewSubChipInstance(desc);
}
static void OpenCustomizationMenu()
{
ActiveCustomizeChip = CreatePreviewSubChipInstance(ActiveCustomizeDescription);
CustomizeStateBeforeEnteringCustomizeMenu = CreatePreviewSubChipInstance(Saver.CloneChipDescription(ActiveCustomizeDescription));
UIDrawer.SetActiveMenu(UIDrawer.MenuType.ChipCustomization);
}
public static void RevertCustomizationStateToBeforeEnteringCustomizeMenu()
{
ActiveCustomizeChip = CustomizeStateBeforeEnteringCustomizeMenu;
CustomizeStateBeforeEnteringCustomizeMenu = CreatePreviewSubChipInstance(Saver.CloneChipDescription(ActiveCustomizeDescription));
}
static SubChipInstance CreatePreviewSubChipInstance(ChipDescription desc)
{
SubChipDescription subChipDesc = new(desc.Name, 0, string.Empty, Vector2.zero, Array.Empty<OutputPinColourInfo>());
return new SubChipInstance(desc, subChipDesc);
}
public static bool ValidateChipNameInput(string nameInput) => nameInput.Length <= MaxLengthChipName.Length && !SaveUtils.NameContainsForbiddenChar(nameInput);
static bool IsValidSaveName(string chipName)
{
Project project = Project.ActiveProject;
bool validName = !string.IsNullOrWhiteSpace(chipName) && SaveUtils.ValidFileName(chipName);
bool nameAlreadyUsed = project.chipLibrary.HasChip(chipName);
bool isNameOfActiveChip = ChipDescription.NameMatch(project.ActiveDevChipName, chipName);
bool isValid = validName && (!nameAlreadyUsed || isNameOfActiveChip);
return isValid;
}
static void InitUIFromDescription(ChipDescription chipDesc)
{
// Set input field to current chip name
InputFieldState inputFieldState = UI.GetInputFieldState(ID_ChipNameField);
inputFieldState.SetText(chipDesc.Name);
}
static void Save(Project.SaveMode mode)
{
Project.ActiveProject.SaveFromDescription(ActiveCustomizeDescription, mode);
CloseMenu();
}
static void Cancel()
{
CloseMenu();
}
static void CloseMenu()
{
ActiveCustomizeChip = null;
UIDrawer.SetActiveMenu(UIDrawer.MenuType.None);
}
public static void Reset()
{
ActiveCustomizeChip = null;
CustomizeStateBeforeEnteringCustomizeMenu = null;
}
static Color RandomInitialColour()
{
float h = (float)rng.NextDouble();
float s = Mathf.Lerp(0.2f, 1, (float)rng.NextDouble());
float v = Mathf.Lerp(0.2f, 1, (float)rng.NextDouble());
return Color.HSVToRGB(h, s, v);
}
}
}