View Javadoc

1   /**********************************************
2    * Copyright (C) 2011 Lukas Laag
3    * This file is part of svgreal.
4    * 
5    * svgreal is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * svgreal is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with svgreal.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.svg.edit.client.command.path;
19  
20  import java.util.List;
21  
22  import org.vectomatic.dom.svg.OMSVGDocument;
23  import org.vectomatic.dom.svg.OMSVGGElement;
24  import org.vectomatic.dom.svg.OMSVGPathSeg;
25  import org.vectomatic.dom.svg.OMSVGPoint;
26  import org.vectomatic.dom.svg.OMSVGRectElement;
27  import org.vectomatic.svg.edit.client.command.path.IPathRepOwner.VertexState;
28  
29  import com.google.gwt.dom.client.Element;
30  
31  /**
32   * Base class to path segment representation
33   * @author laaglu
34   */
35  public abstract class SVGSegRep {
36  	/**
37  	 * The owner of this segment
38  	 */
39  	protected IPathRepOwner owner;
40  	/**
41  	 * The previous segment in the path
42  	 */
43  	protected SVGSegRep previous;
44  	/**
45  	 * The next segment in the path
46  	 */
47  	protected SVGSegRep next;
48  	/**
49  	 * The group where SVG elements representing tangents
50  	 * to this segment are nested
51  	 */
52  	protected OMSVGGElement tangents;
53  	/**
54  	 * The endpoint of the segment
55  	 */
56  	protected OMSVGRectElement vertex;
57  
58  	public SVGSegRep(IPathRepOwner owner) {
59  		this.owner = owner;
60  		OMSVGDocument document = (OMSVGDocument) owner.getSvg().getOwnerDocument();
61  		tangents = document.createSVGGElement();
62  		vertex = document.createSVGRectElement();
63  	}
64  
65  	public OMSVGGElement getTangents() {
66  		return tangents;
67  	}
68  
69  	public OMSVGRectElement getVertex() {
70  		return vertex;
71  	}
72  
73  	public abstract OMSVGPathSeg getElement();
74  	public abstract float getX();
75  	public abstract float getY();
76  	public abstract void setX(float x);
77  	public abstract void setY(float y);
78  	public float getX1() {
79  		return 0;
80  	}
81  	public float getY1() {
82  		return 0;
83  	}
84  	public float getX2() {
85  		return 0;
86  	}
87  	public float getY2() {
88  		return 0;
89  	}
90  	public Element getCp1() {
91  		return null;
92  	}
93  	public void setCp1(OMSVGPoint p, float hs) {
94  	}
95  	public Element getCp2() {
96  		return null;
97  	}
98  	public void setCp2(OMSVGPoint p, float hs) {
99  	}
100 	/**
101 	 * Updates the vertex, control points and tangents defining the segment
102 	 * @param delta
103 	 * The translation to apply
104 	 * @param target
105 	 * The vertex, control point or tangeant to update. null means the segment endpoint vertex
106 	 * @param hs
107 	 * The control point size
108 	 * @param isCtrlKeyDown
109 	 * True if the control key is pressed (spline will interpret this to preserve
110 	 * the angle between tangents when a tangent control point is dragged)
111 	 */
112 	public abstract void processMouseMove(OMSVGPoint delta, Element target, float hs, boolean isCtrlKeyDown);
113 	public void updateStart(OMSVGPoint delta, float hs) {
114 	}
115 	public abstract void updateEnd(OMSVGPoint delta, float hs);
116 	public abstract void update(float hs);
117 
118 	public VertexState getState() {
119 		return VertexState.read(vertex);
120 	}
121 	
122 	public void setState(VertexState state) {
123 		state.write(vertex);
124 	}
125 	
126 	public SVGSegRep getPreviousSplineSeg() {
127 		SVGSegRep splineSeg = null;
128 		List<SVGSegRep> segments = owner.getSegments();
129 		splineSeg = previous;
130 		return (splineSeg instanceof SVGCubicSegRep || splineSeg instanceof SVGQuadraticSegRep) ? splineSeg : null;
131 	}
132 
133 	public SVGSegRep getNextSplineSeg() {
134 		SVGSegRep splineSeg = null;
135 		splineSeg = next;
136 		return (splineSeg instanceof SVGCubicSegRep || splineSeg instanceof SVGQuadraticSegRep) ? splineSeg : null;
137 	}
138 	
139 	public void setNext(SVGSegRep seg) {
140 		next = seg;
141 	}
142 	public void setPrevious(SVGSegRep seg) {
143 		previous = seg;
144 	}
145 
146 }
147 
148