Android Open Source - ninja-trials Volume Bar






From Project

Back to project page ninja-trials.

License

The source code is released under:

Apache License

If you think the Android project ninja-trials 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

/*
 * Ninja Trials is an old school style Android Game developed for OUYA & using
 * AndEngine. It features several minigames with simple gameplay.
 * Copyright 2013 Mad Gear Games <madgeargames@gmail.com>
 *//from  w  w  w  .  jav  a2 s. co  m
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.madgear.ninjatrials.hud;

import org.andengine.entity.Entity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;

import com.madgear.ninjatrials.managers.ResourceManager;

/**
 * Draws a "volume" bar. It is composed by two bars, the background bar, and the active bar that
 * is painted on top of the background one. The portion of the active bar that is drawed depends
 * on the value of the bar.
 * 
 * @author Madgear Games
 *
 */
public class VolumeBar extends Entity {
    private float value;
    private float x;
    private float y;
    private final static float VALUE_MIN = 0;
    private final static float VALUE_MAX = 1;
    private static float texture_width;
    private final static int texture_height = 110;
    private ITextureRegion shownBarTR = null;
    private Sprite shownBar;
    private Sprite bgBar;
    
    /**
     * Constructs a VolumeBar object.
     * @param posX The x axis position.
     * @param posY The y axis position.
     * @param valueInit The initial value (from VALUE_MIN to VALUE_MAX)
     */
    public VolumeBar(float posX, float posY, float valueInit) {
        x = posX;
        y = posY;
        if(valueInit >= VALUE_MIN && valueInit <= VALUE_MAX) value = valueInit;
        else value = 0;
        
        bgBar = new Sprite(x, y,
                ResourceManager.getInstance().mainOptionsSoundBarsInactive,
                ResourceManager.getInstance().engine.getVertexBufferObjectManager());
        attachChild(bgBar);
        
        texture_width = ResourceManager.getInstance().mainOptionsSoundBarsInactive.getWidth();

        shownBarTR = null;
        drawActiveBar();
    }
    
    /**
     * Draws the volume bar (only thr active colored part).
     */
    public void drawActiveBar() {
        int xPartShown;
        
        // Clean memory if the texture is loaded:
        if(shownBarTR!=null) {
            if(shownBarTR.getTexture().isLoadedToHardware()) {
                //shownBarTR.getTexture().unload();
                shownBarTR = null;
                shownBar.detachSelf();
            }
        }
        // Calculates the pixels that must be shown:
        xPartShown = Math.round(value * texture_width / VALUE_MAX);
        // Load texture showing only the part on the left of value:
        shownBarTR = TextureRegionFactory.extractFromTexture(
                ResourceManager.getInstance().mainOptionsSoundBarsActive.getTexture(),
                0, 0, xPartShown, texture_height, false);
        shownBar = new Sprite(
                x - texture_width/2 + shownBarTR.getWidth()/2, y,
                shownBarTR,
                ResourceManager.getInstance().engine.getVertexBufferObjectManager());
        attachChild(shownBar);
    }
    
    /**
     * Increments the value of the bar.
     * @param v The amount increased.
     */
    public void addValue(int v) {
        value+= v;
        if(value > VALUE_MAX) value = VALUE_MAX;
        if(value < VALUE_MIN) value = VALUE_MIN;
        drawActiveBar();
    }
    
    /**
     * Sets the value of the bar.
     * @param v The new value.
     */
    public void setValue(float v) {
        if(v >= VALUE_MIN && v <= VALUE_MAX) value = v;
        drawActiveBar();
    }
    
    /**
     * Returns the value of the bar.
     * @return The value.
     */
    public float getValue() {
        return value;        
    }
    
    /**
     * Returns the value of the bar in %
     * @return The %
     */
    public int getValuePercent() {
        return Math.round(value * 100);
    }
}




Java Source Code List

