1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2 * license. See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3 * full text of the license. */
6 * Class: OpenLayers.Renderer
7 * This is the base class for all renderers.
9 * This is based on a merger code written by Paul Spencer and Bertil Chapuis.
10 * It is largely composed of virtual functions that are to be implemented
11 * in technology-specific subclasses, but there is some generic code too.
13 * The functions that *are* implemented here merely deal with the maintenance
14 * of the size and extent variables, as well as the cached 'resolution'
17 * A note to the user that all subclasses should use getResolution() instead
18 * of directly accessing this.resolution in order to correctly use the
22 OpenLayers.Renderer = OpenLayers.Class({
38 * {<OpenLayers.Bounds>}
44 * {Boolean} If the renderer is currently in a state where many things
45 * are changing, the 'locked' property is set to true. This means
46 * that renderers can expect at least one more drawFeature event to be
47 * called with the 'locked' property set to 'true': In some renderers,
48 * this might make sense to use as a 'only update local information'
60 * Property: resolution
61 * {Float} cache of current map resolution
67 * {<OpenLayers.Map>} Reference to the map -- this is set in Vector's setMap()
72 * Constructor: OpenLayers.Renderer
75 * containerID - {<String>}
76 * options - {Object} options for this renderer. See sublcasses for
79 initialize: function(containerID, options) {
80 this.container = OpenLayers.Util.getElement(containerID);
87 this.container = null;
90 this.resolution = null;
95 * APIMethod: supported
96 * This should be overridden by specific subclasses
99 * {Boolean} Whether or not the browser supports the renderer class
101 supported: function() {
107 * Set the visible part of the layer.
109 * Resolution has probably changed, so we nullify the resolution
110 * cache (this.resolution) -- this way it will be re-computed when
112 * We nullify the resolution cache (this.resolution) if resolutionChanged
113 * is set to true - this way it will be re-computed on the next
114 * getResolution() request.
117 * extent - {<OpenLayers.Bounds>}
118 * resolutionChanged - {Boolean}
120 setExtent: function(extent, resolutionChanged) {
121 this.extent = extent.clone();
122 if (resolutionChanged) {
123 this.resolution = null;
129 * Sets the size of the drawing surface.
131 * Resolution has probably changed, so we nullify the resolution
132 * cache (this.resolution) -- this way it will be re-computed when
136 * size - {<OpenLayers.Size>}
138 setSize: function(size) {
139 this.size = size.clone();
140 this.resolution = null;
144 * Method: getResolution
145 * Uses cached copy of resolution if available to minimize computing
148 * The current map's resolution
150 getResolution: function() {
151 this.resolution = this.resolution || this.map.getResolution();
152 return this.resolution;
156 * Method: drawFeature
157 * Draw the feature. The optional style argument can be used
158 * to override the feature's own style. This method should only
159 * be called from layer.drawFeature().
162 * feature - {<OpenLayers.Feature.Vector>}
166 * {Boolean} true if the feature has been drawn completely, false if not,
167 * undefined if the feature had no geometry
169 drawFeature: function(feature, style) {
171 style = feature.style;
173 if (feature.geometry) {
174 var bounds = feature.geometry.getBounds();
176 if (!bounds.intersectsBounds(this.extent)) {
177 style = {display: "none"};
179 var rendered = this.drawGeometry(feature.geometry, style, feature.id);
180 if(style.display != "none" && style.label && rendered !== false) {
181 this.drawText(feature.id, style, feature.geometry.getCentroid());
183 this.removeText(feature.id);
192 * Method: drawGeometry
194 * Draw a geometry. This should only be called from the renderer itself.
195 * Use layer.drawFeature() from outside the renderer.
199 * geometry - {<OpenLayers.Geometry>}
201 * featureId - {<String>}
203 drawGeometry: function(geometry, style, featureId) {},
207 * Function for drawing text labels.
208 * This method is only called by the renderer itself.
211 * featureId - {String}
213 * location - {<OpenLayers.Geometry.Point>}
215 drawText: function(featureId, style, location) {},
219 * Function for removing text labels.
220 * This method is only called by the renderer itself.
223 * featureId - {String}
225 removeText: function(featureId) {},
229 * Clear all vectors from the renderer.
232 clear: function() {},
235 * Method: getFeatureIdFromEvent
236 * Returns a feature id from an event on the renderer.
237 * How this happens is specific to the renderer. This should be
238 * called from layer.getFeatureFromEvent().
242 * evt - {<OpenLayers.Event>}
245 * {String} A feature id or null.
247 getFeatureIdFromEvent: function(evt) {},
250 * Method: eraseFeatures
251 * This is called by the layer to erase features
254 * features - {Array(<OpenLayers.Feature.Vector>)}
256 eraseFeatures: function(features) {
257 if(!(features instanceof Array)) {
258 features = [features];
260 for(var i=0, len=features.length; i<len; ++i) {
261 this.eraseGeometry(features[i].geometry);
262 this.removeText(features[i].id);
267 * Method: eraseGeometry
268 * Remove a geometry from the renderer (by id).
272 * geometry - {<OpenLayers.Geometry>}
274 eraseGeometry: function(geometry) {},
278 * moves this renderer's root to a (different) renderer.
279 * To be implemented by subclasses that require a common renderer root for
283 * renderer - {<OpenLayers.Renderer>} target renderer for the moved root
285 moveRoot: function(renderer) {},
288 * Method: getRenderLayerId
289 * Gets the layer that this renderer's output appears on. If moveRoot was
290 * used, this will be different from the id of the layer containing the
291 * features rendered by this renderer.
294 * {String} the id of the output layer.
296 getRenderLayerId: function() {
297 return this.container.id;
300 CLASS_NAME: "OpenLayers.Renderer"