PageRenderTime 35ms CodeModel.GetById 20ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

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