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

Side by Side Diff: client/samples/dartcombat/views.dart

Issue 8363040: Implement measurement using futures (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Respond to all comments Created 9 years, 1 month 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** Base class for all views. */ 5 /** Base class for all views. */
6 class View { 6 class View {
7 Document doc; 7 Document doc;
8 View(this.doc) {} 8 View(this.doc) {}
9 } 9 }
10 10
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 : super(rootNode.document), _rootNode = rootNode {} 84 : super(rootNode.document), _rootNode = rootNode {}
85 85
86 void attach() { 86 void attach() {
87 _rootNode.on.mouseDown.add( 87 _rootNode.on.mouseDown.add(
88 Isolate.bind((e) { handleMouseDown(e); })); 88 Isolate.bind((e) { handleMouseDown(e); }));
89 _rootNode.on.mouseUp.add( 89 _rootNode.on.mouseUp.add(
90 Isolate.bind((e) { handleMouseUp(e); })); 90 Isolate.bind((e) { handleMouseUp(e); }));
91 } 91 }
92 92
93 void handleMouseDown(e) { 93 void handleMouseDown(e) {
94 final pos = ViewUtil.positionFromEvent(_rootNode, e);
95 _boatStartX = pos[0];
96 _boatStartY = pos[1];
97 // error case when the mouse was released out of the boat-placing area
98 if (_moveListener != null) {
99 _rootNode.on.mouseMove.remove(_moveListener, false);
100 _possibleBoat.remove();
101 _moveListener = null;
102 }
103 _possibleBoat = ViewUtil.createDiv(this, "icons boat2");
104 ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY);
105 _rootNode.nodes.add(_possibleBoat);
106 _moveListener = Isolate.bind((e) { handleMouseMove(e); });
107 _rootNode.on.mouseMove.add(_moveListener);
108 e.preventDefault(); 94 e.preventDefault();
95 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) {
96 _boatStartX = pos[0];
97 _boatStartY = pos[1];
98 // error case when the mouse was released out of the boat-placing area
99 if (_moveListener != null) {
100 _rootNode.on.mouseMove.remove(_moveListener, false);
101 _possibleBoat.remove();
102 _moveListener = null;
103 }
104 _possibleBoat = ViewUtil.createDiv(this, "icons boat2");
105 ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY);
106 _rootNode.nodes.add(_possibleBoat);
107 _moveListener = Isolate.bind((e) { handleMouseMove(e); });
108 _rootNode.on.mouseMove.add(_moveListener);
109 });
109 } 110 }
110 111
111 void handleMouseMove(e) { 112 void handleMouseMove(e) {
112 final pos = ViewUtil.positionFromEvent(_rootNode, e); 113 e.preventDefault();
113 if (_boatLastX == pos[0] && _boatLastY == pos[1]) { 114 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) {
114 return; 115 if (_boatLastX == pos[0] && _boatLastY == pos[1]) {
115 } 116 return;
116 _boatLastX = pos[0]; 117 }
117 _boatLastY = pos[1]; 118 _boatLastX = pos[0];
118 int deltaX = _boatLastX - _boatStartX; 119 _boatLastY = pos[1];
119 int deltaY = _boatLastY - _boatStartY; 120 int deltaX = _boatLastX - _boatStartX;
121 int deltaY = _boatLastY - _boatStartY;
120 122
121 String dir; 123 String dir;
122 bool flip = false; 124 bool flip = false;
123 int boatSize = 2; 125 int boatSize = 2;
124 if (deltaX.abs() >= deltaY.abs()) { 126 if (deltaX.abs() >= deltaY.abs()) {
125 dir = deltaX < 0 ? "right" : "left"; 127 dir = deltaX < 0 ? "right" : "left";
126 boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1)); 128 boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1));
127 } else { 129 } else {
128 dir = deltaY < 0 ? "up" : "down"; 130 dir = deltaY < 0 ? "up" : "down";
129 boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1)); 131 boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1));
130 } 132 }
131 133
132 _possibleBoat.attributes["class"] = "icons boat${boatSize} boatdir-${dir}"; 134 _possibleBoat.attributes["class"] = "icons boat${boatSize} boatdir-${dir}" ;
133 e.preventDefault(); 135 });
134 } 136 }
135 137
136 /** Handle end of positioning of a boat. */ 138 /** Handle end of positioning of a boat. */
137 void handleMouseUp(e) { 139 void handleMouseUp(e) {
138 _rootNode.on.mouseMove.remove(_moveListener, false); 140 _rootNode.on.mouseMove.remove(_moveListener, false);
139 _moveListener = null; 141 _moveListener = null;
140 final pos = ViewUtil.positionFromEvent(_rootNode, e); 142 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) {
141 int _boatEndX = pos[0]; 143 int _boatEndX = pos[0];
142 int _boatEndY = pos[1]; 144 int _boatEndY = pos[1];
143 145
144 int deltaX = _boatEndX - _boatStartX; 146 int deltaX = _boatEndX - _boatStartX;
145 int deltaY = _boatEndY - _boatStartY; 147 int deltaY = _boatEndY - _boatStartY;
146 Boat boat; 148 Boat boat;
147 149
148 if (deltaX.abs() >= deltaY.abs()) { 150 if (deltaX.abs() >= deltaY.abs()) {
149 int boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1)); 151 int boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1));
150 boat = new Boat(deltaX < 0 ? (_boatStartX - boatSize + 1) : _boatStartX, 152 boat = new Boat(deltaX < 0 ? (_boatStartX - boatSize + 1) : _boatStartX,
151 _boatStartY, true, boatSize); 153 _boatStartY, true, boatSize);
152 } else { 154 } else {
153 int boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1)); 155 int boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1));
154 boat = new Boat(_boatStartX, 156 boat = new Boat(_boatStartX,
155 deltaY < 0 ? (_boatStartY - boatSize + 1) : _boatStartY, 157 deltaY < 0 ? (_boatStartY - boatSize + 1) : _boatStartY,
156 false, boatSize); 158 false, boatSize);
157 } 159 }
158 160
159 state.addBoat(boat); 161 state.addBoat(boat);
162 });
160 } 163 }
161 } 164 }
162 165
163 /** The view displayed to the player for its enemy's grid. */ 166 /** The view displayed to the player for its enemy's grid. */
164 class EnemyGridView extends View { 167 class EnemyGridView extends View {
165 PlayerState state; 168 PlayerState state;
166 bool _enemyReady; 169 bool _enemyReady;
167 Element _rootNode; 170 Element _rootNode;
168 ShootingStatusView statusBar; 171 ShootingStatusView statusBar;
169 172
(...skipping 22 matching lines...) Expand all
192 _rootNode.nodes.add(statusBar._rootNode); 195 _rootNode.nodes.add(statusBar._rootNode);
193 _rootNode.on.click.add( 196 _rootNode.on.click.add(
194 Isolate.bind((Event e) { 197 Isolate.bind((Event e) {
195 MouseEvent mouseEvent = e; 198 MouseEvent mouseEvent = e;
196 handleClick(mouseEvent); 199 handleClick(mouseEvent);
197 }), false); 200 }), false);
198 } 201 }
199 202
200 /** Interpret clicks as a shooting action. */ 203 /** Interpret clicks as a shooting action. */
201 void handleClick(MouseEvent e) { 204 void handleClick(MouseEvent e) {
202 final pos = ViewUtil.positionFromEvent(_rootNode, e); 205 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) {
203 state.shoot(pos[0], pos[1]); 206 state.shoot(pos[0], pos[1]);
207 });
204 } 208 }
205 209
206 /** Update the view to indicate the enemy is ready. */ 210 /** Update the view to indicate the enemy is ready. */
207 void setEnemyReady() { 211 void setEnemyReady() {
208 if (!_enemyReady) { 212 if (!_enemyReady) {
209 _enemyReady = true; 213 _enemyReady = true;
210 _rootNode.query(".notready").remove(); 214 _rootNode.query(".notready").remove();
211 } 215 }
212 } 216 }
213 217
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 final sunk = state.boatsSunk; 273 final sunk = state.boatsSunk;
270 _rootNode.innerHTML = 274 _rootNode.innerHTML =
271 "${total} &lt;= ${accounted} (${hit} + ${miss}); ${sunk}"; 275 "${total} &lt;= ${accounted} (${hit} + ${miss}); ${sunk}";
272 } 276 }
273 } 277 }
274 278
275 /** Utility methods used by the views above. */ 279 /** Utility methods used by the views above. */
276 class ViewUtil { 280 class ViewUtil {
277 281
278 /** Extract the position of a mouse event in a containing 500x500 grid. */ 282 /** Extract the position of a mouse event in a containing 500x500 grid. */
279 static List<int> positionFromEvent(Element gridNode, MouseEvent e) { 283 static Future<List<int>> positionFromEvent(Element gridNode, MouseEvent e) {
280 int x = (e.pageX - gridNode.offsetLeft) ~/ 50; 284 final completer = new Completer<List<int>>();
281 int y = (e.pageY - gridNode.offsetTop) ~/ 50; 285 gridNode.rect.then((ElementRect rect) {
282 return const [x, y]; 286 int x = (e.pageX - rect.offset.left) ~/ 50;
287 int y = (e.pageY - rect.offset.top) ~/ 50;
288 completer.complete(const [x, y]);
289 });
290 return completer.future;
283 } 291 }
284 292
285 /** Given a grid node (square or boat) place it at a grid coordinate. */ 293 /** Given a grid node (square or boat) place it at a grid coordinate. */
286 static void placeNodeAt(Element node, int x, int y) { 294 static void placeNodeAt(Element node, int x, int y) {
287 int xoffset = x * 50; 295 int xoffset = x * 50;
288 int yoffset = y * 50; 296 int yoffset = y * 50;
289 node.style.setProperty("top", yoffset.toString() + "px"); 297 node.style.setProperty("top", yoffset.toString() + "px");
290 node.style.setProperty("left", xoffset.toString() + "px"); 298 node.style.setProperty("left", xoffset.toString() + "px");
291 } 299 }
292 300
293 /** Create a div node with a given class name. */ 301 /** Create a div node with a given class name. */
294 static Element createDiv(View view, String className) { 302 static Element createDiv(View view, String className) {
295 Element node = view.doc.createElement("div"); 303 Element node = view.doc.createElement("div");
296 node.attributes["class"] = className; 304 node.attributes["class"] = className;
297 return node; 305 return node;
298 } 306 }
299 } 307 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698