Subversion Repositories AndroidProjects

Rev

Rev 337 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.pingk.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.gebauz.Bauzoid.graphics.Font;
import com.gebauz.Bauzoid.graphics.sprite.Sprite;
import com.gebauz.Bauzoid.math.Vector2;
import com.gebauz.Bauzoid.math.Vector4;
import com.gebauz.pingk.PingkCustomServices;
import com.gebauz.pingk.game.GameConsts;
import com.gebauz.pingk.game.GameLogic;

public class MenuBar extends Entity
{
        private Font mFont = null;
       
        private Sprite mSoundOn = null;
        private Sprite mSoundOff = null;
        private Sprite mPause = null;
        private Sprite mResume = null;
        private Sprite mLogo = null;
       
        private Sprite mBackground = null;
        private Sprite mPingkLogo = null;
       
        private boolean mLastSoundPressed = false;
        private boolean mLastPausePressed = false;
       
        private boolean mPreviousMusicEnabled = true;
       
        public MenuBar(GameLogic gameLogic)
        {
                super(gameLogic);
        }

        @Override
        public void init()
        {
                mFont = PingkCustomServices.getInstance().getFont();
               
                float size = getVirtualHeight() - 15.0f;
                float adSize = getGameLogic().dipToVirtualX(GameConsts.MENUBAR_AD_WIDTH_DIP);
                float leftOfAdX = (GameConsts.VIRTUAL_SCREEN_WIDTH - adSize) / 2.0f;
               
/*              float density = Gdx.graphics.getDensity();
               
                Gdx.app.log(GameConsts.LOG_TAG, "MENUBAR_AD_WIDTH_DIP = " + GameConsts.MENUBAR_AD_WIDTH_DIP);
                Gdx.app.log(GameConsts.LOG_TAG, "density = " + density);
                Gdx.app.log(GameConsts.LOG_TAG, "adSize = " + adSize);*/

               
                mBackground = new Sprite(getGameLogic().getGraphics(), new Texture(Gdx.files.internal("data/textures/scorebarbg.png")));
                mBackground.x = 0.0f;
                mBackground.y = getGameLogic().getPlayField().getVirtualHeight();
                mBackground.w = GameConsts.VIRTUAL_SCREEN_WIDTH;
                mBackground.h = getVirtualHeight();
                mBackground.pivotX = 0.0f;
                mBackground.pivotY = 0.0f;
               
                mPingkLogo = new Sprite(getGameLogic().getGraphics(), new Texture(Gdx.files.internal("data/textures/logo.png")));
                mPingkLogo.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mPingkLogo.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mPingkLogo.w = 2*size;
                mPingkLogo.h = size;
                mPingkLogo.pivotX = size;
                mPingkLogo.pivotY = 0.0f;
                mPingkLogo.color = GameConsts.PINGK_COLOR.copy();
       
                mSoundOn = new Sprite(getGameLogic().getGraphics(),
                                new Texture(Gdx.files.internal("data/textures/menu_soundon.png")));
                mSoundOn.x = leftOfAdX - GameConsts.MENUBAR_OFFSET_X;
                mSoundOn.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mSoundOn.w = size;
                mSoundOn.h = size;
                mSoundOn.pivotX = size;
                mSoundOn.pivotY = 0;
                mSoundOn.color = GameConsts.PINGK_COLOR;
               
                mSoundOff = new Sprite(getGameLogic().getGraphics(),
                                new Texture(Gdx.files.internal("data/textures/menu_soundoff.png")));
                mSoundOff.x = leftOfAdX - GameConsts.MENUBAR_OFFSET_X;
                mSoundOff.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mSoundOff.w = size;
                mSoundOff.h = size;
                mSoundOff.pivotX = size;
                mSoundOff.pivotY = 0;
                mSoundOff.color = GameConsts.PINGK_COLOR;
               
                mPause = new Sprite(getGameLogic().getGraphics(),
                                new Texture(Gdx.files.internal("data/textures/menu_pause.png")));
                mPause.x = leftOfAdX + adSize + GameConsts.MENUBAR_OFFSET_X;
                mPause.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mPause.w = size;
                mPause.h = size;
                mPause.pivotX = 0;
                mPause.pivotY = 0;
                mPause.color = GameConsts.PINGK_COLOR;
               
                mResume = new Sprite(getGameLogic().getGraphics(),
                                new Texture(Gdx.files.internal("data/textures/menu_resume.png")));
                mResume.x = leftOfAdX + adSize + GameConsts.MENUBAR_OFFSET_X;
                mResume.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mResume.w = size;
                mResume.h = size;
                mResume.pivotX = 0;
                mResume.pivotY = 0;
                mResume.color = GameConsts.PINGK_COLOR;
               
                mLogo = new Sprite(getGameLogic().getGraphics(),
                                new Texture(Gdx.files.internal("data/textures/logo.png")));
                mLogo.x = GameConsts.VIRTUAL_SCREEN_WIDTH / 2.0f;
                mLogo.y = getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y;
                mLogo.w = 2*size;
                mLogo.h = size;
                mLogo.pivotX = size;
                mLogo.pivotY = 0;
                mLogo.color = GameConsts.PINGK_COLOR;
        }
       
