home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 14 / IOPROG_14.ISO / soft / sdkjava / dxma.exe / DXMA05.cab / samples / da / java / apps / chess / ChessModel.java < prev    next >
Encoding:
Java Source  |  1997-11-13  |  20.8 KB  |  596 lines

  1. import com.ms.dxmedia.*;
  2. import java.util.*;
  3. import java.net.*;
  4. import Piece;
  5.  
  6.  
  7. public class ChessModel extends Model {
  8.  
  9.   // here are a bunch of constants we'll use:
  10.   // the size of each square on the board, and the location of square 0,0
  11.   protected NumberBvr squareSize = toBvr(1.0/8.0);
  12.   protected Vector3Bvr pieceOrigin = vector3(toBvr(-3.5/8),toBvr(0),toBvr(3.5/8));
  13.  
  14.   //static URL mybase = null;
  15.   // the geometry for the pieces (initialized in SetupBoard())
  16.   protected GeometryBvr knight = null;
  17.   protected GeometryBvr queen = null;
  18.   protected GeometryBvr rook = null;
  19.   protected GeometryBvr bishop = null;
  20.   protected GeometryBvr king = null;
  21.   protected GeometryBvr pawn = null;
  22.   protected SoundBvr    moveSnd = null;
  23.   protected SoundBvr    captureSnd = null;
  24.   // end of constants
  25.  
  26.   // The geoArray keeps track of which geometry is at each board location
  27.   // The switcherArray keeps track of the switchers needed to tell these
  28.   // geometries to move around
  29.   protected GeometryBvr[][] m_geoArray = new GeometryBvr[8][8];
  30.   protected ModifiableBehavior[][] m_switcherArray = new ModifiableBehavior[8][8];
  31.  
  32.   // Same information is needed for captured geometries
  33.   protected Vector m_capturedGeos = new Vector(); 
  34.   protected Vector m_capturedSws = new Vector();
  35.   protected int[] m_capturedCounts = new int[2];
  36.  
  37.   // Switcher requires to get an updating behavior of the canvas size.
  38.   protected ModifiableBehavior m_sizeSw = null;
  39.  
  40.   public LabelImage m_resultLabel = null;
  41.  
  42.   public void createModel(BvrsToRun b2run) {
  43.  
  44.     //mybase = getImportBase();
  45.  
  46.     m_resultLabel = new LabelImage();
  47.     // here we setup the size Behavior to give us the size of the canvas. Every time
  48.     // SetSize() is called the switcher is used to change the behavior.
  49.  
  50.     m_sizeSw = new ModifiableBehavior(vector2(mul(toBvr(320),pixelBvr),
  51.             mul(toBvr(240),pixelBvr)));
  52.     Vector2Bvr size = (Vector2Bvr) m_sizeSw.getBvr();
  53.     
  54.      
  55.     // This sets up the arrow buttons to rotate the board
  56.     ButtonPanel bp = new ButtonPanel();
  57.     ImageBvr buttons = bp.getImg();
  58.     Vector2Bvr rots = bp.getRots();
  59.     
  60.     // calculate the vector to put the buttonpanel in the upper right corner
  61.     Vector2Bvr buttonXform = add(size.div(toBvr(2)),
  62.                           sub(origin2,buttons.boundingBox().getMax()));
  63.     buttonXform = vector2(mul(buttonXform.getX(),toBvr(-1)),
  64.             mul(buttonXform.getY(),toBvr(-1)));
  65.         buttons = buttons.transform(translate(buttonXform)); //problem with making the buttons work.
  66.  
  67.     // set up the buttons for zooming in and out
  68.     ZoomPanel zp = new ZoomPanel();
  69.     ImageBvr zoomButtons = zp.getImg();
  70.  
  71.         NumberBvr zoomNum = (NumberBvr)zp.getZoom().runOnce();
  72.  
  73.     //Transform2Bvr imgScale = scale2(zp.getZoom());
  74.  
  75.         Transform3Bvr zoomScale = scale(zoomNum, zoomNum,toBvr(1));
  76.  
  77.     // calculate the translate to put it in the lower right corner
  78.     Vector2Bvr zbtranslate = add(size.div(2),
  79.                  sub(origin2,zoomButtons.boundingBox().getMax()));
  80.     zbtranslate = vector2(zbtranslate.getX(), mul(zbtranslate.getY(),toBvr(-1)));
  81.     zoomButtons = zoomButtons.transform(translate(zbtranslate));
  82.     
  83.  
  84.     // setup the camera according to a pretty first position, and the rotation from the 
  85.     // button panel and scale the image with the info from the zoomPanel.
  86.     Transform3Bvr cameraXform = compose(
  87.                     rotate(yVector3, neg(rots.getX())),
  88.                     compose(rotate(xVector3, add(toBvr(-Math.PI/6),rots.getY())),
  89.                       compose(zoomScale,translate(toBvr(0),toBvr(0),toBvr(2.5)))));
  90.         
  91.     CameraBvr camera= perspectiveCamera(toBvr(1),toBvr(0)).transform(compose(
  92.                                          cameraXform,scale(toBvr(6),toBvr(6),toBvr(1))));
  93.  
  94.     MicrophoneBvr mic = defaultMicrophone.transform(cameraXform);
  95.         
  96.     // setup the initial board geometry
  97.     GeometryBvr geo = SetupBoard();
  98.  
  99.         //geo = geo.transform(zoomScale);
  100.  
  101.     GeometryBvr headlight = directionalLight.lightColor(colorRgb(toBvr(.6),toBvr(.6),toBvr(.6))).
  102.       transform(cameraXform);
  103.     geo = union(geo,headlight);
  104.  
  105.     ImageBvr img = geo.render(camera);
  106.     SoundBvr snd = geo.render(mic);
  107.  
  108.     // scale the image with the info from the zoomPanel
  109.     //img = img.transform(imgScale);
  110.  
  111.     // overlay the rendered image with the buttons
  112.     img = overlay(zoomButtons,overlay(buttons,img));
  113.  
  114.     // make a pretty background color
  115.     ImageBvr model = overlay(m_resultLabel.getBvr(), 
  116.                  overlay(img, solidColorImage(colorRgb(toBvr(.2),toBvr(.27),toBvr(.3)))));
  117.  
  118.     setImage(model);
  119.     setSound(snd);
  120.   }
  121.  
  122.  
  123.   // this updates size via the size Switcher when the model changes size.
  124.   public void SetSize(int width, int height){
  125.     if (m_sizeSw != null) {
  126.       Vector2Bvr size = vector2(mul(toBvr(width),pixelBvr),
  127.                 mul(toBvr(height),pixelBvr));
  128.       m_sizeSw.switchTo(size);
  129.     }
  130.   }
  131.  
  132.  
  133.   protected GeometryBvr SetupBoard() {
  134.  
  135.     // import all the geometry we're going to use
  136.     NumberBvr scale = toBvr(1.0/18.0);
  137.  
  138.     try {
  139.  
  140.       knight = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/knight.x")).transform(scale3(scale));
  141.       knight = knight.transform(translate(0.28,0.0,0.38));
  142.       queen = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/queen.x")).transform(scale3(scale));
  143.       queen = queen.transform(translate(-0.05,0,-0.40));
  144.       rook = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/rook.x")).transform(scale3(scale));
  145.       rook = rook.transform(translate(0.38,0,0.38));      
  146.       bishop = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/bishop.x")).transform(scale3(scale));
  147.       bishop = bishop.transform(translate(0.17,0,-0.40));      
  148.       king = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/king.x")).transform(scale3(scale));
  149.       king = king.transform(translate(0.05,0,-0.40));
  150.       pawn = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/pawn.x")).transform(scale3(scale));
  151.       pawn = pawn.transform(translate(-0.05,0,-0.28));
  152.  
  153.       NumberBvr foo[] = new NumberBvr[2];
  154.  
  155.       moveSnd = importSound(new URL("file:///c:/program files/da_pgn_viewer/media/zoomin2.wav"),foo);
  156.       captureSnd = importSound(new URL("file:///c:/program files/da_pgn_viewer/media/bangblow.wav"),foo);
  157.  
  158.                 } catch (MalformedURLException e) {
  159.       System.err.println("malformed URL!!!!!!!!!!!");
  160.     }
  161.  
  162.     // set the middle part of the board to empty.
  163.     for (int i = 2; i < 6; i++) {
  164.       for (int j = 0; j < 8; j++) {
  165.         m_switcherArray[i][j] = null;
  166.         m_geoArray[i][j] = null;
  167.       }
  168.     }
  169.  
  170.     // put pieces on the rest of the board.
  171.     setPiece(rook, 0, 0, Piece.WHITE);
  172.     setPiece(knight, 1, 0, Piece.WHITE);
  173.     setPiece(bishop, 2, 0, Piece.WHITE);
  174.     setPiece(queen, 3, 0, Piece.WHITE);
  175.     setPiece(king, 4, 0, Piece.WHITE);
  176.     setPiece(bishop, 5, 0, Piece.WHITE);
  177.     setPiece(knight, 6, 0, Piece.WHITE);
  178.     setPiece(rook, 7, 0, Piece.WHITE);
  179.  
  180.     setPiece(pawn, 0, 1, Piece.WHITE);
  181.     setPiece(pawn, 1, 1, Piece.WHITE);
  182.     setPiece(pawn, 2, 1, Piece.WHITE);
  183.     setPiece(pawn, 3, 1, Piece.WHITE);
  184.     setPiece(pawn, 4, 1, Piece.WHITE);
  185.     setPiece(pawn, 5, 1, Piece.WHITE);
  186.     setPiece(pawn, 6, 1, Piece.WHITE);
  187.     setPiece(pawn, 7, 1, Piece.WHITE);
  188.  
  189.     setPiece(pawn, 0, 6, Piece.BLACK);
  190.     setPiece(pawn, 1, 6, Piece.BLACK);
  191.     setPiece(pawn, 2, 6, Piece.BLACK);
  192.     setPiece(pawn, 3, 6, Piece.BLACK);
  193.     setPiece(pawn, 4, 6, Piece.BLACK);
  194.     setPiece(pawn, 5, 6, Piece.BLACK);
  195.     setPiece(pawn, 6, 6, Piece.BLACK);
  196.     setPiece(pawn, 7, 6, Piece.BLACK);
  197.  
  198.     setPiece(rook, 0, 7, Piece.BLACK);
  199.     setPiece(knight, 1, 7, Piece.BLACK);
  200.     setPiece(bishop, 2, 7, Piece.BLACK);
  201.     setPiece(queen, 3, 7, Piece.BLACK);
  202.     setPiece(king, 4, 7, Piece.BLACK);
  203.     setPiece(bishop, 5, 7, Piece.BLACK);
  204.     setPiece(knight, 6, 7, Piece.BLACK);
  205.     setPiece(rook, 7, 7, Piece.BLACK);
  206.  
  207.  
  208.     // setup the board geometry
  209.     GeometryBvr boardGeo = null;
  210.     GeometryBvr whiteSquares = null;
  211.     GeometryBvr blackSquares = null;
  212.     GeometryBvr finalBoard = null;
  213.     try {
  214.       whiteSquares = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/white_sqrs.x"));
  215.       blackSquares = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/blk_sqrs.x"));
  216.       boardGeo = importGeometry(new URL("file:///c:/program files/da_pgn_viewer/media/board.x"));//.transform(
  217.               //scale3(toBvr(1.0/15.0)));
  218.       finalBoard = union(blackSquares,union(whiteSquares,boardGeo));
  219.       finalBoard = finalBoard.transform(scale3(toBvr(1.0/16.0)));
  220.     
  221.     
  222.     //                                        compose(rotate(xVector3, toBvr(-Math.PI/2)), scale3(toBvr(1.0/80.0))));
  223.     } catch (MalformedURLException e) {
  224.       System.err.println("another bad URL!!!");
  225.     }
  226.  
  227.     GeometryBvr lights = union(union(directionalLight.
  228.                      lightColor(colorRgb(toBvr(.5),toBvr(.5),toBvr(.5))).
  229.                      transform(compose(rotate(xVector3,toBvr(-Math.PI/4)),
  230.                                rotate(yVector3,toBvr(0))) 
  231.                            ),
  232.       
  233.                      directionalLight.lightColor(colorRgb(toBvr(.5),toBvr(.5),toBvr(.5))).
  234.                      transform(compose(
  235.                                rotate(xVector3,toBvr(-.75*Math.PI)),
  236.                                rotate(yVector3,toBvr(0))
  237.                                ))),
  238.       
  239.                    ambientLight.lightColor(colorRgb(toBvr(.1),toBvr(.1),toBvr(.1))));
  240.  
  241.     GeometryBvr allGeo = union(finalBoard, lights);
  242.  
  243.     // union the board geometry with the geometry of all the pieces. We get the geometry for the
  244.     // pieces from the switchers so they can be changed by the switcher.
  245.     for (int i = 0; i < 8; i++){
  246.       for (int j = 0; j < 8; j++){
  247.     if (m_switcherArray[i][j] != null) {
  248.       allGeo = union(allGeo, (GeometryBvr)m_switcherArray[i][j].getBvr());
  249.     }
  250.       }
  251.     }
  252.     return allGeo;
  253.  
  254.   }
  255.  
  256.   protected void setPiece(GeometryBvr geo, int x, int y, int color) {
  257.     
  258.     GeometryBvr cgeo = null;
  259.     
  260.     // color and rotate the piece appropriate to the player
  261.     if (color == Piece.WHITE) {
  262.       cgeo = geo.diffuseColor( colorRgb(toBvr(0.7),toBvr(0.7),toBvr(0.7)));/*.
  263.                                            specularColor(colorRgb(toBvr(.9),toBvr(.9),toBvr(.9))).
  264.                                            specularExponent(toBvr(10)); */      
  265.       cgeo = cgeo.transform(rotate(yVector3,toBvr(Math.PI)));
  266.     } else {
  267.       cgeo = geo.diffuseColor( colorRgb(toBvr(0.6),toBvr(0),toBvr(0.3)));/*.
  268.                                        specularColor(colorRgb(toBvr(.5),toBvr(.5),toBvr(.5))).
  269.                                        specularExponent(toBvr(10));       */
  270.     }
  271.  
  272.     // put the piece on the board
  273.     m_switcherArray[x][y] = new ModifiableBehavior(cgeo.transform(LocationToTransform(x,y)));
  274.     m_geoArray[x][y] = cgeo;
  275.  
  276.   }
  277.  
  278.   // returns a vector that will translate a piece to a given spot on the board
  279.   protected Vector3Bvr LocationToTranslate(int x, int y) {
  280.     return add(pieceOrigin, vector3(toBvr(x),toBvr(0),toBvr(-y)).mul(squareSize));
  281.   }
  282.   // same thing but returns a transform3Bvr
  283.   protected Transform3Bvr LocationToTransform(int x, int y) {
  284.     return translate(LocationToTranslate(x,y));
  285.   }
  286.  
  287.   // handy utility function, returns a nice number
  288.   public static NumberBvr Smooth0to1(int duration) {
  289.     NumberBvr rising = sub (toBvr(.5), 
  290.                 div(cos(mul(div(localTime,toBvr(duration)), toBvr(Math.PI))),toBvr(2)));
  291.     NumberBvr done = toBvr(1);
  292.  
  293.     DXMEvent ev = timer(toBvr(duration));
  294.  
  295.     return (NumberBvr)(until(rising, ev, done));
  296.   }
  297.  
  298.   // returns a transform that will slide a piece from one location to another
  299.   protected Transform3Bvr SlidingTransform(int xfrom, int yfrom, int xto, int yto,int duration) {
  300.     Vector3Bvr fromVec = LocationToTranslate(xfrom,yfrom);
  301.     Vector3Bvr toVec = LocationToTranslate(xto,yto);
  302.     Vector3Bvr moveVec = sub(toVec, fromVec);
  303.     return  translate(add(moveVec.mul(Smooth0to1(duration)),LocationToTranslate(xfrom,yfrom)));
  304.   }
  305.  
  306.   //  animates a piece moving from one location to another
  307.   public void MovePiece(int xfrom, int yfrom, int xto, int yto) {
  308.         
  309.     GeometryBvr newGeo = union(m_geoArray[xfrom][yfrom],soundSource(moveSnd));
  310.     newGeo = newGeo.transform(SlidingTransform(xfrom,yfrom,xto,yto,3));
  311.  
  312.     m_switcherArray[xto][yto] = m_switcherArray[xfrom][yfrom];
  313.     m_switcherArray[xfrom][yfrom] = null;
  314.     m_geoArray[xto][yto] = m_geoArray[xfrom][yfrom];
  315.     m_geoArray[xfrom][yfrom] = null;
  316.     m_switcherArray[xto][yto].switchTo(newGeo);
  317.   }
  318.  
  319.   public void CapturePiece(int x, int y, int player) {
  320.  
  321.     //keep track of the fact that its been captured
  322.     m_capturedSws.addElement(m_switcherArray[x][y]);
  323.     m_capturedGeos.addElement(m_geoArray[x][y]);
  324.  
  325.     // calculate where on the board to put the captured piece. We want two columns along side 
  326.     // the board
  327.     int xTo = m_capturedCounts[player]/10;
  328.     int yTo = m_capturedCounts[player]%10;
  329.  
  330.     if (player == Piece.WHITE) {
  331.       xTo += 9;
  332.       yTo -= 1;
  333.     } else {
  334.       xTo = -2 -xTo;
  335.       yTo = 9 -yTo;
  336.     }
  337.  
  338.     // calculate the transform to make it fly and twist in agony
  339.     int duration = 4;
  340.     Transform3Bvr slide = SlidingTransform(x,y,xTo,yTo,duration);
  341.  
  342.     // here is the height of the toss
  343.     NumberBvr height = sub(mul(sin(mul(localTime,toBvr(Math.PI/duration))),toBvr(.35)),
  344.                mul(div(localTime,toBvr(duration)),squareSize));
  345.  
  346.     Transform3Bvr rot = rotate(vector3(toBvr(1),toBvr(0.2),toBvr(.5)), mul(localTime,toBvr(Math.PI*2/duration)));
  347.  
  348.     // this toss just puts a piece straight up int the air and then back down.
  349.     Transform3Bvr toss = (Transform3Bvr)until(compose(translate(toBvr(0),height,toBvr(0)),rot),
  350.                           timer(toBvr(duration)),translate(toBvr(0),mul(toBvr(-1),squareSize),toBvr(0)));
  351.  
  352.     // add the slide to move the piece off the board
  353.     Transform3Bvr capturedXform =compose(slide,toss);
  354.  
  355.     // tell the piece to execute the move with sound
  356.     m_switcherArray[x][y].switchTo(union(m_geoArray[x][y],soundSource(captureSnd)).transform(capturedXform));
  357.  
  358.     // take the piece off the board
  359.     m_geoArray[x][y] = null;
  360.     m_switcherArray[x][y] = null;
  361.     m_capturedCounts[player]++;
  362.     
  363.   }
  364.  
  365.  
  366.   public void RevivePiece(int x, int y, int player) {
  367.  
  368.     // move the last captured piece of color player back onto the board
  369.     m_switcherArray[x][y] = (ModifiableBehavior)m_capturedSws.lastElement();
  370.     m_capturedSws.removeElementAt(m_capturedSws.size() -1);
  371.     m_geoArray[x][y] = (GeometryBvr)m_capturedGeos.lastElement();
  372.     m_capturedGeos.removeElementAt(m_capturedGeos.size() -1);
  373.  
  374.     m_switcherArray[x][y].switchTo(m_geoArray[x][y].transform(LocationToTransform(x,y)));
  375.     m_capturedCounts[player]--;
  376.   }
  377.  
  378.   public void PromotePiece(int x, int y, int type) {
  379.     // change the geometry of a specific piece
  380.     switch (type) {
  381.     case Piece.QUEEN:
  382.       m_geoArray[x][y] = queen;
  383.       break;
  384.     case Piece.PAWN:
  385.       m_geoArray[x][y] = pawn;
  386.       break;
  387.     case Piece.ROOK:
  388.       m_geoArray[x][y] = rook;
  389.       break;
  390.     case Piece.KNIGHT:
  391.       m_geoArray[x][y] = knight;
  392.       break;
  393.     case Piece.BISHOP:
  394.       m_geoArray[x][y] = bishop;
  395.       break;
  396.     case Piece.KING:
  397.       m_geoArray[x][y] = king;
  398.       break;
  399.     }
  400.     m_switcherArray[x][y].switchTo(m_geoArray[x][y].transform(LocationToTransform(x,y)));      
  401.   }
  402. } // end ChessModel
  403.  
  404.  
  405.  
  406. // class: ButtonPanel
  407. // this class generates the arrow buttons for the upper right hand corner of the screen. 
  408. // we will get from this an image to put onscreen, and a vector2 that is the amount to 
  409. // rotate the board on the X and Y axis.
  410.  
  411. class ButtonPanel extends Statics implements UntilNotifier {
  412.  
  413.   DXMEvent m_arrowPick = null;
  414.   Vector2Bvr m_rots = null;
  415.   PickableImage m_pimg =  null;
  416.  
  417.   public ButtonPanel() {
  418.  
  419.     // setup the pickable image
  420.     try {
  421.       m_pimg = new PickableImage(importImage(new URL("file:///c:/program files/da_pgn_viewer/media/arrows.gif")));
  422.     }catch (MalformedURLException e) {
  423.       System.err.println("AWWWWWWWWWW!");
  424.     }
  425.     m_arrowPick = m_pimg.getPickEvent();
  426.  
  427.     // speeds is zero until we get clicked on...
  428.     Vector2Bvr speeds = (Vector2Bvr)untilNotify(zeroVector2, 
  429.                         andEvent(leftButtonDown,m_arrowPick), this);
  430.  
  431.         Vector2Bvr actualZoomSpeed = Vector2Bvr.newUninitBvr();
  432.           actualZoomSpeed.init(until(
  433.                 speeds,orEvent(notEvent(m_arrowPick),leftButtonUp),
  434.                 actualZoomSpeed));
  435.  
  436.  
  437.     // total amount to rotate is the integral of how long we've been holding
  438.     // the button down.
  439.     m_rots = integral(actualZoomSpeed);
  440.     m_rots.runOnce();
  441.   }
  442.  
  443.   public ImageBvr getImg() {
  444.     return m_pimg.getImageBvr();
  445.   }
  446.   public Vector2Bvr getRots() {
  447.     return m_rots;
  448.   }
  449.  
  450.   // utility function
  451.   public static NumberBvr sign(NumberBvr b) {
  452.     return (NumberBvr)cond(gte(b, toBvr(0)), toBvr(1), toBvr(-1));
  453.   }
  454.   
  455.   public static NumberBvr min(NumberBvr a, NumberBvr b) {
  456.     return (NumberBvr)cond(lte(a,b),a,b);
  457.   }
  458.   public static NumberBvr max(NumberBvr a, NumberBvr b) {
  459.     return (NumberBvr)cond(gte(a,b),a,b);
  460.   }
  461.  
  462.   // called when we're picked on
  463.   public Behavior notify(Object eventData, Behavior prev, 
  464.              BvrsToRun b2r) {
  465.     // cast the event data for easier access
  466.     PairObject andEv = (PairObject)eventData;
  467.     
  468.     PairObject pickData = (PairObject)andEv.getSecond();
  469.  
  470.     Point2Bvr pickPt= (Point2Bvr)pickData.getFirst();
  471.     Vector2Bvr pickVec = (Vector2Bvr)pickData.getSecond();
  472.  
  473.     // the Pt picked on the image 
  474.     pickPt = add(pickPt,pickVec);
  475.  
  476.     // turn on/off latspeed/longspeed depending on which quadrant of the image we're in
  477.     NumberBvr latSpeed = (NumberBvr)cond (gt(abs(pickPt.getX()), abs(pickPt.getY())), 
  478.                       sign(pickPt.getX()),toBvr(0));
  479.  
  480.     NumberBvr longSpeed = (NumberBvr)cond (gt(abs(pickPt.getX()), abs(pickPt.getY())), 
  481.                        toBvr(0), sign(pickPt.getY()));
  482.  
  483.     longSpeed.runOnce();
  484.  
  485.     NumberBvr cappedLongSpeed =(NumberBvr) cond(gte(m_rots.getY(), toBvr(Math.PI/2.0)),min(longSpeed,toBvr(0)),longSpeed);
  486.  
  487.     Vector2Bvr speeds = vector2(latSpeed,cappedLongSpeed).div(toBvr(3));
  488.  
  489.         return speeds;
  490.  
  491.     // return the speeds we just calculated, until we either let go of the mouse button 
  492.     // or move the mouse of the picture, then become a zeroVector that will run through this 
  493.     // notifier when clicked on.
  494.     //return until(speeds, orEvent(notEvent(m_arrowPick),leftButtonUp), 
  495.         // untilNotify(zeroVector2, andEvent(leftButtonDown,m_arrowPick), this));
  496.   }
  497. }
  498.  
  499.  
  500.    
  501. // class: ZoomPanel
  502. // Very similar to  the ButtonPanel, except here we only have to detect if we are on
  503. // the top or bottom half of the image.
  504.  
  505. class ZoomPanel extends Statics implements UntilNotifier {
  506.   DXMEvent m_zoomPick = null;
  507.  
  508.   NumberBvr m_zoom = null;
  509.   PickableImage m_pimg =  null;
  510.  
  511.   public ZoomPanel() {
  512.     // set up the pickable image
  513.     try {
  514.           ImageBvr img = importImage(new URL("file:///c:/program files/da_pgn_viewer/media/zoom.bmp"));
  515.  
  516.       m_pimg = new PickableImage(img);
  517.     }catch (MalformedURLException e) {
  518.       System.err.println("AWWW!");
  519.     }
  520.     m_zoomPick = m_pimg.getPickEvent();
  521.  
  522.     // set zoomspeed to be zero until we get clicked on
  523.     NumberBvr zoomSpeed = (NumberBvr)untilNotify(toBvr(0), 
  524.                          andEvent(leftButtonDown,m_zoomPick), this);
  525.  
  526.         NumberBvr actualZoomSpeed = NumberBvr.newUninitBvr();
  527.           actualZoomSpeed.init(until(
  528.                 zoomSpeed,orEvent(notEvent(m_zoomPick),leftButtonUp),
  529.                 actualZoomSpeed));
  530.  
  531.     // integrate the zoom speed and scale it to look nice.
  532.     m_zoom = add(div(integral(actualZoomSpeed),toBvr(4)),toBvr(1));
  533.  
  534.   }
  535.  
  536.   public ImageBvr getImg() {
  537.     return m_pimg.getImageBvr();
  538.   }
  539.   public NumberBvr getZoom() {
  540.     return m_zoom;
  541.   }
  542.  
  543.     public static NumberBvr sign(NumberBvr b) {
  544.     return (NumberBvr)cond(gte(b, toBvr(0)), toBvr(-1), toBvr(1));
  545.   }
  546.  
  547.   public Behavior notify(Object eventData, Behavior prev, 
  548.              BvrsToRun b2r) {
  549.     // get at the event data
  550.     PairObject andEv = (PairObject)eventData;
  551.     PairObject pickData = (PairObject)andEv.getSecond();
  552.  
  553.         Point2Bvr pickPt= (Point2Bvr)pickData.getFirst();
  554.     Vector2Bvr pickVec = (Vector2Bvr)pickData.getSecond();
  555.  
  556.     // pickPt is the pt that the mouse is currently on in image coordinates
  557.     pickPt = add(pickPt,pickVec);
  558.  
  559.     // are we on the top or bottom of the image
  560.     NumberBvr zoomSpeed = sign(pickPt.getY());
  561.          
  562.         return zoomSpeed;
  563.   }
  564.  
  565. }
  566.  
  567.  
  568. class LabelImage extends Statics {
  569.   ModifiableBehavior sw = null;
  570.  
  571.   LabelImage() {
  572.     sw = new ModifiableBehavior(emptyImage);
  573.   }
  574.   
  575.   ImageBvr getBvr() {
  576.     return (ImageBvr)sw.getBvr();
  577.   }
  578.  
  579.  
  580.   void switchText(String text) {
  581.         FontStyleBvr fs = defaultFont.bold().color(white);
  582.     if (text == null) {
  583.       sw.switchTo(emptyImage);
  584.     } else {
  585.       ImageBvr textIm = textImage(text,fs).transform(scale2(2));
  586.       Bbox2Bvr bbox = textIm.boundingBox();
  587.  
  588.       ImageBvr background = solidColorImage(black)
  589.                 .crop(bbox.getMin(), bbox.getMax()).opacity(.5).
  590.         transform(scale(2,1));
  591.       sw.switchTo(overlay(textIm,background));
  592.     }
  593.  
  594.   }
  595. }
  596.