Android Open Source - LucyTheMoocher Menu Button






From Project

Back to project page LucyTheMoocher.

License

The source code is released under:

MIT License

If you think the Android project LucyTheMoocher 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.lucythemoocher.gui;
/*from w  w  w .  j a va 2 s.  co m*/

import com.lucythemoocher.Globals.Globals;
import com.lucythemoocher.graphics.Drawable;
import com.lucythemoocher.graphics.Image;

/**
 * MenuButton
 * A button has 3 images: one in normal state, one when focussed, one when clicked.
 * You can connect a listener to the button: when the button is clicked, onButtonClicked(int) 
 * will be called from the listener, with the value of the button's index. Reimplement this method
 * to to whatever you want when the button is clicked
 * 
 * The button has to be destroyed with destroy() method when you don't use it anymore (or the button will stay alive, 
 * and won't be rendered but will be clicked)
 */
public class MenuButton implements Drawable {
  private float posx_;
  private float posy_;
  private int index_;
  private boolean active_;
  private boolean clicked_;
  private boolean focussed_;
  private Image normalImage_;
  private Image focussedImage_;
  private Image clickedImage_;
  private Image currentImage_;
  private MenuButtonListener listenerToNotify_;
  private MenuButtonTouchListener menuButtonTouchListener_; // handle events for the button
  
  /**
   * Public constructor
   * @param x initial position
   * @param y initial position
   * @param index Index of the button
   * @param idImage Image resource for normal state
   * @param idImageFocussed Image resource for focussed state
   * @param idImageClicked Image resource for clicked state
   * @param listener The listener connected with the button
   */
  public MenuButton(float x, float y, int index, int idImage, int idImageFocussed, int idImageClicked, MenuButtonListener listener) {
    posx_ = x;
    posy_ = x;
    index_ = index;
    active_ = true;
    clicked_ = false;
    focussed_ = false;
    normalImage_ = new Image(idImage);
    focussedImage_ = new Image(idImageFocussed);
    clickedImage_ = new Image(idImageClicked);
    currentImage_ = normalImage_;
    listenerToNotify_ = listener;
    menuButtonTouchListener_ = new MenuButtonTouchListener(this);
  }
  
  /**
   * Draw the button at the correct position, without using scrolling
   */
  public void draw() {
    if (active_)
      Globals.getInstance().getCamera().drawImageOnHud(posx_, posy_, currentImage());
  }

  /**
   * Getter
   * @return Button's width
   */
  public float w() {
    return currentImage().w();
  }
  
  /**
   * Getter
   * @return Button's height
   */  
  public float h() {
    return currentImage().h();
  }
  
  
  /**
   * Getter
   * @return Button's position x
   */
  public float x() {
    return posx_;
  }
  
  /**
   * Getter
   * @return Button's position y
   */  
  public float y() {
    return posy_;
  }
  
  public void setPos(float x, float y) {
    posx_ = x;
    posy_ = y;
  }
  
  public Image currentImage() {
    return currentImage_;
  }
  
  public void setClicked(boolean clicked) {
    if (active_) {
      clicked_ = true;
      currentImage_ = clickedImage_;
      if (listenerToNotify_ != null)
        listenerToNotify_.onButtonClicked(index_);
    }
  }
  
  public void setFocussed(boolean focussed) {
    if (active_) {
      if (!focussed_ && focussed) {
        focussed_ = true;
        currentImage_ = focussedImage_;
      } else if (focussed_ && !focussed) {
        focussed_ = false;
        currentImage_ = normalImage_;
      }
    }
  }
  
  /**
   * Move the button so that it's center is (x, y)
   * @param x
   * @param y
   */
  public void centerOn(float x, float y) {
    posx_ = x - w() / 2;
    posy_ = y - h() / 2;
  }
  
  public void hide() {
    active_ = false;
  }
  
  public void show() {
    active_ = true;
  }
  
  /**
   * Has to be called when you don't use the button anymore
   * (or the button will stay activated but won't be rendered)
   */
  public void destroy() {
    hide();
    menuButtonTouchListener_.unregister();
  }
}




