/
display_object_children.dart
70 lines (51 loc) · 1.85 KB
/
display_object_children.dart
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
part of '../display.dart';
/// This class is returned by the [DisplayObjectParent.children] getter.
///
/// It implements an [Iterable] of display objects and therefore allows
/// an easy iteration over the children of a [DisplayObjectParent].
class DisplayObjectChildren<T extends DisplayObject> extends IterableBase<T> {
final DisplayObjectParent parent;
final List<T> _children;
DisplayObjectChildren._(this.parent, this._children);
//---------------------------------------------------------------------------
void clear() {
parent.removeChildren();
}
void add(T displayObject) {
parent.addChild(displayObject);
}
void addAll(Iterable<T> displayObjects) {
for (var displayObject in displayObjects) {
parent.addChild(displayObject);
}
}
int indexOf(T displayObject) => _children.indexOf(displayObject);
void insert(int index, T displayObject) {
parent.addChildAt(displayObject, index);
}
void insertAll(int index, Iterable<T> displayObjects) {
for (var displayObject in displayObjects) {
parent.addChildAt(displayObject, index++);
}
}
bool remove(T displayObject) {
final index = _children.indexOf(displayObject);
if (index >= 0) parent.removeChildAt(index);
return index >= 0;
}
T removeAt(int index) {
final displayObject = _children[index];
parent.removeChildAt(index);
return displayObject;
}
T removeLast() => removeAt(_children.length - 1);
void removeRange(int start, int end) => parent.removeChildren(start, end);
T operator [](int index) => _children[index];
void operator []=(int index, T displayObject) {
parent.replaceChildAt(displayObject, index);
}
Iterable<T> get reversed => _children.reversed;
//---------------------------------------------------------------------------
@override
Iterator<T> get iterator => _children.iterator;
}