# HG changeset patch # User Jaroslav Tulach # Date 1244293029 -7200 # Node ID b6b0a99745b480e3269b2ff8cda369c538d19dbf # Parent 8e6a6857c7b5fa46a1329f05679c5b960ad8047a Board can be modified by dropping the fence diff -r 8e6a6857c7b5 -r b6b0a99745b4 visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Jun 06 14:51:46 2009 +0200 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java Sat Jun 06 14:57:09 2009 +0200 @@ -177,24 +177,11 @@ } public void movementFinished(Widget widget) { - int column = widget.getBounds().x / 50 - 1; - if (column < 0) { - column = 0; - } - if (column >= 9) { - column = 8; - } - int row = widget.getBounds().y / 50 - 1; - if (row < 0) { - row = 0; - } - if (row >= 8) { - row = 7; - } - - Orientation o = widget.getBounds().width > widget.getBounds().height ? Orientation.HORIZONTAL : Orientation.VERTICAL; try { - board = board.apply(Move.fence((char) ('A' + column), 1 + row, o)); + final Rectangle bounds = widget.getBounds(); + final Move m = createMoveForDrop( bounds); + System.err.println("finish: " + m); + board = board.apply(m); } catch (IllegalPositionException ex) { Exceptions.printStackTrace(ex); } @@ -203,24 +190,59 @@ } public Point getOriginalLocation(Widget widget) { - orig = new Point(widget.getBounds().x, widget.getBounds().y); - System.err.println("orig: " + orig); + final Rectangle b = widget.getBounds(); + orig = new Point(b.x, b.y); + try { + System.err.println(" where: " + createMoveForDrop(b)); + } catch (IllegalPositionException ex) { + Exceptions.printStackTrace(ex); + } return orig; } public void setNewLocation(Widget widget, Point location) { - widget.setPreferredBounds(new Rectangle( + final Rectangle b = new Rectangle( round(location.x, true) + (horizontal ? 5 : 0), round(location.y, false) + (horizontal ? 0 : 5), horizontal ? 90 : 10, - horizontal ? 10 : 90) + horizontal ? 10 : 90 ); + widget.setPreferredBounds(b); + try { + System.err.println("move: " + createMoveForDrop(b)); + } catch (IllegalPositionException ex) { + System.err.println("no move"); + } + } + + Move createMoveForDrop(final Rectangle bounds) throws IllegalPositionException { + int column = bounds.x / 50; + int row = bounds.y / 50; + Orientation o = bounds.width > bounds.height ? Orientation.HORIZONTAL : Orientation.VERTICAL; + switch (o) { + case HORIZONTAL: row--; break; + case VERTICAL: column--; break; + default: assert false; + } + if (column < 0) { + column = 0; + } + if (column >= 9) { + column = 8; + } + if (row < 0) { + row = 0; + } + if (row > 8) { + row = 8; + } + Move m = Move.fence((char) ('A' + column), 1 + row, o); + return m; } int round(int p, boolean cmpHori) { // p = horizontal ? orig.x + p: orig.y + p; int onboard = p / 50; - System.err.println(cmpHori ? "hori: " + onboard : " vert: " + onboard); if (onboard < 0) { onboard = 0; }