/src/org/mt4j/components/visibleComponents/widgets/buttons/MTImageButton.java

http://mt4j.googlecode.com/ · Java · 172 lines · 77 code · 30 blank · 65 comment · 4 complexity · 39cd1d48e9f9d7b0f3fdbf84ca55a551 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.buttons;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21. import java.util.ArrayList;
  22. import org.mt4j.components.bounds.BoundsZPlaneRectangle;
  23. import org.mt4j.components.bounds.IBoundingShape;
  24. import org.mt4j.components.interfaces.IclickableButton;
  25. import org.mt4j.components.visibleComponents.shapes.AbstractShape;
  26. import org.mt4j.components.visibleComponents.shapes.MTRectangle;
  27. import org.mt4j.input.gestureAction.DefaultButtonClickAction;
  28. import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
  29. import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
  30. import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
  31. import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapEvent;
  32. import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
  33. import processing.core.PApplet;
  34. import processing.core.PImage;
  35. /**
  36. * The Class MTImageButton. Can be used as a button displaying an image.
  37. * A tapprocessor is registered automatically. We can check if the button was
  38. * clicked by adding an actionlistener to it.
  39. * @author Christopher Ruff
  40. */
  41. public class MTImageButton extends MTRectangle implements IclickableButton {
  42. /** The selected. */
  43. private boolean selected;
  44. /** The registered action listeners. */
  45. private ArrayList<ActionListener> registeredActionListeners;
  46. /**
  47. * Instantiates a new mT image button.
  48. *
  49. * @param texture the texture
  50. * @param pApplet the applet
  51. * @deprecated constructor will be deleted! Please , use the constructor with the PApplet instance as the first parameter.
  52. */
  53. public MTImageButton(PImage texture, PApplet pApplet) {
  54. this(pApplet, texture);
  55. }
  56. /**
  57. * Instantiates a new mT image button.
  58. * @param pApplet the applet
  59. * @param texture the texture
  60. */
  61. public MTImageButton(PApplet pApplet, PImage texture) {
  62. super(pApplet, texture);
  63. this.registeredActionListeners = new ArrayList<ActionListener>();
  64. this.setName("Unnamed image button");
  65. this.selected = false;
  66. this.setGestureAllowance(DragProcessor.class, false);
  67. this.setGestureAllowance(RotateProcessor.class, false);
  68. this.setGestureAllowance(ScaleProcessor.class, false);
  69. this.setEnabled(true);
  70. this.setBoundsBehaviour(AbstractShape.BOUNDS_ONLY_CHECK);
  71. //Make clickable
  72. this.setGestureAllowance(TapProcessor.class, true);
  73. this.registerInputProcessor(new TapProcessor(pApplet));
  74. this.addGestureListener(TapProcessor.class, new DefaultButtonClickAction(this));
  75. //Draw this component and its children above
  76. //everything previously drawn and avoid z-fighting
  77. this.setDepthBufferDisabled(true);
  78. }
  79. @Override
  80. protected void setDefaultGestureActions() {
  81. //Dont register the usual drag,scale,rot processors
  82. }
  83. @Override
  84. protected IBoundingShape computeDefaultBounds(){
  85. return new BoundsZPlaneRectangle(this);
  86. }
  87. /**
  88. * Adds the action listener.
  89. *
  90. * @param listener the listener
  91. */
  92. public synchronized void addActionListener(ActionListener listener){
  93. if (!registeredActionListeners.contains(listener)){
  94. registeredActionListeners.add(listener);
  95. }
  96. }
  97. /**
  98. * Removes the action listener.
  99. *
  100. * @param listener the listener
  101. */
  102. public synchronized void removeActionListener(ActionListener listener){
  103. if (registeredActionListeners.contains(listener)){
  104. registeredActionListeners.remove(listener);
  105. }
  106. }
  107. /**
  108. * Gets the action listeners.
  109. *
  110. * @return the action listeners
  111. */
  112. public synchronized ActionListener[] getActionListeners(){
  113. return registeredActionListeners.toArray(new ActionListener[this.registeredActionListeners.size()]);
  114. }
  115. /**
  116. * Fire action performed.
  117. */
  118. protected synchronized void fireActionPerformed() {
  119. ActionListener[] listeners = this.getActionListeners();
  120. for (ActionListener listener : listeners) {
  121. listener.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "action performed on tangible button"));
  122. }
  123. }
  124. /**
  125. * fires an action event with a ClickEvent Id as its ID.
  126. *
  127. * @param ce the ce
  128. */
  129. public synchronized void fireActionPerformed(TapEvent ce) {
  130. ActionListener[] listeners = this.getActionListeners();
  131. for (ActionListener listener : listeners) {
  132. listener.actionPerformed(new ActionEvent(this, ce.getTapID(), "action performed on tangible button"));
  133. }
  134. }
  135. public boolean isSelected() {
  136. return selected;
  137. }
  138. public void setSelected(boolean selected) {
  139. this.selected = selected;
  140. // this.setStrokeWeight(selected ? this.getStrokeWeight() + 2 : 0);
  141. }
  142. }