OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 typedef WebComponent ComponentConstructorThunk(); | |
6 | |
7 class Cell extends DivElementImpl implements WebComponent, Hashable { | |
Siggi Cherem (dart-lang)
2012/09/05 20:53:03
+dartdoc in classes and importnat methods (e.g. st
samhop
2012/09/07 21:41:30
Done.
| |
8 Collection<Cell> neighbors; | |
9 ShadowRoot _root; | |
10 CellCoordinator coordinator; | |
11 bool aliveThisStep; | |
12 bool aliveNextStep; | |
13 | |
14 bool get alive => this.classes.contains('alive'); | |
15 | |
16 void step() { | |
17 var numAlive = neighbors.filter((n) => n.aliveThisStep).length; | |
18 // We could compress this into one line, but it's clearer this way. | |
19 aliveNextStep = false; | |
20 if (aliveThisStep) { | |
21 if (numAlive == 2 || numAlive == 3) { | |
22 aliveNextStep = true; | |
23 } | |
24 } else { | |
25 if (numAlive == 3) { | |
26 aliveNextStep = true; | |
27 } | |
28 } | |
29 } | |
30 | |
31 void resolve() { | |
32 if (aliveNextStep) { | |
33 classes.add('alive'); | |
34 } else { | |
35 classes.remove('alive'); | |
36 } | |
37 aliveThisStep = aliveNextStep; | |
38 } | |
39 | |
40 static ComponentConstructorThunk _$constr; | |
Siggi Cherem (dart-lang)
2012/09/05 20:53:03
it might be worth to indicate (possibly move to th
samhop
2012/09/07 21:41:30
Done.
| |
41 factory Cell.component() { | |
42 if(_$constr == null) { | |
43 _$constr = () => new Cell._internal(); | |
44 } | |
45 var t1 = new DivElement(); | |
46 t1.attributes['is'] = 'x-cell'; | |
47 rewirePrototypeChain(t1, _$constr, 'Cell'); | |
48 return t1; | |
49 } | |
50 | |
51 factory Cell() { | |
52 return manager.expandHtml('<div is="x-cell"></div>'); | |
53 } | |
54 | |
55 Cell._internal(); | |
56 | |
57 void created(ShadowRoot root) { | |
58 _root = root; | |
59 neighbors = <Cell>[]; | |
60 this.classes.add('cell'); | |
61 } | |
62 | |
63 void inserted() { } | |
64 | |
65 void bound() { | |
66 on.click.add((event) { | |
67 classes.toggle('alive'); | |
68 aliveThisStep = !aliveThisStep; | |
69 }); | |
70 | |
71 coordinator.on.step.add(step); | |
72 coordinator.on.resolve.add(resolve); | |
73 | |
74 // find neighbors | |
75 var parsedCoordinates = this.id.substring(1).split('y'); | |
76 var x = Math.parseInt(parsedCoordinates[0]); | |
77 var y = Math.parseInt(parsedCoordinates[1]); | |
78 for (int dx = -1; dx <= 1; dx++) { | |
79 for (int dy = -1; dy <= 1; dy++) { | |
80 if (inGrid(x + dx, y + dy) && !(dx == 0 && dy == 0)) { | |
81 var neighbor = query('#x${x + dx}y${y + dy}'); | |
82 neighbors.add(neighbor); | |
83 } | |
84 } | |
85 } | |
86 } | |
87 | |
88 static bool inGrid(x, y) => | |
89 (x >=0 && y >=0 && x < GAME_SIZE && y < GAME_SIZE); | |
90 | |
91 void attributeChanged(String name, String oldValue, String newValue) { } | |
92 | |
93 void removed() { } | |
94 | |
95 } | |
96 | |
97 class ControlPanel extends DivElementImpl implements WebComponent { | |
98 ShadowRoot _root; | |
99 CellCoordinator coordinator; | |
100 | |
101 static ComponentConstructorThunk _$constr; | |
102 factory ControlPanel.component() { | |
103 if(_$constr == null) { | |
104 _$constr = () => new ControlPanel._internal(); | |
105 } | |
106 var t1 = new DivElement(); | |
107 t1.attributes['is'] = 'x-control-panel'; | |
108 rewirePrototypeChain(t1, _$constr, 'ControlPanel'); | |
109 return t1; | |
110 } | |
111 | |
112 factory ControlPanel() { | |
113 return manager.expandHtml('<div is="x-control-panel"></div>'); | |
114 } | |
115 | |
116 ControlPanel._internal(); | |
117 | |
118 void created(ShadowRoot root) { | |
119 _root = root; | |
120 } | |
121 | |
122 void inserted() { | |
123 _root.query('#start').on.click.add((e) => COORDINATOR.run()); | |
124 _root.query('#stop').on.click.add((e) => COORDINATOR.stop()); | |
125 _root.query('#step').on.click.add((e) => COORDINATOR.step()); | |
126 } | |
127 | |
128 void attributeChanged(String name, String oldValue, String newValue) { } | |
129 | |
130 void removed() { } | |
131 } | |
OLD | NEW |