-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDistrictDropdown.cs
490 lines (430 loc) · 17.2 KB
/
DistrictDropdown.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
using ColossalFramework.UI;
using System;
using UnityEngine;
namespace ZoneInfo
{
/// <summary>
/// a district dropdown selector
/// </summary>
public class DistrictDropdown : UIPanel
{
// custom event
public event EventHandler<SelectedDistrictChangedEventArgs> eventSelectedDistrictChanged;
// UI elements
private UIPanel _panel;
private UILabel _label;
private UIDropDown _dropdown;
private UILabel _disabledLabel;
// district data
private byte[] _districtIDs;
private int _districtCount;
private byte _selectedDistrictID;
// define special district IDs
// in many parts of the game logic, data for district 0 means data for the entire city
// here, district 0 means no district, which is different than entire city
public const byte DistrictIDNoDistrict = 0;
public const byte DistrictIDEntireCity = DistrictManager.MAX_DISTRICT_COUNT;
// define special district names
private const string DistrictHeading = "District:";
private const string DistrictNameNoDistrict = "No District";
private const string DistrictNameEntireCity = "Entire City";
// controls for Update
private uint _framecounter = 0;
private bool _updateNow = false;
private string _localeID = "";
/// <summary>
/// initialize the district dropdown
/// </summary>
public DistrictDropdown()
{
// create a new dropdown from the template
// the template is a UIPanel that contains a UILabel and a UIDropdown
_panel = AttachUIComponent(UITemplateManager.GetAsGameObject("OptionsDropdownTemplate")) as UIPanel;
if (_panel == null)
{
throw new TypeLoadException("Unable to attach component from template [OptionsDropdownTemplate].");
}
_panel.relativePosition = Vector3.zero;
_panel.size = size;
_panel.eventIsEnabledChanged += _panel_eventIsEnabledChanged;
// get label on the panel
_label = _panel.Find<UILabel>("Label");
if (_label == null)
{
throw new TypeLoadException($"Unable to find component [Label] on panel [{_panel.name}].");
}
_label.text = DistrictHeading;
// get dropdown on the panel
_dropdown = _panel.Find<UIDropDown>("Dropdown");
if (_dropdown == null)
{
throw new TypeLoadException($"Unable to find component [Dropdown] on panel [{_panel.name}]");
}
_dropdown.autoSize = false;
_dropdown.width = width;
_dropdown.eventSelectedIndexChanged += _dropdown_eventSelectedIndexChanged;
// create label to show when disabled
_disabledLabel = AddUIComponent<UILabel>();
if (_dropdown == null)
{
throw new TypeLoadException($"Unable to add disabled label on panel [DistrictDropdown]");
}
_disabledLabel.name = "DisabledLabel";
_disabledLabel.relativePosition = _label.relativePosition;
_disabledLabel.autoSize = false;
_disabledLabel.size = new Vector2(size.x - _disabledLabel.relativePosition.x, size.y - _disabledLabel.relativePosition.y);
_disabledLabel.isVisible = false; // start hidden because dropdown starts enabled
_disabledLabel.isEnabled = false; // default to disabled so disabledTextColor is used
// populate dropdown with initial districts
GetDistricts(out _districtIDs, out string[] names, out _districtCount);
UpdateDropdownList(names, _districtCount);
// by default, first entry (Entire City) is selected
_selectedDistrictID = DistrictIDEntireCity;
// save locale ID
_localeID = _dropdown.localeID;
// successfully initialized
_initialized = true;
}
// expose some properties that access the underlying components
public new bool builtinKeyNavigation
{
get { return _dropdown.builtinKeyNavigation; }
set { _dropdown.builtinKeyNavigation = value; }
}
public Color32 disabledTextColor
{
get { return _label.disabledTextColor; }
set { _label.disabledTextColor = value; _dropdown.disabledTextColor = value; _disabledLabel.disabledTextColor = value; }
}
public UIDropDown dropdown
{
get { return _dropdown; }
}
public float dropdownHeight
{
get { return _dropdown.height; }
set { _dropdown.height = value; }
}
public UIFont font
{
get { return _label.font; }
set { _label.font = value; _dropdown.font = value; _disabledLabel.font = value; }
}
/// <summary>
/// this flag must be checked after instantiation to ensure the dropdown was successfully initialized
/// </summary>
private bool _initialized = false;
public bool initialized
{
get { return _initialized; }
}
public int itemHeight
{
get { return _dropdown.itemHeight; }
set { _dropdown.itemHeight = value; }
}
public string[] items
{
get { return _dropdown.items; }
set { _dropdown.items = value; }
}
public UILabel label
{
get { return _label; }
}
public int listHeight
{
get { return _dropdown.listHeight; }
set { _dropdown.listHeight = value; }
}
public byte selectedDistrictID
{
get { return _selectedDistrictID; }
set
{
// find district ID and select that entry in the dropdown
for (int i = 0; i < _districtCount; i++)
{
if (_districtIDs[i] == value)
{
_dropdown.selectedIndex = i;
return;
}
}
// if got here then district ID was not found
throw new ArgumentOutOfRangeException("selectedDistrictID", $"District ID [{value}] is not defined.");
}
}
public string selectedDistrictName
{
get { return _dropdown.selectedValue; }
set { _dropdown.selectedValue = value; }
}
public Color32 textColor
{
get { return _label.textColor; }
set { _label.textColor = value; _dropdown.textColor = value; _disabledLabel.textColor = value; }
}
public float textScale
{
get { return _label.textScale; }
set { _label.textScale = value; _dropdown.textScale = value; _disabledLabel.textScale = value; }
}
/// <summary>
/// when this component is resized: resize child components
/// </summary>
protected override void OnSizeChanged()
{
base.OnSizeChanged();
if (_initialized)
{
_panel.size = size;
_label.width = size.x;
_dropdown.width = size.x;
_disabledLabel.size = new Vector2(size.x - _disabledLabel.relativePosition.x, size.y - _disabledLabel.relativePosition.y);
}
}
/// <summary>
/// when this component is made visible: update the dropdown immediately
/// </summary>
protected override void OnVisibilityChanged()
{
base.OnVisibilityChanged();
if (isVisible)
{
_updateNow = true;
}
}
/// <summary>
/// handle change in enabled status
/// </summary>
private void _panel_eventIsEnabledChanged(UIComponent component, bool value)
{
// show/hide normal dropdown UI
_label.isVisible = value;
_dropdown.isVisible = value;
// hide/show disabled label
_disabledLabel.isVisible = !value;
UpdateDisabledLabel();
}
/// <summary>
/// convert dropdown index changed event to a possible SelectedDistrictChanged event
/// </summary>
private void _dropdown_eventSelectedIndexChanged(UIComponent component, int value)
{
// check if district ID changed
byte newDistrictID = _districtIDs[value];
if (newDistrictID != _selectedDistrictID)
{
// save selected district ID
_selectedDistrictID = newDistrictID;
// update disabled label in case selected district was changed while disabled
UpdateDisabledLabel();
// raise SelectedDistrictChanged event
OnSelectedDistrictChanged(new SelectedDistrictChangedEventArgs(newDistrictID));
}
}
/// <summary>
/// raise the SelectedDistrictChanged event
/// </summary>
protected virtual void OnSelectedDistrictChanged(SelectedDistrictChangedEventArgs args)
{
eventSelectedDistrictChanged?.Invoke(this, args);
}
/// <summary>
/// update the text on the disabled label
/// </summary>
private void UpdateDisabledLabel()
{
_disabledLabel.text = DistrictHeading + Environment.NewLine + " " + selectedDistrictName;
}
/// <summary>
/// Update is called every frame
/// check for and handle changes in the districts
/// </summary>
public override void Update()
{
// do base processing
base.Update();
// must be initialized
if (!_initialized)
return;
// must be visible (performance enhancement)
if (!isVisible)
return;
// manager must be ready
if (!DistrictManager.exists)
return;
// update only every 11th frame (performance enhancement) unless need to update now
++_framecounter;
if (!(_framecounter % 11 == 0 || _updateNow))
return;
_updateNow = false;
// check for a change in districts
GetDistricts(out byte[] IDs, out string[] names, out int count);
bool districtsChanged = false;
if (count != _districtCount || _dropdown.localeID != _localeID)
{
// a change in count is obviously a change (i.e. a district was added or removed)
// a change in locale ID is handled the same
districtsChanged = true;
_localeID = _dropdown.localeID;
}
else
{
// arrays have same count
// check for a change in district ID at each index (e.g. one district was replaced with another district)
for (int i = 0; i < count; i++)
{
if (IDs[i] != _districtIDs[i])
{
districtsChanged = true;
break;
}
}
}
// check if districts changed
if (districtsChanged)
{
// update dropdown list
UpdateDropdownList(names, count);
// save new district info
_districtIDs = IDs;
_districtCount = count;
// find index of last selected district ID in the new list
int newSelectedIndex = -1;
for (int i = 0; i < count; i++)
{
if (_selectedDistrictID == IDs[i])
{
newSelectedIndex = i;
break;
}
}
// check if last selected district ID is in the new list
if (newSelectedIndex == -1)
{
// last selected district ID is not in the new list (i.e. selected district was removed)
// select the first entry in the list
// this will trigger the index changed event which will raise the SelectedDistrictChanged event (as desired)
_dropdown.selectedIndex = 0;
}
else
{
// last selected district ID is in the new list
// check if index changed
if (_dropdown.selectedIndex != newSelectedIndex)
{
// set the new dropdown index so the same district ID remains selected
// this will trigger the index changed event, but the SelectedDistrictChanged event will not be raised (as desired) because the selected district ID did not change
_dropdown.selectedIndex = newSelectedIndex;
}
}
}
else
{
// no district ID change
// update changed names in dropdown list
// this also automatically updates selectedValue in the dropdown
bool nameChanged = false;
for (int i = 0; i < count; i++)
{
if (_dropdown.items[i] != names[i])
{
_dropdown.items[i] = names[i];
nameChanged = true;
}
}
// if a name changed, refresh the dropdown
if (nameChanged)
{
_dropdown.Invalidate();
}
}
}
/// <summary>
/// get the current district IDs and names
/// </summary>
private void GetDistricts(out byte[] IDs, out string[] names, out int count)
{
// create return arrays
// index into arrays is dropdown selected index
// +1 to make room for Entire City entry
IDs = new byte [DistrictManager.MAX_DISTRICT_COUNT + 1];
names = new string[DistrictManager.MAX_DISTRICT_COUNT + 1];
count = 0;
// always include first entry for Entire City
IDs[count] = DistrictIDEntireCity;
names[count] = DistrictNameEntireCity;
count++;
// loop over each district
// skip district ID 0, which represents No District and is added only if there is another district
DistrictManager instance = DistrictManager.instance;
bool entryNoDistrictAdded = false;
for (byte districtID = 1; districtID < DistrictManager.MAX_DISTRICT_COUNT; districtID++)
{
// district must be created
District district = instance.m_districts.m_buffer[districtID];
if ((district.m_flags & District.Flags.Created) != 0)
{
// if entry for No Distrct was not yet added, add it now
if (!entryNoDistrictAdded)
{
IDs[count] = DistrictIDNoDistrict;
names[count] = DistrictNameNoDistrict;
count++;
entryNoDistrictAdded = true;
}
// add the district
IDs[count] = districtID;
names[count] = instance.GetDistrictName(districtID);
count++;
}
}
// alphabetize by name, except for Entire City entry and No District entry (if present)
for (int i = (entryNoDistrictAdded ? 2 : 1); i < count - 1; i++)
{
for (int j = i + 1; j < count; j++)
{
if (names[j].CompareTo(names[i]) < 0)
{
// swap IDs and names
byte tempID = IDs[i];
IDs[i] = IDs[j];
IDs[j] = tempID;
string tempName = names[i];
names[i] = names[j];
names[j] = tempName;
}
}
}
}
/// <summary>
/// update the district names in the dropdown list
/// </summary>
private void UpdateDropdownList(string[] names, int count)
{
// construct a new array of the exact needed size
string[] newItems = new string[count];
for (int i = 0; i < count; i++)
{
newItems[i] = names[i];
}
// use the new array
_dropdown.items = newItems;
_dropdown.Invalidate();
}
}
/// <summary>
/// arguments for SelectedDistrictChanged event
/// </summary>
public class SelectedDistrictChangedEventArgs : EventArgs
{
public byte districtID { get; }
private SelectedDistrictChangedEventArgs() { }
public SelectedDistrictChangedEventArgs(byte districtID)
{
this.districtID = districtID;
}
}
}