📄 gameselection.java
字号:
/*
* Copyright, 2005, S.E.Morris, C.Speirs
*
* This file is part of SudokuME.
*
* SudokuME is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* SudokuME is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with SudokuME; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package sudoku;
import javax.microedition.lcdui.*;
// *********************************************************************
// A tabbed screen with various functionality. This is the 'title' page
// of the Sukodu MIDlet, providing housekeeping facilities as well as
// options to start a new game.
//
// The code uses separate model and listener classes for each tab,
// implementing the SelectorModel and SelectorListener interfaces. The
// model provides a way for the selector to query the data underlying
// the tab. The listener provides a way for each tab to respond to
// key/selection event from the selector.
//
// Of all the models/listeners the most complex is the one for the
// online tab. This has to manage the fetching of data across a slow
// network connection. The model provides methods which allow the
// selector to work with data while it is being loaded.
// *********************************************************************
class GameSelection extends Canvas implements CommandListener
{ private Display display; // Screen display
private Displayable displayable;
private GameSelectionListener listener; // Listener, singleton
private int tab=0; // Which tab?
private int saveRecDisplay=0; // Toggle display on rec.store tab
private Selector loadSelector; // Load tab list selector
private Selector newSelector; // New game tab selector
private Selector downloadSelector; // Download tab selector
private Command prevCommand,nextCommand; // Commands to navigate tabs
private RecordEntry[] recordCatalogue; // MIDlet record catalogue for load tab
private int state; // The state of the 'downloadSelector'
private boolean destroyed=false; // See paint()
private final static int TOPLEFT = Graphics.TOP|Graphics.LEFT;
private final static int CENTERED = Graphics.TOP|Graphics.HCENTER;
private final static int TABS=3; // Number of tabs
private final static int LOAD_TAB=0; // [Load]
private final static int NEW_TAB=1; // [New game]
private final static int DOWNLOAD_TAB=2; // [Download]
// -----The various states of the 'downloadSelector' etc.
private final static int UNLOADED = 0; // Nothing loaded
private final static int LOADING_CAT = 1; // In process of loading cat file
private final static int LOADED_CAT = 2; // Catalogue loaded
private final static int LOADING_BUN = 3; // In process of loading bundle file
private final static int LOADED_BUN =4; // Bundle loaded
// -----------------------------------------------------------------
// CONSTRUCTORS
// -----------------------------------------------------------------
GameSelection(Display d,GameSelectionListener lml)
{ display=d; listener=lml;
displayable=this;
recordCatalogue=GameData.catalogue();
if(recordCatalogue.length<=0) tab=NEW_TAB; else tab=LOAD_TAB;
// -----From what data exists, determine the current state
if(SudokuME.onlineCatalogue==null) state=UNLOADED;
else if(SudokuME.selectedBundle==null) state=LOADED_CAT;
else state=LOADED_BUN;
// -----Create the selectors for loading/new/online tabs
int x=1,y=24,w=getWidth()-2,h=getHeight()-24;
loadSelector = new Selector(new LoadSelectorListener(),this,x,y,w,h);
loadSelector.setModel(new LoadSelectorModel());
newSelector = new Selector(new NewSelectorListener(),this,x,y,w,h);
newSelector.setModel(new NewSelectorModel());
downloadSelector = new Selector(new DownloadSelectorListener(),this,x,y,w,h);
switch(state)
{ default :
case UNLOADED : downloadSelector.setModel(null); break;
case LOADED_CAT : downloadSelector.setModel(new DownloadSelectorModelCat()); break;
case LOADED_BUN : downloadSelector.setModel(new DownloadSelectorModelBun()); break;
}
// -----Navigate tabs using soft keys
prevCommand = new Command("<<<",Command.BACK,1);
nextCommand = new Command(">>>",Command.OK,1);
this.addCommand(prevCommand); this.addCommand(nextCommand);
this.setCommandListener(this);
display.setCurrent(this); // Set display to us
}
void destroy()
{ loadSelector=null; newSelector=null; downloadSelector=null;
recordCatalogue=null;
destroyed=true;
}
// -----------------------------------------------------------------
// Handle the display and input from [Load] tab
// -----------------------------------------------------------------
private class LoadSelectorListener implements SelectorListener, MenuListener
{ private int selIndex=-1; // Passed between listeners (bit kludgey, but works!)
public void itemSelected(int idx,int key)
{ switch(getGameAction(key))
{ case LEFT :
saveRecDisplay=(saveRecDisplay+1)%3; break;
case RIGHT :
saveRecDisplay=(saveRecDisplay+3+1)%3; break;
case FIRE :
// -----Use a record store game
listener.gameSelected(GameData.load(recordCatalogue[idx]));
break;
case GAME_A :
// -----Delete selected game
selIndex=idx;
String[][] optStr = { { "","","Yes delete", "","" , "No, don't delete" } };
int[][] optInt = { { 0,0 , 100 , 0,0 , 0 } };
new Menu(display,this,optStr,optInt,-1,-1);
break;
}
}
public void menuSelection(int code)
{ if(code==100)
{ boolean success=GameData.delete(recordCatalogue[selIndex]);
recordCatalogue=GameData.catalogue();
loadSelector.index=0;
Alert alert;
if(success)
{ alert = new Alert("Delete Game","Record has been deleted.",null,AlertType.CONFIRMATION);
}
else
{ alert= new Alert("Delete Game","FAILED: "+GameData.lastErrorMessage,null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
}
display.setCurrent(alert,GameSelection.this);
}
}
}
private class LoadSelectorModel implements SelectorModel
{ public String itemAt(int i) { return recordCatalogue[i].display[saveRecDisplay]; }
public int length() { return recordCatalogue.length; }
public int loaded() { return 0; }
public String loadingErrorMessage() { return null; }
}
// -----------------------------------------------------------------
// Handle the display and input from [New game] tab
// -----------------------------------------------------------------
private class NewSelectorListener implements SelectorListener {
public void itemSelected(int idx,int key) {
if(getGameAction(key)==FIRE) {
if(idx<2) {
int a=0;
switch(idx)
{ default :
case 0 : a=9; break;
case 1 : a=12; break;
}
listener.gameSelected(new GameData(a));
} else if (idx==2) {
listener.gameSelected(GameData.load("/game1.bin"));
} else if (idx == 3){
// Program key-codes
SudokuME.programmedKeyCodes.loadCanvas(display, displayable);
} else if (idx == 4){
// load key-codes
SudokuME.programmedKeyCodes.loadKeys();
} else {
// save key-codes
SudokuME.programmedKeyCodes.saveKeys();
}
}
}
}
private class NewSelectorModel implements SelectorModel
{ private String[] OPTIONS = { "9x9 grid","12x12 grid","Test game","Set Keys", "Load Keys", "Save Keys" };
public String itemAt(int i) { return OPTIONS[i]; }
public int length() { return OPTIONS.length; }
public int loaded() { return 0; }
public String loadingErrorMessage() { return null; }
}
// -----------------------------------------------------------------
// Handle the display and input from [D/load] tab (two models, the
// first for the catalogue list, the second for the bundle list).
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -