| 1 | /*
|
|---|
| 2 | * SVGDiagram.java
|
|---|
| 3 | *
|
|---|
| 4 | *
|
|---|
| 5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
|---|
| 6 | * Copyright (C) 2004 Mark McKay
|
|---|
| 7 | *
|
|---|
| 8 | * This library is free software; you can redistribute it and/or
|
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | * License as published by the Free Software Foundation; either
|
|---|
| 11 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This library is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * Lesser General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | * License along with this library; if not, write to the Free Software
|
|---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 21 | *
|
|---|
| 22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 23 | * projects can be found at http://www.kitfox.com
|
|---|
| 24 | *
|
|---|
| 25 | * Created on February 18, 2004, 5:04 PM
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | package com.kitfox.svg;
|
|---|
| 29 |
|
|---|
| 30 | import java.awt.Graphics2D;
|
|---|
| 31 | import java.awt.Rectangle;
|
|---|
| 32 | import java.awt.geom.AffineTransform;
|
|---|
| 33 | import java.awt.geom.Point2D;
|
|---|
| 34 | import java.awt.geom.Rectangle2D;
|
|---|
| 35 | import java.io.Serializable;
|
|---|
| 36 | import java.net.URI;
|
|---|
| 37 | import java.util.ArrayList;
|
|---|
| 38 | import java.util.HashMap;
|
|---|
| 39 | import java.util.List;
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Top level structure in an SVG tree.
|
|---|
| 44 | *
|
|---|
| 45 | * @author Mark McKay
|
|---|
| 46 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 47 | */
|
|---|
| 48 | public class SVGDiagram implements Serializable
|
|---|
| 49 | {
|
|---|
| 50 | public static final long serialVersionUID = 0;
|
|---|
| 51 |
|
|---|
| 52 | //Indexes elements within this SVG diagram
|
|---|
| 53 | final HashMap idMap = new HashMap();
|
|---|
| 54 |
|
|---|
| 55 | SVGRoot root;
|
|---|
| 56 | final SVGUniverse universe;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * This is used by the SVGRoot to determine the width of the
|
|---|
| 60 | */
|
|---|
| 61 | private Rectangle deviceViewport = new Rectangle(100, 100);
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * If true, no attempt will be made to discard geometry based on it being
|
|---|
| 65 | * out of bounds. This trades potentially drawing many out of bounds
|
|---|
| 66 | * shapes with having to recalculate bounding boxes every animation iteration.
|
|---|
| 67 | */
|
|---|
| 68 | protected boolean ignoreClipHeuristic = false;
|
|---|
| 69 |
|
|---|
| 70 | /**
|
|---|
| 71 | * URL which uniquely identifies this document
|
|---|
| 72 | */
|
|---|
| 73 | // final URI docRoot;
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * URI that uniquely identifies this document. Also used to resolve
|
|---|
| 77 | * relative urls. Default base for document.
|
|---|
| 78 | */
|
|---|
| 79 | final URI xmlBase;
|
|---|
| 80 |
|
|---|
| 81 | /** Creates a new instance of SVGDiagram */
|
|---|
| 82 | public SVGDiagram(URI xmlBase, SVGUniverse universe)
|
|---|
| 83 | {
|
|---|
| 84 | this.universe = universe;
|
|---|
| 85 | // this.docRoot = docRoot;
|
|---|
| 86 | this.xmlBase = xmlBase;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | /**
|
|---|
| 90 | * Draws this diagram to the passed graphics context
|
|---|
| 91 | */
|
|---|
| 92 | public void render(Graphics2D g) throws SVGException
|
|---|
| 93 | {
|
|---|
| 94 | root.render(g);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /**
|
|---|
| 98 | * Searches thorough the scene graph for all RenderableElements that have
|
|---|
| 99 | * shapes that contain the passed point.
|
|---|
| 100 | *
|
|---|
| 101 | * For every shape which contains the pick point, a List containing the
|
|---|
| 102 | * path to the node is added to the return list. That is, the result of
|
|---|
| 103 | * SVGElement.getPath() is added for each entry.
|
|---|
| 104 | *
|
|---|
| 105 | * @return the passed in list
|
|---|
| 106 | */
|
|---|
| 107 | public List pick(Point2D point, List retVec) throws SVGException
|
|---|
| 108 | {
|
|---|
| 109 | return pick(point, false, retVec);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | public List pick(Point2D point, boolean boundingBox, List retVec) throws SVGException
|
|---|
| 113 | {
|
|---|
| 114 | if (retVec == null)
|
|---|
| 115 | {
|
|---|
| 116 | retVec = new ArrayList();
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | root.pick(point, boundingBox, retVec);
|
|---|
| 120 |
|
|---|
| 121 | return retVec;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | public List pick(Rectangle2D pickArea, List retVec) throws SVGException
|
|---|
| 125 | {
|
|---|
| 126 | return pick(pickArea, false, retVec);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | public List pick(Rectangle2D pickArea, boolean boundingBox, List retVec) throws SVGException
|
|---|
| 130 | {
|
|---|
| 131 | if (retVec == null)
|
|---|
| 132 | {
|
|---|
| 133 | retVec = new ArrayList();
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | root.pick(pickArea, new AffineTransform(), boundingBox, retVec);
|
|---|
| 137 |
|
|---|
| 138 | return retVec;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public SVGUniverse getUniverse()
|
|---|
| 142 | {
|
|---|
| 143 | return universe;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | public URI getXMLBase()
|
|---|
| 147 | {
|
|---|
| 148 | return xmlBase;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // public URL getDocRoot()
|
|---|
| 152 | // {
|
|---|
| 153 | // return docRoot;
|
|---|
| 154 | // }
|
|---|
| 155 |
|
|---|
| 156 | public float getWidth()
|
|---|
| 157 | {
|
|---|
| 158 | if (root == null) return 0;
|
|---|
| 159 | return root.getDeviceWidth();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | public float getHeight()
|
|---|
| 163 | {
|
|---|
| 164 | if (root == null) return 0;
|
|---|
| 165 | return root.getDeviceHeight();
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | /**
|
|---|
| 169 | * Returns the viewing rectangle of this diagram in device coordinates.
|
|---|
| 170 | */
|
|---|
| 171 | public Rectangle2D getViewRect(Rectangle2D rect)
|
|---|
| 172 | {
|
|---|
| 173 | if (root != null) return root.getDeviceRect(rect);
|
|---|
| 174 | return rect;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | public Rectangle2D getViewRect()
|
|---|
| 178 | {
|
|---|
| 179 | return getViewRect(new Rectangle2D.Double());
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | public SVGElement getElement(String name)
|
|---|
| 183 | {
|
|---|
| 184 | return (SVGElement)idMap.get(name);
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | public void setElement(String name, SVGElement node)
|
|---|
| 188 | {
|
|---|
| 189 | idMap.put(name, node);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | public void removeElement(String name)
|
|---|
| 193 | {
|
|---|
| 194 | idMap.remove(name);
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | public SVGRoot getRoot()
|
|---|
| 198 | {
|
|---|
| 199 | return root;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | public void setRoot(SVGRoot root)
|
|---|
| 203 | {
|
|---|
| 204 | this.root = root;
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | public boolean ignoringClipHeuristic() { return ignoreClipHeuristic; }
|
|---|
| 208 |
|
|---|
| 209 | public void setIgnoringClipHeuristic(boolean ignoreClipHeuristic) { this.ignoreClipHeuristic = ignoreClipHeuristic; }
|
|---|
| 210 |
|
|---|
| 211 | /**
|
|---|
| 212 | * Updates all attributes in this diagram associated with a time event.
|
|---|
| 213 | * Ie, all attributes with track information.
|
|---|
| 214 | */
|
|---|
| 215 | public void updateTime(double curTime) throws SVGException
|
|---|
| 216 | {
|
|---|
| 217 | if (root == null) return;
|
|---|
| 218 | root.updateTime(curTime);
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | public Rectangle getDeviceViewport()
|
|---|
| 222 | {
|
|---|
| 223 | return deviceViewport;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * Sets the dimensions of the device being rendered into. This is used by
|
|---|
| 228 | * SVGRoot when its x, y, width or height parameters are specified as
|
|---|
| 229 | * percentages.
|
|---|
| 230 | */
|
|---|
| 231 | public void setDeviceViewport(Rectangle deviceViewport)
|
|---|
| 232 | {
|
|---|
| 233 | this.deviceViewport.setBounds(deviceViewport);
|
|---|
| 234 | if (root != null)
|
|---|
| 235 | {
|
|---|
| 236 | try
|
|---|
| 237 | {
|
|---|
| 238 | root.build();
|
|---|
| 239 | } catch (SVGException ex)
|
|---|
| 240 | {
|
|---|
| 241 | ex.printStackTrace();
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|