-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUIManager.cs
More file actions
226 lines (185 loc) · 9.34 KB
/
UIManager.cs
File metadata and controls
226 lines (185 loc) · 9.34 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
using System;
using System.Collections.Generic;
using UnityEngine;
namespace com.github.lhervier.ksp {
public class UIManager {
private readonly ConfigManager configManager;
// UI - Main window
private bool showUI = false;
private Rect mainWindowRect = new Rect(50, 50, 300, 200);
// UI - Editor window
private bool showEditorWindow = false;
private Rect editorWindowRect = new Rect(400, 50, 400, 700);
// UI - State
private int editingMarkerIndex = -1;
private VisualMarker editingMarker = null;
private bool isCreatingNewMarker = false;
public bool ShowUI {
get => showUI;
set => showUI = value;
}
public VisualMarker EditingMarker => editingMarker;
public int EditingMarkerIndex => editingMarkerIndex;
public UIManager(ConfigManager configManager) {
this.configManager = configManager;
}
public void DrawUI() {
if (!showUI) return;
mainWindowRect = GUI.Window(12345, mainWindowRect, DrawMainWindow, "DrawLayer - Visual Markers");
if (showEditorWindow) {
editorWindowRect = GUI.Window(12346, editorWindowRect, DrawEditorWindow, "Marker Editor");
}
}
private void DrawMainWindow(int windowID) {
GUILayout.Label("Visual Markers", GUI.skin.box);
GUILayout.Space(10);
// Button to create a new marker
if (GUILayout.Button("Create Marker")) {
isCreatingNewMarker = true;
editingMarkerIndex = -1;
editingMarker = new VisualMarker();
showEditorWindow = true;
}
GUILayout.Space(10);
// List of existing markers
GUILayout.Label("Existing markers:", GUI.skin.box);
var markers = configManager.Markers;
for (int i = 0; i < markers.Count; i++) {
GUILayout.BeginHorizontal();
bool visible = GUILayout.Toggle(markers[i].visible, "");
if (visible != markers[i].visible) {
markers[i].visible = visible;
configManager.UpdateMarker(i, markers[i]);
}
if (GUILayout.Button($"Edit {markers[i].name}", GUILayout.ExpandWidth(true))) {
editingMarkerIndex = i;
isCreatingNewMarker = false;
editingMarker = new VisualMarker(markers[i]);
showEditorWindow = true;
}
if (GUILayout.Button("Del", GUILayout.Width(50))) {
configManager.RemoveMarker(i);
if (editingMarkerIndex == i) {
editingMarkerIndex = -1;
editingMarker = null;
showEditorWindow = false;
} else if (editingMarkerIndex > i) {
editingMarkerIndex--;
}
break;
}
GUILayout.EndHorizontal();
}
GUI.DragWindow();
}
private void DrawEditorWindow(int windowID) {
GUILayout.BeginVertical(GUI.skin.box);
// ===== COMMON PROPERTIES =====
GUILayout.Label("Common Properties:", GUI.skin.box);
// Nom
GUILayout.Label("Name:");
editingMarker.name = GUILayout.TextField(editingMarker.name, GUILayout.Width(200));
// Position
GUILayout.Label("Position (% width, % height):");
GUILayout.BeginHorizontal();
editingMarker.positionX = GUILayout.HorizontalSlider(editingMarker.positionX, 0f, 100f);
editingMarker.positionY = GUILayout.HorizontalSlider(editingMarker.positionY, 0f, 100f);
GUILayout.EndHorizontal();
GUILayout.Label($"X: {editingMarker.positionX:F1}%, Y: {editingMarker.positionY:F1}%");
// Button to reset the position (common to both types)
if (GUILayout.Button("Reset Position to Center (50%, 50%)")) {
editingMarker.positionX = 50f;
editingMarker.positionY = 50f;
}
// Color
GUILayout.Label("Color:");
// Color selection by buttons
string[] colorNames = Enum.GetNames(typeof(PredefinedColors));
int colorIndex = (int) editingMarker.color;
int newColorIndex = GUILayout.SelectionGrid(
colorIndex,
colorNames,
4
);
if (newColorIndex != colorIndex && newColorIndex >= 0) {
editingMarker.color = (PredefinedColors) newColorIndex;
}
// Current color preview
GUILayout.BeginHorizontal();
GUILayout.Label("Current color:");
Color originalColor = GUI.color;
GUI.color = editingMarker.color.ToColor();
GUILayout.Box("", GUILayout.Height(20), GUILayout.Width(100));
GUI.color = originalColor;
GUILayout.EndHorizontal();
GUILayout.Space(10);
// ===== MARKER TYPE =====
GUILayout.Label("Marker Type:", GUI.skin.box);
editingMarker.type = (MarkerType)GUILayout.SelectionGrid((int)editingMarker.type,
new string[] { "Cross Lines", "Circle" }, 2);
GUILayout.Space(10);
// ===== SPECIFIC PROPERTIES FOR TYPE =====
if (editingMarker.type == MarkerType.CrossLines) {
GUILayout.Label("Cross Lines Properties:", GUI.skin.box);
GUILayout.Label("No additional properties for cross lines.");
} else if (editingMarker.type == MarkerType.Circle) {
GUILayout.Label("Circle Properties:", GUI.skin.box);
GUILayout.Label("Radius (% width):");
editingMarker.radius = GUILayout.HorizontalSlider(editingMarker.radius, 1f, 50f);
GUILayout.Label($"Radius: {editingMarker.radius:F1}%");
GUILayout.Label("Main graduation divisions:");
// Slider for predefined divisions (1, 2, 3, 4, 6, 8, 12, 36)
int[] divisions = { 1, 2, 3, 4, 6, 8, 12, 36 };
int currentIndex = System.Array.IndexOf(divisions, editingMarker.divisions);
if (currentIndex == -1) currentIndex = 2; // Default value (4 divisions)
currentIndex = (int)GUILayout.HorizontalSlider(currentIndex, 0, divisions.Length - 1);
editingMarker.divisions = divisions[currentIndex];
// Display the corresponding degrees
float degrees = editingMarker.divisions > 1 ? 360f / editingMarker.divisions : 0f;
string divisionText = editingMarker.divisions == 1 ? "No graduations" : $"{editingMarker.divisions} divisions ({degrees:F0}°)";
GUILayout.Label($"Divisions: {divisionText}");
}
// Action buttons
GUILayout.BeginHorizontal();
if (isCreatingNewMarker) {
if (GUILayout.Button("Create")) {
configManager.AddMarker(editingMarker);
editingMarker = null;
isCreatingNewMarker = false;
showEditorWindow = false;
editingMarkerIndex = -1;
}
if (GUILayout.Button("Cancel")) {
editingMarker = null;
isCreatingNewMarker = false;
showEditorWindow = false;
editingMarkerIndex = -1;
}
} else {
if (GUILayout.Button("Update")) {
configManager.UpdateMarker(editingMarkerIndex, editingMarker);
editingMarker = null;
isCreatingNewMarker = false;
showEditorWindow = false;
editingMarkerIndex = -1;
}
if (GUILayout.Button("Cancel")) {
editingMarker = null;
isCreatingNewMarker = false;
showEditorWindow = false;
editingMarkerIndex = -1;
}
}
GUILayout.EndHorizontal();
GUILayout.EndVertical();
GUI.DragWindow();
}
// Method to detect if two colors are similar
private bool IsColorSimilar(Color color1, Color color2) {
float tolerance = 0.1f; // Tolerance for comparison
return Mathf.Abs(color1.r - color2.r) < tolerance &&
Mathf.Abs(color1.g - color2.g) < tolerance &&
Mathf.Abs(color1.b - color2.b) < tolerance;
}
}
}