Board can be modified by dropping the fence
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 06 Jun 2009 14:57:09 +0200
changeset 32b6b0a99745b4
parent 31 8e6a6857c7b5
child 33 6a6d1dbea99e
Board can be modified by dropping the fence
visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
     1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sat Jun 06 14:51:46 2009 +0200
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sat Jun 06 14:57:09 2009 +0200
     1.3 @@ -177,24 +177,11 @@
     1.4          }
     1.5  
     1.6          public void movementFinished(Widget widget) {
     1.7 -            int column = widget.getBounds().x / 50 - 1;
     1.8 -            if (column < 0) {
     1.9 -                column = 0;
    1.10 -            }
    1.11 -            if (column >= 9) {
    1.12 -                column = 8;
    1.13 -            }
    1.14 -            int row = widget.getBounds().y / 50 - 1;
    1.15 -            if (row < 0) {
    1.16 -                row = 0;
    1.17 -            }
    1.18 -            if (row >= 8) {
    1.19 -                row = 7;
    1.20 -            }
    1.21 -
    1.22 -            Orientation o = widget.getBounds().width > widget.getBounds().height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
    1.23              try {
    1.24 -                board = board.apply(Move.fence((char) ('A' + column), 1 + row, o));
    1.25 +                final Rectangle bounds = widget.getBounds();
    1.26 +                final Move m = createMoveForDrop( bounds);
    1.27 +                System.err.println("finish: " + m);
    1.28 +                board = board.apply(m);
    1.29              } catch (IllegalPositionException ex) {
    1.30                  Exceptions.printStackTrace(ex);
    1.31              }
    1.32 @@ -203,24 +190,59 @@
    1.33          }
    1.34  
    1.35          public Point getOriginalLocation(Widget widget) {
    1.36 -            orig = new Point(widget.getBounds().x, widget.getBounds().y);
    1.37 -            System.err.println("orig: " + orig);
    1.38 +            final Rectangle b = widget.getBounds();
    1.39 +            orig = new Point(b.x, b.y);
    1.40 +            try {
    1.41 +                System.err.println("  where: " + createMoveForDrop(b));
    1.42 +            } catch (IllegalPositionException ex) {
    1.43 +                Exceptions.printStackTrace(ex);
    1.44 +            }
    1.45              return orig;
    1.46          }
    1.47  
    1.48          public void setNewLocation(Widget widget, Point location) {
    1.49 -            widget.setPreferredBounds(new Rectangle(
    1.50 +            final Rectangle b = new Rectangle(
    1.51                  round(location.x, true) + (horizontal ? 5 : 0),
    1.52                  round(location.y, false) + (horizontal ? 0 : 5),
    1.53                  horizontal ? 90 : 10,
    1.54 -                horizontal ? 10 : 90)
    1.55 +                horizontal ? 10 : 90
    1.56              );
    1.57 +            widget.setPreferredBounds(b);
    1.58 +            try {
    1.59 +                System.err.println("move: " + createMoveForDrop(b));
    1.60 +            } catch (IllegalPositionException ex) {
    1.61 +                System.err.println("no move");
    1.62 +            }
    1.63 +        }
    1.64 +
    1.65 +        Move createMoveForDrop(final Rectangle bounds) throws IllegalPositionException {
    1.66 +            int column = bounds.x / 50;
    1.67 +            int row = bounds.y / 50;
    1.68 +            Orientation o = bounds.width > bounds.height ? Orientation.HORIZONTAL : Orientation.VERTICAL;
    1.69 +            switch (o) {
    1.70 +                case HORIZONTAL: row--; break;
    1.71 +                case VERTICAL: column--; break;
    1.72 +                default: assert false;
    1.73 +            }
    1.74 +            if (column < 0) {
    1.75 +                column = 0;
    1.76 +            }
    1.77 +            if (column >= 9) {
    1.78 +                column = 8;
    1.79 +            }
    1.80 +            if (row < 0) {
    1.81 +                row = 0;
    1.82 +            }
    1.83 +            if (row > 8) {
    1.84 +                row = 8;
    1.85 +            }
    1.86 +            Move m = Move.fence((char) ('A' + column), 1 + row, o);
    1.87 +            return m;
    1.88          }
    1.89  
    1.90          int round(int p, boolean cmpHori) {
    1.91      //        p = horizontal ? orig.x + p: orig.y + p;
    1.92              int onboard = p / 50;
    1.93 -            System.err.println(cmpHori ? "hori: " + onboard : "  vert: " + onboard);
    1.94              if (onboard < 0) {
    1.95                  onboard = 0;
    1.96              }