Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: dart/samples/webcomponents/game_of_life/game_of_life.dart

Issue 10918082: adding game_of_life sample (Closed) Base URL: git@github.com:samhopkins/bleeding_edge.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #library('game_of_life');
6
7 #import('dart:html');
8 #import('dart:isolate');
9 #import('dart:math', prefix: 'Math');
10 #import('package:dart-web-components/webcomponents.dart');
11
12 #source('components/components.dart');
13
14 typedef void Ping();
15
16 /** How should the (square) board be? Measured in cells/side. */
17 int GAME_SIZE = 40;
18
19 /** How many pixels long is the side of a cell? (Note: must match the CSS!) */
20 int CELL_SIZE = 20;
21
22 /** How many pixels from the game should the control panel be? */
23 int PANEL_OFFSET = 20;
24
25 /** How many milliseconds between steps? */
26 int STEP_TIME = 100;
Siggi Cherem (dart-lang) 2012/09/05 20:53:03 btw... we'll probably want to be able to set this
samhop 2012/09/07 21:41:30 Done.
27
28 /** Singletons */
29 CellCoordinator get COORDINATOR {
30 if (CellCoordinator._ONLY == null) {
31 CellCoordinator._ONLY = new CellCoordinator._internal();
32 }
33 return CellCoordinator._ONLY;
34 }
35
36 void main() {
37 _componentsSetup();
38
39 // TODO(samhop): fix webcomponents.dart so that attributes are preserved.
40 query('div').id = 'panel';
41
42 COORDINATOR.populate();
43 }
44
45 void _componentsSetup() {
46 Map<String, Function> map = {
47 'x-cell' : () => new Cell.component(),
48 'x-control-panel' : () => new ControlPanel.component()
49 };
50 initializeComponents((String name) => map[name], true);
51 }
52
53 class CellCoordinator {
Siggi Cherem (dart-lang) 2012/09/05 20:53:03 +dartdoc
samhop 2012/09/07 21:41:30 Done.
54 CellEvents on;
55 Timer timer;
56 int lastRefresh;
57 bool _stop;
58 StyleElement computedStyles;
59
60 void stop() {
61 _stop = true;
62 }
63
64 CellCoordinator._internal()
65 : on = new CellCoordinatorEvents(),
66 lastRefresh = 0;
67
68 void increment(int time) {
69 // TODO(samhop): factor this functionality out so that we can benchmark it
70 // running as fast as possible
71 if (new Date.now().millisecondsSinceEpoch - lastRefresh > 100) {
72 on.step.forEach((f) => f());
73 on.resolve.forEach((f) => f());
74 lastRefresh = new Date.now().millisecondsSinceEpoch;
75 }
76 if (!_stop) {
77 window.requestAnimationFrame(increment);
78 }
79 }
80
81 void run() {
82 _stop = false;
83 window.requestAnimationFrame(increment);
84 }
85
86 void step() {
87 _stop = true;
88 increment(null);
89 }
90
91 void populate() {
92 // set up position styles
93 computedStyles = new StyleElement();
94 document.body.nodes.add(computedStyles);
95 var positionStyles = '';
96 _forEachCell((i, j) =>
97 positionStyles = _addPositionId(positionStyles, i, j));
98 computedStyles.innerHTML = positionStyles;
99
100 // add cells
101 _forEachCell((i, j) {
102 var cell = new Cell();
103 cell.coordinator = this;
104 cell.id = 'x${i}y${j}';
105 document.body.nodes.add(cell);
106 });
107
108 // position the control panel
109 var panelStyle =
110 '''
111 #panel {
112 top: ${CELL_SIZE * GAME_SIZE + PANEL_OFFSET}px;
113 left: ${PANEL_OFFSET}px;
114 }
115 ''';
116 computedStyles.innerHTML = '${computedStyles.innerHTML}\n$panelStyle';
Siggi Cherem (dart-lang) 2012/09/05 20:53:03 yikes! would some other operation work here? nodes
samhop 2012/09/07 21:41:30 Done.
117
118 print(computedStyles.innerHTML);
119
120
121 // TODO(samhop) fix webcomponents.dart so we don't have to do this
122 queryAll('.cell').forEach((cell) => cell.bound());
123 }
124
125 static _forEachCell(f) {
126 for (var i = 0; i < GAME_SIZE; i++) {
127 for (var j = 0; j < GAME_SIZE; j++) {
128 f(i, j);
129 }
130 }
131 }
132
133 // Singleton -- there is only one CellCoordinator
134 static CellCoordinator _ONLY;
135
136 static String _addPositionId(curr, i, j) =>
137 '''
138 $curr
139 #x${i}y${j} {
140 left: ${CELL_SIZE * i}px;
141 top: ${CELL_SIZE * j}px;
142 }
143 ''';
144 }
145
146 class CellCoordinatorEvents implements Events {
147 List<Ping> _step_list;
148 List<Ping> _resolve_list;
149
150 CellCoordinatorEvents()
151 : _step_list = <Ping>[],
152 _resolve_list = <Ping>[];
153
154 List<Ping> get step => _step_list;
155 List<Ping> get resolve => _resolve_list;
156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698