Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.menu;

import java.lang.reflect.InvocationTargetException;
import java.util.Vector;

import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.GameObject;
import com.gebauz.bauzoid.graphics.renderstates.RenderStates;
import com.gebauz.bauzoid.graphics.sprite.TileBatch;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;

public class Menu extends GameObject
{

        // Constants========================================================================================
       
        // Embedded Types===================================================================================
       
        public enum FadeState
        {
                FADE_IN,
                NO_FADE,
                FADE_OUT
        }
       
        // Fields===========================================================================================
       
        private String mName = "";
        private Vector<MenuItem> mMenuItems = new Vector<MenuItem>();
       
        private MenuEventListener mEventListener = null;
       
        /** Origin position (0, 0) of the Menu. Can be used to shift the entire menu. */
        private Vector2 mOrigin = new Vector2(0, 0);
       
        private boolean mVisible = true;
        private boolean mEnabled = true;
       
        /** Time required for fade in. */
        private float mFadeTime = 0.5f;
        private float mFadeTimer = 0.0f;
        private FadeState mFadeState = FadeState.NO_FADE;
       
        // Methods==========================================================================================

        public Menu(Game game, String name)
        {
                super(game);
                mName = name;
        }
       
        public void init()
        {
                if (mFadeTime > 0.0f)
                {
                        mFadeTimer = mFadeTime;
                        mFadeState = FadeState.FADE_IN;
                }
               
                for (int i = 0; i < mMenuItems.size(); i++)
                {
                        mMenuItems.get(i).init();
                }
        }
       
        public void exit()     
        {
                for (int i = 0; i < mMenuItems.size(); i++)
                {
                        mMenuItems.get(i).exit();
                }
                mMenuItems.clear();
        }
       
        public void parseLine(Tokenizer tokenizer) throws ScanException
        {
                String identifier = tokenizer.readIdentifier();
               
                if (identifier.equalsIgnoreCase("origin"))
                {
                        mOrigin = MenuUtil.parseVector2(tokenizer);                    
                }
                else if (identifier.equalsIgnoreCase("fadeTime"))
                {
                        mFadeTime = MenuUtil.parseNumber(tokenizer);
                }
                else
                {
                        // otherwise check if it's a proper menu item
                        parseMenuItem(identifier, tokenizer);
                }              
        }
       
        public void parseMenuItem(String menuItemId, Tokenizer tokenizer) throws ScanException
        {
                String menuItemName = tokenizer.readString();
               
                tokenizer.readToken("{");
               
                // find item class
                try
                {
                        Class<? extends MenuItem> menuItemClass = Class.forName(Menu.class.getPackage().getName() + "." + menuItemId).asSubclass(MenuItem.class);
                       
                        //MenuItem menuItem = menuItemClass.newInstance();
                        MenuItem menuItem = (MenuItem)menuItemClass.getDeclaredConstructor(Game.class, Menu.class, String.class).newInstance(getGame(), this, menuItemName);
                       
                        while (!tokenizer.checkToken("}"))
                        {
                                String identifier = tokenizer.readIdentifier();
                                menuItem.parseLine(identifier, tokenizer);
                        }                                      
                        tokenizer.readToken("}");
                       
                        // add item to menu item list
                        mMenuItems.add(menuItem);
                }
                catch (ClassNotFoundException e)
                {
                        throw new ScanException(e.getMessage(), tokenizer.getSurroundings());
                }
                catch (NoSuchMethodException ex)
                {
                        throw new ScanException("NoSuchMethodException when trying to instantiate " + menuItemId + "(" + menuItemName + ")", tokenizer.getSurroundings());
                }
                catch (InvocationTargetException ex)
                {
                        throw new ScanException("InvocationTargetEception when trying to instantiate " + menuItemId + "(" + menuItemName + ")", tokenizer.getSurroundings());
                }
                catch (InstantiationException ex)
                {
                        throw new ScanException("InstantiationException when trying to instantiate " + menuItemId + "(" + menuItemName + ")", tokenizer.getSurroundings());
                }
                catch (IllegalAccessException ex)
                {
                        throw new ScanException("IllegalAccessException when trying to instantiate " + menuItemId + "(" + menuItemName + ")", tokenizer.getSurroundings());
                }
        }
       
