Index: client/samples/dartcombat/views.dart |
diff --git a/client/samples/dartcombat/views.dart b/client/samples/dartcombat/views.dart |
index 64f5122d8df5fc5501823823422371511fee2c1b..a565f5d831b630b2b2decd9b488e9f3e2ee5a646 100644 |
--- a/client/samples/dartcombat/views.dart |
+++ b/client/samples/dartcombat/views.dart |
@@ -1,4 +1,4 @@ |
-// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
+// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
// for details. All rights reserved. Use of this source code is governed by a |
// BSD-style license that can be found in the LICENSE file. |
@@ -90,30 +90,26 @@ class PlaceBoatView extends View { |
void handleMouseDown(e) { |
e.preventDefault(); |
- window.requestMeasurementFrame(() { |
- final pos = ViewUtil.positionFromEvent(_rootNode, e); |
+ ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
_boatStartX = pos[0]; |
_boatStartY = pos[1]; |
- return () { |
- // error case when the mouse was released out of the boat-placing area |
- if (_moveListener != null) { |
- _rootNode.on.mouseMove.remove(_moveListener, false); |
- _possibleBoat.remove(); |
- _moveListener = null; |
- } |
- _possibleBoat = ViewUtil.createDiv("icons boat2"); |
- ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY); |
- _rootNode.nodes.add(_possibleBoat); |
- _moveListener = handleMouseMove; |
- _rootNode.on.mouseMove.add(_moveListener); |
- }; |
+ // error case when the mouse was released out of the boat-placing area |
+ if (_moveListener != null) { |
+ _rootNode.on.mouseMove.remove(_moveListener, false); |
+ _possibleBoat.remove(); |
+ _moveListener = null; |
+ } |
+ _possibleBoat = ViewUtil.createDiv("icons boat2"); |
+ ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY); |
+ _rootNode.nodes.add(_possibleBoat); |
+ _moveListener = handleMouseMove; |
+ _rootNode.on.mouseMove.add(_moveListener); |
}); |
} |
void handleMouseMove(e) { |
e.preventDefault(); |
- window.requestMeasurementFrame(() { |
- final pos = ViewUtil.positionFromEvent(_rootNode, e); |
+ ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
if (_boatLastX == pos[0] && _boatLastY == pos[1]) { |
return; |
} |
@@ -133,10 +129,7 @@ class PlaceBoatView extends View { |
boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1)); |
} |
- return () { |
- _possibleBoat.attributes["class"] = |
- "icons boat${boatSize} boatdir-${dir}"; |
- }; |
+ _possibleBoat.attributes["class"] = "icons boat${boatSize} boatdir-${dir}"; |
}); |
} |
@@ -144,28 +137,26 @@ class PlaceBoatView extends View { |
void handleMouseUp(e) { |
_rootNode.on.mouseMove.remove(_moveListener, false); |
_moveListener = null; |
- window.requestMeasurementFrame(() { |
- final pos = ViewUtil.positionFromEvent(_rootNode, e); |
+ ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
int _boatEndX = pos[0]; |
int _boatEndY = pos[1]; |
int deltaX = _boatEndX - _boatStartX; |
int deltaY = _boatEndY - _boatStartY; |
- return () { |
- Boat boat; |
- if (deltaX.abs() >= deltaY.abs()) { |
- int boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1)); |
- boat = new Boat(deltaX < 0 ? (_boatStartX - boatSize + 1) : _boatStartX, |
- _boatStartY, true, boatSize); |
- } else { |
- int boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1)); |
- boat = new Boat(_boatStartX, |
- deltaY < 0 ? (_boatStartY - boatSize + 1) : _boatStartY, |
- false, boatSize); |
- } |
- |
- state.addBoat(boat); |
- }; |
+ Boat boat; |
+ |
+ if (deltaX.abs() >= deltaY.abs()) { |
+ int boatSize = Math.max(2, Math.min(5, deltaX.abs() + 1)); |
+ boat = new Boat(deltaX < 0 ? (_boatStartX - boatSize + 1) : _boatStartX, |
+ _boatStartY, true, boatSize); |
+ } else { |
+ int boatSize = Math.max(2, Math.min(5, deltaY.abs() + 1)); |
+ boat = new Boat(_boatStartX, |
+ deltaY < 0 ? (_boatStartY - boatSize + 1) : _boatStartY, |
+ false, boatSize); |
+ } |
+ |
+ state.addBoat(boat); |
}); |
} |
} |
@@ -208,9 +199,8 @@ class EnemyGridView extends View { |
/** Interpret clicks as a shooting action. */ |
void handleClick(MouseEvent e) { |
- window.requestMeasurementFrame(() { |
- final pos = ViewUtil.positionFromEvent(_rootNode, e); |
- return () { state.shoot(pos[0], pos[1]); }; |
+ ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
+ state.shoot(pos[0], pos[1]); |
}); |
} |
@@ -286,15 +276,15 @@ class ShootingStatusView extends View { |
/** Utility methods used by the views above. */ |
class ViewUtil { |
- /** |
- * Extract the position of a mouse event in a containing 500x500 grid. |
- * Must be run from within a measurement frame. |
- */ |
- static List<int> positionFromEvent(Element gridNode, MouseEvent e) { |
- assert(window.inMeasurementFrame); |
- int x = (e.pageX - gridNode.rect.offset.left) ~/ 50; |
- int y = (e.pageY - gridNode.rect.offset.top) ~/ 50; |
- return [x, y]; |
+ /** Extract the position of a mouse event in a containing 500x500 grid. */ |
+ static Future<List<int>> positionFromEvent(Element gridNode, MouseEvent e) { |
+ final completer = new Completer<List<int>>(); |
+ gridNode.rect.then((ElementRect rect) { |
+ int x = (e.pageX - rect.offset.left) ~/ 50; |
+ int y = (e.pageY - rect.offset.top) ~/ 50; |
+ completer.complete([x, y]); |
+ }); |
+ return completer.future; |
} |
/** Given a grid node (square or boat) place it at a grid coordinate. */ |