Subversion Repositories AndroidProjects

Rev

Rev 273 | Rev 317 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.Bauzoid.input;

import com.badlogic.gdx.Gdx;
import com.gebauz.Bauzoid.app.Game;
import com.gebauz.Bauzoid.app.GameObject;

public class Input extends GameObject
{
        private int mPhysicalW = 800;
        private int mPhysicalH = 480;
        private float mRealToVirtualScaleW = 1.0f;
        private float mRealToVirtualScaleH = 1.0f;

        public Input(Game game)
        {
                super(game);

        }

        public void init()
        {
                mPhysicalW = Gdx.graphics.getWidth();
                mPhysicalH = Gdx.graphics.getHeight();
                setVirtualSize(800, 480);
        }
       
        public void exit()
        {
               
        }
       
        public void update(float deltaTime)
        {
               
        }
       
        public void render()
        {
               
        }
       
        public void onPause()
        {
               
        }
       
        public void onResume()
        {
               
        }
       
        /** Get the pointer id of a touch point inside the rectangle spanned from (x1, y1) to (x2, y2) in virtual coordinates. */
        public int getIdInsideVirtual(float x1, float y1, float x2, float y2)
        {
                for (int i = 0; i < 10; i++)
                {
                        if (isInsideVirtual(i, x1, y1, x2, y2))
                                return i;
                }

                return -1;
        }
       
        /** Check if the pointer is inside the rectangle spanned from (x1, y1) to (x2, y2) in virtual coordinates. */
        public boolean isInsideVirtual(int id, float x1, float y1, float x2, float y2)
        {
                if (Gdx.input.isTouched(id))
                {
                        int x = getVirtualX(id);
                        int y = getVirtualY(id);
                       
                        if ((x >= x1) && (x <= x2) &&
                                (y >= y1) && (y <= y2))
                        {
                                return true;
                        }
                }
               
                return false;
        }
       
        /** Get the X coordinate of a touch point in virtual coordinates. */
        public int getVirtualX(int i)
        {
                int x = Gdx.input.getX(i);
               
                return (int)physicalToVirtualX(x);
        }
       
        /** Get the Y coordinate of a touch point in virtual coordinates. */
        public int getVirtualY(int i)
        {
                int y = Gdx.input.getY(i);
               
                return (int)physicalToVirtualY(y);             
        }
       
        /** Set the virtual coordinate width/height. */
        public void setVirtualSize(float w, float h)
        {
                mRealToVirtualScaleW = (float)mPhysicalW / (float)w;
                mRealToVirtualScaleH = (float)mPhysicalH / (float)h;
        }
       
        /** Convert a horizontal physical coordinate to virtual. */
        public float physicalToVirtualX(float x)
        {
                return (x / (float)mRealToVirtualScaleW);
        }
       
        /** Convert a vertical physical coordinate to virtual. */
        public float physicalToVirtualY(float y)
        {
                return (y / (float)mRealToVirtualScaleH);
        }
       
        /** Convert a horizontal virtual coordinate to physical. */
        public float virtualToPhysicalX(float x)
        {
                return (x * (float)mRealToVirtualScaleW);
        }
       
        /** Convert a vertical virtual coordinate to physical. */
        public float virtualToPhysicalY(float y)
        {
                return (y * (float)mRealToVirtualScaleH);
        }
}