PageRenderTime 29ms CodeModel.GetById 16ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/components/visibleComponents/shapes/MTRoundRectangle.java

http://mt4j.googlecode.com/
Java | 306 lines | 89 code | 38 blank | 179 comment | 4 complexity | 673f82b3cec2934e2da8d9b6e283ddfb MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2009 C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3 *  
  4 *   This program is free software: you can redistribute it and/or modify
  5 *   it under the terms of the GNU General Public License as published by
  6 *   the Free Software Foundation, either version 3 of the License, or
  7 *   (at your option) any later version.
  8 *
  9 *   This program is distributed in the hope that it will be useful,
 10 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 *   GNU General Public License for more details.
 13 *
 14 *   You should have received a copy of the GNU General Public License
 15 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 16 *
 17 ***********************************************************************/
 18package org.mt4j.components.visibleComponents.shapes;
 19
 20import java.util.ArrayList;
 21import java.util.List;
 22
 23import org.mt4j.components.bounds.BoundsZPlaneRectangle;
 24import org.mt4j.components.bounds.IBoundingShape;
 25import org.mt4j.util.MTColor;
 26import org.mt4j.util.math.ToolsGeometry;
 27import org.mt4j.util.math.Vertex;
 28
 29import processing.core.PApplet;
 30
 31/**
 32 * This class can be used to display a rounded rectangle shape.
 33 * 
 34 * @author Christopher Ruff
 35 */
 36public class MTRoundRectangle extends MTPolygon {
 37	
 38	//Draw first lines
 39	private Vertex upperLineP1;
 40	private Vertex upperLineP2;
 41	
 42	private Vertex rLinep1;
 43	private Vertex rLinep2;
 44	
 45	//Draw the first arc
 46	private Vertex lowerLinep1;
 47	private Vertex lowerLinep2;
 48	
 49	private Vertex lLinep1;
 50	private Vertex lLinep2;
 51	
 52	private float arcWidth;
 53	private float arcHeight;
 54	private int arcSegments;
 55	
 56	private float x,y,z;
 57	private float width;
 58	private float height;
 59	
 60	
 61	/**
 62	 * Instantiates a new mT round rectangle.
 63	 *
 64	 * @param x the x
 65	 * @param y the y
 66	 * @param z the z
 67	 * @param width the width
 68	 * @param height the height
 69	 * @param arcWidth the arc width
 70	 * @param arcHeight the arc height
 71	 * @param pApplet the applet
 72	 * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
 73	 */
 74	public MTRoundRectangle(float x, float y, float z, float width, float height, float arcWidth, float arcHeight,  PApplet pApplet) {
 75		this(pApplet, x, y, z, width, height, arcWidth, arcHeight);
 76	}
 77	
 78	/**
 79	 * Instantiates a new mT round rectangle.
 80	 * @param pApplet the applet
 81	 * @param x the x
 82	 * @param y the y
 83	 * @param z the z
 84	 * @param width the width
 85	 * @param height the height
 86	 * @param arcWidth the arc width
 87	 * @param arcHeight the arc height
 88	 */
 89	public MTRoundRectangle(PApplet pApplet, float x, float y, float z, float width, float height, float arcWidth,  float arcHeight) {
 90		this(pApplet, x, y, z, width, height, arcWidth, arcHeight, 50);
 91	}
 92
 93	
 94	/**
 95	 * Instantiates a new mT round rectangle.
 96	 *
 97	 * @param x the x
 98	 * @param y the y
 99	 * @param z the z
100	 * @param width the width
101	 * @param height the height
102	 * @param arcWidth the arc width
103	 * @param arcHeight the arc height
104	 * @param segments the segments
105	 * @param pApplet the applet
106	 * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
107	 */
108	public MTRoundRectangle(float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments, PApplet pApplet) {
109		this(pApplet, x, y, z, width, height, arcWidth, arcHeight, segments);
110	}
111	
112	/**
113	 * Instantiates a new mT round rectangle.
114	 * @param pApplet the applet
115	 * @param x the x
116	 * @param y the y
117	 * @param z the z
118	 * @param width the width
119	 * @param height the height
120	 * @param arcWidth the arc width
121	 * @param arcHeight the arc height
122	 * @param segments the segments
123	 */
124	public MTRoundRectangle(PApplet pApplet, float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments) {
125		super(pApplet, new Vertex[]{});
126		
127		this.x = x;
128		this.y = y;
129		this.z = z;
130		this.arcWidth = arcWidth;
131		this.arcHeight = arcHeight;
132		this.width = width;
133		this.height = height;
134		
135		//defines the resolution and thereby the vertex count of the arcs
136		this.arcSegments = segments;
137		
138		//Arc Width may not be greater than the rectangles width
139		//and Arc height may not be greater than rectangles height!
140		this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, segments, true));
141		
142		this.setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
143	}
144
145	@Override
146	protected IBoundingShape computeDefaultBounds(){
147		return new BoundsZPlaneRectangle(this);
148	}
149
150	/**
151	 * Gets the round rect verts.
152	 * 
153	 * @param x the x
154	 * @param y the y
155	 * @param z the z
156	 * @param width the width
157	 * @param height the height
158	 * @param arcWidth the arc width
159	 * @param arcHeight the arc height
160	 * @param segments the segments
161	 * 
162	 * @return the round rect verts
163	 */
164	private Vertex[] getRoundRectVerts(float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments, boolean createTexCoords){
165		MTColor currentFillColor = getFillColor();
166		
167		//Draw first lines
168		Vertex upperLineP1 	= new Vertex(x + arcWidth, y, 0);
169		Vertex upperLineP2 	= new Vertex(x + width - arcWidth , y, 0);
170//		Vertex upperLineP2 	= new Vertex(x + arcWidth + width - 2*arcWidth , y, 0);
171		
172		Vertex rLinep1 = new Vertex(x + width, y + arcHeight			, 0);
173		Vertex rLinep2 = new Vertex(x + width, y + height	- arcHeight, 0);
174		
175		//Draw the first arc
176		List<Vertex> upperRightCorner = ToolsGeometry.arcTo(upperLineP2.x,upperLineP2.y, arcWidth, arcHeight, 0, false,true, rLinep1.x,rLinep1.y, arcSegments);
177		Vertex lowerLinep1 = new Vertex(x + width - arcWidth	, y + height, 0);
178		Vertex lowerLinep2 = new Vertex(x	+ arcWidth				, y + height, 0);
179			
180		
181		List<Vertex> lowerRightCorner = ToolsGeometry.arcTo(rLinep2.x,rLinep2.y, arcWidth, arcHeight, 0, false,true, lowerLinep1.x,lowerLinep1.y, arcSegments);
182		Vertex lLinep1 = new Vertex(x , y + height - arcHeight, 0);
183		Vertex lLinep2 = new Vertex(x , y + arcHeight, 0);
184			
185		List<Vertex> lowerLeftCorner = ToolsGeometry.arcTo(lowerLinep2.x,lowerLinep2.y, arcWidth, arcHeight, 0, false,true, lLinep1.x,lLinep1.y, arcSegments);
186		
187		List<Vertex> upperLeftCorner = ToolsGeometry.arcTo(lLinep2.x,lLinep2.y, arcWidth, arcHeight, 0, false,true, upperLineP1.x,upperLineP1.y, arcSegments);
188		
189		ArrayList<Vertex> verts = new ArrayList<Vertex>(); 
190		verts.add(upperLineP1); 
191//		verts.add(upperLineP2);
192		verts.addAll(upperRightCorner);
193		verts.add(rLinep1); 
194//		verts.add(rLinep2);
195		verts.addAll(lowerRightCorner);
196		verts.add(lowerLinep1); 
197//		verts.add(lowerLinep2);
198		verts.addAll(lowerLeftCorner);
199		verts.add(lLinep1); 
200//		verts.add(lLinep2);
201		verts.addAll(upperLeftCorner);
202		Vertex[] newVertices = verts.toArray(new Vertex[verts.size()]);
203		
204		//Set texture coordinates
205		for (Vertex vertex : newVertices) {
206			if (createTexCoords){
207				vertex.setTexCoordU((vertex.x - x) / width);
208				vertex.setTexCoordV((vertex.y - y) / height);
209				//System.out.println("TexU:" + vertex.getTexCoordU() + " TexV:" + vertex.getTexCoordV());
210			}
211			vertex.setRGBA(currentFillColor.getR(), currentFillColor.getG(), currentFillColor.getB(), currentFillColor.getAlpha());
212		}
213		return newVertices;
214	}
215	
216	
217	
218	public void setSizeLocal(float width, float height){
219		if (width > 0 && height > 0){
220			this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, arcSegments, true));
221		}
222	}
223	
224//	/**
225//	 * Sets the size of the rectangle.
226//	 * Changes the vertices themself, not the transform, to allow for hassle-free non-uniform scaling.
227//	 * <p>Overridden because shearing will occur if the component was rotated and then scaled non-uniformly!
228//	 * <br>This method preserves the orientation
229//	 * 
230//	 * @param width the width
231//	 * @param height the height
232//	 * 
233//	 * @return true, if sets the size xy relative to parent
234//	 */
235//	@Override
236//	public boolean setSizeXYRelativeToParent(float width, float height){
237////		/*
238//		if (width > 0 && height > 0){
239//			this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, arcSegments, true));
240//			return true;
241//		}else{
242//			return false;
243//		}
244//		
245////		*/
246//		
247//		
248////		if (width > 0 && height > 0){
249////			Vertex[] v = this.getVerticesObjSpace();
250////			this.setVertices(new Vertex[]{
251////					new Vertex(v[0].x,			v[0].y, 		v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()), 
252////					new Vertex(v[0].x+width, 	v[1].y, 		v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()), 
253////					new Vertex(v[0].x+width, 	v[1].y+height, 	v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()), 
254////					new Vertex(v[3].x,			v[0].y+height,	v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()), 
255////					new Vertex(v[4].x,			v[4].y,			v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()), 
256////			});
257////			return true;
258////		}else
259////			return false;
260//	}
261	
262//	/* (non-Javadoc)
263//	 * @see com.jMT.components.visibleComponents.shapes.MTPolygon#setHeightXYRelativeToParent(float)
264//	 */
265//	@Override
266//	public boolean setHeightXYRelativeToParent(float height){
267//		if (height > 0){
268//			Vertex[] v = this.getVerticesLocal();
269//			this.setVertices(new Vertex[]{
270//					new Vertex(v[0].x,	v[0].y, 		v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()), 
271//					new Vertex(v[1].x, 	v[1].y, 		v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()), 
272//					new Vertex(v[2].x, 	v[1].y+height, 	v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()), 
273//					new Vertex(v[3].x,	v[1].y+height,	v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()), 
274//					new Vertex(v[4].x,	v[4].y,			v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()), 
275//			});
276//			return true;
277//		}else
278//			return false;
279//	}
280	
281//	/**
282//	 * Scales the shape to the given width.
283//	 * Uses the bounding rectangle for calculation!
284//	 * Aspect ratio is preserved!
285//	 * 
286//	 * @param width the width
287//	 * 
288//	 * @return true, if the width isnt negative
289//	 */
290//	@Override
291//	public boolean setWidthXYRelativeToParent(float width){
292//		if (width > 0){
293//			Vertex[] v = this.getVerticesLocal();
294//			this.setVertices(new Vertex[]{
295//					new Vertex(v[0].x,			v[0].y, 		v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()), 
296//					new Vertex(v[0].x+width, 	v[1].y, 		v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()), 
297//					new Vertex(v[0].x+width, 	v[2].y, 		v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()), 
298//					new Vertex(v[3].x,			v[3].y,			v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()), 
299//					new Vertex(v[4].x,			v[4].y,			v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()), 
300//			});
301//			return true;
302//		}else
303//			return false;
304//	}
305
306}