Subversion Repositories AndroidProjects

Rev

Blame | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.menu;

import com.badlogic.gdx.audio.Sound;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.input.Input;
import com.gebauz.bauzoid.input.Input.Finger;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;


/** Implements a Text Button Menu item.
 * The Button has the following additional properties:
 * - onTouch
 * - pushColor
 *
 * The size parameter is unused/ignored. The text is aligned relative to position.
 *
 * @author chiu
 *
 */

public class Button extends MenuItem
{
        // Constants========================================================================================
       
        public static String EVENT_TOUCH = "TOUCH";

        // Embedded Types===================================================================================

        // Fields===========================================================================================

        private String mOnTouchParam = "";
        private Vector4 mPushColor = new Vector4(0.5f, 0.5f, 0.5f, 1.0f);
        private Vector2 mPushOffset = new Vector2(1, 1);
       
        private Vector2 mActiveMargin = new Vector2(0, 0);
       
        /** Sound for button pushes. */
        private Sound mPushSound = null;
       
        /** Sound for button clicks. */
        private Sound mClickSound = null;
       
        private TextElement mText = null;
       
        private boolean mIsTouching[] = new boolean[Input.NUM_FINGERS];
        private boolean mIsFingerInside[] = new boolean[Input.NUM_FINGERS];
       
       
       
        // Methods==========================================================================================

        public Button(Game game, Menu parent, String name)
        {
                super(game, parent, name);
                mText = new TextElement(this);
               
                for (int i = 0; i < Input.NUM_FINGERS; i++)
                        mIsTouching[i] = false;
        }

        @Override
        public void init()
        {
                mText.init();
               
                // default width/height is the text element
                if (getWidth() == 0.0f)
                        setWidth(mText.getTextWidth());
                if (getHeight() == 0.0f)
                        setHeight(mText.getTextHeight());
        }

        @Override
        public void exit()
        {
                mText.exit();
               
                mPushSound = null;
                mClickSound = null;
        }

        @Override
        public void update(float deltaTime)
        {
                mText.update(deltaTime);
               
/*              if (isEnabled() && getParent().isEnabled())
                        handleInput(deltaTime);*/

        }
       
        public void handleInput(float deltaTime)
        {
                Input input = getGame().getInput();
               
                boolean prevButtonDown = isButtonDown();
               
                for (int i = 0; i < Input.NUM_FINGERS; i++)
                {
                        Finger finger = input.getFinger(i);
                        if (finger != null)
                        {
                                mIsFingerInside[i] = (finger.isInsideVirtual(getLeft() - mActiveMargin.x, getTop() - mActiveMargin.y, getRight() + mActiveMargin.x, getBottom() + mActiveMargin.y));
                               
                                if (finger.isJustTouched() && mIsFingerInside[i])
                                {
                                        mIsTouching[i] = true;
                                }
                               
                                if (finger.isJustReleased() && mIsTouching[i])
                                {
                                        if (mIsFingerInside[i])
                                        {
                                                if ((mClickSound != null) && (getAudio().isSoundEnabled()))
                                                        mClickSound.play();
                                               
                                                getParent().sendEvent(this, EVENT_TOUCH, mOnTouchParam);
                                        }
                                       
                                        mIsTouching[i] = false;
                                }
                        }
                }
               
                if (!prevButtonDown && isButtonDown())
                {
                        if ((mPushSound != null) && (getAudio().isSoundEnabled()))
                                mPushSound.play();
                }
        }

        @Override
        public void render()
        {
                if (isButtonDown())
                {
                        mText.render(getLeft()+mPushOffset.x, getTop()+mPushOffset.y, getRight()+mPushOffset.x, getBottom()+mPushOffset.y, getPushColor());
                }
                else
                {
                        mText.render(getLeft(), getTop(), getRight(), getBottom());
                }
        }
       
        public boolean parseLine(String identifier, Tokenizer tokenizer) throws ScanException  
        {
                if (identifier.equalsIgnoreCase("onTouch"))
                {
                        mOnTouchParam = MenuUtil.parseString(tokenizer);
                }
                else if (identifier.equalsIgnoreCase("pushColor"))
                {
                        setPushColor(MenuUtil.parseVector4(tokenizer));
                }
                else if (identifier.equalsIgnoreCase("pushOffset"))
                {
                        setPushOffset(MenuUtil.parseVector2(tokenizer));
                }
                else if (identifier.equalsIgnoreCase("activeMargin"))
                {
                        mActiveMargin = MenuUtil.parseVector2(tokenizer);
                }
                else if (identifier.equalsIgnoreCase("clickSound"))
                {
                        String name = MenuUtil.parseString(tokenizer);
                        mClickSound = getParent().getGame().getAudio().getSound(name);
                }
                else if (identifier.equalsIgnoreCase("pushSound"))
                {
                        String name = MenuUtil.parseString(tokenizer);
                        mPushSound = getParent().getGame().getAudio().getSound(name);                  
                }
                else if (!mText.parseLine(identifier, tokenizer))
                {
                        return super.parseLine(identifier, tokenizer);
                }
                return true;
        }
       
        // Getters/Setters==================================================================================

        public Vector4 getPushColor() { return mPushColor; }
        public void setPushColor(Vector4 pushColor) { mPushColor = pushColor; }

        public Vector2 getPushOffset() { return mPushOffset; }
        public void setPushOffset(Vector2 pushOffset) { mPushOffset = pushOffset; }
       
        public Vector2 getActiveMargin() { return mActiveMargin; }
        public void setActiveMargin(Vector2 activeMargin) { mActiveMargin = activeMargin; }

        public Sound getButtonPushSound() { return mPushSound; }
        public void setButtonPushSound(Sound buttonPushSound) { mPushSound = buttonPushSound; }

        public Sound getButtonClickSound() { return mClickSound; }
        public void setButtonClickSound(Sound buttonClickSound) { mClickSound = buttonClickSound; }

        public TextElement getTextElement() { return mText; }
        public void setTextElement(TextElement text) { mText = text; }
       
        public final boolean isTouching()
        {
                for (int i = 0; i < Input.NUM_FINGERS; i++)
                {
                        if (mIsTouching[i])
                                return true;
                }
                return false;
        }
       
        public final boolean isFingerInside()
        {
                for (int i = 0; i < Input.NUM_FINGERS; i++)
                {
                        if (mIsFingerInside[i])
                                return true;
                }
                return false;
        }
       
        public final boolean isButtonDown()
        {
                for (int i = 0; i < Input.NUM_FINGERS; i++)
                {
                        if (mIsFingerInside[i] && mIsTouching[i])
                                return true;
                }
                return false;
        }


}