Subversion Repositories AndroidProjects

Rev

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

package com.gebauz.pingk.ui;

import com.badlogic.gdx.Gdx;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.input.Input;
import com.gebauz.pingk.game.GameConsts;

public abstract class BaseButton extends UiObject
{
        public static float TOUCH_TOLERANCE = 15.0f;
       
        public static class TouchListener
        {
                public void onTouched(BaseButton sender, int tag)
                {                      
                }
        }
       
        public enum HorizontalAlignment
        {
                LEFT,
                CENTER,
                RIGHT
        };
       
        public enum VerticalAlignment
        {
                TOP,
                MIDDLE,
                BOTTOM
        };
       
        public float x = 0.0f;
        public float y = 0.0f;
        public float scale = 1.0f;

        private HorizontalAlignment mHorizontalAlignment = HorizontalAlignment.LEFT;
        private VerticalAlignment mVerticalAlignment = VerticalAlignment.TOP;
       
        private boolean mWasTouched = false;
        private boolean mPushed = false;
        private boolean mCurrentlyOnButton = false;
       
        protected TouchListener mTouchListener = null;
       
        private int mTag = 0;
       
        public BaseButton(Game game)
        {
                super(game);

        }
       
        @Override
        public void init()
        {
               
        }
       
        @Override
        public void exit()
        {
               
        }

        @Override
        public void update(float deltaTime)
        {
                float topLeftX = getTopLeftX() - TOUCH_TOLERANCE;
                float topLeftY = getTopLeftY() - TOUCH_TOLERANCE;
                float bottomRightX = topLeftX + getWidth() + 2 *TOUCH_TOLERANCE;
                float bottomRightY = topLeftY + getHeight() + 2 * TOUCH_TOLERANCE;
               
/*              int id = getGame().getInput().getIdInsideVirtual(topLeftX, topLeftY, bottomRightX, bottomRightY);
                if ((id != -1) && (Gdx.input.justTouched()) && (!mWasTouched))
                {
                        if (mTouchListener != null)
                        {
                                // TODO: sound
                                mTouchListener.onTouched(this, mTag);
                        }
                }*/

               
                //Input.Finger finger = getGame().getInput().getFingerInsideVirtual(topLeftX, topLeftY, bottomRightX, bottomRightY);
                //if ((finger != null) && (finger.isJustTouched()) /*&& (!mWasTouched)*/)
                Input.Finger finger = getGame().getInput().getJustReleasedFingerInsideVirtual(topLeftX, topLeftY, bottomRightX, bottomRightY);
                if (finger != null)
                {
                        if (mTouchListener != null)
                        {
                                // TODO: sound                         
                                mTouchListener.onTouched(this, mTag);
                                //Gdx.app.log(GameConsts.LOG_TAG, "TEST");
                        }
                }
               
/*              Finger finger = MultitouchInput.getInstance().getTouchPointInside(topLeftX, topLeftY, bottomRightX, bottomRightY);
                //if ((finger == null) && (mWasTouched) && (MultitouchInput.getInstance().getTouchPoint(mLastId) == null))
                if ((finger != null) && (finger.isNew()) && (!mWasTouched))
                {
                        // activate on touch down
                        if (mTouchListener != null)
                        {
                                GameServices.playSound(R.raw.paddleimpact);
                                mTouchListener.onTouched(this, mTag);
                        }
                }*/

                //mPushed = (finger != null) && (finger.isNew());
                finger = getGame().getInput().getFingerInsideVirtual(topLeftX, topLeftY, bottomRightX, bottomRightY);
                mPushed = (finger != null);
                mCurrentlyOnButton = (finger != null);
                mWasTouched = (finger != null);
        }

        @Override
        public void render()
        {

        }

        public float getTopLeftX()
        {
                switch (mHorizontalAlignment)
                {
                case LEFT:
                        return x;
                case CENTER:
                        return (x - (getWidth() / 2.0f));
                case RIGHT:
                        return (x - getWidth());
                }
               
                return x;
        }
       
        public float getTopLeftY()
        {
                switch (mVerticalAlignment)
                {
                case TOP:
                        return y;
                case MIDDLE:
                        return (y - (getHeight() / 2.0f));
                case BOTTOM:
                        return (y - getHeight());
                }
               
                return y;
        }

        public abstract float getWidth();
       
        public abstract float getHeight();
       
        public void setAlignment(HorizontalAlignment horizontal, VerticalAlignment vertical)
        {
                mHorizontalAlignment = horizontal;
                mVerticalAlignment = vertical;
        }
       
        public void setTouchListener(TouchListener listener)
        {
                mTouchListener = listener;
        }

        public void setTag(int tag)
        {
                mTag = tag;            
        }
       
        public int getTag()
        {
                return mTag;
        }
       
        public boolean isPushed()
        {
                return mPushed;
        }
       
        public boolean isCurrentlyTouched()
        {
                return mCurrentlyOnButton;
        }
       
}