Update to Worldwind release 0.4.1
[worldwind-tracker.git] / gov / nasa / worldwind / layers / RenderableLayer.java
blob9f2af0bb8829ca07e6061f95f8df9ca465b054a9
1 /*
2 Copyright (C) 2001, 2006 United States Government
3 as represented by the Administrator of the
4 National Aeronautics and Space Administration.
5 All Rights Reserved.
6 */
7 package gov.nasa.worldwind.layers;
9 import gov.nasa.worldwind.*;
10 import gov.nasa.worldwind.pick.PickSupport;
11 import gov.nasa.worldwind.render.*;
12 import gov.nasa.worldwind.util.Logging;
14 import javax.media.opengl.GL;
15 import java.awt.*;
16 import java.util.*;
18 /**
19 * @author tag
20 * @version $Id: RenderableLayer.java 3044 2007-09-26 02:08:22Z tgaskins $
22 public class RenderableLayer extends AbstractLayer
24 private Collection<Renderable> renderables = new ArrayList<Renderable>();
25 private final PickSupport pickSupport = new PickSupport();
26 private final Layer delegateOwner;
28 public RenderableLayer()
30 this.delegateOwner = null;
33 public RenderableLayer(Layer delegateOwner)
35 this.delegateOwner = delegateOwner;
38 public void addRenderables(Iterable<Renderable> shapeIterator)
40 this.renderables = new ArrayList<Renderable>();
42 if (shapeIterator == null)
43 return;
45 for (Renderable renderable : shapeIterator)
47 this.renderables.add(renderable);
51 public void addRenderable(Renderable renderable)
53 if (renderable == null)
55 String msg = Logging.getMessage("nullValue.Shape");
56 Logging.logger().severe(msg);
57 throw new IllegalArgumentException(msg);
60 this.renderables.add(renderable);
63 private void clearRenderables()
65 if (this.renderables != null && this.renderables.size() > 0)
67 for (Renderable renderable : this.renderables)
69 if (renderable instanceof Disposable)
70 ((Disposable) renderable).dispose();
72 this.renderables.clear();
76 public void setRenderables(Iterable<Renderable> shapeIterator)
78 this.renderables = new ArrayList<Renderable>();
80 this.clearRenderables();
81 if (shapeIterator == null)
82 this.addRenderables(shapeIterator);
85 public void setRenderable(final Renderable renderable)
87 this.clearRenderables();
88 if (renderable != null)
89 this.addRenderable(renderable);
92 public void removeRenderable(Renderable renderable)
94 if (renderable == null)
96 String msg = Logging.getMessage("nullValue.Shape");
97 Logging.logger().severe(msg);
98 throw new IllegalArgumentException(msg);
101 this.renderables.remove(renderable);
104 public Collection<Renderable> getRenderables()
106 return this.renderables;
109 public void dispose()
111 for (Renderable renderable : this.renderables)
113 if (renderable instanceof Disposable)
114 ((Disposable) renderable).dispose();
118 @Override
119 protected void doPick(DrawContext dc, java.awt.Point pickPoint)
121 this.pickSupport.clearPickList();
122 this.pickSupport.beginPicking(dc);
124 for (Renderable renderable : this.renderables)
126 float[] inColor = new float[4];
127 dc.getGL().glGetFloatv(GL.GL_CURRENT_COLOR, inColor, 0);
128 Color color = dc.getUniquePickColor();
129 dc.getGL().glColor3ub((byte) color.getRed(), (byte) color.getGreen(), (byte) color.getBlue());
131 renderable.render(dc);
133 dc.getGL().glColor4fv(inColor, 0);
135 if (renderable instanceof Locatable)
137 this.pickSupport.addPickableObject(color.getRGB(), renderable,
138 ((Locatable) renderable).getPosition(), false);
140 else
142 this.pickSupport.addPickableObject(color.getRGB(), renderable);
146 this.pickSupport.resolvePick(dc, pickPoint, this.delegateOwner != null ? this.delegateOwner : this);
147 this.pickSupport.endPicking(dc);
150 @Override
151 protected void doRender(DrawContext dc)
153 for (Renderable renderable : this.renderables)
155 renderable.render(dc);
159 public Layer getDelegateOwner()
161 return delegateOwner;
164 @Override
165 public String toString()
167 return Logging.getMessage("layers.RenderableLayer.Name");