📄 menucanvas.java
字号:
// Copyright (c) 2005 Sony Ericsson Mobile Communications AB
//
// This software is provided "AS IS," without a warranty of any kind.
// ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
// INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
//
// THIS SOFTWARE IS COMPLEMENTARY OF JAYWAY AB (www.jayway.se)
package bluegammon.gui;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import bluegammon.Audio;
import bluegammon.Device;
import bluegammon.Bluegammon;
import bluegammon.Resources;
import bluegammon.RmsFacade;
import bluegammon.gui.menu.BinaryPageItem;
import bluegammon.gui.menu.DefaultMenuPainter;
import bluegammon.gui.menu.ItemAction;
import bluegammon.gui.menu.Menu;
import bluegammon.gui.menu.MenuListener;
import bluegammon.gui.menu.MenuPage;
import bluegammon.gui.menu.PageItem;
import bluegammon.gui.popup.Popup;
import bluegammon.logic.GameRecord;
import bluegammon.logic.Rules;
/**
* The <code>MenuCanvas</code> is the handler of the Bluegammon
* menu. There is only one menu per game, thus the singleton pattern.
*
* @see bluegammon.gui.menu.Menu
* @author Peter Andersson
*/
public class MenuCanvas extends PopupCanvas
implements MenuListener, ItemAction, CommandListener
{
/** Item property key containing help text */
protected static final int ITEM_HELP = 0;
/** Action key for starting a local game */
protected static final int ACTION_LOCAL_START = 1;
/** Action key for resuming a local game */
protected static final int ACTION_LOCAL_RESUME = 2;
/** Action key for quitting midlet */
protected static final int ACTION_QUIT = 3;
/** Action key for showing specific score*/
protected static final int ACTION_SCORE = 4;
/** Action key for turning audio on/off */
protected static final int ACTION_AUDIO = 100;
/** Action key for turning vibra on/off */
protected static final int ACTION_VIBRA = 101;
/** Action key for showing about box */
protected static final int ACTION_ABOUT = 200;
/** Softbutton back command */
protected static final Command CMD_BACK =
new Command(Resources.getString(Resources.TXT_C_BACK), Command.BACK, 1);
/** Softbutton help command */
protected static final Command CMD_HELP =
new Command(Resources.getString(Resources.TXT_C_HELP), Command.ITEM, 1);
/** The menu instance */
protected Menu m_menu;
/** Softbuttons of the backgammon menu canvas */
protected SoftButtonControl m_softButtons;
/** The background image */
protected Image m_background;
/** Special page with scores */
protected MenuPage m_scorePage;
/** The local resume page item */
protected PageItem m_resumeGameItem;
/** The scores item */
protected PageItem m_scoresItem;
/** Rule setting, max five, item */
protected RuleFlagItem m_max5Item;
/** Rule setting, even out, item */
protected RuleFlagItem m_evenOutItem;
/** Singleton instance */
protected static MenuCanvas m_inst = null;
/**
* Returns the singleton instance.
* @return The singleton instance.
*/
public static MenuCanvas getInstance()
{
if (m_inst == null)
{
m_inst = new MenuCanvas();
}
return m_inst;
}
/**
* Creates the menu and initiates the gui controls
*/
protected MenuCanvas()
{
m_background = Resources.getImage(Resources.IMG_BACKGROUND);
m_softButtons = new SoftButtonControl();
PageItem item;
// Create menu
MenuPage mainPage =
new MenuPage(Resources.getChars(Resources.TXT_T_BLUEGAMMON), null);
m_menu =
new Menu(mainPage, this, new BackgammonMenuPainter(getWidth()));
// Menu titles
MenuPage btPage =
new MenuPage(Resources.getChars(Resources.TXT_T_BLUETOOTH), null);
MenuPage phonePage =
new MenuPage(Resources.getChars(Resources.TXT_T_PHONEGAME), null);
MenuPage settingsPage =
new MenuPage(Resources.getChars(Resources.TXT_T_SETTINGS), null);
MenuPage rulesPage =
new MenuPage(Resources.getChars(Resources.TXT_T_RULES), null);
m_scorePage =
new MenuPage(Resources.getChars(Resources.TXT_T_SCORES), null);
// Menu main page
PageItem btItem =
new PageItem(Resources.getChars(Resources.TXT_I_BTGAME), null, null, btPage);
btItem.setEnabled(Device.canBluetooth());
btItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_BT_GAME));
PageItem phoneItem =
new PageItem(Resources.getChars(Resources.TXT_I_PHONE_GAME), null, null, phonePage);
phoneItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_PHONE_GAME));
m_scoresItem =
new PageItem(Resources.getChars(Resources.TXT_I_BT_SCORES), null, this, m_scorePage);
m_scoresItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_SCORES));
PageItem settingsItem =
new PageItem(Resources.getChars(Resources.TXT_I_SETTINGS), null, null, settingsPage);
settingsItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_SETTINGS));
mainPage.addItem(btItem);
mainPage.addItem(phoneItem);
mainPage.addItem(m_scoresItem);
mainPage.addItem(settingsItem);
mainPage.addItem(
new PageItem(Resources.getChars(Resources.TXT_I_ABOUT), null, this, null, ACTION_ABOUT));
mainPage.addItem(
new PageItem(Resources.getChars(Resources.TXT_I_EXIT), null, this, null, ACTION_QUIT));
// Menu bluetooth page
PageItem btServerItem =
new PageItem(Resources.getChars(Resources.TXT_I_BT_SERVER), null, new BluetoothServerWorkflow(), null);
btServerItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_BT_SERVER));
BluetoothDevicePage bdp =
new BluetoothDevicePage(Resources.getChars(Resources.TXT_T_DEVICES), m_menu, this, m_softButtons);
BluetoothClientWorkflow bch = new BluetoothClientWorkflow(bdp);
bdp.setClientWorkflow(bch);
PageItem btClientItem =
new PageItem(Resources.getChars(Resources.TXT_I_BT_CLIENT), null, bch, bdp);
btClientItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_BT_CLIENT));
btPage.addItem(btServerItem);
btPage.addItem(btClientItem);
// Menu local phone page
m_resumeGameItem =
new PageItem(Resources.getChars(Resources.TXT_I_PG_RESUME), null, this, null, ACTION_LOCAL_RESUME);
m_resumeGameItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_PG_RESUME));
PageItem pgNewGameItem =
new PageItem(Resources.getChars(Resources.TXT_I_PG_NEW_GAME), null, this, null, ACTION_LOCAL_START);
pgNewGameItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_PG_NEW_GAME));
phonePage.addItem(m_resumeGameItem);
phonePage.addItem(pgNewGameItem);
// Rules page
m_max5Item = new RuleFlagItem (
Rules.MAX_FIVE, Resources.getChars(Resources.TXT_I_R_MAXFIVE),
Resources.getImage(Resources.IMG_ENABLED),
Resources.getImage(Resources.IMG_DISABLED), Integer.MIN_VALUE);
m_max5Item.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_R_MAX_FIVE));
m_evenOutItem = new RuleFlagItem (
Rules.EVEN_OUT, Resources.getChars(Resources.TXT_I_R_EVENOUT),
Resources.getImage(Resources.IMG_ENABLED),
Resources.getImage(Resources.IMG_DISABLED), Integer.MIN_VALUE);
m_evenOutItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_R_EVEN_OUT));
rulesPage.addItem(m_max5Item);
rulesPage.addItem(m_evenOutItem);
// Settings page
PersistenceFlagItem perfColItem = new PersistenceFlagItem(
Bluegammon.BLACK_PREFERRED, Resources.getChars(Resources.TXT_I_S_COLOR),
Resources.getImage(Resources.IMG_BLACK_PIECE),
Resources.getImage(Resources.IMG_WHITE_PIECE), Integer.MIN_VALUE);
perfColItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_S_COLOR));
PersistenceFlagItem audioItem = new PersistenceFlagItem(
Bluegammon.AUDIO_OFF, Resources.getChars(Resources.TXT_I_S_AUDIO),
Resources.getImage(Resources.IMG_AUDIO_OFF),
Resources.getImage(Resources.IMG_AUDIO_ON), ACTION_AUDIO);
audioItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_S_AUDIO));
PersistenceFlagItem vibraItem = new PersistenceFlagItem(
Bluegammon.VIBRA_OFF, Resources.getChars(Resources.TXT_I_S_VIBRA),
Resources.getImage(Resources.IMG_VIBRA_OFF),
Resources.getImage(Resources.IMG_VIBRA_ON), ACTION_VIBRA);
vibraItem.setEnabled(Device.canVibrate());
vibraItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_S_VIBRA));
PageItem rulesItem =
new PageItem(Resources.getChars(Resources.TXT_I_PG_RULES), null, null, rulesPage);
rulesItem.setLayout(PageItem.LAYOUT_ALIGN_RIGHT);
rulesItem.setProperty(ITEM_HELP, Resources.getChars(Resources.TXT_H_S_RULES));
settingsPage.addItem(perfColItem);
settingsPage.addItem(audioItem);
settingsPage.addItem(vibraItem);
settingsPage.addItem(rulesItem);
setFullScreenMode(true);
// Initiate softbuttons
m_softButtons.init(this,
Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_SMALL),
CMD_BACK, CMD_HELP);
m_softButtons.setCommandListener(this);
m_softButtons.enable(CMD_HELP, false);
// Setup menu
int menuPadding = 16;
m_menu.setLocation(0, menuPadding);
m_menu.setDimensions(getWidth(), getHeight() - menuPadding * 2);
m_menu.setFrameData(10, 20);
m_menu.setListener(this);
m_menu.start();
}
/**
* Initializes the states of items and
* starts the muzak, called on an already
* initialized menu when it is focused again.
*/
public void initShow()
{
m_max5Item.setBoolean(Rules.isSet(Rules.MAX_FIVE));
m_evenOutItem.setBoolean(Rules.isSet(Rules.EVEN_OUT));
m_resumeGameItem.setEnabled(Bluegammon.hasSavedLocalGame());
m_scoresItem.setEnabled(GameRecord.countRecords() > 0);
m_scorePage.removeAllItems();
Vector recs = GameRecord.getAllRecords();
for (int i = 0; i < recs.size(); i++)
{
GameRecord rec = (GameRecord)recs.elementAt(i);
if (rec.getGameCount() > 0)
{
m_scorePage.addItem(new GameRecordPageItem(rec));
}
}
PageItem selItem = m_menu.getSelectedItem();
if (selItem != null)
{
Object helpTxt = selItem.getProperty(ITEM_HELP);
m_softButtons.enable(CMD_HELP, helpTxt != null);
}
Audio.playSound(Audio.MUSIC);
}
/**
* Paints the menu, the possible popup, and the softbuttons.
* @param g The graphics context to draw on.
*/
protected void paint(Graphics g)
{
g.setColor(0x888833);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(m_background,
getWidth() / 2, getHeight() / 2, Graphics.VCENTER | Graphics.HCENTER);
m_menu.paint(g);
m_softButtons.paint(g);
if (getPopup() != null && getPopup().isActive())
{
getPopup().paint(g);
}
}
/**
* Called when user presses a key. Dispatches the keypress to
* possible popup, menu and softbuttons.
* @param keyCode The code of the key that is pressed.
*/
protected void keyPressed(int keyCode)
{
if (getPopup() != null && getPopup().isActive())
{
getPopup().keyPressed(keyCode, getGameAction(keyCode));
repaint();
}
else
{
m_menu.keyPressed(keyCode);
m_softButtons.keyPressed(keyCode);
}
}
// Commandlistener implementation
// See interface javadoc
public void commandAction(Command c, Displayable d)
{
if (c == CMD_BACK)
{
m_menu.goBack();
}
else if (c == CMD_HELP)
{
PageItem item = m_menu.getSelectedItem();
if (item != null)
{
char[] helpTxt = (char[])item.getProperty(ITEM_HELP);
if (helpTxt != null)
{
Bluegammon.showPopup(helpTxt, Popup.ALT_OK, 0, 0, 0, null);
}
}
}
}
/**
* ItemAction implementation, called from items in
* this menu. Instead of creating one class per action,
* we centralize the action behaviour here. Each action
* is identified by its id.
*
* @param page The page from which the action was called
* @param item The item to which the action belongs
*/
public void itemAction(MenuPage page, PageItem item)
{
int id = item.getId();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -