package com.gebauz.pingK.game;
import com.gebauz.pingK.MultitouchInput;
import com.gebauz.pingK.MultitouchInput.Finger;
public class TextButton
{
public static class TouchListener
{
public void onTouched
()
{
}
}
public enum HorizontalAlignment
{
LEFT,
CENTER,
RIGHT
};
public enum VerticalAlignment
{
TOP,
MIDDLE,
BOTTOM
};
public enum Style
{
PUSHBUTTON,
TOGGLEBUTTON
};
private Font mFont =
null;
private String mText
;
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 Style mStyle =
Style.
PUSHBUTTON;
private boolean mWasTouched =
false;
private boolean mActivated =
false;
private TouchListener mTouchListener =
null;
public TextButton
(Font font,
String text
)
{
mFont = font
;
mText = text
;
}
public void update
(float deltaTime
)
{
float topLeftX = getTopLeftX
();
float topLeftY = getTopLeftY
();
float bottomRightX = topLeftX + getWidth
();
float bottomRightY = topLeftY + getHeight
();
Finger finger = MultitouchInput.
getInstance().
getTouchPointInside(topLeftX, topLeftY, bottomRightX, bottomRightY
);
if ((finger
!=
null) && (!mWasTouched
))
{
// activate
if (mTouchListener
!=
null)
{
mTouchListener.
onTouched();
if (mStyle ==
Style.
PUSHBUTTON)
mActivated =
true;
}
}
mWasTouched =
(finger
!=
null);
}
public void render
()
{
if (mText.
length() > 0)
{
if (mActivated
)
{
float offsetX =
Font.
CHARACTER_SIZE * scale
;
mFont.
drawText("=" + mText +
"=", getTopLeftX
() - offsetX, getTopLeftY
(), scale
);
}
else
{
mFont.
drawText(mText, getTopLeftX
(), getTopLeftY
(), scale
);
}
}
}
public void setText
(String text
)
{
mText = text
;
}
public String getText
()
{
return mText
;
}
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 float getWidth
()
{
return ((float)mText.
length() * (float)Font.
CHARACTER_SIZE * scale
);
}
public float getHeight
()
{
return ((float)Font.
CHARACTER_SIZE * scale
);
}
public void setAlignment
(HorizontalAlignment horizontal, VerticalAlignment vertical
)
{
mHorizontalAlignment = horizontal
;
mVerticalAlignment = vertical
;
}
public void setTouchListener
(TouchListener listener
)
{
mTouchListener = listener
;
}
public void setStyle
(Style style
)
{
mStyle = style
;
}
}