PageRenderTime 27ms CodeModel.GetById 18ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/org/mt4j/input/gestureAction/DefaultSvgButtonClickAction.java

http://mt4j.googlecode.com/
Java | 193 lines | 82 code | 37 blank | 74 comment | 7 complexity | 9d0113c3522f606220f039a724a9754c 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.input.gestureAction;
 19
 20import org.mt4j.components.MTComponent;
 21import org.mt4j.components.TransformSpace;
 22import org.mt4j.components.visibleComponents.shapes.AbstractShape;
 23import org.mt4j.input.inputProcessors.IGestureEventListener;
 24import org.mt4j.util.math.Matrix;
 25import org.mt4j.util.math.Vector3D;
 26
 27
 28/**
 29 * The Class DefaultSvgButtonClickAction.
 30 * @author Christopher Ruff
 31 */
 32public class DefaultSvgButtonClickAction extends DefaultButtonClickAction implements IGestureEventListener {
 33
 34	/** The width obj space. */
 35	private Vector3D widthObjSpace;
 36	
 37	/** The center obj space. */
 38	private Vector3D centerObjSpace;
 39	
 40	/** The svg comp. */
 41	private MTComponent svgComp;
 42	
 43	/**
 44	 * Instantiates a new default svg button click action.
 45	 * 
 46	 * @param svgComp the svg comp
 47	 */
 48	public DefaultSvgButtonClickAction(MTComponent svgComp) { //, MTPolygon largestPolyInSvg
 49		super(DefaultSvgButtonClickAction.getLargestSvgComp(svgComp, null));
 50		this.svgComp = svgComp;
 51		this.width = this.getReferenceComp().getWidthXY(TransformSpace.RELATIVE_TO_PARENT);
 52		this.widthObjSpace = this.getWidthVectorLocal();
 53		this.centerObjSpace =  this.getReferenceComp().getCenterPointLocal();
 54	}
 55	
 56	
 57	/**
 58	 * Gets the width obj space vector.
 59	 * 
 60	 * @return the width obj space vector
 61	 */
 62	private Vector3D getWidthVectorLocal(){
 63		if (this.getReferenceComp().hasBounds()){
 64			return this.getReferenceComp().getBounds().getWidthXYVectLocal();
 65		}else{
 66			throw new RuntimeException("Couldnt extract the width vector from the svg shape: '" + svgComp.getName() + "'. We need a component or boundingshape that defines the method getWidthXYVectObjSpace()");
 67		}
 68	}
 69	
 70	/**
 71	 * Returns the component with the largest x,y dimension to use for picking by default and scaling.
 72	 * 
 73	 * @param comp the comp
 74	 * @param compWithBiggestBoundingRect the comp with biggest bounding rect
 75	 * 
 76	 * @return the largest svg comp
 77	 */
 78	public static AbstractShape getLargestSvgComp(MTComponent comp, AbstractShape compWithBiggestBoundingRect){
 79		if (comp instanceof AbstractShape) {
 80			AbstractShape shape = (AbstractShape) comp;
 81			float rectWidthGlobal = shape.getWidthXY(TransformSpace.GLOBAL);
 82			float rectHeightGlobal = shape.getHeightXY(TransformSpace.GLOBAL);
 83
 84			if (
 85					compWithBiggestBoundingRect != null
 86			){
 87//				System.out.println("Fromer biggest != null, checking if " + comp.getName() + " is bigger.");
 88				if (	rectWidthGlobal 	>= compWithBiggestBoundingRect.getWidthXY(TransformSpace.GLOBAL)//biggestWidth
 89					|| 	rectHeightGlobal 	>= compWithBiggestBoundingRect.getHeightXY(TransformSpace.GLOBAL)//biggestHeight 
 90				){
 91//					System.out.println(comp.getName() + " is bigger!");
 92					compWithBiggestBoundingRect = shape;
 93				}else{
 94//					System.out.println(compWithBiggestBoundingRect.getName() + " is still bigger");
 95				}
 96			}else{
 97//				System.out.println("Reference is null, take " + comp.getName() + " as the new biggest.");
 98				compWithBiggestBoundingRect = shape;
 99			}
100
101		}
102		for(MTComponent child : comp.getChildren()){
103			compWithBiggestBoundingRect = getLargestSvgComp(child, compWithBiggestBoundingRect);
104		}
105		return compWithBiggestBoundingRect;
106	}
107	
108	
109	
110	@Override
111	public MTComponent getCompToResize() {
112		return this.svgComp;
113	}
114	
115	@Override
116	public float getCurrentUnscaledWidth(){
117		Vector3D v = getWidthVectorLocal();
118		
119		Matrix refCompLocalToWorld = new Matrix(this.getReferenceComp().getGlobalMatrix());
120		//Remove translation for direction vectors(width/height)
121		refCompLocalToWorld.removeTranslationFromMatrix();
122		
123		//obj width vect to world space 
124		v.transform(refCompLocalToWorld);
125		
126		Matrix svgButtonAbsInv = new Matrix(this.getCompToResize().getGlobalInverseMatrix());
127		//TODO doch wieder localbase von svg dazutransformen?
128		//svgbutton inverse parent relative machen
129		svgButtonAbsInv.multLocal(this.getCompToResize().getLocalMatrix());
130		//Remove translation for direction vectors(width/height)
131		svgButtonAbsInv.removeTranslationFromMatrix();
132		//Width vect in svgbutton parent relative space
133		v.transform(svgButtonAbsInv);
134		float width = v.length();
135		return width;
136	}
137	
138	
139	@Override
140	public void resize(float newWidth, float newHeight){ 
141		Matrix refCompLocalToWorld = new Matrix(this.getReferenceComp().getGlobalMatrix());
142		
143		//Center into world space
144		Vector3D refCompCenter = this.centerObjSpace.getCopy();
145		refCompCenter.transform(refCompLocalToWorld);
146		
147		//Remove translation for direction vectors(width/height)
148		refCompLocalToWorld.removeTranslationFromMatrix();
149		
150		//Width vect into world space
151		Vector3D objSpaceWidth = this.widthObjSpace.getCopy();
152		objSpaceWidth.transform(refCompLocalToWorld);
153//		System.out.println(" world Width vect of reference component: " + objSpaceWidth);
154		
155		/////Transform width/height/center to svgbutton relative
156		Matrix svgButtonAbsInv = new Matrix(this.getCompToResize().getGlobalInverseMatrix());
157		
158		//Center in svgbutton relative
159		refCompCenter.transform(svgButtonAbsInv);
160//		System.out.println("Centerpoint svgRelative: " + refCompCenter);
161		
162		//TODO doch wieder localbase von svg dazutransformen?
163		//svgbutton inverse parent relative machen
164		svgButtonAbsInv.multLocal(this.getCompToResize().getLocalMatrix());
165		
166//		//Center in svgbutton relative
167//		refCompCenter.transform(svgButtonAbsInv);
168//		System.out.println("Centerpoint svgRelative: " + refCompCenter);
169		
170		//Remove translation for direction vectors(width/height)
171		svgButtonAbsInv.removeTranslationFromMatrix();
172		
173		//Width vect in svgbutton parent relative
174		objSpaceWidth.transform(svgButtonAbsInv);
175//		System.out.println(" svgbutton space Width vect of reference component: " + objSpaceWidth);
176		
177		float width = objSpaceWidth.length();
178//		System.out.println("WIDTH: " + width);
179//		System.out.println("new width to set: " + newWidth);
180
181//		this.getCompToResize().scale(1/width, 1/width, 1, refCompCenter, TransformSpace.RELATIVE_TO_PARENT);
182//		this.getCompToResize().scale(newWidth, newWidth, 1, refCompCenter, TransformSpace.RELATIVE_TO_PARENT);
183		
184		//Svgbutton so scalen, dass reference comp auf unit width 1 gescaled wird
185		this.getCompToResize().scale(newWidth* (1/width),  newWidth* (1/width), 1, refCompCenter, TransformSpace.LOCAL);
186		//Svgbutton so scalen, dass reference comp auf unit gew???nschte width gescaled wird
187//		this.getCompToResize().scale(newWidth, newWidth, 1, refCompCenter, TransformSpace.LOCAL);
188	}
189	
190	
191	
192
193}