Back to index

wims  3.65+svn20090927
Tracker.java
Go to the documentation of this file.
00001 package rene.zirkel.tools;
00002 
00003 // file: Tracker.java
00004 
00005 import java.awt.event.MouseEvent;
00006 import java.util.Enumeration;
00007 import java.util.Vector;
00008 
00009 import rene.util.xml.XmlWriter;
00010 import rene.zirkel.Zirkel;
00011 import rene.zirkel.ZirkelCanvas;
00012 import rene.zirkel.constructors.*;
00013 import rene.zirkel.graphics.*;
00014 import rene.zirkel.objects.*;
00015 import rene.zirkel.structures.Coordinates;
00016 
00017 public class Tracker extends ObjectConstructor
00018        implements TrackPainter
00019 {      ConstructionObject PM;
00020        int PMax=8,PN;
00021        ConstructionObject P;
00022        ConstructionObject PO[]=new ConstructionObject[PMax];
00023        Vector V=new Vector();
00024        Vector VO[]=new Vector[PMax];
00025        double X,Y,DX,DY;
00026        double XO[]=new double[PMax],YO[]=new double[PMax],
00027               DXO[]=new double[PMax],DYO[]=new double[PMax];
00028        boolean Started;
00029        boolean StartedO[]=new boolean[PMax];
00030        boolean Other;
00031 
00032        public Tracker (ConstructionObject p, ConstructionObject po[])
00033        {      super();
00034               P=p;
00035               PN=0;
00036               for (int i=0; i<po.length; i++)
00037               {      if (i>=PMax || po[i]==null) break;
00038                      PO[PN]=po[i];
00039                      VO[i]=new Vector();
00040                      PN++;
00041               }
00042        }
00043        
00044        public Tracker ()
00045        {      super();
00046        }
00047 
00048        public void mousePressed (MouseEvent e, ZirkelCanvas zc)
00049        {      double x=zc.x(e.getX()),y=zc.y(e.getY());
00050               if (P==null)
00051               {      P=zc.selectPoint(e.getX(),e.getY());
00052                      if (P==null) P=zc.selectLine(e.getX(),e.getY());
00053                      if (P==null) return;
00054                      P.setSelected(true);
00055                      zc.repaint();
00056                      if (e.isShiftDown()) Other=true;
00057                      else Other=false;
00058                      showStatus(zc);
00059               }
00060               else if (Other)
00061               {      ConstructionObject P=zc.selectPoint(e.getX(),e.getY());
00062                      if (P==null) P=zc.selectLine(e.getX(),e.getY());
00063                      if (P!=null)
00064                      {      P.setSelected(true);
00065                             zc.repaint();
00066                             PO[PN]=P;
00067                             VO[PN]=new Vector();
00068                             PN++;
00069                      }
00070                      if (!e.isShiftDown() || PN>=PMax) Other=false;
00071                      showStatus(zc);
00072               }             
00073               else
00074               {      ConstructionObject pm=zc.selectMoveableObject(e.getX(),e.getY());
00075                      PM=pm;
00076                      if (PM!=null)
00077                      {      zc.clearSelected();
00078                             PM.setSelected(true);
00079                             ((MoveableObject)PM).startDrag(x,y);
00080                             zc.repaint();
00081                             showStatus(zc);
00082                      }
00083                      Started=false;
00084               }
00085        }
00086 
00087        public void mouseMoved (MouseEvent e, ZirkelCanvas zc, boolean simple)
00088        {      if (P==null || Other)
00089                      zc.indicatePointsOrLines(e.getX(),e.getY());
00090               else if (PM==null)
00091                      zc.indicateMoveableObjects(e.getX(),e.getY());
00092               else
00093                      zc.clearIndicated();
00094        }
00095 
00096        public void mouseDragged (MouseEvent e, ZirkelCanvas zc)
00097        {      if (PM==null) return;
00098               ((MoveableObject)PM).dragTo(zc.x(e.getX()),zc.y(e.getY()));
00099               zc.validate(); track(zc); zc.repaint();
00100        }
00101 
00102        public void mouseReleased (MouseEvent e, ZirkelCanvas zc)
00103        {      if (PM==null) return;
00104               PM.setSelected(false);
00105               PM=null;
00106               zc.repaint();
00107               showStatus(zc);
00108        }
00109 
00110        public void reset (ZirkelCanvas zc)
00111        {      zc.clearSelected();
00112               P=PM=null; PN=0;
00113               V=new Vector(); 
00114               showStatus(zc); zc.repaint();
00115        }
00116 
00117        public void showStatus (ZirkelCanvas zc)
00118        {      if (P==null || Other) zc.showStatus(
00119                      Zirkel.name("message.tracker.select"));
00120               else if (PM==null) zc.showStatus(
00121                      Zirkel.name("message.tracker.selectpoint"));
00122               else zc.showStatus(
00123                      Zirkel.name("message.tracker.move"));
00124        }
00125 
00126        public void track (ZirkelCanvas zc)
00127        {      if (P==null) return;
00128               if (P instanceof PointObject)
00129               {      PointObject p=(PointObject)P;
00130                      if (p.valid())
00131                             V.addElement(new Coordinates(p.getX(),p.getY()));
00132               }
00133               else if (P instanceof PrimitiveLineObject)
00134               {      PrimitiveLineObject L=(PrimitiveLineObject)P;
00135                      if (L.valid())
00136                      {      if (!Started)
00137                             {      X=L.getX(); Y=L.getY(); DX=L.getDX(); DY=L.getDY();
00138                                    Started=true;
00139                             }
00140                             else
00141                             {      double x,y,dx,dy;
00142                                    x=L.getX(); y=L.getY(); dx=L.getDX(); dy=L.getDY();
00143                                    double det=dx*DY-dy*DX;
00144                                    if (Math.sqrt(Math.abs(det))>1e-9)
00145                                    {      double a=(-(X-x)*DY+DX*(Y-y))/(-det);
00146                                           V.addElement(new Coordinates(x+a*dx,y+a*dy));
00147                                    }
00148                                    X=x; Y=y; DX=dx; DY=dy;
00149                             }
00150                      }
00151               }
00152               for (int i=0; i<PN; i++)
00153               {      if (PO[i]==null || !PO[i].valid()) continue;
00154                      if (PO[i] instanceof PointObject)
00155                      {      PointObject p=(PointObject)PO[i];
00156                             VO[i].addElement(new Coordinates(p.getX(),p.getY()));
00157                      }
00158                      else if (PO[i] instanceof PrimitiveLineObject)
00159                      {      PrimitiveLineObject L=(PrimitiveLineObject)PO[i];
00160                             if (!StartedO[i])
00161                             {      XO[i]=L.getX(); YO[i]=L.getY();
00162                                    DXO[i]=L.getDX(); DYO[i]=L.getDY();
00163                                    StartedO[i]=true;
00164                             }
00165                             else
00166                             {      double x,y,dx,dy;
00167                                    x=L.getX(); y=L.getY(); dx=L.getDX(); dy=L.getDY();
00168                                    double det=dx*DYO[i]-dy*DXO[i];
00169                                    if (Math.sqrt(Math.abs(det))>1e-9)
00170                                    {      double a=(-(XO[i]-x)*DYO[i]+
00171                                                  DXO[i]*(YO[i]-y))/(-det);
00172                                           VO[i].addElement(new Coordinates(x+a*dx,y+a*dy));
00173                                    }
00174                                    XO[i]=x; YO[i]=y; DXO[i]=dx; DYO[i]=dy;
00175                             }
00176                      }
00177               }
00178        }
00179 
00180        public Enumeration elements ()
00181        {      return V.elements();
00182        }
00183 
00184        public void paint (MyGraphics g, ZirkelCanvas zc)
00185        {      if (P==null) return;
00186               Coordinates C;
00187               Enumeration e=V.elements();
00188               g.setColor(P);
00189               double c0,r0,c,r;
00190               int maxd=zc.width()/20;
00191               if (e.hasMoreElements())
00192               {      C=(Coordinates)e.nextElement();
00193                      c0=zc.col(C.X); r0=zc.row(C.Y);
00194                      while (e.hasMoreElements())
00195                      {      C=(Coordinates)e.nextElement();
00196                             c=zc.col(C.X); r=zc.row(C.Y);
00197                             if (Math.abs(c0-c)<maxd && Math.abs(r0-r)<maxd)
00198                                    g.drawLine(c0,r0,c,r,P);
00199                             else
00200                                    g.drawLine(c0,r0,c0,r0,P);
00201                             c0=c; r0=r;
00202                      }
00203               }
00204               for (int i=0; i<PN; i++)
00205               {      if (PO[i]==null) continue;
00206                      g.setColor(PO[i]);
00207                      e=VO[i].elements();
00208                      if (e.hasMoreElements())
00209                      {      C=(Coordinates)e.nextElement();
00210                             c0=zc.col(C.X); r0=zc.row(C.Y);
00211                             while (e.hasMoreElements())
00212                             {      C=(Coordinates)e.nextElement();
00213                                    c=zc.col(C.X); r=zc.row(C.Y);
00214                                    if (Math.abs(c0-c)<maxd && Math.abs(r0-r)<maxd)
00215                                           g.drawLine(c0,r0,c,r,PO[i]);
00216                                    else
00217                                           g.drawLine(c0,r0,c0,r0,PO[i]);
00218                                    c0=c; r0=r;
00219                             }
00220                      }
00221               }
00222        }
00223        public void validate (ZirkelCanvas zc)
00224        {
00225        }
00226 
00227        public void save (XmlWriter xml)
00228        {      if (P==null) return;
00229               xml.startTagStart("Track");
00230               xml.printArg("track",P.getName());
00231               for (int i=0; i<PN; i++)
00232               {      xml.printArg("track"+i,PO[i].getName());
00233               }
00234               if (PM!=null) xml.printArg("move",PM.getName());
00235               xml.finishTagNewLine();
00236        }
00237 
00238        public boolean useSmartBoard ()
00239        {      return false;
00240        }
00241 }