        public void update(float deltaTime)
        {
                if (mFadeTimer > 0.0f)
                        mFadeTimer -= deltaTime;
               
                if (mFadeTimer <= 0.0f)
                {
                        mFadeTimer = 0.0f;
                       
                        if (mFadeState == FadeState.FADE_OUT)
                        {
                                // disable/hide this menu
                                setVisible(false);
                                setEnabled(false);
                        }
                       
                        mFadeState = FadeState.NO_FADE;
                }
               
                for (int i = 0; i < mMenuItems.size(); i++)
                {
                        mMenuItems.get(i).update(deltaTime);
                        if (mMenuItems.get(i).getUpdateListener() != null)
                                mMenuItems.get(i).getUpdateListener().onUpdate(mMenuItems.get(i), deltaTime);
                }
               
                handleInput(deltaTime);
        }
       
        public void handleInput(float deltaTime)
        {
                if (!isEnabled())
                        return;
               
                if (mFadeState == FadeState.NO_FADE)
                {
                        for (int i = 0; i < mMenuItems.size(); i++)
                        {
                                if (mMenuItems.get(i).isEnabled())
                                        mMenuItems.get(i).handleInput(deltaTime);
                        }
                }
        }
       
        public void render()
        {
                if (!isVisible())
                        return;
               
                TileBatch batch = getGraphics().getBatch();
               
       
                RenderStates rs = getGame().getGraphics().renderStates;
                rs.pushViewMatrix();
                //rs.view = Matrix4.createTranslation(mOrigin.x, mOrigin.y, 0);
                rs.view.setTranslation(mOrigin.x, mOrigin.y, 0);
                for (int i = 0; i < mMenuItems.size(); i++)
                {
                        MenuItem item = mMenuItems.get(i);
                        if (item.isVisible())
                        {
                                // adjust alpha for fading
                               
                                batch.begin();
                               
                                float oldAlpha = item.getColor().w;
                                item.getColor().w *= getFadeAlpha();
                                item.render();
                                item.getColor().w = oldAlpha;
                               
                                batch.end();
                        }
                }
                rs.popViewMatrix();

        }
       
        public void sendEvent(MenuItem sender, String msgType, String param)
        {
                if (mEventListener != null)
                {
                        mEventListener.onMessage(this, sender, msgType, EventProcessor.parseParameter(param));
                }
        }
       
        /** Find a Menu item (first occurence) with the specified name. */
        public MenuItem findMenuItem(String name)
        {
                for (int i = 0; i < mMenuItems.size(); i++)
                {
                        MenuItem item = mMenuItems.get(i);
                        if (item.getName().equalsIgnoreCase(name))
                                return item;
                }
                return null;
        }
       
        /** Get numbers of Menu Items. */
        public int getNumMenuItems()
        {
                return mMenuItems.size();
        }
       
        /** Get a specific menu item per index. */
        public MenuItem getMenuItem(int index)
        {
                if ((index < 0) || (index >= mMenuItems.size()))
                        return null;
               
                return mMenuItems.get(index);
        }
       
        /** Start to fade in. */
        public void startFadeIn()
        {
                mFadeTimer = mFadeTime;
                mFadeState = FadeState.FADE_IN;
        }
       
        /** Start to fade out. */
        public void startFadeOut()
        {
                mFadeTimer = mFadeTime;
                mFadeState = FadeState.FADE_OUT;
        }
       
        public float getFadeAlpha()
        {
                float fadeAlpha = 1.0f;
                if (mFadeState == FadeState.FADE_IN)
                        fadeAlpha = 1.0f - mFadeTimer / mFadeTime;
                else if (mFadeState == FadeState.FADE_OUT)
                        fadeAlpha = mFadeTimer / mFadeTime;            
                return fadeAlpha;
        }

       
        // Getters/Setters==================================================================================

        public String getName() { return mName; }
       
        public final MenuEventListener getEventListener() { return mEventListener; }
        public void setEventListener(MenuEventListener handler) { mEventListener = handler; }
       
        public Vector2 getOrigin() { return mOrigin; }
        public void setOrigin(Vector2 origin) { mOrigin = origin; }
       
        public boolean isFadingDone() { return (mFadeTimer <= 0.0f); }
        public FadeState getFadeState() { return mFadeState; }

        public boolean isVisible() { return mVisible; }
        public void setVisible(boolean visible) { mVisible = visible; }

        public boolean isEnabled() { return mEnabled; }
        public void setEnabled(boolean enabled) { mEnabled = enabled; }
       
}