GenericItem.java
Go to the documentation of this file.
00001 /*
00002  * Created on Oct 19, 2009
00003  *
00004  * TODO To change the template for this generated file go to
00005  * Window - Preferences - Java - Code Style - Code Templates
00006  */
00007 package edu.tum.cs.ias.knowrob.vis.items;
00008 
00009 import java.util.Vector;
00010 
00011 import edu.tum.cs.ias.knowrob.vis.Canvas;
00012 import edu.tum.cs.ias.knowrob.vis.Drawable;
00013 import edu.tum.cs.ias.knowrob.vis.DrawableAnimated;
00014 import edu.tum.cs.ias.knowrob.vis.items.transform.DrawingTransformation;
00015 
00022 public abstract class GenericItem<T extends DrawingTransformation> implements Drawable, DrawableAnimated {
00023         
00024         protected Vector<T> animation = new Vector<T>();
00025         
00026         public GenericItem(T transform) {
00027                 animation.add(transform);
00028         }
00029         
00030         public boolean isAnimated() {
00031                 return animation.size() > 0;
00032         }
00033         
00034         public void draw(Canvas c) {
00035                 draw(c, 0);
00036         }
00037         
00038         public void draw(Canvas c, int step) {
00039                 T currentTransform; 
00040                 try {
00041                         currentTransform = animation.get(step);
00042                 }
00043                 catch(ArrayIndexOutOfBoundsException e) {
00044                         currentTransform = animation.lastElement();
00045                 }
00046                 c.pushMatrix();
00047                 currentTransform.applyTransformation(c);
00048                 drawActualItem(c);
00049                 c.popMatrix();
00050         }
00051         
00052         public int getMaxStep() {
00053                 return animation.size()-1;
00054         }
00055 
00056         protected abstract void drawActualItem(Canvas c);       
00057         
00058         public void setFrame(int frame, T transformation) {
00059                 setFrames(frame, frame, transformation);
00060         }
00061         
00068         public void setFrames(int from, int to, T transformation) {
00069                 int lastFrame = animation.size()-1;
00070                 if(to == -1)
00071                         to = Math.max(to, lastFrame);
00072                 makeAnimated(from-1);           
00073                 if(lastFrame < from) 
00074                         animation.add(transformation);
00075                 else {
00076                         int resetTo = Math.min(lastFrame, to);
00077                         for(int i = from; i < resetTo; i++)
00078                                 animation.set(i, transformation);
00079                 }
00080                 makeAnimated(to);
00081         }
00082         
00089         public void setFrames(int from, int to, PartialTransformationReplacer<T> ptr) {
00090                 if(to == -1) 
00091                         to = Math.max(from, animation.size()-1);
00092                 makeAnimated(to);
00093                 
00094                 T prev = null;
00095                 for(int i = from; i <= to; i++) {                       
00096                         T newT = ptr.replace(animation.get(i));                 
00097                         if(prev != null && newT.equals(prev))
00098                                 animation.set(i, prev);
00099                         else {
00100                                 animation.set(i, newT);
00101                                 prev = newT;
00102                         }
00103                 }
00104         }
00105         
00110         public static abstract class PartialTransformationReplacer<T extends DrawingTransformation> {
00111                 T replacement;
00112 
00113                 public PartialTransformationReplacer(T replacement) {
00114                         this.replacement = replacement;
00115                 }
00116 
00123                 public abstract T replace(T before);
00124         }
00125         
00131         protected void makeAnimated(int untilFrame) {
00132                 if(untilFrame < animation.size())
00133                         return;
00134                 T transform = this.animation.lastElement();
00135                 for(int i = animation.size()-1; i <= untilFrame; i++)
00136                         animation.add(transform);
00137         }
00138 }


mod_vis
Author(s): Moritz Tenorth, Jakob Engel
autogenerated on Mon Oct 6 2014 01:30:00