PageRenderTime 36ms CodeModel.GetById 1ms app.highlight 28ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://mt4j.googlecode.com/
Java | 250 lines | 130 code | 36 blank | 84 comment | 27 complexity | 2742f675fb42f8ad20d2f8f820048817 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.MTCanvas;
 21import org.mt4j.components.MTComponent;
 22import org.mt4j.components.StateChange;
 23import org.mt4j.components.StateChangeEvent;
 24import org.mt4j.components.StateChangeListener;
 25import org.mt4j.components.clusters.Cluster;
 26import org.mt4j.components.clusters.ClusterManager;
 27import org.mt4j.input.inputProcessors.IGestureEventListener;
 28import org.mt4j.input.inputProcessors.MTGestureEvent;
 29import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
 30import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.IdragClusterable;
 31import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.LassoEvent;
 32import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
 33import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
 34import org.mt4j.util.MTColor;
 35
 36import processing.core.PApplet;
 37
 38/**
 39 * The Class DefaultLassoAction.
 40 * 
 41 * @author Christopher Ruff
 42 */
 43public class DefaultLassoAction implements IGestureEventListener {
 44	
 45	/** The cluster mgr. */
 46	private ClusterManager clusterMgr;
 47	
 48	/** The canvas. */
 49	private MTCanvas canvas;
 50	
 51	/** The pa. */
 52	private PApplet pa;
 53
 54	/**
 55	 * Instantiates a new default clustering action.
 56	 * 
 57	 * @param pa the pa
 58	 * @param clustermgr the clustermgr
 59	 * @param canvas the canvas
 60	 */
 61	public DefaultLassoAction(PApplet pa, ClusterManager clustermgr, MTCanvas canvas){
 62		this.pa = pa;
 63		this.clusterMgr = clustermgr;
 64		this.canvas = canvas;
 65		
 66	}
 67
 68	/* (non-Javadoc)
 69	 * @see org.mt4j.input.inputProcessors.IGestureEventListener#processGestureEvent(org.mt4j.input.inputProcessors.MTGestureEvent)
 70	 */
 71	public boolean processGestureEvent(MTGestureEvent g) {
 72		if (g instanceof LassoEvent){
 73			LassoEvent dse = (LassoEvent)g;
 74			switch (dse.getId()) {
 75			case MTGestureEvent.GESTURE_RESUMED:
 76			case MTGestureEvent.GESTURE_STARTED:
 77				//System.out.println("dse detected");
 78				canvas.addChild(dse.getSelectionPoly());
 79				break;
 80			case MTGestureEvent.GESTURE_UPDATED:
 81				//System.out.println("dse updated");
 82				break;
 83			case MTGestureEvent.GESTURE_CANCELED: 
 84			case MTGestureEvent.GESTURE_ENDED: 
 85				//TODO make method addSelection and do the stuff here
 86				//so it can be called from the outside too /addNewSelection(comps[])
 87				
 88				//System.out.println("dse ended");
 89				IdragClusterable[] selectedComps = dse.getClusteredComponents();
 90				
 91				//Create new selection only if at least more than 1 is selected
 92				if (selectedComps.length > 1){
 93					//Create new Cluster
 94					Cluster cluster = new Cluster(pa, dse.getSelectionPoly());
 95					//Attach a cam to the cluster because it doesent have the canvas as a parent as it is now..
 96					cluster.attachCamera(selectedComps[0].getViewingCamera());
 97					
 98					//Add gestures //TODO what about the isRotatable/dragable settings of the childs?
 99					//TODO What if we want to click a item of the cluster only? ->Maybe cluster should 
100					//delegate other gestures to the components..
101//					cluster.assignGestureClassAndAction(DragGestureAnalyzer.class, 	 new DefaultDragAction());
102					cluster.registerInputProcessor(new DragProcessor(pa));
103					cluster.addGestureListener(DragProcessor.class, new DefaultDragAction());
104					
105					cluster.addGestureListener(DragProcessor.class, new InertiaDragAction());
106					
107					cluster.registerInputProcessor(new RotateProcessor(pa));
108					cluster.addGestureListener(RotateProcessor.class, new DefaultRotateAction());
109					
110					cluster.registerInputProcessor(new ScaleProcessor(pa));
111					cluster.addGestureListener(ScaleProcessor.class,  new DefaultScaleAction());
112					
113					dse.getSelectionPoly().setFillColor(new MTColor(100,150,250, 50));
114					
115					dse.getSelectionPoly().setGestureAllowance(DragProcessor.class, true);
116					dse.getSelectionPoly().setGestureAllowance(RotateProcessor.class, true);
117					dse.getSelectionPoly().setGestureAllowance(ScaleProcessor.class, true);
118					
119					
120					System.out.println("\n" + selectedComps.length + " Selected:");
121//					int n = -1;
122					int n = Integer.MAX_VALUE;
123					//Set all cards selected
124					for (IdragClusterable currentComp : selectedComps){
125						System.out.print((currentComp).getName() + "  "); //remove later
126						
127						if (currentComp instanceof MTComponent){//Add selected comps to selection - RIGHT NOW ONLY SUPPORTS INSTANCES OF MTCOMPONENT!
128							MTComponent mtCurrentComp = (MTComponent)currentComp;
129							
130///////////////////////////////							
131							// Listen to destroy events of the clustered components, to remove them from
132							// the cluster and pack the polygon.
133							mtCurrentComp.addStateChangeListener(StateChange.COMPONENT_DESTROYED, new StateChangeListener(){
134								public void stateChanged(StateChangeEvent evt) {
135									if (evt.getSource() instanceof MTComponent) {
136										MTComponent sourceComp = (MTComponent) evt.getSource();
137										
138										//Remove component from cluster it is in
139										Cluster clusterOfComponent = clusterMgr.getCluster(sourceComp);
140										if (clusterOfComponent != null){
141											((IdragClusterable)sourceComp).setSelected(false);
142											//Remvove the component from its former selection
143											clusterOfComponent.removeChild(sourceComp);
144											
145											//System.out.println("Comp destroyed and removed from cluster: " + sourceComp.getName());
146											
147											//remove the former selection from the selectionmanager if it consists only of 1 less components
148											if (clusterOfComponent.getChildCount() <= 2){
149												clusterMgr.removeCluster(clusterOfComponent);
150												
151											}else{
152												//Tighten convex hull of reduced cluster
153												clusterOfComponent.packClusterPolygon();
154											}
155										}
156									}
157								}
158							});
159////////////////////////////////
160							
161							//Remove comp from former selection if it is in a new selection
162							Cluster formerSelection = clusterMgr.getCluster(currentComp);
163							if (formerSelection != null){
164								formerSelection.removeChild(mtCurrentComp);
165								//Remove the former selection from the selectionmanager if it consists only of 1 less components
166								if (formerSelection.getChildCount() <= 2){ //2 because the selection polygon is also always in the selection
167//									SceneManager.getInstance().getCurrentScene().getMainCanvas().getSelectionManager().removeSelection(formerSelection);
168									clusterMgr.removeCluster(formerSelection);
169								}else{
170									//Tighten convex hull of reduced cluster
171									formerSelection.packClusterPolygon();
172								}
173							}
174							
175							//Get the last index of the selected component in the parent list to know where to add the selectionpoly
176							if (mtCurrentComp.getParent() != null){
177								int indexInParentList = mtCurrentComp.getParent().getChildIndexOf(mtCurrentComp);
178//								if (indexInParentList > n){
179//									n = indexInParentList;
180//								}
181								if (indexInParentList < n){
182									n = indexInParentList;
183								}
184							}
185								
186							//ADD components to the selection and set it to selected
187							cluster.addChild(mtCurrentComp);
188							currentComp.setSelected(true);
189						}//if instance mtbasecomp
190					}//for
191					
192					//Draw a convex hull around all selected shapes
193					cluster.packClusterPolygon();
194					dse.getSelectionPoly().setLineStipple((short)0xDDDD);
195					dse.getSelectionPoly().setStrokeColor(new MTColor(0,0,0,255));
196					
197					//Add the selection poly 1 index after the index of the highest index of the selected components
198					if (selectedComps[0] instanceof MTComponent 
199						&& ((MTComponent)selectedComps[0]).getParent() != null){
200							MTComponent firstSelectedComp = (MTComponent)selectedComps[0];
201							
202//							System.out.println("n:" + n);
203//							System.out.println("Parent childcount: " + firstSelectedComp.getParent().getChildCount());
204							
205							firstSelectedComp.getParent().addChild(n, dse.getSelectionPoly());
206							
207//							if (n < firstSelectedComp.getParent().getChildCount())
208//								firstSelectedComp.getParent().addChild(n+1, dse.getSelectionPoly());
209//							else //FIXME this has caused out of bounds
210//								firstSelectedComp.getParent().addChild(n, dse.getSelectionPoly());
211						}
212					
213					//Add selection to selection manager
214					clusterMgr.addCluster(cluster);
215				//IF exactly 1 component is selected and its already part of an selection remove it from it without making a new selection with it
216				}else if (selectedComps.length == 1){ 
217					for (IdragClusterable currentComp : selectedComps){
218						if (currentComp instanceof MTComponent){//Add selected comps to selection - RIGHT NOW ONLY SUPPORTS INSTANCES OF MTCOMPONENT!
219							//Remove comp from former selection if it is in a new selection
220							Cluster formerSelection = clusterMgr.getCluster(currentComp);
221							if (formerSelection != null){
222								currentComp.setSelected(false);
223								//Remvove the component from its former selection
224								formerSelection.removeChild((MTComponent)currentComp);
225								//remove the former selection from the selectionmanager if it consists only of 1 less components
226								if (formerSelection.getChildCount() <= 2){
227									clusterMgr.removeCluster(formerSelection);
228								}else{
229									//Tighten convex hull of reduced cluster
230									formerSelection.packClusterPolygon();
231								}
232							}
233						}
234					}
235					
236					//Remove the Selection Polygon from the canvas when only 1 component is selected
237					clusterMgr.removeClusterPolyFromCanvas(dse.getSelectionPoly());
238				}
239				//If no comp is selected, just remove the selection polygon from canvas
240				else if (selectedComps.length < 1){ 
241					//Remove the Selection Polygon from the canvas when no component is selected
242					clusterMgr.removeClusterPolyFromCanvas(dse.getSelectionPoly());
243				}
244				break;
245			}//switch
246		}//instanceof clusterevt
247		return false;
248	}//processgesture()
249
250}