Blame |
Last modification |
View Log
| RSS feed
package com.gebauz.bauzoid2.input;
import com.badlogic.gdx.Gdx;
import com.gebauz.bauzoid2.game.Time;
public class Input
{
public static final int NUM_FINGERS =
10;
protected int mPhysicalW =
800;
protected int mPhysicalH =
480;
protected float mVirtualW =
800;
protected float mVirtualH =
800;
protected float mRealToVirtualScaleW = 1.0f
;
protected float mRealToVirtualScaleH = 1.0f
;
public class Finger
{
private int mIndex = -
1;
private boolean mIsTouched =
false;
private boolean mIsTouchedLastFrame =
false;
private boolean mIsJustTouched =
false;
private boolean mIsJustReleased =
false;
private float x = 0.0f
;
private float y = 0.0f
;
public Finger
(int index
)
{
mIndex = index
;
}
public void update
(float deltaTime
)
{
//mIsTouched = Gdx.input.isTouched(mIndex);
mIsTouched = Input.
this.
isTouched(mIndex
);
if (mIsTouched
)
{
x = Input.
this.
getX(mIndex
);
y = Input.
this.
getY(mIndex
);
}
mIsJustTouched =
(mIsTouched
&& !mIsTouchedLastFrame
);
mIsJustReleased =
(!mIsTouched
&& mIsTouchedLastFrame
);
mIsTouchedLastFrame = mIsTouched
;
}
public final int getIndex
()
{
return mIndex
;
}
public boolean isTouched
()
{
return mIsTouched
;
}
public boolean isJustTouched
()
{
return mIsJustTouched
;
}
public boolean isJustReleased
()
{
return mIsJustReleased
;
}
public float getVirtualX
()
{
return physicalToVirtualX
(x
);
}
public float getVirtualY
()
{
return physicalToVirtualY
(y
);
}
public boolean isInsideVirtual
(float x1,
float y1,
float x2,
float y2
)
{
float x = getVirtualX
();
float y = getVirtualY
();
if ((x
>= x1
) && (x
<= x2
) &&
(y
>= y1
) && (y
<= y2
))
{
return true;
}
return false;
}
}
protected Finger
[] mFingers =
new Finger
[NUM_FINGERS
];
public Input
()
{
for (int i =
0; i
< NUM_FINGERS
; i++
)
{
mFingers
[i
] =
new Finger
(i
);
}
}
public void init
()
{
setVirtualSize
(800,
480);
}
public void exit
()
{
}
public void update
()
{
for (int i =
0; i
< NUM_FINGERS
; i++
)
{
mFingers
[i
].
update(Time.
deltaTime);
}
}
public void render
()
{
}
public void onPause
()
{
}
public void onResume
()
{
}
public void updateSurfaceDimensions
(int w,
int h
)
{
setVirtualSize
(mVirtualW, mVirtualH
);
}
public Finger getFinger
(int index
)
{
return mFingers
[index
];
}
public Finger getFingerInsideVirtual
(float x1,
float y1,
float x2,
float y2
)
{
int i = getIdInsideVirtual
(x1, y1, x2, y2
);
if (i
!= -
1)
return getFinger
(i
);
return null;
}
public Finger getJustReleasedFingerInsideVirtual
(float x1,
float y1,
float x2,
float y2
)
{
for (int i =
0; i
< NUM_FINGERS
; i++
)
{
Finger finger = mFingers
[i
];
if (finger.
isJustReleased() && finger.
isInsideVirtual(x1, y1, x2, y2
))
{
return finger
;
}
}
return null;
}
/** 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
< NUM_FINGERS
; 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))
if (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);
int x = 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);
int y = getY
(i
);
return (int)physicalToVirtualY
(y
);
}
/** Set the virtual coordinate width/height. */
public void setVirtualSize
(float w,
float h
)
{
mPhysicalW = Gdx.
graphics.
getWidth();
mPhysicalH = Gdx.
graphics.
getHeight();
mVirtualW = w
;
mVirtualH = h
;
mRealToVirtualScaleW =
(float)mPhysicalW /
(float)mVirtualW
;
mRealToVirtualScaleH =
(float)mPhysicalH /
(float)mVirtualH
;
}
/** 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
);
}
public int getX
(int index
)
{
return Gdx.
input.
getX(index
);
}
public int getY
(int index
)
{
return Gdx.
input.
getY(index
);
}
public boolean isTouched
(int index
)
{
return Gdx.
input.
isTouched(index
);
}
}