Android Open Source - androidtbsgame Image Button Sprite






From Project

Back to project page androidtbsgame.

License

The source code is released under:

MIT License

If you think the Android project androidtbsgame listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package de.mmssb.androidtbsgame.andengine.hud;
//from   w  w  w  . j  a  v a  2  s  . c  o  m
import org.andengine.entity.scene.Scene;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

/**
 * @author Manu
 */
public class ImageButtonSprite extends ButtonSprite {
  private float imageX;
  private float imageY;
  private Sprite buttonImage;
  private boolean selected;

  /**
   * Creates an instance of {@link ImageButtonSprite}
   * 
   * @param pX
   *            local x translation
   * @param pY
   *            local y translation
   * @param width
   *            ImageButtonSprite's width
   * @param height
   *            ImageButtonSprite's height
   * @param pTiledTextureRegion
   *            three tiled region used for: pressed, not pressed, disabled
   * @param textureRegion
   *            button icon
   * @param parent
   *            parent scene
   * @param pVertexBufferObjectManager
   *            {@link VertexBufferObjectManager} used to initialize the
   *            sprites
   */
  public ImageButtonSprite(float pX, float pY, float width, float height,
      ITiledTextureRegion pTiledTextureRegion,
      ITextureRegion textureRegion, Scene parent,
      VertexBufferObjectManager pVertexBufferObjectManager) {
    super(pX, pY, pTiledTextureRegion, pVertexBufferObjectManager);
    setWidth(width);
    setHeight(height);
    buttonImage = new Sprite(0, 0, textureRegion,
        pVertexBufferObjectManager);
    // Initialize bounds to fit every device
    float resizeFactor = this.getHeight() * 0.5f / buttonImage.getHeight();
    buttonImage.setWidth(buttonImage.getWidth() * resizeFactor);
    buttonImage.setHeight(buttonImage.getHeight() * resizeFactor);
    imageX = (this.getWidth() - buttonImage.getWidth()) / 2;
    imageY = (this.getHeight() - buttonImage.getHeight()) / 2;
    buttonImage.setPosition(imageX, imageY);
    this.attachChild(buttonImage);
    parent.registerTouchArea(this);
  }

  /*
   * (non-Javadoc)
   * 
   * @see
   * org.andengine.entity.sprite.ButtonSprite#onAreaTouched(org.andengine.
   * input.touch.TouchEvent, float, float)
   */
  @Override
  public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
      float pTouchAreaLocalX, float pTouchAreaLocalY) {
    if (pSceneTouchEvent.isActionDown()) {
      onSelected();
    } else if (pSceneTouchEvent.isActionUp()) {
      onUnselected();
    }
    return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
        pTouchAreaLocalY);
  }

  /**
   * Visual reaction on selection
   */
  public void onSelected() {
    selected = true;
    setCurrentTileIndex(1);
    if (this.buttonImage != null) {
      buttonImage.setX(imageX + 5);
      buttonImage.setY(imageY + 5);
    }
  }

  /**
   * Visual reaction on unselect
   */
  public void onUnselected() {
    selected = false;
    setCurrentTileIndex(0);
    if (this.buttonImage != null) {
      buttonImage.setX(imageX);
      buttonImage.setY(imageY);
    }
  }

  /**
   * @return the selected
   */
  public boolean isSelected() {
    return selected;
  }

  /**
   * @param selected
   *            the selected to set
   */
  public void setSelected(boolean selected) {
    this.selected = selected;
  }
}




Java Source Code List

de.mmssb.androidtbsgame.andengine.BoardMap.java
de.mmssb.androidtbsgame.andengine.GameControl.java
de.mmssb.androidtbsgame.andengine.MainActivity.java
de.mmssb.androidtbsgame.andengine.Model.java
de.mmssb.androidtbsgame.andengine.entities.board.Board.java
de.mmssb.androidtbsgame.andengine.entities.board.Cell.java
de.mmssb.androidtbsgame.andengine.entities.board.Mark.java
de.mmssb.androidtbsgame.andengine.hud.ImageButtonSprite.java
de.mmssb.androidtbsgame.andengine.hud.ModelHUD.java
de.mmssb.androidtbsgame.andengine.managers.CellControl.java
de.mmssb.androidtbsgame.andengine.managers.Manager.java
de.mmssb.androidtbsgame.andengine.managers.ResourceManager.java
de.mmssb.androidtbsgame.andengine.managers.SceneManager.java
de.mmssb.androidtbsgame.andengine.managers.SharedPreferencesManager.java
de.mmssb.androidtbsgame.andengine.scenes.GameScene.java
de.mmssb.androidtbsgame.andengine.scenes.ModelScene.java
de.mmssb.androidtbsgame.andengine.units.AnimationProperties.java