/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. ***********************************************************************/
  18. package org.mt4j.components.visibleComponents.shapes;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import org.mt4j.components.bounds.BoundsZPlaneRectangle;
  22. import org.mt4j.components.bounds.IBoundingShape;
  23. import org.mt4j.util.MTColor;
  24. import org.mt4j.util.math.ToolsGeometry;
  25. import org.mt4j.util.math.Vertex;
  26. import processing.core.PApplet;
  27. /**
  28. * This class can be used to display a rounded rectangle shape.
  29. *
  30. * @author Christopher Ruff
  31. */
  32. public class MTRoundRectangle extends MTPolygon {
  33. //Draw first lines
  34. private Vertex upperLineP1;
  35. private Vertex upperLineP2;
  36. private Vertex rLinep1;
  37. private Vertex rLinep2;
  38. //Draw the first arc
  39. private Vertex lowerLinep1;
  40. private Vertex lowerLinep2;
  41. private Vertex lLinep1;
  42. private Vertex lLinep2;
  43. private float arcWidth;
  44. private float arcHeight;
  45. private int arcSegments;
  46. private float x,y,z;
  47. private float width;
  48. private float height;
  49. /**
  50. * Instantiates a new mT round rectangle.
  51. *
  52. * @param x the x
  53. * @param y the y
  54. * @param z the z
  55. * @param width the width
  56. * @param height the height
  57. * @param arcWidth the arc width
  58. * @param arcHeight the arc height
  59. * @param pApplet the applet
  60. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  61. */
  62. public MTRoundRectangle(float x, float y, float z, float width, float height, float arcWidth, float arcHeight, PApplet pApplet) {
  63. this(pApplet, x, y, z, width, height, arcWidth, arcHeight);
  64. }
  65. /**
  66. * Instantiates a new mT round rectangle.
  67. * @param pApplet the applet
  68. * @param x the x
  69. * @param y the y
  70. * @param z the z
  71. * @param width the width
  72. * @param height the height
  73. * @param arcWidth the arc width
  74. * @param arcHeight the arc height
  75. */
  76. public MTRoundRectangle(PApplet pApplet, float x, float y, float z, float width, float height, float arcWidth, float arcHeight) {
  77. this(pApplet, x, y, z, width, height, arcWidth, arcHeight, 50);
  78. }
  79. /**
  80. * Instantiates a new mT round rectangle.
  81. *
  82. * @param x the x
  83. * @param y the y
  84. * @param z the z
  85. * @param width the width
  86. * @param height the height
  87. * @param arcWidth the arc width
  88. * @param arcHeight the arc height
  89. * @param segments the segments
  90. * @param pApplet the applet
  91. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  92. */
  93. public MTRoundRectangle(float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments, PApplet pApplet) {
  94. this(pApplet, x, y, z, width, height, arcWidth, arcHeight, segments);
  95. }
  96. /**
  97. * Instantiates a new mT round rectangle.
  98. * @param pApplet the applet
  99. * @param x the x
  100. * @param y the y
  101. * @param z the z
  102. * @param width the width
  103. * @param height the height
  104. * @param arcWidth the arc width
  105. * @param arcHeight the arc height
  106. * @param segments the segments
  107. */
  108. public MTRoundRectangle(PApplet pApplet, float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments) {
  109. super(pApplet, new Vertex[]{});
  110. this.x = x;
  111. this.y = y;
  112. this.z = z;
  113. this.arcWidth = arcWidth;
  114. this.arcHeight = arcHeight;
  115. this.width = width;
  116. this.height = height;
  117. //defines the resolution and thereby the vertex count of the arcs
  118. this.arcSegments = segments;
  119. //Arc Width may not be greater than the rectangles width
  120. //and Arc height may not be greater than rectangles height!
  121. this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, segments, true));
  122. this.setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
  123. }
  124. @Override
  125. protected IBoundingShape computeDefaultBounds(){
  126. return new BoundsZPlaneRectangle(this);
  127. }
  128. /**
  129. * Gets the round rect verts.
  130. *
  131. * @param x the x
  132. * @param y the y
  133. * @param z the z
  134. * @param width the width
  135. * @param height the height
  136. * @param arcWidth the arc width
  137. * @param arcHeight the arc height
  138. * @param segments the segments
  139. *
  140. * @return the round rect verts
  141. */
  142. private Vertex[] getRoundRectVerts(float x, float y, float z, float width, float height, float arcWidth, float arcHeight, int segments, boolean createTexCoords){
  143. MTColor currentFillColor = getFillColor();
  144. //Draw first lines
  145. Vertex upperLineP1 = new Vertex(x + arcWidth, y, 0);
  146. Vertex upperLineP2 = new Vertex(x + width - arcWidth , y, 0);
  147. // Vertex upperLineP2 = new Vertex(x + arcWidth + width - 2*arcWidth , y, 0);
  148. Vertex rLinep1 = new Vertex(x + width, y + arcHeight , 0);
  149. Vertex rLinep2 = new Vertex(x + width, y + height - arcHeight, 0);
  150. //Draw the first arc
  151. List<Vertex> upperRightCorner = ToolsGeometry.arcTo(upperLineP2.x,upperLineP2.y, arcWidth, arcHeight, 0, false,true, rLinep1.x,rLinep1.y, arcSegments);
  152. Vertex lowerLinep1 = new Vertex(x + width - arcWidth , y + height, 0);
  153. Vertex lowerLinep2 = new Vertex(x + arcWidth , y + height, 0);
  154. List<Vertex> lowerRightCorner = ToolsGeometry.arcTo(rLinep2.x,rLinep2.y, arcWidth, arcHeight, 0, false,true, lowerLinep1.x,lowerLinep1.y, arcSegments);
  155. Vertex lLinep1 = new Vertex(x , y + height - arcHeight, 0);
  156. Vertex lLinep2 = new Vertex(x , y + arcHeight, 0);
  157. List<Vertex> lowerLeftCorner = ToolsGeometry.arcTo(lowerLinep2.x,lowerLinep2.y, arcWidth, arcHeight, 0, false,true, lLinep1.x,lLinep1.y, arcSegments);
  158. List<Vertex> upperLeftCorner = ToolsGeometry.arcTo(lLinep2.x,lLinep2.y, arcWidth, arcHeight, 0, false,true, upperLineP1.x,upperLineP1.y, arcSegments);
  159. ArrayList<Vertex> verts = new ArrayList<Vertex>();
  160. verts.add(upperLineP1);
  161. // verts.add(upperLineP2);
  162. verts.addAll(upperRightCorner);
  163. verts.add(rLinep1);
  164. // verts.add(rLinep2);
  165. verts.addAll(lowerRightCorner);
  166. verts.add(lowerLinep1);
  167. // verts.add(lowerLinep2);
  168. verts.addAll(lowerLeftCorner);
  169. verts.add(lLinep1);
  170. // verts.add(lLinep2);
  171. verts.addAll(upperLeftCorner);
  172. Vertex[] newVertices = verts.toArray(new Vertex[verts.size()]);
  173. //Set texture coordinates
  174. for (Vertex vertex : newVertices) {
  175. if (createTexCoords){
  176. vertex.setTexCoordU((vertex.x - x) / width);
  177. vertex.setTexCoordV((vertex.y - y) / height);
  178. //System.out.println("TexU:" + vertex.getTexCoordU() + " TexV:" + vertex.getTexCoordV());
  179. }
  180. vertex.setRGBA(currentFillColor.getR(), currentFillColor.getG(), currentFillColor.getB(), currentFillColor.getAlpha());
  181. }
  182. return newVertices;
  183. }
  184. public void setSizeLocal(float width, float height){
  185. if (width > 0 && height > 0){
  186. this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, arcSegments, true));
  187. }
  188. }
  189. // /**
  190. // * Sets the size of the rectangle.
  191. // * Changes the vertices themself, not the transform, to allow for hassle-free non-uniform scaling.
  192. // * <p>Overridden because shearing will occur if the component was rotated and then scaled non-uniformly!
  193. // * <br>This method preserves the orientation
  194. // *
  195. // * @param width the width
  196. // * @param height the height
  197. // *
  198. // * @return true, if sets the size xy relative to parent
  199. // */
  200. // @Override
  201. // public boolean setSizeXYRelativeToParent(float width, float height){
  202. //// /*
  203. // if (width > 0 && height > 0){
  204. // this.setVertices(this.getRoundRectVerts(x, y, z, width, height, arcWidth, arcHeight, arcSegments, true));
  205. // return true;
  206. // }else{
  207. // return false;
  208. // }
  209. //
  210. //// */
  211. //
  212. //
  213. //// if (width > 0 && height > 0){
  214. //// Vertex[] v = this.getVerticesObjSpace();
  215. //// this.setVertices(new Vertex[]{
  216. //// new Vertex(v[0].x, v[0].y, v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()),
  217. //// new Vertex(v[0].x+width, v[1].y, v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()),
  218. //// new Vertex(v[0].x+width, v[1].y+height, v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()),
  219. //// new Vertex(v[3].x, v[0].y+height, v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()),
  220. //// new Vertex(v[4].x, v[4].y, v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()),
  221. //// });
  222. //// return true;
  223. //// }else
  224. //// return false;
  225. // }
  226. // /* (non-Javadoc)
  227. // * @see com.jMT.components.visibleComponents.shapes.MTPolygon#setHeightXYRelativeToParent(float)
  228. // */
  229. // @Override
  230. // public boolean setHeightXYRelativeToParent(float height){
  231. // if (height > 0){
  232. // Vertex[] v = this.getVerticesLocal();
  233. // this.setVertices(new Vertex[]{
  234. // new Vertex(v[0].x, v[0].y, v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()),
  235. // new Vertex(v[1].x, v[1].y, v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()),
  236. // new Vertex(v[2].x, v[1].y+height, v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()),
  237. // new Vertex(v[3].x, v[1].y+height, v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()),
  238. // new Vertex(v[4].x, v[4].y, v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()),
  239. // });
  240. // return true;
  241. // }else
  242. // return false;
  243. // }
  244. // /**
  245. // * Scales the shape to the given width.
  246. // * Uses the bounding rectangle for calculation!
  247. // * Aspect ratio is preserved!
  248. // *
  249. // * @param width the width
  250. // *
  251. // * @return true, if the width isnt negative
  252. // */
  253. // @Override
  254. // public boolean setWidthXYRelativeToParent(float width){
  255. // if (width > 0){
  256. // Vertex[] v = this.getVerticesLocal();
  257. // this.setVertices(new Vertex[]{
  258. // new Vertex(v[0].x, v[0].y, v[0].z, v[0].getTexCoordU(), v[0].getTexCoordV()),
  259. // new Vertex(v[0].x+width, v[1].y, v[1].z, v[1].getTexCoordU(), v[1].getTexCoordV()),
  260. // new Vertex(v[0].x+width, v[2].y, v[2].z, v[2].getTexCoordU(), v[2].getTexCoordV()),
  261. // new Vertex(v[3].x, v[3].y, v[3].z, v[3].getTexCoordU(), v[3].getTexCoordV()),
  262. // new Vertex(v[4].x, v[4].y, v[4].z, v[4].getTexCoordU(), v[4].getTexCoordV()),
  263. // });
  264. // return true;
  265. // }else
  266. // return false;
  267. // }
  268. }