📄 xmain.java
字号:
int numOptions = 0;
String[][] tempOptions = createOptions();
if (tempOptions != null) {
numOptions = tempOptions.length;
}
if (numOptions == 0) {
options = null;
} else {
options = new String[numOptions][];
if (tempOptions != null) {
for (int i = 0; i < tempOptions.length; i++) {
options[i] = tempOptions[i];
}
}
}
// create records
int numRecords = 0;
byte[][] tempRecords = recordInitialData();
if (tempRecords != null) {
numRecords = tempRecords.length;
}
if (numOptions > 0) {
numRecords++;
}
recordData = new byte[numRecords][];
if (tempRecords != null) {
for (int i = 0; i < tempRecords.length; i++) {
recordData[i] = tempRecords[i];
}
}
if (numOptions > 0) {
recordData[numRecords - 1] = new byte[numOptions];
for (int i = 0; i < numOptions; i++) {
recordData[numRecords - 1][i] = 0;
}
}
// load options
if (numOptions > 0) {
byte[][] temp = new byte[1][];
recordLoad(temp, recordData.length - 1);
optionSelected = temp[0];
}
break;
case STATE_LOGO_TWO:
dataLoad();
break;
}
}
protected void inputLogo() {
if (inputPressed != INPUT_NONE) {
counter = 20;
}
}
protected void logicLogo() {
if (counter >= 20) {
switch (state) {
case STATE_LOGO_ONE:
setState(STATE_LOGO_TWO);
break;
case STATE_LOGO_TWO:
setState(STATE_TITLE);
break;
}
}
}
protected void paintLogo(Graphics g) {
switch (state) {
case STATE_LOGO_ONE:
g.setColor(0xFFFFFF);
break;
case STATE_LOGO_TWO:
g.setColor(0x000000);
break;
}
g.fillRect(0, 0, width, height);
}
// ************************************
// state: title
// ************************************
protected void enterTitle() {
counter = 0;
}
protected void inputTitle() {
if (inputPressed != INPUT_NONE) {
counter = 20;
}
}
protected void logicTitle() {
if (counter >= 20) {
setState(STATE_MENU_MAIN);
}
}
protected void paintTitle(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, width, height);
}
// ************************************
// state: menu
// ************************************
protected int menuX;
protected int menuY;
protected int menuW;
protected int menuH;
protected int menuItemHeight;
protected int menuItemSelected;
protected String[] menuItemName;
protected boolean[] menuItemVisible;
protected void enterMenu() {
menuItemName = null;
menuItemVisible = null;
createMenu();
if (menuItemName == null) {
menuItemName = new String[0];
}
if (menuItemVisible == null) {
int length = menuItemName.length;
menuItemVisible = new boolean[length];
for (int i = 0; i < length; i++) {
menuItemVisible[i] = true;
}
}
if (state == STATE_MENU_MAIN) {
int length = menuItemName.length + 3;
String[] tempName = new String[length];
boolean[] tempVisible = new boolean[length];
for (int i = 0; i < length - 3; i++) {
tempName[i] = menuItemName[i];
tempVisible[i] = menuItemVisible[i];
}
tempName[length - 3] = "@MENU_MAIN_OPTION@";
tempName[length - 2] = "@MENU_MAIN_HELP@";
tempName[length - 1] = "@MENU_MAIN_QUIT@";
tempVisible[length - 3] = (options != null);
tempVisible[length - 2] = true;
tempVisible[length - 1] = true;
menuItemName = tempName;
menuItemVisible = tempVisible;
}
menuItemSelected = -1;
menuDown();
if (state == STATE_MENU_MAIN) {
setSoftkey(SOFTKEY_OK, SOFTKEY_NONE);
} else {
setSoftkey(SOFTKEY_OK, SOFTKEY_BACK);
}
}
protected void inputMenu() {
switch (inputPressed) {
case INPUT_UP:
menuUp();
repaint = true;
break;
case INPUT_DOWN:
menuDown();
repaint = true;
break;
case INPUT_OK:
case INPUT_FIRE:
actionMenu();
break;
case INPUT_CANCEL:
if (state == STATE_MENU_HELP) {
setState(STATE_MENU_MAIN);
}
break;
}
}
protected void logicMenu() {
}
protected void paintMenu(Graphics g) {
drawMenuBackground(g);
int total = 0;
for (int i = 0; i < menuItemVisible.length; i++) {
if (menuItemVisible[i]) {
total++;
}
}
int n = menuH / menuItemHeight;
if (n > total) {
n = total;
}
int space = (menuH - menuItemHeight * n) / (n + 1);
int start = menuItemSelected - (n - 1) / 2;
if (start < 0) {
start = 0;
}
if (start > total - n) {
start = total - n;
}
int y = menuY + space;
int temp = 0;
for (int i = 0; i < menuItemVisible.length; i++) {
if (menuItemVisible[i]) {
if (temp >= start) {
drawMenuItem(g, i, menuX, y);
if ((state == STATE_OPTIONS) && (i == menuItemSelected)
&& (objectArrow >= 0)) {
int tempY = y + menuItemHeight / 2;
drawObject(g, objectArrow, 2, counter, menuX, tempY);
drawObject(g, objectArrow, 3, counter, menuX + menuW,
tempY);
}
y += menuItemHeight + space;
}
temp++;
if (temp >= start + n) {
break;
}
}
}
if (objectArrow >= 0) {
if (start > 0) {
drawObject(g, objectArrow, 0, counter, menuX + menuW / 2, menuY);
}
if (start < total - n) {
drawObject(g, objectArrow, 1, counter, menuX + menuW / 2, menuY
+ menuH);
}
}
}
protected void createMenu() {
menuX = 0;
menuY = 0;
menuW = width;
menuH = height;
menuItemHeight = fontHeight + 2;
}
protected void actionMenu() {
if (state == STATE_MENU_MAIN) {
switch (menuItemName.length - menuItemSelected) {
case 3:
setState(STATE_OPTIONS);
break;
case 2:
setState(STATE_MENU_HELP);
break;
case 1:
destroy();
break;
}
}
}
protected void drawMenuBackground(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, width, height);
}
protected void drawMenuItem(Graphics g, int item, int x, int y) {
if (item == menuItemSelected) {
g.setColor(0xFF0000);
} else {
g.setColor(0x000000);
}
g.drawString(menuItemName[item], x, y, TOP_LEFT);
}
private void menuUp() {
int temp = menuItemSelected - 1;
while ((temp >= 0) && (!menuItemVisible[temp])) {
temp--;
}
if (temp >= 0) {
menuItemSelected = temp;
}
}
private void menuDown() {
int temp = menuItemSelected + 1;
while ((temp < menuItemVisible.length) && (!menuItemVisible[temp])) {
temp++;
}
if (temp < menuItemVisible.length) {
menuItemSelected = temp;
}
}
// ************************************
// state: options
// ************************************
private String[][] options;
protected byte[] optionSelected;
protected void enterOptions() {
int numOptions = options.length;
menuItemName = new String[numOptions];
menuItemVisible = new boolean[numOptions];
for (int i = 0; i < numOptions; i++) {
menuItemName[i] = options[i][optionSelected[i]];
menuItemVisible[i] = true;
}
menuItemSelected = -1;
menuDown();
setSoftkey(SOFTKEY_OK, SOFTKEY_BACK);
}
protected void inputOptions() {
switch (inputPressed) {
case INPUT_UP:
case INPUT_DOWN:
inputMenu();
break;
case INPUT_LEFT:
case INPUT_RIGHT:
int temp = optionSelected[menuItemSelected];
if (inputPressed == INPUT_LEFT) {
temp--;
if (temp < 0) {
temp = options[menuItemSelected].length - 1;
}
} else {
temp++;
if (temp > options[menuItemSelected].length - 1) {
temp = 0;
}
}
optionSelected[menuItemSelected] = (byte) (temp);
menuItemName[menuItemSelected] = options[menuItemSelected][temp];
repaint = true;
break;
case INPUT_OK:
case INPUT_CANCEL:
byte[][] data = new byte[1][];
if (inputPressed == INPUT_OK) {
data[0] = optionSelected;
recordSave(data, recordData.length - 1);
if (optionSelected[options.length - 1] == 0) {
playLoop(musicLoop);
} else {
stopLoop();
}
} else {
recordLoad(data, recordData.length - 1);
optionSelected = data[0];
}
setState(STATE_MENU_MAIN);
break;
}
}
protected void logicOptions() {
}
protected void paintOptions(Graphics g) {
paintMenu(g);
}
protected String[][] createOptions() {
return null;
}
// ************************************
// state: message
// ************************************
protected String[] message;
protected int messageX;
protected int messageY;
protected int messageW;
protected int messageH;
protected int messageColor;
protected int messageSelected;
protected int messageStateOK;
protected int messageStateCancel;
protected void enterMessage() {
createMessage();
messageSelected = 0;
createText(message[messageSelected], messageX, messageY, messageW,
messageH, messageColor);
}
protected void inputMessage() {
switch (inputPressed) {
case INPUT_UP:
textUp();
repaint = true;
break;
case INPUT_DOWN:
textDown();
repaint = true;
break;
case INPUT_OK:
case INPUT_FIRE:
messageSelected++;
if (messageSelected < message.length) {
createText(message[messageSelected], messageX, messageY,
messageW, messageH, messageColor);
} else {
setState(messageStateOK);
}
break;
case INPUT_CANCEL:
setState(messageStateCancel);
break;
}
}
protected void logicMessage() {
}
protected void paintMessage(Graphics g) {
drawMessageBackground(g);
drawText(g);
}
protected void createMessage() {
messageX = 0;
messageY = 0;
messageW = width;
messageH = height;
messageColor = 0x000000;
}
protected void drawMessageBackground(Graphics g) {
g.setColor(0xFFFFFF);
g.fillRect(0, 0, width, height);
}
// ************************************
// state: game
// ************************************
protected void enterGame() {
}
protected void leaveGame() {
}
protected void logicGame() {
}
// ************************************
// sound
// ************************************
private static final String[] SOUND_OPTIONS = { "@OPTIONS_SOUND_ON@",
"@OPTIONS_SOUND_OFF@" };
private Object musicLoop = null;
private boolean isPlaying = false;
protected final Object createSound(String name) {
return createMusic(name);
}
protected final void deleteSound(Object sound) {
deleteMusic(sound);
}
protected final void playSound(Object sound) {
playMusic(sound);
}
protected final Object createMusic(String name) {
return null;
}
protected final void deleteMusic(Object music) {
}
protected void playMusic(Object music) {
stopLoop();
musicLoop = null;
if (optionSelected[options.length - 1] == 1) {
return;
}
}
protected void playLoop(Object music) {
if (isPlaying) {
if (music == musicLoop) {
return;
}
stopLoop();
}
musicLoop = music;
if (optionSelected[options.length - 1] == 1) {
return;
}
isPlaying = true;
}
protected void stopLoop() {
if (!isPlaying) {
return;
}
isPlaying = false;
}
// ************************************
// debug
// ************************************
protected final void showInfo(String info) {
System.out.println(info);
}
protected final void showError(Exception e) {
e.printStackTrace();
}
// ************************************
// graphics
// ************************************
public static final int TOP_LEFT = Graphics.TOP | Graphics.LEFT;
public static final int TOP_RIGHT = Graphics.TOP | Graphics.RIGHT;
protected final void drawClip(Graphics g, Image img, int posX, int posY,
int clipX, int clipY, int clipW, int clipH) {
int tempX = g.getClipX();
int tempY = g.getClipY();
int tempW = g.getClipWidth();
int tempH = g.getClipHeight();
if ((posX + clipW <= tempX) || (posX >= tempX + tempW)
|| (posY + clipH <= tempY) || (posY >= tempY + tempH)) {
return;
}
int x = posX;
int y = posY;
if (x < tempX) {
x = tempX;
}
if (y < tempY) {
y = tempY;
}
int w = posX + clipW - x;
int h = posY + clipH - y;
if (x + w > tempX + tempW) {
w = tempX + tempW - x;
}
if (y + h > tempY + tempH) {
h = tempY + tempH - y;
}
g.setClip(x, y, w, h);
g.drawImage(img, posX - clipX, posY - clipY, TOP_LEFT);
g.setClip(tempX, tempY, tempW, tempH);
}
protected final void drawNumber(Graphics g, int object, int n, int x,
int y, int w) {
if (n == 0) {
drawObject(g, object, 0, 0, x, y);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -