| 1 | /*
|
|---|
| 2 | * SVG Salamander
|
|---|
| 3 | * Copyright (c) 2004, Mark McKay
|
|---|
| 4 | * All rights reserved.
|
|---|
| 5 | *
|
|---|
| 6 | * Redistribution and use in source and binary forms, with or
|
|---|
| 7 | * without modification, are permitted provided that the following
|
|---|
| 8 | * conditions are met:
|
|---|
| 9 | *
|
|---|
| 10 | * - Redistributions of source code must retain the above
|
|---|
| 11 | * copyright notice, this list of conditions and the following
|
|---|
| 12 | * disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above
|
|---|
| 14 | * copyright notice, this list of conditions and the following
|
|---|
| 15 | * disclaimer in the documentation and/or other materials
|
|---|
| 16 | * provided with the distribution.
|
|---|
| 17 | *
|
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|---|
| 22 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|---|
| 23 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|---|
| 24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|---|
| 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 30 | *
|
|---|
| 31 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 32 | * projects can be found at http://www.kitfox.com
|
|---|
| 33 | *
|
|---|
| 34 | * Created on January 26, 2004, 5:25 PM
|
|---|
| 35 | */
|
|---|
| 36 | package com.kitfox.svg;
|
|---|
| 37 |
|
|---|
| 38 | import com.kitfox.svg.xml.StyleAttribute;
|
|---|
| 39 | import java.awt.Graphics2D;
|
|---|
| 40 | import java.awt.Shape;
|
|---|
| 41 | import java.awt.geom.Rectangle2D;
|
|---|
| 42 | import java.awt.geom.RectangularShape;
|
|---|
| 43 | import java.awt.geom.RoundRectangle2D;
|
|---|
| 44 | import java.io.IOException;
|
|---|
| 45 | import java.io.ObjectInputStream;
|
|---|
| 46 | import java.io.ObjectOutputStream;
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * @author Mark McKay
|
|---|
| 50 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 51 | */
|
|---|
| 52 | public class Rect extends ShapeElement
|
|---|
| 53 | {
|
|---|
| 54 | public static final String TAG_NAME = "rect";
|
|---|
| 55 |
|
|---|
| 56 | float x = 0f;
|
|---|
| 57 | float y = 0f;
|
|---|
| 58 | float width = 0f;
|
|---|
| 59 | float height = 0f;
|
|---|
| 60 | float rx = 0f;
|
|---|
| 61 | float ry = 0f;
|
|---|
| 62 | RectangularShape rect;
|
|---|
| 63 |
|
|---|
| 64 | /**
|
|---|
| 65 | * Creates a new instance of Rect
|
|---|
| 66 | */
|
|---|
| 67 | public Rect()
|
|---|
| 68 | {
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | public String getTagName()
|
|---|
| 72 | {
|
|---|
| 73 | return TAG_NAME;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | private void writeObject(ObjectOutputStream out) throws IOException
|
|---|
| 77 | {
|
|---|
| 78 | out.writeFloat(x);
|
|---|
| 79 | out.writeFloat(y);
|
|---|
| 80 | out.writeFloat(width);
|
|---|
| 81 | out.writeFloat(height);
|
|---|
| 82 | out.writeFloat(rx);
|
|---|
| 83 | out.writeFloat(ry);
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | private void readObject(ObjectInputStream in) throws IOException
|
|---|
| 87 | {
|
|---|
| 88 | x = in.readFloat();
|
|---|
| 89 | y = in.readFloat();
|
|---|
| 90 | width = in.readFloat();
|
|---|
| 91 | height = in.readFloat();
|
|---|
| 92 | rx = in.readFloat();
|
|---|
| 93 | ry = in.readFloat();
|
|---|
| 94 |
|
|---|
| 95 | if (rx == 0f && ry == 0f)
|
|---|
| 96 | {
|
|---|
| 97 | rect = new Rectangle2D.Float(x, y, width, height);
|
|---|
| 98 | } else
|
|---|
| 99 | {
|
|---|
| 100 | rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | /*
|
|---|
| 105 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
|---|
| 106 | {
|
|---|
| 107 | //Load style string
|
|---|
| 108 | super.loaderStartElement(helper, attrs, parent);
|
|---|
| 109 |
|
|---|
| 110 | String x = attrs.getValue("x");
|
|---|
| 111 | String y = attrs.getValue("y");
|
|---|
| 112 | String width = attrs.getValue("width");
|
|---|
| 113 | String height = attrs.getValue("height");
|
|---|
| 114 | String rx = attrs.getValue("rx");
|
|---|
| 115 | String ry = attrs.getValue("ry");
|
|---|
| 116 |
|
|---|
| 117 | if (rx == null) rx = ry;
|
|---|
| 118 | if (ry == null) ry = rx;
|
|---|
| 119 |
|
|---|
| 120 | this.x = XMLParseUtil.parseFloat(x);
|
|---|
| 121 | this.y = XMLParseUtil.parseFloat(y);
|
|---|
| 122 | this.width = XMLParseUtil.parseFloat(width);
|
|---|
| 123 | this.height = XMLParseUtil.parseFloat(height);
|
|---|
| 124 | if (rx != null)
|
|---|
| 125 | {
|
|---|
| 126 | this.rx = XMLParseUtil.parseFloat(rx);
|
|---|
| 127 | this.ry = XMLParseUtil.parseFloat(ry);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | build();
|
|---|
| 131 | // setBounds(this.x, this.y, this.width, this.height);
|
|---|
| 132 | }
|
|---|
| 133 | */
|
|---|
| 134 | protected void build() throws SVGException
|
|---|
| 135 | {
|
|---|
| 136 | super.build();
|
|---|
| 137 |
|
|---|
| 138 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 139 |
|
|---|
| 140 | // SVGElement parent = this.getParent();
|
|---|
| 141 | // if (parent instanceof RenderableElement)
|
|---|
| 142 | // {
|
|---|
| 143 | // RenderableElement re = (RenderableElement)parent;
|
|---|
| 144 | // Rectangle2D bounds = re.getBoundingBox();
|
|---|
| 145 | // bounds = null;
|
|---|
| 146 | // }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | if (getPres(sty.setName("x")))
|
|---|
| 150 | {
|
|---|
| 151 | x = sty.getFloatValueWithUnits();
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | if (getPres(sty.setName("y")))
|
|---|
| 155 | {
|
|---|
| 156 | y = sty.getFloatValueWithUnits();
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | if (getPres(sty.setName("width")))
|
|---|
| 160 | {
|
|---|
| 161 | width = sty.getFloatValueWithUnits();
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | if (getPres(sty.setName("height")))
|
|---|
| 165 | {
|
|---|
| 166 | height = sty.getFloatValueWithUnits();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | boolean rxSet = false;
|
|---|
| 170 | if (getPres(sty.setName("rx")))
|
|---|
| 171 | {
|
|---|
| 172 | rx = sty.getFloatValueWithUnits();
|
|---|
| 173 | rxSet = true;
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | boolean rySet = false;
|
|---|
| 177 | if (getPres(sty.setName("ry")))
|
|---|
| 178 | {
|
|---|
| 179 | ry = sty.getFloatValueWithUnits();
|
|---|
| 180 | rySet = true;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | if (!rxSet)
|
|---|
| 184 | {
|
|---|
| 185 | rx = ry;
|
|---|
| 186 | }
|
|---|
| 187 | if (!rySet)
|
|---|
| 188 | {
|
|---|
| 189 | ry = rx;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | if (rx == 0f && ry == 0f)
|
|---|
| 194 | {
|
|---|
| 195 | rect = new Rectangle2D.Float(x, y, width, height);
|
|---|
| 196 | } else
|
|---|
| 197 | {
|
|---|
| 198 | rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
|
|---|
| 199 | }
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | public void render(Graphics2D g) throws SVGException
|
|---|
| 203 | {
|
|---|
| 204 | beginLayer(g);
|
|---|
| 205 | renderShape(g, rect);
|
|---|
| 206 | finishLayer(g);
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | public Shape getShape()
|
|---|
| 210 | {
|
|---|
| 211 | return shapeToParent(rect);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | public Rectangle2D getBoundingBox() throws SVGException
|
|---|
| 215 | {
|
|---|
| 216 | return boundsToParent(includeStrokeInBounds(rect.getBounds2D()));
|
|---|
| 217 | }
|
|---|
| 218 |
|
|---|
| 219 | /**
|
|---|
| 220 | * Updates all attributes in this diagram associated with a time event. Ie,
|
|---|
| 221 | * all attributes with track information.
|
|---|
| 222 | *
|
|---|
| 223 | * @return - true if this node has changed state as a result of the time
|
|---|
| 224 | * update
|
|---|
| 225 | */
|
|---|
| 226 | public boolean updateTime(double curTime) throws SVGException
|
|---|
| 227 | {
|
|---|
| 228 | // if (trackManager.getNumTracks() == 0) return false;
|
|---|
| 229 | boolean changeState = super.updateTime(curTime);
|
|---|
| 230 |
|
|---|
| 231 | //Get current values for parameters
|
|---|
| 232 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 233 | boolean shapeChange = false;
|
|---|
| 234 |
|
|---|
| 235 | if (getPres(sty.setName("x")))
|
|---|
| 236 | {
|
|---|
| 237 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 238 | if (newVal != x)
|
|---|
| 239 | {
|
|---|
| 240 | x = newVal;
|
|---|
| 241 | shapeChange = true;
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | if (getPres(sty.setName("y")))
|
|---|
| 246 | {
|
|---|
| 247 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 248 | if (newVal != y)
|
|---|
| 249 | {
|
|---|
| 250 | y = newVal;
|
|---|
| 251 | shapeChange = true;
|
|---|
| 252 | }
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | if (getPres(sty.setName("width")))
|
|---|
| 256 | {
|
|---|
| 257 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 258 | if (newVal != width)
|
|---|
| 259 | {
|
|---|
| 260 | width = newVal;
|
|---|
| 261 | shapeChange = true;
|
|---|
| 262 | }
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | if (getPres(sty.setName("height")))
|
|---|
| 266 | {
|
|---|
| 267 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 268 | if (newVal != height)
|
|---|
| 269 | {
|
|---|
| 270 | height = newVal;
|
|---|
| 271 | shapeChange = true;
|
|---|
| 272 | }
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | if (getPres(sty.setName("rx")))
|
|---|
| 276 | {
|
|---|
| 277 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 278 | if (newVal != rx)
|
|---|
| 279 | {
|
|---|
| 280 | rx = newVal;
|
|---|
| 281 | shapeChange = true;
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | if (getPres(sty.setName("ry")))
|
|---|
| 286 | {
|
|---|
| 287 | float newVal = sty.getFloatValueWithUnits();
|
|---|
| 288 | if (newVal != ry)
|
|---|
| 289 | {
|
|---|
| 290 | ry = newVal;
|
|---|
| 291 | shapeChange = true;
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | if (shapeChange)
|
|---|
| 296 | {
|
|---|
| 297 | build();
|
|---|
| 298 | // if (rx == 0f && ry == 0f)
|
|---|
| 299 | // {
|
|---|
| 300 | // rect = new Rectangle2D.Float(x, y, width, height);
|
|---|
| 301 | // }
|
|---|
| 302 | // else
|
|---|
| 303 | // {
|
|---|
| 304 | // rect = new RoundRectangle2D.Float(x, y, width, height, rx * 2, ry * 2);
|
|---|
| 305 | // }
|
|---|
| 306 | // return true;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | return changeState || shapeChange;
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|