Java Source Code List

com.lucythemoocher.LucyTheMoocherActivity.java
com.lucythemoocher.FX.FXManager.java
com.lucythemoocher.FX.FX.java
com.lucythemoocher.Globals.Globals.java
com.lucythemoocher.actors.Actor.java
com.lucythemoocher.actors.ActorsManager.java
com.lucythemoocher.actors.Monster.java
com.lucythemoocher.actors.MonstersManager.java
com.lucythemoocher.actors.PlayerCharacter.java
com.lucythemoocher.actors.Projectile.java
com.lucythemoocher.actors.ProjectilesManager.java
com.lucythemoocher.actors.Tank.java
com.lucythemoocher.actors.TargetCharacter.java
com.lucythemoocher.actors.maincharacter.state.StateAttack.java
com.lucythemoocher.actors.maincharacter.state.StateFalling.java
com.lucythemoocher.actors.maincharacter.state.StateJumping.java
com.lucythemoocher.actors.maincharacter.state.StateNone.java
com.lucythemoocher.actors.maincharacter.state.StateRunning.java
com.lucythemoocher.actors.maincharacter.state.StateWallSliding.java
com.lucythemoocher.actors.maincharacter.state.StateWallWalking.java
com.lucythemoocher.actors.maincharacter.state.State.java
com.lucythemoocher.controls.AIController.java
com.lucythemoocher.controls.ActionController.java
com.lucythemoocher.controls.ButtonListener.java
com.lucythemoocher.controls.Controllable.java
com.lucythemoocher.controls.Controller.java
com.lucythemoocher.controls.GlobalController.java
com.lucythemoocher.controls.KeysListener.java
com.lucythemoocher.controls.TouchListener.java
com.lucythemoocher.events.EventNormal.java
com.lucythemoocher.events.EventSlow.java
com.lucythemoocher.events.Event.java
com.lucythemoocher.game.GameThread.java
com.lucythemoocher.game.Game.java
com.lucythemoocher.game.LevelLoader.java
com.lucythemoocher.graphics.ActorDrawer.java
com.lucythemoocher.graphics.Animation.java
com.lucythemoocher.graphics.Background.java
com.lucythemoocher.graphics.Camera.java
com.lucythemoocher.graphics.Drawable.java
com.lucythemoocher.graphics.Grid.java
com.lucythemoocher.graphics.HUD.java
com.lucythemoocher.graphics.Image.java
com.lucythemoocher.graphics.PersistentEffect.java
com.lucythemoocher.graphics.PersistentPic.java
com.lucythemoocher.graphics.PictureContainer.java
com.lucythemoocher.gui.MenuButtonListener.java
com.lucythemoocher.gui.MenuButtonTouchListener.java
com.lucythemoocher.gui.MenuButton.java
com.lucythemoocher.loops.CreditsLoop.java
com.lucythemoocher.loops.GameOverLoop.java
com.lucythemoocher.loops.InitMenuLoop.java
com.lucythemoocher.loops.LivesMenuLoop.java
com.lucythemoocher.loops.LoopGame.java
com.lucythemoocher.loops.LoopPause.java
com.lucythemoocher.loops.Loop.java
com.lucythemoocher.loops.MasterLoop.java
com.lucythemoocher.physics.Box.java
com.lucythemoocher.physics.Cinematic.java
com.lucythemoocher.physics.Map.java
com.lucythemoocher.sounds.SoundManager.java
com.lucythemoocher.sounds.SoundsState.java
com.lucythemoocher.sounds.StateLevel1.java
com.lucythemoocher.sounds.StateLevel2.java
com.lucythemoocher.sounds.StateLevel3.java
com.lucythemoocher.sounds.StateNormal.java
com.lucythemoocher.util.Direction.java
com.lucythemoocher.util.MathUtil.java
com.lucythemoocher.util.Resources.java
com.lucythemoocher.util.Timer.java