Rev 130 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
package com.gebauz.pingK.common.game;
import com.gebauz.pingK.common.R;
import com.gebauz.pingK.common.game.MultitouchInput.Finger;
public abstract class BaseButton
{
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()
{
}
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;
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());
mCurrentlyOnButton = (finger != null);
mWasTouched = (finger != null);
}
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;
}
}