-
Notifications
You must be signed in to change notification settings - Fork 436
/
Copy pathContainer.cs
542 lines (468 loc) · 20.4 KB
/
Container.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
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using JetBrains.Annotations;
using osu.Framework.Extensions.TypeExtensions;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Effects;
using osu.Framework.Lists;
using osuTK;
namespace osu.Framework.Graphics.Containers
{
/// <summary>
/// A drawable which can have children added to it. Transformations applied to
/// a container are also applied to its children.
/// Additionally, containers support various effects, such as masking, edge effect,
/// padding, and automatic sizing depending on their children.
/// If all children are of a specific non-<see cref="Drawable"/> type, use the
/// generic version <see cref="Container{T}"/>.
/// </summary>
public partial class Container : Container<Drawable>
{
}
/// <summary>
/// A drawable which can have children added to it. Transformations applied to
/// a container are also applied to its children.
/// Additionally, containers support various effects, such as masking, edge effect,
/// padding, and automatic sizing depending on their children.
/// </summary>
public partial class Container<T> : CompositeDrawable, IContainerEnumerable<T>, IContainerCollection<T>, ICollection<T>, IReadOnlyList<T>
where T : Drawable
{
/// <summary>
/// This is checked when enumerating through this <see cref="Container"/> to throw when
/// <see cref="Children"/> was mutated while enumerating (in <see cref="Enumerator"/>).
/// This is incremented whenever <see cref="Children"/> is mutated (e.g. with <see cref="Add(T)"/>).
/// </summary>
private int enumeratorVersion;
/// <summary>
/// Constructs a <see cref="Container"/> that stores children.
/// </summary>
public Container()
{
if (typeof(T) == typeof(Drawable))
internalChildrenAsT = (IReadOnlyList<T>)InternalChildren;
else
internalChildrenAsT = new LazyList<Drawable, T>(InternalChildren, c => (T)c);
if (typeof(T) == typeof(Drawable))
aliveInternalChildrenAsT = (IReadOnlyList<T>)AliveInternalChildren;
else
aliveInternalChildrenAsT = new LazyList<Drawable, T>(AliveInternalChildren, c => (T)c);
}
/// <summary>
/// The content of this container. <see cref="Children"/> and all methods that mutate
/// <see cref="Children"/> (e.g. <see cref="Add(T)"/> and <see cref="Remove(T, bool)"/>) are
/// forwarded to the content. By default a container's content is itself, in which case
/// <see cref="Children"/> refers to <see cref="CompositeDrawable.InternalChildren"/>.
/// This property is useful for containers that require internal children that should
/// not be exposed to the outside world, e.g. <see cref="ScrollContainer{T}"/>.
/// </summary>
protected virtual Container<T> Content => this;
/// <summary>
/// The publicly accessible list of children. Forwards to the children of <see cref="Content"/>.
/// If <see cref="Content"/> is this container, then returns <see cref="CompositeDrawable.InternalChildren"/>.
/// Assigning to this property will dispose all existing children of this Container.
/// <remarks>
/// If a foreach loop is used, iterate over the <see cref="Container"/> directly rather than its <see cref="Children"/>.
/// </remarks>
/// </summary>
public IReadOnlyList<T> Children
{
get
{
if (Content != this)
return Content.Children;
return internalChildrenAsT;
}
set => ChildrenEnumerable = value;
}
/// <summary>
/// The publicly accessible list of alive children. Forwards to the alive children of <see cref="Content"/>.
/// If <see cref="Content"/> is this container, then returns <see cref="CompositeDrawable.AliveInternalChildren"/>.
/// </summary>
public IReadOnlyList<T> AliveChildren
{
get
{
if (Content != this)
return Content.AliveChildren;
return aliveInternalChildrenAsT;
}
}
/// <summary>
/// Accesses the <paramref name="index"/>-th child.
/// </summary>
/// <param name="index">The index of the child to access.</param>
/// <returns>The <paramref name="index"/>-th child.</returns>
public T this[int index] => Children[index];
/// <summary>
/// The amount of elements in <see cref="Children"/>.
/// </summary>
public int Count => Children.Count;
/// <summary>
/// Whether this <see cref="Container{T}"/> can have elements added and removed. Always false.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Copies the elements of the <see cref="Container{T}"/> to an Array, starting at a particular Array index.
/// </summary>
/// <param name="array">The Array into which all children should be copied.</param>
/// <param name="arrayIndex">The starting index in the Array.</param>
public void CopyTo(T[] array, int arrayIndex)
{
foreach (var c in Children)
array[arrayIndex++] = c;
}
bool ICollection<T>.Remove(T item)
{
ArgumentNullException.ThrowIfNull(item);
return Remove(item, true);
}
public Enumerator GetEnumerator() => new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Sets all children of this container to the elements contained in the enumerable.
/// </summary>
public IEnumerable<T> ChildrenEnumerable
{
set
{
if (IsDisposed)
return;
Clear();
AddRange(value);
}
}
/// <summary>
/// Gets or sets the only child of this container.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public T Child
{
get
{
if (Children.Count != 1)
throw new InvalidOperationException($"Cannot call {nameof(InternalChild)} unless there's exactly one {nameof(Drawable)} in {nameof(Children)} (currently {Children.Count})!");
return Children[0];
}
set
{
if (IsDisposed)
return;
Clear();
Add(value);
}
}
private readonly IReadOnlyList<T> internalChildrenAsT;
private readonly IReadOnlyList<T> aliveInternalChildrenAsT;
/// <summary>
/// The index of a given child within <see cref="Children"/>.
/// </summary>
/// <returns>
/// If the child is found, its index. Otherwise, the negated index it would obtain
/// if it were added to <see cref="Children"/>.
/// </returns>
public int IndexOf(T drawable)
{
if (Content != this)
return Content.IndexOf(drawable);
return IndexOfInternal(drawable);
}
/// <summary>
/// Checks whether a given child is contained within <see cref="Children"/>.
/// </summary>
public bool Contains(T drawable)
{
int index = IndexOf(drawable);
return index >= 0 && this[index] == drawable;
}
/// <summary>
/// Adds a child to this container. This amounts to adding a child to <see cref="Content"/>'s
/// <see cref="Children"/>, recursing until <see cref="Content"/> == this.
/// </summary>
public virtual void Add(T drawable)
{
if (drawable == Content)
throw new InvalidOperationException("Content may not be added to itself.");
ArgumentNullException.ThrowIfNull(drawable);
ObjectDisposedException.ThrowIf(drawable.IsDisposed, drawable);
if (Content == this)
AddInternal(drawable);
else
Content.Add(drawable);
}
/// <summary>
/// Adds a range of children. This is equivalent to calling <see cref="Add(T)"/> on
/// each element of the range in order.
/// </summary>
public void AddRange(IEnumerable<T> range)
{
if (range is IContainerEnumerable<Drawable>)
{
throw new InvalidOperationException($"Attempting to add a {nameof(IContainer)} as a range of children to {this}."
+ $"If intentional, consider using the {nameof(IContainerEnumerable<Drawable>.Children)} property instead.");
}
foreach (T d in range)
Add(d);
}
protected override void AddInternal(Drawable drawable)
{
if (Content == this && drawable != null && !(drawable is T))
{
throw new InvalidOperationException(
$"Only {typeof(T).ReadableName()} type drawables may be added to a container of type {GetType().ReadableName()} which does not redirect {nameof(Content)}.");
}
enumeratorVersion++;
base.AddInternal(drawable);
}
/// <summary>
/// Removes a given child from this container.
/// </summary>
public virtual bool Remove(T drawable, bool disposeImmediately)
{
if (Content != this)
return Content.Remove(drawable, disposeImmediately);
return RemoveInternal(drawable, disposeImmediately);
}
/// <summary>
/// Removes all children which match the given predicate.
/// This is equivalent to calling <see cref="Remove(T, bool)"/> for each child that
/// matches the given predicate.
/// </summary>
/// <returns>The amount of removed children.</returns>
public int RemoveAll(Predicate<T> pred, bool disposeImmediately)
{
if (Content != this)
return Content.RemoveAll(pred, disposeImmediately);
int removedCount = 0;
for (int i = 0; i < InternalChildren.Count; i++)
{
var tChild = (T)InternalChildren[i];
if (pred.Invoke(tChild))
{
RemoveInternal(tChild, disposeImmediately);
removedCount++;
i--;
}
}
return removedCount;
}
/// <summary>
/// Removes a range of children. This is equivalent to calling <see cref="Remove(T, bool)"/> on
/// each element of the range in order.
/// </summary>
public void RemoveRange([CanBeNull] IEnumerable<T> range, bool disposeImmediately)
{
if (range == null)
return;
foreach (T p in range)
Remove(p, disposeImmediately);
}
protected internal override bool RemoveInternal(Drawable drawable, bool disposeImmediately)
{
enumeratorVersion++;
return base.RemoveInternal(drawable, disposeImmediately);
}
/// <summary>
/// Removes all children.
/// </summary>
public void Clear() => Clear(true);
/// <summary>
/// Removes all children.
/// </summary>
/// <param name="disposeChildren">
/// Whether removed children should also get disposed.
/// Disposal will be recursive.
/// </param>
public virtual void Clear(bool disposeChildren)
{
if (Content != null && Content != this)
Content.Clear(disposeChildren);
else
ClearInternal(disposeChildren);
}
protected internal override void ClearInternal(bool disposeChildren = true)
{
enumeratorVersion++;
base.ClearInternal(disposeChildren);
}
/// <summary>
/// Changes the depth of a child. This affects ordering of children within this container.
/// </summary>
/// <param name="child">The child whose depth is to be changed.</param>
/// <param name="newDepth">The new depth value to be set.</param>
public void ChangeChildDepth(T child, float newDepth)
{
if (Content != this)
Content.ChangeChildDepth(child, newDepth);
else
ChangeInternalChildDepth(child, newDepth);
}
/// <summary>
/// If enabled, only the portion of children that falls within this <see cref="Container"/>'s
/// shape is drawn to the screen.
/// </summary>
public new bool Masking
{
get => base.Masking;
set => base.Masking = value;
}
/// <summary>
/// Determines over how many pixels the alpha component smoothly fades out when an inner <see cref="EdgeEffect"/> or <see cref="BorderThickness"/> is present.
/// Only has an effect when <see cref="Masking"/> is true.
/// </summary>
public new float MaskingSmoothness
{
get => base.MaskingSmoothness;
set => base.MaskingSmoothness = value;
}
/// <summary>
/// Determines how large of a radius is masked away around the corners.
/// Only has an effect when <see cref="Masking"/> is true.
/// </summary>
public new float CornerRadius
{
get => base.CornerRadius;
set => base.CornerRadius = value;
}
/// <summary>
/// Determines how gentle the curve of the corner straightens. A value of 2 results in
/// circular arcs, a value of 2.5 (default) results in something closer to apple's "continuous corner".
/// Values between 2 and 10 result in varying degrees of "continuousness", where larger values are smoother.
/// Values between 1 and 2 result in a "flatter" appearance than round corners.
/// Values between 0 and 1 result in a concave, round corner as opposed to a convex round corner,
/// where a value of 0.5 is a circular concave arc.
/// Only has an effect when <see cref="Masking"/> is true and <see cref="CornerRadius"/> is non-zero.
/// </summary>
public new float CornerExponent
{
get => base.CornerExponent;
set => base.CornerExponent = value;
}
/// <summary>
/// Determines how thick of a border to draw around the inside of the masked region.
/// Only has an effect when <see cref="Masking"/> is true.
/// The border only is drawn on top of children using a sprite shader.
/// </summary>
/// <remarks>
/// Drawing borders is optimized heavily into our sprite shaders. As a consequence
/// borders are only drawn correctly on top of quad-shaped children using our sprite
/// shaders.
/// </remarks>
public new float BorderThickness
{
get => base.BorderThickness;
set => base.BorderThickness = value;
}
/// <summary>
/// Determines the color of the border controlled by <see cref="BorderThickness"/>.
/// Only has an effect when <see cref="Masking"/> is true.
/// </summary>
public new ColourInfo BorderColour
{
get => base.BorderColour;
set => base.BorderColour = value;
}
/// <summary>
/// Determines an edge effect of this <see cref="Container"/>.
/// Edge effects are e.g. glow or a shadow.
/// Only has an effect when <see cref="Masking"/> is true.
/// </summary>
public new EdgeEffectParameters EdgeEffect
{
get => base.EdgeEffect;
set => base.EdgeEffect = value;
}
/// <summary>
/// Shrinks the space children may occupy within this <see cref="Container"/>
/// by the specified amount on each side.
/// </summary>
public new MarginPadding Padding
{
get => base.Padding;
set => base.Padding = value;
}
/// <summary>
/// Whether to use a local vertex batch for rendering. If false, a parenting vertex batch will be used.
/// </summary>
public new bool ForceLocalVertexBatch
{
get => base.ForceLocalVertexBatch;
set => base.ForceLocalVertexBatch = value;
}
/// <summary>
/// The size of the relative position/size coordinate space of children of this <see cref="Container"/>.
/// Children positioned at this size will appear as if they were positioned at <see cref="Drawable.Position"/> = <see cref="Vector2.One"/> in this <see cref="Container"/>.
/// </summary>
public new Vector2 RelativeChildSize
{
get => base.RelativeChildSize;
set => base.RelativeChildSize = value;
}
/// <summary>
/// The offset of the relative position/size coordinate space of children of this <see cref="Container"/>.
/// Children positioned at this offset will appear as if they were positioned at <see cref="Drawable.Position"/> = <see cref="Vector2.Zero"/> in this <see cref="Container"/>.
/// </summary>
public new Vector2 RelativeChildOffset
{
get => base.RelativeChildOffset;
set => base.RelativeChildOffset = value;
}
/// <summary>
/// Controls which <see cref="Axes"/> are automatically sized w.r.t. <see cref="CompositeDrawable.InternalChildren"/>.
/// Children's <see cref="Drawable.BypassAutoSizeAxes"/> are ignored for automatic sizing.
/// Most notably, <see cref="Drawable.RelativePositionAxes"/> and <see cref="Drawable.RelativeSizeAxes"/> of children
/// do not affect automatic sizing to avoid circular size dependencies.
/// It is not allowed to manually set <see cref="Drawable.Size"/> (or <see cref="Drawable.Width"/> / <see cref="Drawable.Height"/>)
/// on any <see cref="Axes"/> which are automatically sized.
/// </summary>
public new Axes AutoSizeAxes
{
get => base.AutoSizeAxes;
set => base.AutoSizeAxes = value;
}
/// <summary>
/// The duration which automatic sizing should approximately take. If zero, then it is instantaneous.
/// AutoSize is being applied continuously so the actual amount of time taken depends on the overall change in value.
/// </summary>
public new float AutoSizeDuration
{
get => base.AutoSizeDuration;
set => base.AutoSizeDuration = value;
}
/// <summary>
/// Immediately resizes to the current target size if <see cref="AutoSizeDuration"/> is non-zero.
/// </summary>
public new void FinishAutoSizeTransforms() => base.FinishAutoSizeTransforms();
public struct Enumerator : IEnumerator<T>
{
private Container<T> container;
private int currentIndex;
private readonly int version;
internal Enumerator(Container<T> container)
{
this.container = container;
currentIndex = -1; // The first MoveNext() should bring the iterator to 0
version = container.enumeratorVersion;
}
public bool MoveNext()
{
if (version != container.enumeratorVersion)
throw new InvalidOperationException($"May not add or remove {nameof(Children)} from this {nameof(Container)} during enumeration.");
return ++currentIndex < container.Count;
}
public void Reset() => currentIndex = -1;
public readonly T Current => container[currentIndex];
readonly object IEnumerator.Current => Current;
public void Dispose()
{
container = null;
}
}
}
}