/**
* This package include the UI components such as Button, EditField, Label etc.
*/
/*
* Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
* License. Full license text can be found in license.txt
*/
package minime.ui;
import javax.microedition.lcdui.Graphics;
import minime.Composite;
import minime.Drawable;
import minime.Image;
import minime.Portability;
import minime.Rectangle;
import minime.core.BoxLayout;
import minime.core.Event;
import minime.core.EventDispatcher;
import minime.core.Runtime;
import minime.core.painter.ImagePainter;
/**
* The view may have title label, SoftBar and displayable area.it can hold other
* drawables, can listen event and handle it.The contents displayed and their
* interaction with the user are defined by subclasses.
*
* @author yishu
*
*/
public class BaseView extends View {
/** The SoftBar for the view */
protected SoftBar softbar;
// private Font font;
protected Label labelTitle;
private Image bgImage;
private int bgImgX = 0;
private int bgImgY = 0;
/**
* Creates a new BaseView instance without title and softbar.
*
*/
public BaseView() {
this(null, false);
}
/**
* Creates a new BaseView instance with given title, if useSoftBar is true,
* the view will have softbar, otherwise it doesn't have.
*
* @param title
* the label instance for view title
* @param useSoftBar
* whether has softbar or not
*/
public BaseView(Label title, boolean useSoftBar) {
if (useSoftBar)
softbar = SoftBar.getInstance();
labelTitle = title;
setBgColor(Portability.defaultBaseViewBgColor);
}
/**
* Creates a new BaseView instance with given title and background image, if
* useSoftBar is true, the view will have softbar, otherwise it doesn't
* have.The background image data obtained from the resource file based on
* imRscId. The name parameter is a resource id as defined by
* ResourceManager.getImage(imRscId).
*
* @param title
* the label instance for view title
* @param useSoftBar
* whether has softbar or not
* @param imRscId
* the image resource id.
*/
public BaseView(Label title, boolean useSoftBar, int imRscId) {
this(title, useSoftBar);
setBgImage(imRscId);
}
public BaseView(Label title, boolean useSoftBar, Image image) {
this(title, useSoftBar);
setBgImage(image);
}
/**
* Attaches title and softbar for drawing and event listening.
*/
public void activate() {
// use BoxLayout to layout the view
// 3 cells: Title, body, softbar
BoxLayout bl = new BoxLayout(BoxLayout.Y_AXIS, 3);
bl.setSizeMode(0, BoxLayout.CELL_SIZE_FIT);
// body in cell 1 is set as default value, STRETCH mode
bl.setSizeMode(2, BoxLayout.CELL_SIZE_FIT);
if (softbar != null) {
// softbar is at the top position to receive event first
addToView(softbar);
// need to setWidth first to get correct softbar size
softbar.setWidth(getWidth());
softbar.layout();
softbar.setFocus(true);
bl.setDrawableAt(2, softbar, Drawable.X_ALIGN_CENTER, Drawable.Y_ALIGN_BOTTOM);
}
if (labelTitle != null) {
addToView(labelTitle);
labelTitle.setFramed(true);
labelTitle.setFrameThicknessAndColor(2, Portability.RED);
labelTitle.setWidth(getWidth()); // set title width
bl.setDrawableAt(0, labelTitle, Drawable.X_ALIGN_CENTER, Drawable.Y_ALIGN_TOP);
}
// attach itself for event listing
setLayoutManager(bl);
EventDispatcher.getInstance().attach(this);
}
/**
* Detaches title and softbar from view and event listening.
*/
public void deactivate()
{
viewComposite.deactivate();
viewComposite.removeAll();
softbar.deactivate();
EventDispatcher.getInstance().detach(this);
}
/**
* destroy the other recourses related about this view.
*/
public void destroy() {
}
/**
* Calculates the view size and updates components' position
*/
public Rectangle layout() {
// always layout base class first
Rectangle da = super.layout();
int x, y;
// update the bgImagePanel position
if (bgImage != null) {
// layout the bgImagePanel, then center horizontally
viewComposite.layout();
x = (getWidth() - viewComposite.getWidth()) / 2;
y = (getHeight() - viewComposite.getHeight()) / 2;
viewComposite.setPosition(x, y);
}
return da;
}
public void setBody(BoxLayout boxLayout) {
Composite com = boxLayout.getTarget();
boxLayout.setName("bodyLayout");
com.setName("bodyLayoutComposite");
addToView(com);
getLayoutManager().setDrawableAt(1, com, Drawable.X_ALIGN_CENTER,
Drawable.Y_ALIGN_CENTER, BoxLayout.CELL_SIZE_STRETCH);
}
public void setBody(Drawable drawable) {
addToView(drawable);
getLayoutManager().setDrawableAt(1, drawable, Drawable.X_ALIGN_CENTER,
Drawable.Y_ALIGN_CENTER, BoxLayout.CELL_SIZE_STRETCH);
}
/**
* Sets softbar three buttons' label
*
* @param leftText
* , the softbar left button's label
* @param middleText
* , the softbar middle button's label
* @param rightText
* , the softbar right button's label
*/
public void setSoftbar(int leftKey, int centerKey, int rightKey) {
if (softbar == null)
throw new IllegalStateException("Softbar not defined");
softbar.setSoftkeyLabel(SoftBar.LEFT, leftKey);
softbar.setSoftkeyLabel(SoftBar.MIDDLE, centerKey);
softbar.setSoftkeyLabel(SoftBar.RIGHT, rightKey);
}
/**
* Sets softbar three buttons' label
*
* @param leftText
* , the softbar left button's label
* @param middleText
* , the softbar middle button's label
* @param rightText
* , the softbar right button's label
*/
public void setSoftbar(String leftKey, String centerKey, String rightKey) {
if (softbar == null)
throw new IllegalStateException("Softbar not defined");
softbar.setSoftkeyLabel(SoftBar.LEFT, leftKey);
softbar.setSoftkeyLabel(SoftBar.MIDDLE, centerKey);
softbar.setSoftkeyLabel(SoftBar.RIGHT, rightKey);
}
/**
* Set softbar background images and text color when selected.
* @param sbBgImgs
* @param sbSelectedImgs
* @param textColorSelected
*/
public void setSoftBarEffectImages(Image[] sbBgImgs, Image[] sbSelectedImgs, int textColorSelected) {
if (softbar == null)
throw new IllegalStateException("Softbar not defined");
softbar.setEffectImages(sbBgImgs, sbSelectedImgs);
softbar.setTextColorSelected(textColorSelected);
}
/**
* Sets view background image with given image resource index id, use id to
* get image from resource file.If want to set backgroumd image null, id
* should be ResourceManager.NONE_RESOURCE.
*
* @param imRscId
* the background image resource index id, If want to set
* backgroumd image null, id should be
* ResourceManager.NONE_RESOURCE.
*/
public void setBgImage(int imRscId) {
if (imRscId >= 0) {
setBgImage(new Image(imRscId));
} else {
setBgImage(null);
}
}
public void setBgImage(Image img) {
// if (img != null)
// {
// bgImage = img;
// // we already have a background, remove it first
// if (bgImagePanel != null)
// {
// viewComposite.removeDrawable(bgImagePanel);
// }
//
// // create a new background image and set it a pos 0 (drawn first)
// bgImagePanel = new ImagePanel(bgImage);
// viewComposite.insertDrawable(bgImagePanel, 0);
// setBgColor(Portability.TRANSPARENT);
// }
// else
// {
// bgImage=null;
//
// if (viewComposite.drawablesInitialized())
// {
// if (viewComposite.drawableAt(0) != null
// && viewComposite.drawableAt(0).equals(bgImagePanel))
// {
// viewComposite.removeDrawable(0);
// bgImagePanel = null;
// }
// }
// }
if (img != null) {
bgImage = img;
viewComposite.setBackgroundPainter(new ImagePainter(bgImage)) ;
} else {
bgImage = null;
setBgColor(Portability.TRANSPARENT) ;
}
}
public void render(Graphics gc) {
viewComposite.render(gc);
viewComposite.setName("viewComposite");
//System.out.println(viewComposite.toString());
}
/**
* when we want to remove the background image,we use this function.
*
* @param color
*/
public void clearBackground(int color) {
setBgImage(null);
setBgColor(color);
Runtime.getInstance().repaint();
}
/**
* Sets background image position.
*
* @param x
* the x coordinate of the background image to be drawn
* @param y
* the y coordinate of the background image to be drawn
*/
public void setBgImagePos(int x, int y) {
bgImgX = x;
bgImgY = y;
}
/**
* Fires a SB_EVENT, ParamA is the one of
* KEY_SOFT_LEFT,KEY_FIRE,KEY_SOFT_RIGHT,paramB is the button label resource
* id.
*/
public boolean onEvent(Event evt) {
boolean handled = false;
if ((evt.type & Event.EVENT_MASK) == Event.POINTER_EVENT) {
handled = handlePointerEvent(evt);
} else if ((evt.type & Event.EVENT_MASK) == Event.KEY_EVENT) {
handled = handleKeyEvent(evt);
} else if ((evt.type & Event.EVENT_MASK) == Event.SB_EVENT) {
handled = onSoftBarEvent(evt);
}
if (handled)
Runtime.getInstance().repaint();
return handled;
}
/**
* Method that processes softbar events. The default implementation does
* nothing. User should override it for process softbar events.
*
* @param evt
* @return true if it is a softbar event and can be consumed by this
* Component
*/
protected boolean onSoftBarEvent(Event evt) {
return false;
}
/**
* Method that processes key events. The default implementation does
* nothing. User should override it to process key events.
*
* @param evt
* @return true if it is a key event and can be consumed by this Component
*/
protected boolean handleKeyEvent(Event evt) {
switch (evt.type) {
case Event.KEY_DOWN_EVENT:
return onKeyPressed(evt);
case Event.KEY_UP_EVENT:
return onKeyReleased(evt);
default:
// wrong pointer event type happened
return false;
}
}
/**
* Method that processes pointer events. The default implementation does
* nothing. User should override it for process pointer events.
*
* @param evt
* @return true if it is a pointer event and can be consumed by this
* Component
*/
private boolean handlePointerEvent(Event evt) {
int x = evt.paramA;
int y = evt.paramB;
switch (evt.type) {
case Event.POINTER_PRESSED:
return onPointerPressed(x, y);
case Event.POINTER_RELEASED:
return onPointerReleased(x, y);
case Event.POINTER_DRAGGED:
return onPointerDragged(x, y);
default:
// wrong pointer event type happened
return false;
}
}
public int getPriority() {
return VIEW_PRIORITY;
}
public Label getViewTitle() {
return labelTitle;
}
public void setViewTitle(Label labelTitle) {
this.labelTitle = labelTitle;
}
public void setViewTitle(int txtRscId) {
Label label = new Label(txtRscId, Portability.defaultFontTextColor);
this.labelTitle = label;
}
public Image getBgImage()
{
return bgImage;
}
/**
* Called when a key is pressed.
*
* User can choose to process either paramA or paramB paramA: key code
* paramB: game action translated from key code
*
* @param evt
*/
public boolean onKeyPressed(Event evt) {
// null implementation as default
return false;
}
/**
* Called when a key is keyReleased.
*
* User can choose to process either paramA or paramB paramA: key code
* paramB: game action translated from key code
*
* @param evt
* @return true if event is consumed
*/
public boolean onKeyReleased(Event evt) {
// null implementation as default
return false;
}
/**
* Called when the pointer is pressed.
*
* @param x
* the horizontal location where the pointer was pressed
* @param y
* the vertical location where the pointer was pressed
* @return true if event is consumed
*/
public boolean onPointerPressed(int x, int y) {
// null implementation as default
return false;
}
/**
* Called when the pointer is released.
*
* @param x
* the horizontal location where the pointer was released
* @param y
* the vertical location where the pointer was released
* @return true if event is consumed
*/
public boolean onPointerReleased(int x, int y) {
// null implementation as default
return false;
}
/**
* Called when the pointer is dragged.
*
* @param x
* the horizontal location where the pointer was dragged
* @param y
* the vertical location where the pointer was dragged
* @return true if event is consumed
*/
public boolean onPointerDragged(int x, int y) {
// null implementation as default
return false;
}
}
|