This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewBuilder.cs
603 lines (524 loc) · 23.5 KB
/
ViewBuilder.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
using Meep.Tech.Collections.Generic;
using Meep.Tech.Data;
using Simple.Ux.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace Simple.Ux.Data {
/// <summary>
/// Used to build the simple Ux
/// </summary>
public class ViewBuilder {
View _view;
Pannel.Tab? _currentPannelTab;
OrderedDictionary<Pannel.Tab, Pannel> _compiledPannels = new();
List<Column> _currentPannelColumns;
List<IUxViewElement> _currentColumnEntries;
Dictionary<string, DataField> _fieldsByKey = new ();
/// <summary>
/// The current panel tab this builder is working on
/// </summary>
public Pannel.Tab CurrentPannelTab {
get => _currentPannelTab ??= new Pannel.Tab(_view.MainTitle.Text) { View = _view };
private set => _currentPannelTab = value;
}
/// <summary>
/// The current panel tab this builder is working on
/// </summary>
public Title CurrentColumnLabel {
get;
private set;
}
/// <summary>
/// How many entries have been added to the current column
/// </summary>
public int FieldCountForCurrentColumn
=> _currentColumnEntries?.Count ?? 0;
/// <summary>
/// How many columns have been added to the current pannel
/// </summary>
public int ColumnCountForCurrentPannel
=> _currentPannelColumns?.Count ?? 0;
/// <summary>
/// How many finished pannels have been added to the current builder
/// </summary>
public int FinishedPannelCountForCurrentBuilder
=> _compiledPannels?.Count ?? 0;
/// <summary>
/// Make a new simple Overworld Ux builder.
/// </summary>
public ViewBuilder(string mainTitle) {
_view = new(new(mainTitle));
} ViewBuilder() {}
/// <summary>
/// Make a new simple Overworld Ux builder.
/// </summary>
public ViewBuilder(Title mainTitle) {
_view = new(mainTitle);
}
/// <summary>
/// Build a default field using the field
/// </summary>
public static DataField BuildDefaultField(FieldInfo field, Dictionary<System.Type, Attribute> overrideAttributes = null)
=> _buildDefaultField(field.FieldType, field, overrideAttributes);
/// <summary>
/// Build a default field using the property
/// </summary>
public static DataField BuildDefaultField(PropertyInfo prop, Dictionary<System.Type, Attribute> overrideAttributes = null)
=> _buildDefaultField(prop.PropertyType, prop, overrideAttributes);
/// <summary>
/// Copy this builder.
/// </summary>
public ViewBuilder Copy() {
View clone = _view.Copy();
return new() {
_view = clone,
_currentPannelTab = _currentPannelTab?.Copy(clone),
_currentPannelColumns = _currentPannelColumns.Select(entry => entry.Copy(clone)).ToList()
};
}
/// <summary>
/// reset and empty this builder
/// </summary>
public ViewBuilder Clear(Title newMainTitle = null) {
_currentPannelColumns = null;
_currentPannelColumns = new List<Column>();
_currentColumnEntries = new List<IUxViewElement>();
_view = new(newMainTitle ?? _view.MainTitle);
_currentPannelTab = null;
return this;
}
/// <summary>
/// Add a custom made element to the current column
/// </summary>
public ViewBuilder AddCustomElement(IUxViewElement customElement) {
if(customElement is DataField) {
throw new ArgumentException($"Cannot add Field of Type:{customElement.GetType()}, to Builder using AddCustomElement. For adding data fields to a Simple Ux View Builder, use AddField.");
}
if(customElement is Column) {
throw new ArgumentException($"Cannot add a column to Builder using AddCustomElement. For adding columns to a Simple Ux View Builder, use AddColumn.");
}
if(customElement is Pannel) {
throw new ArgumentException($"Cannot add a pannel to Builder using AddCustomElement. For adding a pannel to a Simple Ux View Builder, use AddPannel.");
}
if(customElement is Row) {
throw new ArgumentException($"Cannot add a row to Builder using AddCustomElement. For adding rows fields to a Simple Ux View Builder, use AddRow.");
}
if(customElement is Title) {
throw new ArgumentException($"Cannot add a Title to Builder using AddCustomElement. For adding Titles and Headers to a Simple Ux View Builder, use one of the Add Header or Set Header methods that suits your needs.");
}
if(customElement is Pannel.Tab) {
throw new ArgumentException($"Cannot add Pannel Tab to Builder using AddCustomElement. For changing the Current Pannel Tab Data, see SetCurrentPannelTab. You can also add a pannel tab when adding a new pannel");
}
if(customElement is EmptySpacer) {
throw new ArgumentException($"Cannot add a spacer to Builder using AddCustomElement. For changing the Current Pannel Tab Data, see AddEmptySpacer.");
}
_addElementToCurrentPannel(customElement);
return this;
}
/// <summary>
/// Add a data field.
/// </summary>
public ViewBuilder AddField(DataField field) {
_addElementToCurrentPannel(field);
return this;
}
/// <summary>
/// Add a header inside of a column.
/// </summary>
public ViewBuilder AddHeader(Title inColumnHeader) {
_addElementToCurrentPannel(inColumnHeader);
return this;
}
/// <summary>
/// Add an empty row with min height (about the height of a header)
/// </summary>
public ViewBuilder AddEmptySpacer() {
_addElementToCurrentPannel(new EmptySpacer() { View = _view });
return this;
}
/// <summary>
/// Add a formatted row of controls, inputs, or Ux items.
/// </summary>
public ViewBuilder AddRow(params DataField[] fieldsInRow)
=> AddRow((IEnumerable<DataField>)fieldsInRow);
/// <summary>
/// Add a formatted row of controls, inputs, or Ux items. Give it a label to the left:
/// </summary>
public ViewBuilder AddRow(Title label, params DataField[] fieldsInRow)
=> AddRow(fieldsInRow, label);
/// <summary>
/// Add a formatted row of controls, inputs, or Ux items.
/// You can also give it a label to the left:
/// </summary>
public ViewBuilder AddRow(IEnumerable<DataField> fieldsInRow, Title label = null) {
_addElementToCurrentPannel(
new Row(
fieldsInRow,
label
) {
View = _view
}
);
return this;
}
/// <summary>
/// Add a formatted column of controls, inputs, or Ux items.
/// </summary>
public ViewBuilder AddColumn(params IUxViewElement[] fieldsInColumn)
=> AddColumn((IEnumerable<IUxViewElement>)fieldsInColumn);
/// <summary>
/// Add a formatted column of controls, inputs, or Ux items. Give it a label to the left:
/// </summary>
public ViewBuilder AddColumn(Title label, params IUxViewElement[] fieldsInColumn)
=> AddColumn(fieldsInColumn, label);
/// <summary>
/// Add a formatted column of controls, inputs, or Ux items.
/// You can also give it a label to the left:
/// </summary>
public ViewBuilder AddColumn(IEnumerable<IUxViewElement> fieldsInColumn, Title label = null) {
_addElementToCurrentPannel(
new Column(
fieldsInColumn,
label
) {
View = _view
}
);
return this;
}
/// <summary>
/// Starts a new column. Label is optional.
/// </summary>
public ViewBuilder StartNewColumn(Title label = null) {
if(_currentColumnEntries is not null) {
_buildColumn();
}
CurrentColumnLabel = label;
_currentColumnEntries = new();
return this;
}
/// <summary>
/// Adds a whole pre-built pannel after the current one (or first if this is starting).
/// </summary>
public ViewBuilder AddPannel(Pannel.Tab tabData, Pannel pannel) {
// if theres pending entries in a column, build them
if(_currentColumnEntries is not null) {
_buildColumn();
}
if(_currentPannelColumns is not null) {
_buildPannel();
}
_compiledPannels.Add(tabData, pannel);
StartNewPannel(CurrentPannelTab);
return this;
}
/// <summary>
/// Starts a new pannel with the given name.
/// If this isn't called first, everuthing before is put in a default pannel has the main tilte's name.
/// </summary>
public ViewBuilder StartNewPannel(Pannel.Tab tabData) {
if(_currentPannelColumns is not null) {
_buildPannel();
}
CurrentPannelTab = tabData;
_currentPannelColumns = new();
tabData.View = _view;
return this;
}
/// <summary>
/// Sets the current pannel tab's data if there isn't any
/// </summary>
public ViewBuilder SetCurrentPannelTab(Pannel.Tab tabData) {
if(_currentPannelTab is null) {
_currentPannelTab = tabData;
} else
throw new Exception($"No current tab yet, use Start New Pannel instead");
return this;
}
/// <summary>
/// Sets the current columns header if there isn't one already set
/// </summary>
public ViewBuilder SetCurrentColumnHeader(Title label) {
if(CurrentColumnLabel is null) {
CurrentColumnLabel = label;
} else
throw new Exception($"The current column already has a label: {CurrentColumnLabel?.Text}");
return this;
}
/// <summary>
/// Build and return the view.
/// </summary>
public View Build() {
/// finish building:
if(_currentColumnEntries is not null) {
_buildColumn();
}
if(_currentPannelColumns is not null) {
_buildPannel();
}
/// collect and apply data
_view._tabs = new();
_view._pannels = new();
foreach(KeyValuePair<Pannel.Tab, Pannel> entry in _compiledPannels) {
(Pannel.Tab tab, Pannel pannel) = entry;
_view._tabs.Add(tab.Key.ToLower(), tab);
_view._pannels.Add(tab.Key.ToLower(), pannel);
}
_view._fields = _fieldsByKey;
return _view;
}
/// <summary>
/// Add an element like a pre-built column or row or field to the current pannel.
/// </summary>
internal ViewBuilder _addElementToCurrentPannel(IUxViewElement element) {
// start a new pannel if we don't have one.
if(_currentPannelColumns is null) {
StartNewPannel(CurrentPannelTab);
}
// can't add pannels to pannels
if(element is Pannel) {
throw new System.ArgumentException($"Cannot add a SimpleUx Pannel to another Pannel. You can add pannels to views using the addnextpannel function of the builder");
}
// for columns
if(element is Column column) {
if(_currentPannelColumns.Count >= 3) {
throw new System.Exception($"A Simple UX Pannel cannot have more than 3 Columns.");
}
// if theres pending entries in a column, build them
if(_currentColumnEntries is not null && _currentColumnEntries.Any()) {
_buildColumn();
}
// add the col to the current pannel.
_currentPannelColumns.Add(column);
}
/// start a new column if we don't have one
if(_currentColumnEntries is null) {
StartNewColumn();
}
/// add all the sub entries of the key value set and link them:
if(element is DataFieldKeyValueSet keyValueSet) {
// TODO: allow people to apply certain attributes, like the range display one, to a dictionary, and it will apply to the children.
foreach(KeyValuePair<string, object> entry in keyValueSet.Value) {
DataField field = _buildDefaultField(entry.Value.GetType(), null, fieldNameOverride: "", fieldDataKeyOverride: keyValueSet.Name + "::" + entry.Key);
field._controllerField = keyValueSet;
_currentColumnEntries.Add(field);
}
} else
_currentColumnEntries.Add(element);
return this;
}
Column _buildColumn() {
try {
// compile all the fields in the column:
_fieldsByKey = _fieldsByKey
.Merge(_currentColumnEntries._getExpandedFieldsByKey());
_fieldsByKey.ForEach(field => field.Value.View = _view);
Column col = new(_currentColumnEntries, CurrentColumnLabel) {
View = _view
};
_currentColumnEntries = null;
CurrentColumnLabel = null;
_currentPannelColumns.Add(col);
return col;
} catch(Exception e) {
throw new ArgumentException($"Failed to build SimpleUx Column #{_currentPannelColumns?.Count.ToString() ?? "null!"} on Pannel with key: {_currentPannelTab?.Key ?? "null!"}:\n {e}", e.InnerException);
}
}
Pannel _buildPannel() {
try {
if(_currentColumnEntries?.Any() ?? false) {
_buildColumn();
}
Pannel pannel = new(_currentPannelColumns, CurrentPannelTab) {
View = _view
};
_compiledPannels.Add(pannel.Key, pannel);
_currentPannelColumns = null;
_currentPannelTab = null;
return pannel;
} catch (Exception e) {
throw new ArgumentException($"Failed to build SimpleUx Pannel with key: {_currentPannelTab?.Key ?? "null!"}:\n {e}", e.InnerException);
}
}
static void _findAttribute<TAttribute>(out TAttribute found, MemberInfo fieldInfo = null, Dictionary<System.Type, Attribute> overrideAttributes = null)
where TAttribute : Attribute {
if(overrideAttributes is not null) {
if(overrideAttributes.TryGetValue(typeof(TAttribute), out var foundOverride)) {
found = (TAttribute)foundOverride;
return;
}
}
found = fieldInfo?.GetCustomAttribute<TAttribute>();
}
static DataField _buildDefaultField(System.Type fieldType, MemberInfo fieldInfo = null, Dictionary<System.Type, Attribute> overrideAttributes = null, string fieldNameOverride = null, string fieldDataKeyOverride = null) {
// get relevant attributes:
_findAttribute(out DropdownAttribute selectableData, fieldInfo, overrideAttributes);
_findAttribute(out TooltipAttribute tooltipAttribute, fieldInfo, overrideAttributes);
_findAttribute(out DefaultValueAttribute defaultValue, fieldInfo, overrideAttributes);
_findAttribute(out ValidationAttribute validationAttribute, fieldInfo, overrideAttributes);
_findAttribute(out EnableIfAttribute enabledAttribute, fieldInfo, overrideAttributes);
_findAttribute(out ReadOnlyAttribute readonlyAttribute, fieldInfo, overrideAttributes);
_findAttribute(out RangeSliderAttribute rangeSliderData, fieldInfo, overrideAttributes);
List<Attribute> defaultAttributes = new Attribute[] {
tooltipAttribute,
selectableData,
defaultValue,
validationAttribute,
enabledAttribute,
rangeSliderData,
readonlyAttribute
}.ToList();
bool isReadOnly = false;
if(readonlyAttribute is not null) {
isReadOnly = readonlyAttribute.IsReadOnly;
if(isReadOnly && fieldInfo is PropertyInfo property && !property.CanWrite) {
throw new ArgumentException($"Cannot write to property: {property.Name} on {property.DeclaringType}");
}
} else if(fieldInfo is PropertyInfo property) {
isReadOnly = property.CanWrite && property.SetMethod.IsPublic;
}
string name = fieldNameOverride ?? null;
DataField.DisplayType type = null;
object validation = validationAttribute?._validation ?? null;
List<Func<DataField, object, (bool, string)>> validationFunctions = new();
string tooltipText = tooltipAttribute?._text;
object defaultFieldValue = defaultValue?.Value;
bool isClamped = false;
Func<DataField, View, bool> enabled = null;
// Check if the validation points to a local method of some kind that fits what we need
if(validation is not null && validation is string validationFunctionName) {
bool hasMessage = false;
var method = fieldInfo.DeclaringType.GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).Where(method => method.Name == validationFunctionName)
.Where(method => method.GetParameters().Length == 1)
.Where(method => method.ReturnType == typeof(bool) || (hasMessage = method.ReturnType == typeof((bool, string)))).First();
validationFunctions.Add(hasMessage
? (f, v) => ((bool, string))method.Invoke(null, new[] { f, v })
: (f, v) => ((bool)method.Invoke(null, new[] { f, v }), null as string)
);
}
// check the is enabled functionality
if(enabledAttribute is not null) {
if(fieldInfo?.DeclaringType.GetProperty(enabledAttribute._validationFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) is PropertyInfo property) {
if(property.PropertyType != typeof(bool)) {
enabled = (Field, Pannel) => (bool)property.GetValue(Field);
} else if(property.PropertyType != typeof(Func<DataField, View, bool>)) {
enabled = (Field, Pannel) => ((Func<DataField, View, bool>)property.GetValue(Field)).Invoke(Field, Pannel);
} else
throw new NotSupportedException($"Cannot use the field {property.Name} as an isEnabled determination field for simple ux.");
} else if(fieldInfo?.DeclaringType.GetField(enabledAttribute._validationFieldName, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance) is FieldInfo field) {
if(field.FieldType != typeof(bool)) {
enabled = (Field, Pannel) => (bool)field.GetValue(Field);
} else if(field.FieldType != typeof(Func<DataField, View, bool>)) {
enabled = (Field, Pannel) => ((Func<DataField, View, bool>)field.GetValue(Field)).Invoke(Field, Pannel);
} else
throw new NotSupportedException($"Cannot use the field {field.Name} as an isEnabled determination field for simple ux.");
} else
throw new NotSupportedException($"Cannot use the field {enabledAttribute?._validationFieldName ?? "NULL"} as an isEnabled determination field for simple ux.");
}
/// Seletctable dropdown fields
if(selectableData is not null || fieldType.IsEnum) {
selectableData ??= new DropdownAttribute(selectableData?._selectLimit ?? 1);
type = DataField.DisplayType.Dropdown;
Dictionary<string, object> options = selectableData?._options;
if(fieldType.IsEnum) {
Array enumValues = Enum.GetValues(fieldType);
selectableData._options ??= new(Enum.GetValues(fieldType).Cast<object>().Zip(
enumValues.Cast<char>(), (n, v) => new KeyValuePair<string, object>(n.ToString().ToDisplayCase(), v)
));
defaultFieldValue ??= Activator.CreateInstance(fieldType);
}
} else
/// Potential text input fields
// Numeric:
if((isClamped = fieldType == typeof(int)) || fieldType == typeof(double) || fieldType == typeof(float)) {
Func<double, bool> rangeValidation = null;
bool? isIntClamped = null;
(float min, float max)? range = validation is (float mi, float ma)
? (mi, ma)
: null;
// see if there's range validation to be added.
if(range is not null || (rangeSliderData is not null)) {
type = rangeSliderData is not null
? DataField.DisplayType.RangeSlider
: type;
rangeValidation = value
=> value < (rangeSliderData?._min ?? range?.min) && value > (rangeSliderData?._max ?? range?.max);
isIntClamped = rangeSliderData?._isClampedToInt;
} else {
type = DataField.DisplayType.Text;
}
isIntClamped ??= fieldType == typeof(int);
rangeSliderData ??= new RangeSliderAttribute(0, isIntClamped.Value ? 100 : 1, isIntClamped ?? false);
Func<DataField, object, bool> numericValidation = isIntClamped.Value
? (field, value) => int.TryParse(value as string, out int val) && (rangeValidation?.Invoke(val) ?? true)
: (field, value) => double.TryParse(value as string, out double val) && (rangeValidation?.Invoke(val) ?? true);
validationFunctions.Add((f, v) => numericValidation(f, v)
? (true, null)
: (false, $"{v ?? "NULL"} is not a valid {(isIntClamped.Value ? "integer" : "floating point numerical")} value."));
defaultFieldValue ??= 0;
}//String
else if(fieldType == typeof(string)) {
type = DataField.DisplayType.Text;
defaultFieldValue ??= "";
} //Char
else if(fieldType == typeof(char)) {
validation = (Func<object, bool>)(value => ((value as string)?.Length ?? 0) <= 1);
type = DataField.DisplayType.Text;
defaultFieldValue ??= "";
}
/// Boolean toggle
else if(fieldType == typeof(bool)) {
type = DataField.DisplayType.Toggle;
defaultFieldValue ??= false;
} else if(typeof(IDictionary).IsAssignableFrom(fieldType) || fieldType.IsAssignableToGeneric(typeof(IReadOnlyDictionary<,>))) {
// TODO: dictionarys should be special for executables
Dictionary<string, object> @default = new();
if(defaultValue?.Value is not null && defaultValue.Value is object[] defaultItems) {
for(int entryIndex = 0; entryIndex < defaultItems.Count(); entryIndex++) {
@default.Add(defaultItems[entryIndex++] as string, defaultItems[entryIndex]);
}
}
defaultFieldValue ??= @default;
} else if(fieldType.IsAssignableToGeneric(typeof(IEnumerable<>))) {
// throw new NotImplementedException($"Collections not yet implimented");
} /// modular types:
else if(DataField.DisplayType._byDefaultFieldTypes.TryGetValue(fieldType, out var defaultDisplayType)) {
type = defaultDisplayType;
}
/// No match found
if(type is null) {
return null;
}
return DataField.MakeDefault(
title: name ?? fieldInfo?.Name,
type: type,
validations: validationFunctions,
tooltip: tooltipText,
value: defaultFieldValue,
isReadOnly: readonlyAttribute is not null,
enabledIf: enabled,
attributes: defaultAttributes.Where(attribute => attribute is not null)
.ToDictionary(attribute => attribute.GetType()),
dataKey: fieldDataKeyOverride
);
}
}
internal static class BuilderExtensions {
internal static Dictionary<string, DataField> _getExpandedFieldsByKey(this IEnumerable<IUxViewElement> elements)
=> elements.SelectMany((IUxViewElement entry)
=> entry is Column column
? column.SelectMany(_getExpandedFieldsByKey)
: entry._getExpandedFieldsByKey()
).ToDictionary(entry => entry.DataKey.ToLower());
static IEnumerable<DataField> _getExpandedFieldsByKey(this IUxViewElement entry)
=> entry is Row row
? row
: entry is DataField field && field.ShouldBeTrackedByView
? new[] { field }
: Enumerable.Empty<DataField>();
}
}