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