package com.gebauz.bauzoid.menu;
import com.gebauz.bauzoid.game.Game;
import com.gebauz.bauzoid.game.GameObject;
import com.gebauz.bauzoid.math.Vector2;
import com.gebauz.bauzoid.math.Vector4;
import com.gebauz.bauzoid.parser.ScanException;
import com.gebauz.bauzoid.parser.Tokenizer;
/** Base class for all Menu items.
* MenuItem has the following additional properties:
* - position
* - size
* - align
*
* Note: different actual menu items might interpret these properties differently.
*
* @author chiu
*
*/
public abstract class MenuItem extends GameObject
{
// Constants========================================================================================
// Embedded Types===================================================================================
public enum HorizontalAlign
{
LEFT,
CENTER,
RIGHT,
}
public enum VerticalAlign
{
TOP,
CENTER,
BOTTOM,
}
public static interface UpdateListener
{
public void onUpdate
(MenuItem sender,
float deltaTime
);
}
// Fields===========================================================================================
private Menu mParentMenu =
null;
private String mName =
"";
private Vector2 mPosition =
new Vector2
(0,
0);
private Vector2 mSize =
new Vector2
(0,
0);
private HorizontalAlign mHorizontalAlignment = HorizontalAlign.
LEFT;
private VerticalAlign mVerticalAlignment = VerticalAlign.
TOP;
private Vector4 mColor =
new Vector4
(1,
1,
1,
1);
/** Whether this item should be rendered. */
private boolean mIsVisible =
true;
/** Whether this item should react to input. */
private boolean mIsEnabled =
true;
private UpdateListener mUpdateListener =
null;
// Methods==========================================================================================
public MenuItem(Game game,
Menu parent,
String name
)
{
super(game
);
mParentMenu = parent
;
mName = name
;
}
/** Perform initialization. */
public abstract void init
();
/** Destroy resources. */
public abstract void exit
();
/** Update internal structures. */
public abstract void update
(float deltaTime
);
/** Handle input if applicable. */
public void handleInput
(float deltaTime
) {}
/** Render the UI element. */
public abstract void render
();
/** Parse an instruction line in the body of the MenuItem.
* Returns true if the identifier has been processed, or false if not. The caller should then
* appropiately process the identifier in another way.
* @param identifier Identifier of statement to be processed
* @param tokenizer Tokenizer parsing the statement
* @return true if the statement has been processed.
* @throws ScanException
*/
public boolean parseLine
(String identifier, Tokenizer tokenizer
) throws ScanException
{
if (identifier.
equalsIgnoreCase("position"))
{
setPosition
(MenuUtil.
parseVector2(tokenizer
));
}
else if (identifier.
equalsIgnoreCase("size"))
{
setSize
(MenuUtil.
parseVector2(tokenizer
));
}
else if (identifier.
equalsIgnoreCase("align"))
{
mHorizontalAlignment = MenuUtil.
stringToHorizontalAlignment(tokenizer.
readIdentifier());
tokenizer.
readToken(",");
mVerticalAlignment = MenuUtil.
stringToVerticalAlignment(tokenizer.
readIdentifier());
tokenizer.
readToken(";");
}
else if (identifier.
equalsIgnoreCase("color"))
{
setColor
(MenuUtil.
parseVector4(tokenizer
));
}
else if (identifier.
equalsIgnoreCase("visible"))
{
mIsVisible = MenuUtil.
parseBool(tokenizer
);
}
else if (identifier.
equalsIgnoreCase("enabled"))
{
mIsEnabled = MenuUtil.
parseBool(tokenizer
);
}
else
{
// When reached here, nothing interpreted the identifer, so we can throw a ScanException
throw new ScanException
("Syntax error", tokenizer.
getSurroundings());
}
return true;
}
// Getters/Setters==================================================================================
public Menu getParent
() { return mParentMenu
; }
public String getName
() { return mName
; }
public Vector2 getPosition
() { return mPosition
; }
public void setPosition
(Vector2 position
) { mPosition = position
; }
public void setPosition
(float x,
float y
) { mPosition.
x = x
; mPosition.
y = y
; }
public float getPositionX
() { return mPosition.
x; }
public void setPositionX
(float positionX
) { mPosition.
x = positionX
; }
public float getPositionY
() { return mPosition.
y;}
public void setPositionY
(float positionY
) { mPosition.
y = positionY
; }
public HorizontalAlign getHorizontalAlignment
() { return mHorizontalAlignment
;}
public void getHorizontalAlignment
(HorizontalAlign horizonalAlignment
) { mHorizontalAlignment = horizonalAlignment
;}
public VerticalAlign getVerticalAlignment
() { return mVerticalAlignment
; }
public void setVerticalAlignment
(VerticalAlign verticalAlignment
) { mVerticalAlignment = verticalAlignment
;}
public Vector4 getColor
() { return mColor
; }
public void setColor
(Vector4 color
) { mColor = color
; }
public Vector2 getSize
() { return mSize
; }
public void setSize
(Vector2 size
) { mSize = size
; }
public float getWidth
() { return mSize.
x; }
public void setWidth
(float width
) { mSize.
x = width
; }
public final float getHeight
() { return mSize.
y; }
public void setHeight
(float height
) { mSize.
y = height
; }
public boolean isVisible
() { return mIsVisible
; }
public void setVisible
(boolean visible
) { mIsVisible = visible
; }
public boolean isEnabled
() { return mIsEnabled
; }
public void setEnabled
(boolean enabled
) { mIsEnabled = enabled
; }
public void setUpdateListener
(UpdateListener listener
) { mUpdateListener = listener
; }
public UpdateListener getUpdateListener
() { return mUpdateListener
; }
public float getLeft
()
{
if (mHorizontalAlignment == HorizontalAlign.
RIGHT)
return mPosition.
x - mSize.
x;
else if (mHorizontalAlignment == HorizontalAlign.
CENTER)
return mPosition.
x - mSize.
x/
2;
else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
return mPosition.
x;
}
public float getTop
()
{
if (mVerticalAlignment == VerticalAlign.
BOTTOM)
return mPosition.
y - mSize.
y;
else if (mVerticalAlignment == VerticalAlign.
CENTER)
return mPosition.
y - mSize.
y/
2;
else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
return mPosition.
y;
}
public float getRight
()
{
if (mHorizontalAlignment == HorizontalAlign.
RIGHT)
return mPosition.
x;
else if (mHorizontalAlignment == HorizontalAlign.
CENTER)
return mPosition.
x + mSize.
x/
2;
else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
return mPosition.
x + mSize.
x;
}
public float getBottom
()
{
if (mVerticalAlignment == VerticalAlign.
BOTTOM)
return mPosition.
y;
else if (mVerticalAlignment == VerticalAlign.
CENTER)
return mPosition.
y + mSize.
y/
2;
else //if (mHorizontalAlignment == HorizontalAlignment.LEFT)
return mPosition.
y + mSize.
y;
}
}