View Javadoc

1   /*
2    * To change this template, choose Tools | Templates
3    * and open the template in the editor.
4    */
5   
6   package nl.tudelft.goal.ut2004.visualizer.timeline.map;
7   
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.HashSet;
11  import java.util.LinkedList;
12  import java.util.List;
13  import java.util.Set;
14  import javax.media.opengl.GL;
15  
16  import nl.tudelft.goal.ut2004.visualizer.map.BlendTriangle;
17  
18  
19  
20  /**
21   * Basically list of {@link ISubGLRenderer}s with stuff for manipulating it
22   * and rendering it.
23   *
24   * Holds order of subrenderers.
25   *
26   * @param <T>  Type of object subrenderers of this collection draws.
27   * @author Honza
28   */
29  public class GLRendererCollection<T> implements ISubGLRenderer<GLRendererCollection<T>> {
30      
31      private List<ISubGLRenderer<T>> renderers = Collections.synchronizedList(new ArrayList<ISubGLRenderer<T>>());
32  
33      /**
34       * Add renderer to collection. It will be rendered after all previous renders are drawn.
35       * @param subrenderer
36       */
37      public synchronized void addSubRenderer(ISubGLRenderer<T> subrenderer) {
38          renderers.add(subrenderer);
39      }
40  
41      /**
42       * Remove subrenderer from collection
43       * @param subrenderer renderer to be removed
44       * @return true if subrenderes was in collection
45       */
46      public synchronized boolean removeSubRenderer(ISubGLRenderer<T> subrenderer) {
47          return renderers.remove(subrenderer);
48      }
49      
50      /**
51       * List of subrenderes.
52       * @return Unmodifiable list of subrenderes.
53       */
54      public synchronized List<ISubGLRenderer<T>> getSubRenderes() {
55          return Collections.unmodifiableList(new ArrayList<ISubGLRenderer<T>>(renderers));
56      }
57  
58      /**
59       * Return list of renderers that draw passed object
60       * @return list of renderers with object o, empty list if no found
61       */
62      public synchronized List<ISubGLRenderer<T>> getRenderersOf(T o) {
63          ArrayList<ISubGLRenderer<T>> result = new ArrayList<ISubGLRenderer<T>>();
64  
65          for (ISubGLRenderer<T> renderer : renderers) {
66              if (renderer.getObject() == o) {
67                  result.add(renderer);
68              }
69          }
70          return result;
71      }
72  
73      /**
74       * Remove all renderers that draw object o.
75       * @param o object that may have renderers in collection we want to remove
76       */
77      public synchronized void removeRenderersOf(T o) {
78          List<ISubGLRenderer<T>> renderersOf = getRenderersOf(o);
79          for (ISubGLRenderer<T> renderer : renderersOf) {
80              removeSubRenderer(renderer);
81          }
82      }
83  
84      /**
85       * Get set of all objects this collection draws.
86       * @return all objects this collection draws
87       */
88      public synchronized Set<T> getDrawnObjects() {
89          HashSet<T> set = new HashSet<T>();
90          for (ISubGLRenderer<T> renderer : renderers) {
91              set.add(renderer.getObject());
92          }
93          return set;
94      }
95  
96      @Override
97      public synchronized void render(GL gl) {
98          ISubGLRenderer[] renderersArray = renderers.toArray(new ISubGLRenderer[0]);
99          for (ISubGLRenderer subrenderer : renderersArray) {
100             subrenderer.render(gl);
101         }
102     }
103 
104     @Override
105     public GLRendererCollection<T> getObject() {
106         return this;
107     }
108 
109 
110     /**
111      * Prepare all renderers for rendering
112      * @param gl
113      */
114     @Override
115     public synchronized void prepare(GL gl) {
116         for (ISubGLRenderer<T> renderer : renderers) {
117             renderer.prepare(gl);
118         }
119     }
120 
121     @Override
122     public synchronized List<BlendTriangle> getBlendedTris() {
123         List<BlendTriangle> list = new LinkedList<BlendTriangle>();
124         ISubGLRenderer[] renderersArray = renderers.toArray(new ISubGLRenderer[0]);
125 
126         for (ISubGLRenderer subrenderer : renderersArray) {
127             list.addAll(subrenderer.getBlendedTris());
128         }
129         
130         return list;
131     }
132 }