Android Open Source - android_game_engine Toggle Button






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine 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 org.es.engine.hud;
/*from w ww.ja va 2  s.co  m*/
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

import org.es.engine.graphics.utils.DrawingParam;

/**
 * @author Cyril Leroux
 *         Created 02/03/14.
 */
public abstract class ToggleButton extends Button {

    protected final Bitmap mNormalBitmap2;
    protected final Bitmap mPressedBitmap2;

    private boolean mSwitch;

    public ToggleButton(float xCoef, float yCoef, float coefWidth, float coefHeight,
                        Resources resources,
                        int resIdNormal, int resIdPressed,
                        int resIdNormal2, int resIdPressed2) {
        super(xCoef, yCoef, coefWidth, coefHeight, resources, resIdNormal, resIdPressed);

        mNormalBitmap2 = BitmapFactory.decodeResource(resources, resIdNormal2);
        mPressedBitmap2 = BitmapFactory.decodeResource(resources, resIdPressed2);

        mSwitch = true;
    }

    /**
     * Draw the HUD element.
     * @param canvas The canvas on which to draw.
     * @param param Not used for the main HUD. Can not be null for other HUD elements.
     */
    @Override
    public void draw(Canvas canvas, DrawingParam param) {
        if (mSwitch) {
            super.draw(canvas, param);
        } else {
            canvas.drawBitmap((isPressed() ? mPressedBitmap2 : mNormalBitmap2), null, getBounds(), null);
        }
    }

    @Override
    protected void release(boolean inBounds) {
        if (inBounds && mPressed) {
            if (mSwitch) {
                onClick();
            } else {
                onClick2();
            }
            mSwitch = !mSwitch;
        }
        mPressed = false;
    }

    protected abstract void onClick2();

    /** Used to notify a change from outside. */
    public void setState1() { mSwitch = true; }

    /** Used to notify a change from outside. */
    public void setState2() { mSwitch = false; }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java