Android Open Source - game-api-android On Screen Buttons






From Project

Back to project page game-api-android.

License

The source code is released under:

MIT License

If you think the Android project game-api-android 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 android.gameengine.icadroids.input;
//w w  w .  j a v a2 s.c o m
import android.GameAPI.ICA_DROID.R;
import android.app.Activity;
import android.gameengine.icadroids.engine.GameEngine;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

/**
 * This class provides statics and a few function to easily make use of the
 * touch functionality of an android device. This class manages and draws
 * virtual buttons to the screen to detect for input. When a button is enabled (by default)
 * the user can check wether a button is pressed or not, by accessing the static
 * boolean values for each correspodending button.
 * 
 * Do not make an instance of this class yourself. If you want to use the OnScreenButtons
 * in your game, set the static variable <i>use</i> to true.
 * @author Roel & Bas
 */
public class OnScreenButtons{

  // general settings
  /** use : wether or not to actually use these buttons */
  public static boolean use;
  /** feedback: wether or not to allow feedback */
  public static boolean feedback = true;
  /** opacity: sets the opacity of all the buttons in a range from 0 to 255 */
  public static int opacity = 100;


  /**
   * holds the values of wether or not the button has been pressed. Use these
   * to check for input.
   */
  public static boolean 
  /** This static var is TRUE when input has been detected on the up key of the dpad. */ dPadUp, 
  /** This static var is TRUE when input has been detected on the down key of the dpad. */ dPadDown, 
  /** This static var is TRUE when input has been detected on the left key of the dpad. */ dPadLeft, 
  /** This static var is TRUE when input has been detected on the right key of the dpad. */ dPadRight, 
  /** This static var is TRUE when input has been detected on button A.  */ buttonA,
  /** This static var is TRUE when input has been detected on button B.  */ buttonB, 
  /** This static var is TRUE when input has been detected on button X.  */ buttonX, 
  /** This static var is TRUE when input has been detected on button Y. */ buttonY,
  /** This static var is TRUE when input has been detected on button 'start'. */ start,
  /** This static var is TRUE when input has been detected on button 'select'. */ select;


  private static View onScreenButtonsView;

  /**
   * Do not call the constructor yourself this is for the GameEngine.
   * 
   * The constructor will load the onScreenButtons.xml view and places it on
   * top of the canvas.
   * 
   * @param gameEngine
   *            : The context is required by this class so it can receive the
   *            screen width and height.
   */
  public OnScreenButtons(Activity gameEngine) {

    onScreenButtonsView = gameEngine.getLayoutInflater().inflate(
        R.layout.onscreenbuttons, null);

    if (onScreenButtonsView != null) {
      LayoutParams lp = new ViewGroup.LayoutParams(
          ViewGroup.LayoutParams.MATCH_PARENT,
          ViewGroup.LayoutParams.MATCH_PARENT);

      gameEngine.addContentView(onScreenButtonsView, lp);
    } else {
      Log.wtf("onScreenButtons", "Cannot find onscreenbutton layout!");
    }

    GameEngine.getAppView().setHapticFeedbackEnabled(true);
  }

  /**
   * Method that will be called by the OnScreenButton to notify the
   * onscreenbuttons an button has been pressed
   * 
   * @param buttonId
   *            The layout id of the button
   */
  protected static void buttonPressed(int buttonId) {
    setButtonState(buttonId, true);
  }

  /**
   * Method that will be called by the OnScreenButton to notify the
   * onscreenbuttons an button has been pressed
   * 
   * @param buttonId
   */
  protected static void buttonReleased(int buttonId) {
    setButtonState(buttonId, false);
  }

