home *** CD-ROM | disk | FTP | other *** search
/ Boot Disc 13 / boot-disc-1997-09.iso / HyprWire / DATA.Z / WavyPlugIn.java < prev    next >
Text File  |  1997-01-31  |  7KB  |  420 lines

  1. package kinetix.hyperc1.userPlugIns;
  2.  
  3. /********************************************************************************\
  4. **                                                                              **
  5. **  (C) Copyright 1997 by Autodesk, Inc.                                        **
  6. **                                                                              **
  7. **  This program is copyrighted by Autodesk, Inc. and is licensed to you under  **
  8. **  the following conditions.  You may not distribute or publish the source     **
  9. **  code of this program in any form.  You may incorporate this code in object  **
  10. **  form in derivative works provided such derivative works are (i.) are de-    **
  11. **  signed and intended to work solely with Autodesk, Inc. products, and (ii.)  **
  12. **  contain Autodesk's copyright notice "(C) Copyright 1997 by Autodesk, Inc."  **
  13. **                                                                              **
  14. **  AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  AUTODESK SPE-  **
  15. **  CIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR  **
  16. **  A PARTICULAR USE.  AUTODESK, INC.  DOES NOT WARRANT THAT THE OPERATION OF   **
  17. **  THE PROGRAM WILL BE UNINTERRUPTED OR ERROR FREE.                            **
  18. **                                                                              **
  19. \********************************************************************************/
  20.  
  21. /**************************************************************************
  22. WavyPlugIn:  Samply PlugIn using piDraw()
  23.  
  24. Author:  Amalgamated YoYoDyne PlugIns, Inc.(Hyperwire demonstration file)
  25.  
  26. Version:  1.0
  27.  
  28. **************************************************************************/
  29.  
  30.  
  31. import java.applet.Applet;
  32.  
  33. import kinetix.hyperc1.runtime.*;
  34.  
  35. import java.awt.*;
  36.  
  37.  
  38. /*** Object Class ***/
  39.  
  40. public class WavyPlugIn extends HwVisualUserPlugIn
  41.  
  42.     {
  43.  
  44.     private Point cp[] = new Point[4];
  45.  
  46.     private Point vel[] = new Point[4];
  47.  
  48.     private int velocityMultiplier = 10;
  49.  
  50.     private String text = "KINETIX";
  51.     private int fontSize = 18;
  52.  
  53.     private long prevTime = -1;
  54.  
  55.     private Rectangle[] constrainR = new Rectangle[4];
  56.  
  57.     private Font myFont;
  58.  
  59.     private Color textColor = Color.black;
  60.  
  61.     private Point dropShadowOffset = new Point(3, 3);
  62.  
  63.     private boolean useShadow = true;
  64.  
  65.  
  66.  
  67.     private static final double MAX_VEL = 8.0;
  68.  
  69.  
  70.     /* Funtion to generate a random velocity */
  71.  
  72.     private final Point randomVel()
  73.  
  74.         {
  75.  
  76.         return new Point((int) (Math.random() * MAX_VEL), (int) (Math.random() * MAX_VEL));
  77.  
  78.         }
  79.  
  80.  
  81.  
  82.     /* Called at applet initialization */
  83.  
  84.     public void piEventAppletInit(Applet theApplet) throws HwException
  85.  
  86.         {
  87.  
  88.         vel[0] = randomVel();
  89.  
  90.         vel[1] = randomVel();
  91.  
  92.         vel[2] = randomVel();
  93.  
  94.         vel[3] = randomVel();
  95.  
  96.  
  97.         /* register this plugin with the refreshdaemon */
  98.  
  99.         mRunService.siPlugInWantsTicks(true);
  100.  
  101.  
  102.  
  103.         Rectangle r = ((VisualRunService) mRunService).siGetAbsRect();
  104.  
  105.         int d = r.width / 5;
  106.  
  107.         int yc = r.height / 2;
  108.  
  109.         int w4 = r.width / 4;
  110.  
  111.         cp[0] = new Point(d, yc);
  112.  
  113.         cp[1] = new Point(d * 2, yc);
  114.  
  115.         cp[2] = new Point(d * 3, yc);
  116.  
  117.         cp[3] = new Point(d * 4, yc);
  118.  
  119.  
  120.  
  121.         myFont = new Font("System",Font.BOLD, fontSize);
  122.  
  123.         FontMetrics fontMetrics = theApplet.getGraphics().getFontMetrics( myFont );
  124.  
  125.         int charHeight = fontMetrics.getHeight();
  126.  
  127.         int rh = r.height - (2 * charHeight);
  128.  
  129.         constrainR[0] = new Rectangle(0, charHeight, w4, rh);
  130.  
  131.         constrainR[1] = new Rectangle(w4, charHeight, w4 * 2, rh);
  132.  
  133.         constrainR[2] = new Rectangle(w4, charHeight, w4 * 2, rh);
  134.  
  135.         constrainR[3] = new Rectangle(w4 * 3, charHeight, w4 - charHeight, rh);
  136.  
  137.         }
  138.  
  139.  
  140.  
  141.     /* compute point on curve */
  142.  
  143.     private final Point getBezierPT(Point v[], double t)
  144.  
  145.         {
  146.  
  147.         int     i, j;        
  148.  
  149.         Point    q;
  150.  
  151.         Point vTemp[];
  152.  
  153.  
  154.  
  155.         vTemp = new Point[4];
  156.  
  157.         for (i = 0; i < 4; i++)
  158.  
  159.             vTemp[i] = new Point(v[i].x, v[i].y);
  160.  
  161.  
  162.  
  163.         for (i = 1; i < 4; i++)
  164.  
  165.             {    
  166.  
  167.             for (j = 0; j <= (3-i); j++)
  168.  
  169.                 {
  170.  
  171.                 vTemp[j].x = (int) ((1.0 - t) * vTemp[j].x + t * vTemp[j + 1].x);
  172.  
  173.                 vTemp[j].y = (int) ((1.0 - t) * vTemp[j].y + t * vTemp[j + 1].y);
  174.  
  175.                 }
  176.  
  177.             }
  178.  
  179.  
  180.  
  181.         q = vTemp[0];
  182.  
  183.  
  184.  
  185.         return q;
  186.  
  187.         }
  188.  
  189.  
  190.  
  191.     /* called by piDraw() to draw text */
  192.  
  193.     private final void drawTextOnBez(String text, Point v[], int ox, int oy, Graphics g, Point dropOffset)
  194.  
  195.         {
  196.  
  197.         int nChar = text.length();
  198.  
  199.         double t;
  200.  
  201.         Point pos;
  202.  
  203.  
  204.  
  205.         g.setFont(myFont);
  206.  
  207.         for (int i = 0; i < nChar; i++)
  208.  
  209.             {
  210.  
  211.             t = ((double) i / ((double) (nChar - 1)));
  212.  
  213.             pos = getBezierPT(v, t);
  214.  
  215.             if (useShadow)
  216.  
  217.                 {
  218.  
  219.                 Color oldColor = g.getColor();
  220.  
  221.                 g.setColor(Color.black);
  222.  
  223.                 g.drawString(text.substring(i, i + 1), pos.x + ox + dropOffset.x, pos.y + oy + dropOffset.y);
  224.  
  225.                 g.setColor(oldColor);
  226.  
  227.                 }
  228.  
  229.             g.drawString(text.substring(i, i + 1), pos.x + ox, pos.y + oy);
  230.  
  231.             }
  232.  
  233.         }
  234.  
  235.  
  236.  
  237.     /* called by runtime when plugin needs to be redrawn */
  238.  
  239.     public void piDraw(Graphics g, Rectangle aClipRect)
  240.  
  241.             throws HwException
  242.  
  243.         {
  244.  
  245.         Rectangle r = ((VisualRunService) mRunService).siGetAbsRect();
  246.  
  247.         g.setColor(textColor);
  248.  
  249.         drawTextOnBez(text, cp, r.x, r.y, g, dropShadowOffset);
  250.  
  251.         }
  252.  
  253.  
  254.  
  255.     /* keep text to bounding rect */
  256.  
  257.     private final void constrainToBox(Point p, Point v, Rectangle r)
  258.  
  259.         {
  260.  
  261.         if (p.x < r.x)
  262.  
  263.             {
  264.  
  265.             p.x = r.x;
  266.  
  267.             v.x = -v.x;
  268.  
  269.             }
  270.  
  271.         else if (p.x >= (r.x + r.width))
  272.  
  273.             {
  274.  
  275.             p.x = (r.x + r.width) - 1;
  276.  
  277.             v.x = -v.x;
  278.  
  279.             }
  280.  
  281.         if (p.y < r.y)
  282.  
  283.             {
  284.  
  285.             p.y = r.y;
  286.  
  287.             v.y = -v.y;
  288.  
  289.             }
  290.  
  291.         else if (p.y >= (r.y + r.height))
  292.  
  293.             {
  294.  
  295.             p.y = (r.y + r.height) - 1;
  296.  
  297.             v.y = -v.y;
  298.  
  299.             }
  300.  
  301.         }
  302.  
  303.  
  304.     /* called when this plugin receives a timer tick */
  305.  
  306.     public void piEventRefreshDaemonTick(long timeNow, RefreshDaemon worldMover ) 
  307.  
  308.             throws HwException
  309.  
  310.         {
  311.  
  312.         if (prevTime < 0)
  313.  
  314.             prevTime = timeNow;
  315.  
  316.  
  317.  
  318.         long deltaTime = timeNow - prevTime;
  319.  
  320.         prevTime = timeNow;
  321.  
  322.  
  323.  
  324.         int dV = (int) ( deltaTime * velocityMultiplier );
  325.  
  326.         Rectangle newRect = ((VisualRunService) mRunService).siGetAbsRect();
  327.  
  328.         Point rSize = new Point(newRect.width, newRect.height);
  329.  
  330.  
  331.  
  332.         for (int i = 0; i < 4; i++)
  333.  
  334.             {
  335.  
  336.             cp[i].x += (vel[i].x * dV) / 1000;
  337.  
  338.             cp[i].y += (vel[i].y * dV) / 1000;
  339.  
  340.             constrainToBox(cp[i], vel[i], constrainR[i]);
  341.  
  342.             }
  343.  
  344.  
  345.  
  346.          worldMover.invalidateRect(newRect.x, newRect.y, newRect.width, newRect.height);
  347.  
  348.         }
  349.  
  350.  
  351.     /* Properties Interface */
  352.  
  353.     public void fiSetSpeed(int i)
  354.  
  355.         {
  356.  
  357.         velocityMultiplier = i;
  358.  
  359.         }
  360.  
  361.  
  362.  
  363.     public void fiSetText(String s)
  364.  
  365.         {
  366.  
  367.         text = s;
  368.  
  369.         }
  370.  
  371.  
  372.  
  373.     public void fiSetColor(Color c)
  374.  
  375.         {
  376.  
  377.         textColor = c;
  378.  
  379.         }
  380.  
  381.  
  382.  
  383.     public void fiSetUseShadow(boolean b)
  384.  
  385.         {
  386.  
  387.         useShadow = b;
  388.  
  389.         }
  390.  
  391.  
  392.  
  393.     public void fiSetShadowOffset(Point p)
  394.  
  395.         {
  396.  
  397.         dropShadowOffset = p;
  398.  
  399.         }
  400.  
  401.  
  402.     public void fiSetFontSize(int i)
  403.         {
  404.         fontSize = i;
  405.         }
  406.  
  407.  
  408.     /* Wire Interface */
  409.  
  410.     public void wiSetText(String s)
  411.  
  412.         {
  413.  
  414.         text = new String(s);
  415.  
  416.         }
  417.  
  418.     }
  419.  
  420.