-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathChipCustomizationMenu.cs
More file actions
246 lines (210 loc) · 9.88 KB
/
ChipCustomizationMenu.cs
File metadata and controls
246 lines (210 loc) · 9.88 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Linq;
using DLS.Description;
using DLS.Game;
using DLS.Simulation;
using Seb.Helpers;
using Seb.Vis;
using Seb.Vis.UI;
using UnityEngine;
namespace DLS.Graphics
{
public static class ChipCustomizationMenu
{
static readonly string[] nameDisplayOptions =
{
"Name: Middle",
"Name: Top",
"Name: Hidden"
};
static readonly string[] cachingOptions = { "Caching: Off", "Caching: On" };
// ---- State ----
static SubChipInstance[] subChipsWithDisplays;
static string displayLabelString;
static string colHexCodeString;
static readonly UIHandle ID_DisplaysScrollView = new("CustomizeMenu_DisplaysScroll");
static readonly UIHandle ID_ColourPicker = new("CustomizeMenu_ChipCol");
static readonly UIHandle ID_ColourHexInput = new("CustomizeMenu_ChipColHexInput");
static readonly UIHandle ID_NameDisplayOptions = new("CustomizeMenu_NameDisplayOptions");
static readonly UIHandle ID_CachingOptions = new("CustomizeMenu_CachingOptions");
static readonly UI.ScrollViewDrawElementFunc drawDisplayScrollEntry = DrawDisplayScroll;
static readonly Func<string, bool> hexStringInputValidator = ValidateHexStringInput;
public static void OnMenuOpened()
{
DevChipInstance chip = Project.ActiveProject.ViewedChip;
subChipsWithDisplays = chip.GetSubchips().Where(c => c.Description.HasDisplay()).OrderBy(c => c.Position.x).ThenBy(c => c.Position.y).ToArray();
CustomizationSceneDrawer.OnCustomizationMenuOpened();
displayLabelString = $"DISPLAYS ({subChipsWithDisplays.Length}):";
InitUIFromChipDescription();
}
public static void DrawMenu()
{
// Don't draw menu when placing display
if (CustomizationSceneDrawer.IsPlacingDisplay) return;
const float width = 20;
const float pad = UILayoutHelper.DefaultSpacing;
const float pw = width - pad * 2;
DrawSettings.UIThemeDLS theme = DrawSettings.ActiveUITheme;
UI.DrawPanel(UI.TopLeft, new Vector2(width, UI.Height), theme.MenuPanelCol, Anchor.TopLeft);
// ---- Cancel/confirm buttons ----
int cancelConfirmButtonIndex = MenuHelper.DrawButtonPair("CANCEL", "CONFIRM", UI.TopLeft + Vector2.down * pad, pw, false);
// ---- Chip name UI ----
int nameDisplayMode = UI.WheelSelector(ID_NameDisplayOptions, nameDisplayOptions, NextPos(), new Vector2(pw, DrawSettings.ButtonHeight), theme.OptionsWheel, Anchor.TopLeft);
ChipSaveMenu.ActiveCustomizeDescription.NameLocation = (NameDisplayLocation)nameDisplayMode;
// ---- Chip colour UI ----
Color newCol = UI.DrawColourPicker(ID_ColourPicker, NextPos(), pw, Anchor.TopLeft);
InputFieldTheme inputTheme = MenuHelper.Theme.ChipNameInputField;
inputTheme.fontSize = MenuHelper.Theme.FontSizeRegular;
InputFieldState hexColInput = UI.InputField(ID_ColourHexInput, inputTheme, NextPos(), new Vector2(pw, DrawSettings.ButtonHeight), "#", Anchor.TopLeft, 1, hexStringInputValidator);
if (newCol != ChipSaveMenu.ActiveCustomizeDescription.Colour)
{
ChipSaveMenu.ActiveCustomizeDescription.Colour = newCol;
UpdateChipColHexStringFromColour(newCol);
}
else if (colHexCodeString != hexColInput.text)
{
UpdateChipColFromHexString(hexColInput.text);
}
// ---- Chip caching UI ----
UI.DrawText("Chip Caching:", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeDefault, NextPos(1), Anchor.TopLeft, Color.white);
SimChip chip = Project.ActiveProject.ViewedChip.SimChip;
if (chip.IsCombinational())
{
int numberOfInputBits = chip.CalculateNumberOfInputBits();
if (numberOfInputBits <= Simulator.MAX_NUM_INPUT_BITS_WHEN_AUTO_CACHING)
{
UI.DrawText("This chip is being cached.", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
}
else if (numberOfInputBits <= Simulator.MAX_NUM_INPUT_BITS_WHEN_USER_CACHING)
{
int shouldBeCachedNum = UI.WheelSelector(ID_CachingOptions, cachingOptions, NextPos(), new Vector2(pw, DrawSettings.ButtonHeight), theme.OptionsWheel, Anchor.TopLeft);
bool shouldBeCached = false;
if (shouldBeCachedNum == 1) shouldBeCached = true;
ChipSaveMenu.ActiveCustomizeDescription.ShouldBeCached = shouldBeCached;
UI.DrawText("WARNING: Caching chips with many", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("input bits significantly", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("increases the time required to", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("create the cache and may also", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("increase memory consumption!", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
}
else
{
UI.DrawText("This chip has too many input", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("bits to be cached.", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
}
}
else
{
UI.DrawText("Non-combinational chips", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
UI.DrawText("can not be cached.", UIThemeLibrary.DefaultFont, UIThemeLibrary.FontSizeSmall, NextPos(), Anchor.TopLeft, Color.white);
}
// ---- Displays UI ----
Color labelCol = ColHelper.Darken(theme.MenuPanelCol, 0.01f);
Vector2 labelPos = NextPos(1);
UI.TextWithBackground(labelPos, new Vector2(pw, DrawSettings.ButtonHeight), Anchor.TopLeft, displayLabelString, theme.FontBold, theme.FontSizeRegular, Color.white, labelCol);
float scrollViewHeight = 20;
float scrollViewSpacing = UILayoutHelper.DefaultSpacing;
UI.DrawScrollView(ID_DisplaysScrollView, NextPos(), new Vector2(pw, scrollViewHeight), scrollViewSpacing, Anchor.TopLeft, theme.ScrollTheme, drawDisplayScrollEntry, subChipsWithDisplays.Length);
Vector2 NextPos(float extraPadding = 0)
{
return UI.PrevBounds.BottomLeft + Vector2.down * (pad + extraPadding);
}
// Cancel
if (cancelConfirmButtonIndex == 0)
{
RevertChanges();
UIDrawer.SetActiveMenu(UIDrawer.MenuType.ChipSave);
}
// Confirm
else if (cancelConfirmButtonIndex == 1)
{
UpdateCustomizeDescription();
UIDrawer.SetActiveMenu(UIDrawer.MenuType.ChipSave);
}
}
static void DrawDisplayScroll(Vector2 pos, float width, int i, bool isLayoutPass)
{
DrawSettings.UIThemeDLS theme = DrawSettings.ActiveUITheme;
SubChipInstance subChip = subChipsWithDisplays[i];
ChipDescription chipDesc = subChip.Description;
string label = subChip.Label;
string displayName = string.IsNullOrWhiteSpace(label) ? chipDesc.Name : label;
// Don't allow adding same display multiple times
bool enabled = CustomizationSceneDrawer.SelectedDisplay == null || subChip.ID != CustomizationSceneDrawer.SelectedDisplay.Desc.SubChipID; // display is removed from list when selected, so check manually here
foreach (DisplayInstance d in ChipSaveMenu.ActiveCustomizeChip.Displays)
{
if (d.Desc.SubChipID == subChip.ID)
{
enabled = false;
break;
}
}
// Display selected, start placement
if (UI.Button(displayName, theme.ButtonTheme, pos, new Vector2(width, 0), enabled, false, true, Anchor.TopLeft))
{
SubChipDescription subChipDesc = new(chipDesc.Name, subChipsWithDisplays[i].ID, string.Empty, Vector2.zero, null);
SubChipInstance instance = new(chipDesc, subChipDesc);
CustomizationSceneDrawer.StartPlacingDisplay(instance);
}
}
static void RevertChanges()
{
ChipSaveMenu.RevertCustomizationStateToBeforeEnteringCustomizeMenu();
InitUIFromChipDescription();
}
static void InitUIFromChipDescription()
{
// Init col picker to chip colour
ColourPickerState chipColourPickerState = UI.GetColourPickerState(ID_ColourPicker);
Color.RGBToHSV(ChipSaveMenu.ActiveCustomizeDescription.Colour, out chipColourPickerState.hue, out chipColourPickerState.sat, out chipColourPickerState.val);
UpdateChipColHexStringFromColour(chipColourPickerState.GetRGB());
// Init name display mode
WheelSelectorState nameDisplayWheelState = UI.GetWheelSelectorState(ID_NameDisplayOptions);
nameDisplayWheelState.index = (int)ChipSaveMenu.ActiveCustomizeDescription.NameLocation;
// Init cache setting
WheelSelectorState cacheSettingWheelState = UI.GetWheelSelectorState(ID_CachingOptions);
bool cacheBool = ChipSaveMenu.ActiveCustomizeDescription.ShouldBeCached;
int cacheInt = 0;
if (cacheBool) cacheInt = 1;
cacheSettingWheelState.index = cacheInt;
}
static void UpdateCustomizeDescription()
{
List<DisplayInstance> displays = ChipSaveMenu.ActiveCustomizeChip.Displays;
ChipSaveMenu.ActiveCustomizeDescription.Displays = displays.Select(s => s.Desc).ToArray();
}
static void UpdateChipColHexStringFromColour(Color col)
{
int colInt = (byte)(col.r * 255) << 16 | (byte)(col.g * 255) << 8 | (byte)(col.b * 255);
colHexCodeString = "#" + $"{colInt:X6}";
UI.GetInputFieldState(ID_ColourHexInput).SetText(colHexCodeString, false);
}
static void UpdateChipColFromHexString(string hexString)
{
colHexCodeString = hexString;
hexString = hexString.Replace("#", "");
hexString = hexString.PadRight(6, '0');
if (ColHelper.TryParseHexCode(hexString, out Color col))
{
UI.GetColourPickerState(ID_ColourPicker).SetRGB(col);
ChipSaveMenu.ActiveCustomizeDescription.Colour = col;
}
}
static bool ValidateHexStringInput(string text)
{
if (string.IsNullOrWhiteSpace(text)) return true;
int numHexDigits = 0;
for (int i = 0; i < text.Length; i++)
{
if (i == 0 && text[i] == '#') continue;
if (Uri.IsHexDigit(text[i]))
{
numHexDigits++;
}
else return false;
}
return numHexDigits <= 6;
}
}
}