        @Override
        public void exit()
        {
                if (mSoundOn != null)
                {
                        mSoundOn.dispose();
                        mSoundOn = null;
                }
               
                if (mPingkLogo != null)
                {
                        mPingkLogo.dispose();
                        mPingkLogo = null;
                }
               
                if (mSoundOff != null)
                {
                        mSoundOff.dispose();
                        mSoundOff = null;
                }
               
                if (mResume != null)
                {
                        mResume.dispose();
                        mResume = null;
                }
               
                if (mPause != null)
                {
                        mPause.dispose();
                        mPause = null;
                }
               
                if (mLogo != null)
                {
                        mLogo.dispose();
                        mLogo = null;
                }
               
                // release reference only
                mFont = null;
        }
       
        @Override
        public void update(float deltaTime)
        {
                boolean isSoundPressed = checkButtonPressed(mSoundOn, GameConsts.BUTTON_TOUCH_TOLERANCE);
                if ((!mLastSoundPressed) && (isSoundPressed))                  
                {      
                        getAudio().setSoundEnabled(!getAudio().isSoundEnabled());
                        if (!getAudio().isSoundEnabled())
                        {
                                // turn off music also, but store previous music setting
                                mPreviousMusicEnabled = getAudio().isMusicEnabled();
                                getAudio().setMusicEnabled(false);
                        }
                        else
                        {
                                // restore previous music setting
                                getAudio().setMusicEnabled(mPreviousMusicEnabled);
                        }
                }
                mLastSoundPressed = isSoundPressed;
               
                boolean isPausePressed = checkButtonPressed(mPause, GameConsts.BUTTON_TOUCH_TOLERANCE);
                if ((!mLastPausePressed) && (isPausePressed))                  
                {
                        getGameLogic().pause(!getGameLogic().isPaused());                                                                      
                }
                mLastPausePressed = isPausePressed;
        }
       
        public boolean checkButtonPressed(Sprite sprite, float tolerance)
        {
                Vector2 topLeft = sprite.getTopLeft();
                Vector2 bottomRight = sprite.getBottomRight();
               
                int id = getGameLogic().getGame().getInput().getIdInsideVirtual(topLeft.x - tolerance, topLeft.y - tolerance,
                                bottomRight.x + tolerance, bottomRight.y + tolerance);
                if (id != -1)
                {
                        return true;
                }
                return false;
        }
       
        @Override
        public void render()
        {              
                float scale = getGameLogic().getGraphics().getDipToPixelScale();
               
                mBackground.render();
               
                String score1 = String.format("%02d", getGameLogic().getScore(Paddle.PLAYER_1));
                mFont.drawText(score1,
                                GameConsts.SCOREBAR_MARGIN_X,
                                getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y,
                                GameConsts.PINGK_COLOR, scale);
               
                String score2 = String.format("%02d", getGameLogic().getScore(Paddle.PLAYER_2));
                float textWidth = mFont.getTextWidth(score2, scale);
                mFont.drawText(score2,
                                GameConsts.VIRTUAL_SCREEN_WIDTH - GameConsts.SCOREBAR_MARGIN_X - textWidth,
                                getGameLogic().getPlayField().getVirtualHeight() + GameConsts.SCOREBAR_MARGIN_Y,
                                GameConsts.PINGK_COLOR, scale);

                if (getAudio().isSoundEnabled())
                {              
                        mSoundOn.render();
                }
                else
                {
                        mSoundOff.render();
                }

                if (!getGameLogic().isPaused())
                {
                        mPause.render();
                }
                else
                {
                        mResume.render();
                }
               
                if (!getGameLogic().getGame().areAdsEnabled())
                {
                        mPingkLogo.render();
                }
               
                // render pingK logo on non-ad version
/*              if (!GameServices.areAdsEnabled())
                {
                        float alpha = GameServices.getRandomFloat(0.7f, 1.0f);
                        mLogo.setColor(GameConsts.PINGK_COLOR.x, GameConsts.PINGK_COLOR.y, GameConsts.PINGK_COLOR.z, alpha);
                        mLogo.render();
                }*/

        }
       
        public float getVirtualHeight()
        {
                return GameConsts.VIRTUAL_SCREEN_HEIGHT - getGameLogic().getPlayField().getVirtualHeight();    
        }
}