| OLD | NEW |
| 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 27 matching lines...) Expand all Loading... |
| 38 String rows = _rows.toString(); | 38 String rows = _rows.toString(); |
| 39 String table = "<div class='vbox'>${rows}</div>"; | 39 String table = "<div class='vbox'>${rows}</div>"; |
| 40 _rootNode.innerHTML = table; | 40 _rootNode.innerHTML = table; |
| 41 | 41 |
| 42 // Attaches listeners onto this view. | 42 // Attaches listeners onto this view. |
| 43 new PlaceBoatView(state, _rootNode).attach(); | 43 new PlaceBoatView(state, _rootNode).attach(); |
| 44 } | 44 } |
| 45 | 45 |
| 46 /** Adds to this view the respresentation of a missed shot. */ | 46 /** Adds to this view the respresentation of a missed shot. */ |
| 47 void addMiss(int x, int y) { | 47 void addMiss(int x, int y) { |
| 48 Element node = ViewUtil.createDiv(this, "icons miss"); | 48 Element node = ViewUtil.createDiv("icons miss"); |
| 49 ViewUtil.placeNodeAt(node, x, y); | 49 ViewUtil.placeNodeAt(node, x, y); |
| 50 _rootNode.nodes.add(node); | 50 _rootNode.nodes.add(node); |
| 51 } | 51 } |
| 52 | 52 |
| 53 /** Adds to this view the respresentation of a shot that hits our boat. */ | 53 /** Adds to this view the respresentation of a shot that hits our boat. */ |
| 54 void addHit(int x, int y) { | 54 void addHit(int x, int y) { |
| 55 Element node = ViewUtil.createDiv(this, "icons hit-onboat"); | 55 Element node = ViewUtil.createDiv("icons hit-onboat"); |
| 56 ViewUtil.placeNodeAt(node, x, y); | 56 ViewUtil.placeNodeAt(node, x, y); |
| 57 _rootNode.nodes.add(node); | 57 _rootNode.nodes.add(node); |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 /** View used to interactively set a new boat on the board. */ | 61 /** View used to interactively set a new boat on the board. */ |
| 62 class PlaceBoatView extends View { | 62 class PlaceBoatView extends View { |
| 63 PlayerState state; | 63 PlayerState state; |
| 64 | 64 |
| 65 /** root of the grid. */ | 65 /** root of the grid. */ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 92 e.preventDefault(); | 92 e.preventDefault(); |
| 93 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { | 93 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
| 94 _boatStartX = pos[0]; | 94 _boatStartX = pos[0]; |
| 95 _boatStartY = pos[1]; | 95 _boatStartY = pos[1]; |
| 96 // error case when the mouse was released out of the boat-placing area | 96 // error case when the mouse was released out of the boat-placing area |
| 97 if (_moveListener != null) { | 97 if (_moveListener != null) { |
| 98 _rootNode.on.mouseMove.remove(_moveListener, false); | 98 _rootNode.on.mouseMove.remove(_moveListener, false); |
| 99 _possibleBoat.remove(); | 99 _possibleBoat.remove(); |
| 100 _moveListener = null; | 100 _moveListener = null; |
| 101 } | 101 } |
| 102 _possibleBoat = ViewUtil.createDiv(this, "icons boat2"); | 102 _possibleBoat = ViewUtil.createDiv("icons boat2"); |
| 103 ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY); | 103 ViewUtil.placeNodeAt(_possibleBoat, _boatStartX, _boatStartY); |
| 104 _rootNode.nodes.add(_possibleBoat); | 104 _rootNode.nodes.add(_possibleBoat); |
| 105 _moveListener = handleMouseMove; | 105 _moveListener = handleMouseMove; |
| 106 _rootNode.on.mouseMove.add(_moveListener); | 106 _rootNode.on.mouseMove.add(_moveListener); |
| 107 }); | 107 }); |
| 108 } | 108 } |
| 109 | 109 |
| 110 void handleMouseMove(e) { | 110 void handleMouseMove(e) { |
| 111 e.preventDefault(); | 111 e.preventDefault(); |
| 112 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { | 112 ViewUtil.positionFromEvent(_rootNode, e).then((List<int> pos) { |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 void setEnemyReady() { | 208 void setEnemyReady() { |
| 209 if (!_enemyReady) { | 209 if (!_enemyReady) { |
| 210 _enemyReady = true; | 210 _enemyReady = true; |
| 211 _rootNode.query(".notready").remove(); | 211 _rootNode.query(".notready").remove(); |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 | 214 |
| 215 | 215 |
| 216 /** Update the view to indicate a shot that hit an enemy's boat. */ | 216 /** Update the view to indicate a shot that hit an enemy's boat. */ |
| 217 void addHit(int x, int y) { | 217 void addHit(int x, int y) { |
| 218 Element node = ViewUtil.createDiv(this, "icons hit"); | 218 Element node = ViewUtil.createDiv("icons hit"); |
| 219 ViewUtil.placeNodeAt(node, x, y); | 219 ViewUtil.placeNodeAt(node, x, y); |
| 220 _rootNode.nodes.add(node); | 220 _rootNode.nodes.add(node); |
| 221 } | 221 } |
| 222 | 222 |
| 223 /** Update the view to indicate a shot that missed an enemy's boat. */ | 223 /** Update the view to indicate a shot that missed an enemy's boat. */ |
| 224 void addMiss(int x, int y) { | 224 void addMiss(int x, int y) { |
| 225 Element node = ViewUtil.createDiv(this, "icons miss"); | 225 Element node = ViewUtil.createDiv("icons miss"); |
| 226 ViewUtil.placeNodeAt(node, x, y); | 226 ViewUtil.placeNodeAt(node, x, y); |
| 227 _rootNode.nodes.add(node); | 227 _rootNode.nodes.add(node); |
| 228 } | 228 } |
| 229 | 229 |
| 230 /** Update the view to indicate a shot is in progress. */ | 230 /** Update the view to indicate a shot is in progress. */ |
| 231 void addMaybeHit(int x, int y) { | 231 void addMaybeHit(int x, int y) { |
| 232 Element node = ViewUtil.createDiv(this, "icons maybe-hit"); | 232 Element node = ViewUtil.createDiv("icons maybe-hit"); |
| 233 ViewUtil.placeNodeAt(node, x, y); | 233 ViewUtil.placeNodeAt(node, x, y); |
| 234 _rootNode.nodes.add(node); | 234 _rootNode.nodes.add(node); |
| 235 } | 235 } |
| 236 | 236 |
| 237 /** | 237 /** |
| 238 * Remove the icon indicating that a shot is in progress (only called when | 238 * Remove the icon indicating that a shot is in progress (only called when |
| 239 * shots failed due to network errors). | 239 * shots failed due to network errors). |
| 240 */ | 240 */ |
| 241 void removeMaybeHit(int x, int y) { | 241 void removeMaybeHit(int x, int y) { |
| 242 for (Element node in _rootNode.queryAll(".maybe-hit")) { | 242 for (Element node in _rootNode.queryAll(".maybe-hit")) { |
| 243 int xoffset = x * 50; | 243 int xoffset = x * 50; |
| 244 int yoffset = y * 50; | 244 int yoffset = y * 50; |
| 245 if (node.style.getPropertyValue("top") == "${yoffset}px" | 245 if (node.style.getPropertyValue("top") == "${yoffset}px" |
| 246 && node.style.getPropertyValue("left") == "${xoffset}px") { | 246 && node.style.getPropertyValue("left") == "${xoffset}px") { |
| 247 node.remove(); | 247 node.remove(); |
| 248 return; | 248 return; |
| 249 } | 249 } |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 } | 252 } |
| 253 | 253 |
| 254 class ShootingStatusView extends View { | 254 class ShootingStatusView extends View { |
| 255 PlayerState state; | 255 PlayerState state; |
| 256 Element _rootNode; | 256 Element _rootNode; |
| 257 | 257 |
| 258 ShootingStatusView(this.state, Document doc) | 258 ShootingStatusView(this.state, Document doc) |
| 259 : super(doc) { | 259 : super(doc) { |
| 260 _rootNode = ViewUtil.createDiv(this, "shooting-status"); | 260 _rootNode = ViewUtil.createDiv("shooting-status"); |
| 261 updateStatus(); | 261 updateStatus(); |
| 262 } | 262 } |
| 263 | 263 |
| 264 /** Update the view to indicate we sunk another enemy's boat. */ | 264 /** Update the view to indicate we sunk another enemy's boat. */ |
| 265 void updateStatus() { | 265 void updateStatus() { |
| 266 final total = state.totalShots; | 266 final total = state.totalShots; |
| 267 final hit = state.totalHits; | 267 final hit = state.totalHits; |
| 268 final miss = state.totalMisses; | 268 final miss = state.totalMisses; |
| 269 final accounted = hit + miss; | 269 final accounted = hit + miss; |
| 270 final sunk = state.boatsSunk; | 270 final sunk = state.boatsSunk; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 289 | 289 |
| 290 /** 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. */ |
| 291 static void placeNodeAt(Element node, int x, int y) { | 291 static void placeNodeAt(Element node, int x, int y) { |
| 292 int xoffset = x * 50; | 292 int xoffset = x * 50; |
| 293 int yoffset = y * 50; | 293 int yoffset = y * 50; |
| 294 node.style.setProperty("top", yoffset.toString() + "px"); | 294 node.style.setProperty("top", yoffset.toString() + "px"); |
| 295 node.style.setProperty("left", xoffset.toString() + "px"); | 295 node.style.setProperty("left", xoffset.toString() + "px"); |
| 296 } | 296 } |
| 297 | 297 |
| 298 /** Create a div node with a given class name. */ | 298 /** Create a div node with a given class name. */ |
| 299 static Element createDiv(View view, String className) { | 299 static Element createDiv(String className) { |
| 300 Element node = view.doc.createElement("div"); | 300 Element node = new Element.tag("div"); |
| 301 node.attributes["class"] = className; | 301 node.attributes["class"] = className; |
| 302 return node; | 302 return node; |
| 303 } | 303 } |
| 304 } | 304 } |
| OLD | NEW |