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