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 19 matching lines...) Expand all Loading... |
30 _cells.add(cell); | 30 _cells.add(cell); |
31 } | 31 } |
32 String cells = _cells.toString(); | 32 String cells = _cells.toString(); |
33 String row = "<div class='hbox'>${cells}</div>"; | 33 String row = "<div class='hbox'>${cells}</div>"; |
34 StringBuffer _rows = new StringBuffer(); | 34 StringBuffer _rows = new StringBuffer(); |
35 for (int i = 0 ; i < state.localGrid.cells.length; i++) { | 35 for (int i = 0 ; i < state.localGrid.cells.length; i++) { |
36 _rows.add(row); | 36 _rows.add(row); |
37 } | 37 } |
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("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); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 _cells.add(cell); | 180 _cells.add(cell); |
181 } | 181 } |
182 String cells = _cells.toString(); | 182 String cells = _cells.toString(); |
183 String row = "<div class='hbox'>${cells}</div>"; | 183 String row = "<div class='hbox'>${cells}</div>"; |
184 StringBuffer _rows = new StringBuffer(); | 184 StringBuffer _rows = new StringBuffer(); |
185 for (int i = 0 ; i < state.enemyGrid.cells.length; i++) { | 185 for (int i = 0 ; i < state.enemyGrid.cells.length; i++) { |
186 _rows.add(row); | 186 _rows.add(row); |
187 } | 187 } |
188 String rows = _rows.toString(); | 188 String rows = _rows.toString(); |
189 String table = "<div class='vbox'>${rows}</div>"; | 189 String table = "<div class='vbox'>${rows}</div>"; |
190 _rootNode.innerHTML = | 190 _rootNode.innerHtml = |
191 "${table}<div class='notready'>ENEMY IS NOT READY</div>"; | 191 "${table}<div class='notready'>ENEMY IS NOT READY</div>"; |
192 statusBar = new ShootingStatusView(state, doc); | 192 statusBar = new ShootingStatusView(state, doc); |
193 _rootNode.nodes.add(statusBar._rootNode); | 193 _rootNode.nodes.add(statusBar._rootNode); |
194 _rootNode.on.click.add((Event e) { | 194 _rootNode.on.click.add((Event e) { |
195 MouseEvent mouseEvent = e; | 195 MouseEvent mouseEvent = e; |
196 handleClick(mouseEvent); | 196 handleClick(mouseEvent); |
197 }, false); | 197 }, false); |
198 } | 198 } |
199 | 199 |
200 /** Interpret clicks as a shooting action. */ | 200 /** Interpret clicks as a shooting action. */ |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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; |
271 _rootNode.innerHTML = | 271 _rootNode.innerHtml = |
272 "${total} <= ${accounted} (${hit} + ${miss}); ${sunk}"; | 272 "${total} <= ${accounted} (${hit} + ${miss}); ${sunk}"; |
273 } | 273 } |
274 } | 274 } |
275 | 275 |
276 /** Utility methods used by the views above. */ | 276 /** Utility methods used by the views above. */ |
277 class ViewUtil { | 277 class ViewUtil { |
278 | 278 |
279 /** 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. */ |
280 static Future<List<int>> positionFromEvent(Element gridNode, MouseEvent e) { | 280 static Future<List<int>> positionFromEvent(Element gridNode, MouseEvent e) { |
281 final completer = new Completer<List<int>>(); | 281 final completer = new Completer<List<int>>(); |
(...skipping 13 matching lines...) Expand all Loading... |
295 node.style.setProperty("left", "${xoffset}px"); | 295 node.style.setProperty("left", "${xoffset}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(String className) { | 299 static Element createDiv(String className) { |
300 Element node = new Element.tag("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 |