com.madgear.ninjatrials.AchievementsScene.java
com.madgear.ninjatrials.CharacterIntroScene.java
com.madgear.ninjatrials.ControllerOptionsScene.java
com.madgear.ninjatrials.DummyMenu.java
com.madgear.ninjatrials.GameScene.java
com.madgear.ninjatrials.IUserInput.java
com.madgear.ninjatrials.MainMenuScene.java
com.madgear.ninjatrials.MainOptionsScene.java
com.madgear.ninjatrials.ManagedLayer.java
com.madgear.ninjatrials.ManagedScene.java
com.madgear.ninjatrials.MapScene.java
com.madgear.ninjatrials.NinjaTrials.java
com.madgear.ninjatrials.PlayerSelectionScene.java
com.madgear.ninjatrials.RecordsScene.java
com.madgear.ninjatrials.ResultLoseScene.java
com.madgear.ninjatrials.ResultTrial.java
com.madgear.ninjatrials.ResultWinScene.java
com.madgear.ninjatrials.achievements.AchievementSetNinjaTrial.java
com.madgear.ninjatrials.achievements.AchievementSet.java
com.madgear.ninjatrials.achievements.Achievement.java
com.madgear.ninjatrials.hud.AchievementNotify.java
com.madgear.ninjatrials.hud.Chronometer.java
com.madgear.ninjatrials.hud.GameHUD.java
com.madgear.ninjatrials.hud.HeadCharacter.java
com.madgear.ninjatrials.hud.PowerBar.java
com.madgear.ninjatrials.hud.PrecisionAngleBar.java
com.madgear.ninjatrials.hud.PrecisionBar.java
com.madgear.ninjatrials.hud.SelectionStripe.java
com.madgear.ninjatrials.hud.ShurikenEnemyCounter.java
com.madgear.ninjatrials.hud.VolumeBar.java
com.madgear.ninjatrials.layers.GameOverLayer.java
com.madgear.ninjatrials.managers.GameManager.java
com.madgear.ninjatrials.managers.ResourceManager.java
com.madgear.ninjatrials.managers.SFXManager.java
com.madgear.ninjatrials.managers.SceneManager.java
com.madgear.ninjatrials.managers.UserData.java
com.madgear.ninjatrials.records.Record.java
com.madgear.ninjatrials.records.RecordsTableSet.java
com.madgear.ninjatrials.records.RecordsTable.java
com.madgear.ninjatrials.sequences.CreditsScene.java
com.madgear.ninjatrials.sequences.EndingScene.java
com.madgear.ninjatrials.sequences.EndingSequenceRyokoEasy.java
com.madgear.ninjatrials.sequences.EndingSequenceShoEasy.java
com.madgear.ninjatrials.sequences.Intro1Scene.java
com.madgear.ninjatrials.sequences.Intro2Scene.java
com.madgear.ninjatrials.sequences.Intro2SequenceRyokoEasy.java
com.madgear.ninjatrials.sequences.Intro2SequenceShoEasy.java
com.madgear.ninjatrials.sequences.Sequence.java
com.madgear.ninjatrials.sequences.SplashIntroScene.java
com.madgear.ninjatrials.trials.TrialSceneCut.java
com.madgear.ninjatrials.trials.TrialSceneJump.java
com.madgear.ninjatrials.trials.TrialSceneRun.java
com.madgear.ninjatrials.trials.TrialSceneShuriken.java
com.madgear.ninjatrials.trials.run.RunBg.java
com.madgear.ninjatrials.trials.run.RunCharacter.java
com.madgear.ninjatrials.trials.shuriken.ShurikenCoordinates.java
com.madgear.ninjatrials.trials.shuriken.ShurikenEnemy.java
com.madgear.ninjatrials.trials.shuriken.ShurikenHands.java
com.madgear.ninjatrials.trials.shuriken.ShurikenShuriken.java
com.madgear.ninjatrials.utils.AutoDiagonalParallaxBackground.java
com.madgear.ninjatrials.utils.AutoHorizontalParallaxBackground.java
com.madgear.ninjatrials.utils.AutoVerticalParallaxBackground.java
com.madgear.ninjatrials.utils.ParallaxBackground2d.java