Android Open Source - Tanks Button






From Project

Back to project page Tanks.

License

The source code is released under:

MIT License

If you think the Android project Tanks 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 com.ThirtyNineEighty.Game.Menu.Controls;
/* w ww  . j  a v a2s . c  om*/
import com.ThirtyNineEighty.Renderable.Renderable2D.Sprite;

public class Button
  implements IControl
{
  private int pointerId;
  private boolean state;

  private Sprite sprite;

  private IClickListener clickListener;

  private float[] pressed;
  private float[] notPressed;

  private float left;
  private float right;
  private float bottom;
  private float top;

  public Button(float x, float y, float width, float height)
  {
    left = x - width / 2;
    right = x + width / 2;
    bottom = y - height / 2;
    top = y + height / 2;

    pointerId = -1;

    pressed = new float[] { 0, 0, 1, 1 };
    notPressed = new float[] { 0, 0, 1, 1 };

    sprite = new Sprite("button");
    sprite.setPosition(x, y);
    sprite.setSize(width, height);
  }

  public void dispose()
  {
    sprite.dispose();
  }

  @Override
  protected void finalize() throws Throwable
  {
    super.finalize();
    sprite.finalize();
  }

  public void setPressedTextureCoordinates(float x, float y, float width, float height)
  {
    pressed = new float[] { x, y, width, height };
    if (state)
      setTextureCoordinates(pressed);
  }

  public void setNotPressedTextureCoordinates(float x, float y, float width, float height)
  {
    notPressed = new float[] { x, y, width, height };
    if (!state)
      setTextureCoordinates(notPressed);
  }

  private void setTextureCoordinates(float[] texCoords) { sprite.setTextureCoordinates(texCoords[0], texCoords[1], texCoords[2], texCoords[3]); }

  public void setClickListener(IClickListener listener) { clickListener = listener; }
  public boolean getState() { return state; }

  @Override
  public void draw(float[] orthoViewMatrix)
  {
    sprite.draw(orthoViewMatrix);
  }

  @Override
  public void processDown(int pointerId, float x, float y)
  {
    if (isBetween(x, left, right) &&
        isBetween(y, bottom, top))
    {
      state = true;
      this.pointerId = pointerId;

      setTextureCoordinates(pressed);
    }
  }

  @Override
  public void processMove(int pointerId, float x, float y)
  {

  }

  @Override
  public void processUp(int pointerId, float x, float y)
  {
    if (this.pointerId == pointerId)
    {
      state = false;
      this.pointerId = -1;

      setTextureCoordinates(notPressed);

      if (clickListener != null)
        clickListener.onClick();
    }
  }

  private static boolean isBetween(float value, float left, float right)
  {
    return value > left && value < right;
  }

  public interface IClickListener
  {
    void onClick();
  }
}




Java Source Code List

com.ThirtyNineEighty.Game.EngineObject.java
com.ThirtyNineEighty.Game.IEngineObject.java
com.ThirtyNineEighty.Game.Collisions.Collidable.java
com.ThirtyNineEighty.Game.Collisions.Collision2D.java
com.ThirtyNineEighty.Game.Collisions.Collision3D.java
com.ThirtyNineEighty.Game.Collisions.CollisionManager.java
com.ThirtyNineEighty.Game.Collisions.Collision.java
com.ThirtyNineEighty.Game.Collisions.ICollidable.java
com.ThirtyNineEighty.Game.Gameplay.Bullet.java
com.ThirtyNineEighty.Game.Gameplay.GameObject.java
com.ThirtyNineEighty.Game.Gameplay.Tank.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.CharacteristicFactory.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Characteristic.java
com.ThirtyNineEighty.Game.Gameplay.Characteristics.Upgrade.java
com.ThirtyNineEighty.Game.Menu.BaseMenu.java
com.ThirtyNineEighty.Game.Menu.GameMenu.java
com.ThirtyNineEighty.Game.Menu.IMenu.java
com.ThirtyNineEighty.Game.Menu.Controls.Button.java
com.ThirtyNineEighty.Game.Menu.Controls.IControl.java
com.ThirtyNineEighty.Game.Worlds.GameWorld.java
com.ThirtyNineEighty.Game.Worlds.IWorld.java
com.ThirtyNineEighty.Helpers.Plane.java
com.ThirtyNineEighty.Helpers.Vector2.java
com.ThirtyNineEighty.Helpers.Vector3.java
com.ThirtyNineEighty.Helpers.VectorUtils.java
com.ThirtyNineEighty.Helpers.Vector.java
com.ThirtyNineEighty.Renderable.Renderable.java
com.ThirtyNineEighty.Renderable.Shader2D.java
com.ThirtyNineEighty.Renderable.Shader3D.java
com.ThirtyNineEighty.Renderable.Shader.java
com.ThirtyNineEighty.Renderable.Renderable2D.I2DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable2D.Label.java
com.ThirtyNineEighty.Renderable.Renderable2D.Sprite.java
com.ThirtyNineEighty.Renderable.Renderable3D.I3DRenderable.java
com.ThirtyNineEighty.Renderable.Renderable3D.Model3D.java
com.ThirtyNineEighty.System.ConfigChooser.java
com.ThirtyNineEighty.System.Content.java
com.ThirtyNineEighty.System.GameActivity.java
com.ThirtyNineEighty.System.GameContext.java
com.ThirtyNineEighty.System.IContent.java
com.ThirtyNineEighty.System.ISubprogram.java