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