Android Open Source - Froggy Simple Button






From Project

Back to project page Froggy.

License

The source code is released under:

GNU General Public License

If you think the Android project Froggy 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.mopgames.ui;
/*  w ww .  j  av  a 2 s.c  o  m*/
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;

public class SimpleButton {

  private float x, y, width, height;

  private TextureRegion buttonUp;
  private TextureRegion buttonDown;
  private TextureRegion icon;

  private Rectangle bounds;

  private boolean isPressed = false;

  public SimpleButton(float x, float y, float width, float height,
      TextureRegion buttonUp, TextureRegion buttonDown, TextureRegion icon) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.buttonUp = buttonUp;
    this.buttonDown = buttonDown;
    this.icon = icon;

    bounds = new Rectangle(x, y, width, height);

  }

  public boolean isClicked(int screenX, int screenY) {
    return bounds.contains(screenX, screenY);
  }

  public void draw(SpriteBatch batcher) {
    draw(batcher, 0, 0);
  }

  public void draw(SpriteBatch batcher, float xOffset, float yOffset) {
    if (isPressed) {
      batcher.draw(buttonDown, x + xOffset, y + yOffset, width, height);
    } else {
      batcher.draw(buttonUp, x + xOffset, y + yOffset, width, height);
    }
    batcher.draw(icon, width / 2 + x + xOffset - icon.getRegionWidth() / 2,
        height / 2 + y + yOffset - icon.getRegionHeight() / 2 - 1,
        icon.getRegionWidth(), icon.getRegionHeight());
  }

  public boolean isTouchDown(int screenX, int screenY) {
    if (bounds.contains(screenX, screenY)) {
      isPressed = true;
      return true;
    }

    return false;
  }

  public boolean isTouchUp(int screenX, int screenY) {
    // It only counts as a touchUp if the button is in a pressed state.
    if (bounds.contains(screenX, screenY) && isPressed) {
      isPressed = false;
      return true;
    }

    // Whenever a finger is released, we will cancel any presses.
    isPressed = false;
    return false;
  }

}




Java Source Code List

android.UnusedStub.java
com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelper.java
com.mopgames.ActionResolverDesktop.java
com.mopgames.MainActivity.java
com.mopgames.Main.java
com.mopgames.RobovmLauncher.java
com.mopgames.Froggy.FroggyGame.java
com.mopgames.GameObjects.Background.java
com.mopgames.GameObjects.Frog.java
com.mopgames.GameObjects.Lilypad.java
com.mopgames.GameObjects.MenuItem.java
com.mopgames.GameObjects.ScrollHandler.java
com.mopgames.GameObjects.Scrollable.java
com.mopgames.GameWorld.GameRenderer.java
com.mopgames.GameWorld.GameWorld.java
com.mopgames.Helpers.ActionResolver.java
com.mopgames.Helpers.AssetLoader.java
com.mopgames.Helpers.InputHandler.java
com.mopgames.Screens.GameScreen.java
com.mopgames.Screens.SplashScreen.java
com.mopgames.TweenAccessors.SpriteAccessor.java
com.mopgames.TweenAccessors.ValueAccessor.java
com.mopgames.TweenAccessors.Value.java
com.mopgames.client.ActionResolverDesktop.java
com.mopgames.client.GwtLauncher.java
com.mopgames.ui.SimpleButton.java