-
Notifications
You must be signed in to change notification settings - Fork 12
/
CustomControl.cs
268 lines (221 loc) · 9.52 KB
/
CustomControl.cs
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Material_Editor
{
public abstract class CustomControl : IDisposable
{
public readonly string Name;
public bool Serialize = true;
public string BaseToolTip;
protected Func<CustomControl, bool> VisibilityCallback;
protected Action<CustomControl> ChangedCallback;
public CustomControl(string label)
{
Name = label;
CreateControls();
}
public virtual Label LabelControl
{
get { return null; }
}
public virtual Control Control
{
get { return null; }
}
public virtual Control ExtraControl
{
get { return null; }
}
public virtual void CreateControls() { }
public virtual object GetProperty()
{
return null;
}
public void SetVisible(bool visible)
{
if (LabelControl != null)
LabelControl.Visible = visible;
if (Control != null)
Control.Visible = visible;
if (ExtraControl != null)
ExtraControl.Visible = visible;
}
public void SetTooltip(ToolTip parentTooltip, string toolTip)
{
BaseToolTip = toolTip;
if (LabelControl != null)
parentTooltip.SetToolTip(LabelControl, toolTip);
if (Control != null)
parentTooltip.SetToolTip(Control, toolTip);
if (ExtraControl != null)
parentTooltip.SetToolTip(ExtraControl, toolTip);
}
public bool ShouldBeVisible()
{
return VisibilityCallback?.Invoke(this) ?? true;
}
public void InvokeChangedCallback()
{
ChangedCallback?.Invoke(this);
}
public void Dispose()
{
LabelControl?.Dispose();
Control?.Dispose();
ExtraControl?.Dispose();
GC.SuppressFinalize(this);
}
}
public static class ControlFactory
{
private static readonly Dictionary<string, CustomControl> customControls = [];
public static Action<CustomControl> DefaultChangedCallback;
public static void ClearControls()
{
foreach (var control in customControls)
{
(control.Value.Control.Parent as TableLayoutPanel).RowCount = 0;
(control.Value.Control.Parent as TableLayoutPanel).RowStyles.Clear();
control.Value.LabelControl?.Parent.Controls.Remove(control.Value.LabelControl);
control.Value.Control?.Parent.Controls.Remove(control.Value.Control);
control.Value.ExtraControl?.Parent.Controls.Remove(control.Value.ExtraControl);
control.Value.Dispose();
}
customControls.Clear();
}
public static CustomControl Find(string name)
{
if (customControls.TryGetValue(name, out CustomControl value))
return value;
return null;
}
public static bool GetProperty(string name, out object property)
{
if (customControls.TryGetValue(name, out CustomControl control))
{
property = control.GetProperty();
return true;
}
property = null;
return false;
}
public static void SetVisible(string name, bool visible, bool serialize = true)
{
if (customControls.TryGetValue(name, out CustomControl value))
{
var control = value;
control.SetVisible(visible);
control.Serialize = serialize;
}
}
public static void SetSerialize(string name, bool visible)
{
if (customControls.TryGetValue(name, out CustomControl value))
value.Serialize = visible;
}
public static void SetTooltip(string name, ToolTip parentTooltip, string toolTip)
{
if (customControls.TryGetValue(name, out CustomControl value))
{
var control = value;
control.SetTooltip(parentTooltip, toolTip);
}
}
public static void UpdateVisibility()
{
foreach (var control in customControls)
{
control.Value.SetVisible(control.Value.ShouldBeVisible());
}
}
public static void UpdateVisibility(string name)
{
if (customControls.TryGetValue(name, out CustomControl control))
control.SetVisible(control.ShouldBeVisible());
}
public static CustomControl CreateControl(TableLayoutPanel parent, string label, object property, Func<CustomControl, bool> visibilityCallback = null, Action<CustomControl> changedCallback = null)
{
CustomControl control = null;
var type = property.GetType();
if (type == typeof(int))
{
control = NumberControl.ForInteger(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (int)property);
}
else if (type == typeof(uint))
{
control = NumberControl.ForInteger(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (uint)property, uint.MinValue, uint.MaxValue);
}
else if (type == typeof(short))
{
control = NumberControl.ForInteger(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (short)property, short.MinValue, short.MaxValue);
}
else if (type == typeof(ushort))
{
control = NumberControl.ForInteger(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (ushort)property, ushort.MinValue, ushort.MaxValue);
}
else if (type == typeof(byte))
{
control = NumberControl.ForInteger(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (byte)property, byte.MinValue, byte.MaxValue);
}
else if (type == typeof(decimal) || type == typeof(float) || type == typeof(double))
{
control = NumberControl.ForDecimal(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, Convert.ToDecimal(property));
}
else if (type == typeof(bool))
{
control = new CheckControl(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (bool)property);
}
else if (type == typeof(Color))
{
control = new ColorControl(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, (Color)property);
}
else if (type == typeof(object[]))
{
control = new DropdownControl(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, property as object[], 0);
}
if (control != null)
{
AddCustomControl(parent, label, control);
}
return control;
}
public static void AddCustomControl(TableLayoutPanel parent, string label, CustomControl control)
{
parent.RowCount++;
parent.RowStyles.Add(new RowStyle(SizeType.AutoSize));
parent.Controls.Add(control.LabelControl, 0, parent.RowCount - 1);
parent.Controls.Add(control.Control, 1, parent.RowCount - 1);
if (control.ExtraControl != null)
parent.Controls.Add(control.ExtraControl, 2, parent.RowCount - 1);
customControls.Add(label, control);
}
public static CustomControl CreateDropdownControl(TableLayoutPanel parent, string label, object[] entries, int selection, Func<CustomControl, bool> visibilityCallback = null, Action<CustomControl> changedCallback = null)
{
var control = new DropdownControl(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, entries, selection);
AddCustomControl(parent, label, control);
return control;
}
public static CustomControl CreateFlagControl(TableLayoutPanel parent, string label, object[] entries, int flagValue, Func<CustomControl, bool> visibilityCallback = null, Action<CustomControl> changedCallback = null)
{
var control = new FlagControl(label, visibilityCallback, changedCallback ?? DefaultChangedCallback, entries, flagValue);
AddCustomControl(parent, label, control);
return control;
}
public static CustomControl CreateFileControl(TableLayoutPanel parent, string label, Font font, FileControl.FileType fileType, string filePath, Func<CustomControl, bool> visibilityCallback = null, Action<CustomControl> changedCallback = null)
{
CustomControl control = null;
control = fileType switch
{
FileControl.FileType.Material => new FileControl(label, font, visibilityCallback, changedCallback ?? DefaultChangedCallback, FileControl.FileType.Material, filePath),
_ => new FileControl(label, font, visibilityCallback, changedCallback ?? DefaultChangedCallback, FileControl.FileType.Texture, filePath),
};
if (control != null)
{
AddCustomControl(parent, label, control);
}
return control;
}
}
}