PageRenderTime 20ms CodeModel.GetById 6ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://mt4j.googlecode.com/
Java | 245 lines | 132 code | 32 blank | 81 comment | 11 complexity | 186fb9831f8f09eab34a9c9e15a44fd1 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.widgets;
 19
 20import java.awt.event.ActionEvent;
 21import java.awt.event.ActionListener;
 22
 23import org.mt4j.components.MTComponent;
 24import org.mt4j.components.TransformSpace;
 25import org.mt4j.components.visibleComponents.shapes.AbstractShape;
 26import org.mt4j.components.visibleComponents.shapes.MTPolygon;
 27import org.mt4j.components.visibleComponents.shapes.MTRectangle;
 28import org.mt4j.components.visibleComponents.widgets.buttons.MTSvgButton;
 29import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.IdragClusterable;
 30import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
 31import org.mt4j.util.MT4jSettings;
 32import org.mt4j.util.MTColor;
 33import org.mt4j.util.animation.Animation;
 34import org.mt4j.util.animation.AnimationEvent;
 35import org.mt4j.util.animation.IAnimation;
 36import org.mt4j.util.animation.IAnimationListener;
 37import org.mt4j.util.animation.MultiPurposeInterpolator;
 38import org.mt4j.util.math.Vector3D;
 39
 40import processing.core.PApplet;
 41import processing.core.PImage;
 42
 43/**
 44 * The Class MTImage. A widget which can be used to display an image texture surrounded
 45 * by a frame.
 46 * The image itself is actually a child of this class, which acts as the frame.
 47 * 
 48 * @author Christopher Ruff
 49 */
 50public class MTImage extends MTRectangle implements IdragClusterable{
 51	
 52	/** The selected. */
 53	private boolean selected;
 54
 55	
 56	private MTRectangle image;
 57	
 58	/**
 59	 * Instantiates a new framed image.
 60	 *
 61	 * @param texture the texture
 62	 * @param pApplet the applet
 63	 * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
 64	 */
 65	public MTImage(PImage texture, PApplet pApplet) {
 66		this(pApplet, texture);
 67	}
 68	
 69	/**
 70	 * Instantiates a new framed image.
 71	 * @param pApplet the applet
 72	 * @param texture the texture
 73	 */
 74	public MTImage(PApplet pApplet, PImage texture) {
 75		super(pApplet, -7, -7, texture.width + 14, texture.height + 14);
 76		
 77		image = new MTRectangle(pApplet, texture);
 78		image.setStrokeColor(new MTColor(255,255,255,255));
 79		image.setPickable(false);
 80		this.addChild(image);
 81		
 82		//Draw this component and its children above 
 83		//everything previously drawn and avoid z-fighting
 84		this.setDepthBufferDisabled(true);
 85	}
 86	
 87	public MTRectangle getImage(){
 88		return this.image;
 89	}
 90	
 91
 92	
 93	public boolean isSelected() {
 94		return selected;
 95	}
 96
 97	public void setSelected(boolean selected) {
 98		this.selected = selected;
 99	}
100
101	
102	/**
103	 * Sets the display close button.
104	 * 
105	 * @param dispClose the new display close button
106	 */
107	public void setDisplayCloseButton(boolean dispClose){
108		if (dispClose){
109			MTSvgButton keybCloseSvg = new MTSvgButton(this.getRenderer(), MT4jSettings.getInstance().getDefaultSVGPath()
110							+ "keybClose.svg");
111			//Transform
112			keybCloseSvg.scale(0.5f, 0.5f, 1, new Vector3D(0,0,0));
113			keybCloseSvg.translate(new Vector3D(this.getWidthXY(TransformSpace.RELATIVE_TO_PARENT) - 45, 2,0));
114			keybCloseSvg.setBoundsPickingBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
115			keybCloseSvg.addActionListener(new CloseActionListener(new MTComponent[]{this, keybCloseSvg}) );
116//			pic.addChild(keybCloseSvg);
117			keybCloseSvg.setName("closeButton");
118			this.addChild(keybCloseSvg);
119		}else{
120			//Remove svg button and destroy child display lists
121			MTComponent[] childs = this.getChildren();
122            for (MTComponent component : childs) {
123                if (component.getName().equals("closeButton")) {
124                    MTSvgButton svgButton = (MTSvgButton) component;
125                    svgButton.destroy();
126                }
127            }
128		}
129	}
130	
131
132
133
134	/**
135	 * The Class CloseActionListener. 
136	 * 
137	 * @author Cruff
138	 */
139	private class CloseActionListener implements ActionListener{
140			/** The comps. */
141			public MTComponent[] comps;
142			
143			/** The reference poly for resizing the button. */
144			private MTPolygon referencePoly;
145			
146			/**
147			 * Instantiates a new close action listener.
148			 * 
149			 * @param comps the comps
150			 */
151			public CloseActionListener(MTComponent[] comps) {
152				super();
153				this.comps = comps;
154			}
155
156			/* (non-Javadoc)
157			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
158			 */
159			public void actionPerformed(ActionEvent arg0) {
160				switch (arg0.getID()) {
161				case TapEvent.TAPPED:
162					//Get the first polygon type out of the array
163                    for (MTComponent comp : comps) { //TODO this is stupid.. redo this whole thing
164                        if (comp instanceof MTPolygon) {
165                            MTPolygon poly = (MTPolygon) comp;
166                            if (referencePoly == null) {//nur 1. occur zuweisen
167                                referencePoly = poly;
168                            }
169                        }
170                    }
171					float width = referencePoly.getWidthXY(TransformSpace.RELATIVE_TO_PARENT);
172
173					IAnimation closeAnim = new Animation("comp Fade", new MultiPurposeInterpolator(width, 1, 300, 0.5f, 0.8f, 1), referencePoly);
174					closeAnim.addAnimationListener(new IAnimationListener(){
175						public void processAnimationEvent(AnimationEvent ae) {
176							switch (ae.getId()) {
177							case AnimationEvent.ANIMATION_STARTED:
178							case AnimationEvent.ANIMATION_UPDATED:
179								float currentVal = ae.getAnimation().getValue();
180								resize(referencePoly, comps[0], currentVal, currentVal);
181								break;
182							case AnimationEvent.ANIMATION_ENDED:
183								comps[0].setVisible(false);
184								for (int i = comps.length-1; i >0 ; i--) {
185									MTComponent currentComp =  comps[i];
186									//Call destroy which fires a destroy state change event
187									currentComp.destroy();
188									//System.out.println("destroyed: " + currentComp.getName());
189								}
190								destroy();
191								//System.out.println("destroyed: " + getName());
192								break;	
193							default:
194								destroy();
195								break;
196							}//switch
197						}//processanimation
198					});//new IAnimationListener
199					closeAnim.start(); 
200					break;
201				default:
202					break;
203				}//switch aeID
204			}
205			
206			/**
207			 * Resize.
208			 * 
209			 * @param referenceComp the reference comp
210			 * @param compToResize the comp to resize
211			 * @param width the width
212			 * @param height the height
213			 */
214			protected void resize(MTPolygon referenceComp, MTComponent compToResize, float width, float height){ 
215				Vector3D centerPoint = getRefCompCenterRelParent(referenceComp);
216				compToResize.scale(1/referenceComp.getWidthXY(TransformSpace.RELATIVE_TO_PARENT), (float)1/referenceComp.getWidthXY(TransformSpace.RELATIVE_TO_PARENT), 1, centerPoint, TransformSpace.RELATIVE_TO_PARENT);
217				compToResize.scale(width, width, 1, centerPoint, TransformSpace.RELATIVE_TO_PARENT);
218			}
219			
220			
221			/**
222			 * Gets the ref comp center local.
223			 * 
224			 * @param shape the shape
225			 * 
226			 * @return the ref comp center local
227			 */
228			protected Vector3D getRefCompCenterRelParent(AbstractShape shape){
229				Vector3D centerPoint;
230				if (shape.hasBounds()){
231					centerPoint = shape.getBounds().getCenterPointLocal();
232					centerPoint.transform(shape.getLocalMatrix()); //macht den punkt in self space
233				}else{
234					Vector3D localObjCenter = shape.getCenterPointGlobal();
235					localObjCenter.transform(shape.getGlobalInverseMatrix()); //to localobj space
236					localObjCenter.transform(shape.getLocalMatrix()); //to parent relative space
237					centerPoint = localObjCenter;
238				}
239				return centerPoint;
240			}
241	}//Class closebutton actionlistener
242	
243	
244	
245}