/src/org/mt4j/components/visibleComponents/widgets/MTWindow.java

http://mt4j.googlecode.com/ · Java · 219 lines · 103 code · 34 blank · 82 comment · 3 complexity · af9699572277e1d1f6dcd41bdf033c58 MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009 Christopher 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.widgets;
  19. import javax.media.opengl.GL;
  20. import org.mt4j.components.clipping.Clip;
  21. import org.mt4j.components.visibleComponents.AbstractVisibleComponent;
  22. import org.mt4j.components.visibleComponents.shapes.AbstractShape;
  23. import org.mt4j.components.visibleComponents.shapes.MTRectangle;
  24. import org.mt4j.components.visibleComponents.shapes.MTRoundRectangle;
  25. import org.mt4j.input.inputProcessors.IGestureEventListener;
  26. import org.mt4j.input.inputProcessors.MTGestureEvent;
  27. import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleEvent;
  28. import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
  29. import org.mt4j.util.MT4jSettings;
  30. import org.mt4j.util.MTColor;
  31. import org.mt4j.util.math.Matrix;
  32. import org.mt4j.util.math.Vector3D;
  33. import org.mt4j.util.math.Vertex;
  34. import processing.core.PApplet;
  35. import processing.core.PGraphics;
  36. import processing.opengl.PGraphicsOpenGL;
  37. /**
  38. * The Class MTWindow. A round rectangle class that clips its
  39. * children to the bounds of this window. If it is resized with the scaling
  40. * gesture, the window content isnt scaled.
  41. *
  42. * @author Christopher Ruff
  43. */
  44. public class MTWindow extends MTRoundRectangle {
  45. /** The clip. */
  46. private Clip clip;
  47. /** The draw inner border. */
  48. private boolean drawInnerBorder;
  49. /** The saved no stroke setting. */
  50. private boolean savedNoStrokeSetting;
  51. //TODO in abstractviscomp code von hier nehmen, clipshape bounds drüber zeichnen?
  52. //TODO add titlebar, maximize, close buttons
  53. //TODO scale border so its width doesent change..
  54. /**
  55. * Instantiates a new mT window.
  56. *
  57. * @param x the x
  58. * @param y the y
  59. * @param z the z
  60. * @param width the width
  61. * @param height the height
  62. * @param arcWidth the arc width
  63. * @param arcHeight the arc height
  64. * @param applet the applet
  65. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  66. */
  67. public MTWindow(float x, float y, float z, float width, float height,
  68. float arcWidth, float arcHeight, PApplet applet) {
  69. this(applet, x, y, z, width, height, arcWidth, arcHeight);
  70. }
  71. /**
  72. * Instantiates a new mT window.
  73. * @param applet the applet
  74. * @param x the x
  75. * @param y the y
  76. * @param z the z
  77. * @param width the width
  78. * @param height the height
  79. * @param arcWidth the arc width
  80. * @param arcHeight the arc height
  81. */
  82. public MTWindow(PApplet applet, float x, float y, float z, float width,
  83. float height, float arcWidth, float arcHeight) {
  84. super(applet, x, y, z, width, height, arcWidth, arcHeight);
  85. this.setName("unnamed MTWindow");
  86. if (!MT4jSettings.getInstance().isOpenGlMode()){
  87. System.err.println("MTWindow isnt fully supported if not using OpenGL renderer!");
  88. return;
  89. }
  90. //Create inner children clip shape
  91. float border = 10;
  92. GL gl = ((PGraphicsOpenGL)applet.g).gl;
  93. // MTRoundRectangle clipRect = new MTRoundRectangle(x+border, y+border, z, width-(2*border), height-(2*border), arcWidth, arcHeight, applet);
  94. MTRectangle clipRect = new MTRectangle(applet, x+border, y+border, z, width-(2*border), height-(2*border));
  95. clipRect.setDrawSmooth(true);
  96. clipRect.setNoStroke(true);
  97. clipRect.setBoundsBehaviour(MTRectangle.BOUNDS_ONLY_CHECK);
  98. this.clip = new Clip(gl, clipRect);
  99. this.setChildClip(this.clip);
  100. this.drawInnerBorder = true;
  101. //Add window background
  102. final MTRectangle windowBackGround = new MTRectangle(applet, x, y, z, 100, 200);
  103. windowBackGround.setFillColor(new MTColor(200,200,200,255));
  104. windowBackGround.setNoStroke(true);
  105. windowBackGround.setPickable(false);
  106. this.addChild(windowBackGround);
  107. this.removeAllGestureEventListeners(ScaleProcessor.class);
  108. // cr.removeAllGestureEventListeners(RotationDetector.class);
  109. this.addGestureListener(ScaleProcessor.class, new IGestureEventListener(){
  110. //@Override
  111. public boolean processGestureEvent(MTGestureEvent ge) {
  112. ScaleEvent se = (ScaleEvent)ge;
  113. //Scale window background normally
  114. windowBackGround.scaleGlobal(se.getScaleFactorX(), se.getScaleFactorY(), se.getScaleFactorZ(), se.getScalingPoint());
  115. //Scale vertices of the window
  116. AbstractShape target = (AbstractShape)ge.getTarget();
  117. Vertex[] verts = target.getGeometryInfo().getVertices();
  118. Vector3D newScalingPoint = target.globalToLocal(se.getScalingPoint());
  119. Matrix m = Matrix.getScalingMatrix(newScalingPoint, se.getScaleFactorX(), se.getScaleFactorY(), se.getScaleFactorZ());
  120. Vertex.transFormArray(m, verts);
  121. target.setVertices(verts);
  122. //Scale vertices of the clip shape
  123. AbstractShape clip = (AbstractShape)target.getChildClip().getClipShape();
  124. Vertex[] clipVerts = clip.getGeometryInfo().getVertices();
  125. Vertex.transFormArray(m, clipVerts);
  126. clip.setVertices(clipVerts);
  127. return false;
  128. }
  129. });
  130. // */
  131. //Draw this component and its children above
  132. //everything previously drawn and avoid z-fighting //FIXME but we cant use 3D stuff in there then..
  133. this.setDepthBufferDisabled(true);
  134. }
  135. /* (non-Javadoc)
  136. * @see org.mt4j.components.visibleComponents.AbstractVisibleComponent#preDraw(processing.core.PGraphics)
  137. */
  138. @Override
  139. public void preDraw(PGraphics graphics) {
  140. this.savedNoStrokeSetting = this.isNoStroke();
  141. super.preDraw(graphics);
  142. }
  143. /* (non-Javadoc)
  144. * @see org.mt4j.components.visibleComponents.AbstractVisibleComponent#postDrawChildren(processing.core.PGraphics)
  145. */
  146. @Override
  147. public void postDrawChildren(PGraphics g) {
  148. this.clip.disableClip(g);
  149. //Draw clipshape outline over all children to get an
  150. //antialiased border
  151. AbstractVisibleComponent clipShape = this.getChildClip().getClipShape();
  152. // if (!clipShape.isNoStroke()){
  153. if (this.drawInnerBorder){
  154. clipShape.setNoFill(true);
  155. clipShape.setNoStroke(false);
  156. clipShape.drawComponent(g);
  157. clipShape.setNoStroke(true);
  158. clipShape.setNoFill(false);
  159. }
  160. if (!savedNoStrokeSetting){
  161. boolean noFillSetting = this.isNoFill();
  162. this.setNoFill(true);
  163. this.setNoStroke(false);
  164. this.drawComponent(g);
  165. this.setNoFill(noFillSetting);
  166. this.setNoStroke(savedNoStrokeSetting);
  167. }
  168. this.setChildClip(null);
  169. super.postDrawChildren(g);
  170. this.setChildClip(clip);
  171. }
  172. // @Override
  173. // public void setStrokeColor(float r, float g, float b, float a) {
  174. // super.setStrokeColor(r, g, b, a);
  175. // this.clip.getClipShape().setStrokeColor(r, g, b, a);
  176. // }
  177. /* (non-Javadoc)
  178. * @see org.mt4j.components.visibleComponents.shapes.AbstractShape#setStrokeColor(org.mt4j.util.MTColor)
  179. */
  180. @Override
  181. public void setStrokeColor(MTColor strokeColor) {
  182. super.setStrokeColor(strokeColor);
  183. this.clip.getClipShape().setStrokeColor(strokeColor); //FIXME wtf? not needed!?
  184. }
  185. }