  /**
   * Set the correct static boolean state for the button.
   * 
   * @param buttonId
   *            The id of the button that has been pressed
   * @param state
   *            The state to set it to
   */
  private static void setButtonState(int buttonId, boolean state) {
    switch (buttonId) {
    case R.id.buttonA:
      buttonA = state;
      break;
    case R.id.buttonB:
      buttonB = state;
      break;
    case R.id.buttonX:
      buttonX = state;
      break;
    case R.id.buttonY:
      buttonY = state;
      break;
    case R.id.dpadUp:
      dPadUp = state;
      break;
    case R.id.dpadDown:
      dPadDown = state;
      break;
    case R.id.dpadLeft:
      dPadLeft = state;
      break;
    case R.id.dpadRight:
      dPadRight = state;
      break;
    case R.id.buttonSelect:
      select = state;
      break;
    case R.id.buttonStart:
      start = state;
      break;

    }
  }

  /**
   * Disable a onscreenbutton
   * 
   * @param buttonId
   *            The id of the button that needs to be dissabled. you can get
   *            the button id by using R.id.(button name). <br />
   *            <b> For example: </b> disableButton(R.id.dpadCenter); <br />
   *            You also have:
   *            <p />
   *            <i> buttonA, buttonB, buttonX, buttonY, dpadUp, dpadDown,
   *            dpadLeft, dpadRight, buttonSelect and buttonStart </i>
   * 
   * 
   */
  public static void disableButton(int buttonId) {
    OnScreenButton button = (OnScreenButton) onScreenButtonsView
        .findViewById(buttonId);

    if (button != null) {
      button.setVisibility(View.INVISIBLE);
    } else {
      Log.wtf("OnscreenButtons",
          "Cannot find or disable the button with id: " + buttonId);
    }
  }

}




Java Source Code List

android.gameengine.icadroids.alarms.Alarm.java
android.gameengine.icadroids.alarms.IAlarm.java
android.gameengine.icadroids.dashboard.DashboardImageView.java
android.gameengine.icadroids.dashboard.DashboardTextView.java
android.gameengine.icadroids.engine.GameEngine.java
android.gameengine.icadroids.engine.GameFPSCounter.java
android.gameengine.icadroids.engine.GameThread.java
android.gameengine.icadroids.engine.GameView.java
android.gameengine.icadroids.engine.Viewport.java
android.gameengine.icadroids.forms.GameForm.java
android.gameengine.icadroids.forms.IFormInput.java
android.gameengine.icadroids.forms.ViewCreator.java
android.gameengine.icadroids.forms.ViewRemover.java
android.gameengine.icadroids.input.MotionSensor.java
android.gameengine.icadroids.input.OnScreenButton.java
android.gameengine.icadroids.input.OnScreenButtons.java
android.gameengine.icadroids.input.TouchInput.java
android.gameengine.icadroids.objects.GameObject.java
android.gameengine.icadroids.objects.MoveableGameObject.java
android.gameengine.icadroids.objects.collisions.CollidingObject.java
android.gameengine.icadroids.objects.collisions.ICollision.java
android.gameengine.icadroids.objects.collisions.TileCollision.java
android.gameengine.icadroids.objects.graphics.AnimatedSprite.java
android.gameengine.icadroids.objects.graphics.Sprite.java
android.gameengine.icadroids.persistence.GamePersistence.java
android.gameengine.icadroids.sound.GameSound.java
android.gameengine.icadroids.sound.MusicPlayer.java
android.gameengine.icadroids.tiles.GameTiles.java
android.gameengine.icadroids.tiles.Tile.java
com.android.vissenspel.Monster.java
com.android.vissenspel.StrawberryControler.java
com.android.vissenspel.Strawberry.java
com.android.vissenspel.Vis.java
com.android.vissenspel.Vissenkom.java
game.MyAndroidGame.java
testGames.AndroidCraft_demo.java
testGames.FormTest.java
testGames.Player.java
testGames.TestGameBas.java
testGames.TestGameLex.java
testGames.TestGameRoel.java
testGames.gameEngineTest.DebugEngine.java
testGames.gameEngineTest.GameEngineTestGame.java
testGames.gameEngineTest.TestGameObject.java
testGames.gameEngineTest.debugObject.java
testGames.gameEngineTest.randomObject.java
testGames.testGameObjectBas.java
testGames.testGame.java