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

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

Powered by Google App Engine
This is Rietveld 408576698