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