// 6/30/10 import java.io.*; import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.game.*; import javax.microedition.midlet.*; public class TictacGui extends Gui implements Appgui { static public final int CONNECT = 0, DISCONNECT = -1, EXIT = -2, HELP = -3, LOG = -4, REPLAY = -5, SETTINGS = -6; static private final int INFRONT = 0, WHITE = 0xFFFFFF, BACKGROUNDCOLOR = WHITE, BOARDBOTTOM = 300; static private String GAMEOVERFILE = "gameover.png", TICTACBOARDFILE = "tictacboard.png"; static private TictacGui instance_; private int whoseturn_; private Board board_; private Boardisplayer boardisplayer_; private Keyhandler keyhandler_; private Sprite background_, gameover_, tictaclines_; private Touchiconset iconsets_[], tictacicons_; static public TictacGui create (MIDlet midlet) { if (instance_ == null) { instance_ = new TictacGui (midlet); Application.setappgui (instance_); } return instance_; } static public TictacGui getinstance() { return instance_; } private TictacGui (MIDlet midlet) { super (midlet, FULLSCREEN); Graphics g; Image image; Touchmap touchmap; keyhandler_ = new Keyhandler (this); try { background_ = new Sprite (Services.makeimage (getwidth(), getheight(), BACKGROUNDCOLOR)); tictaclines_ = new Sprite (Services.makeimage (TICTACBOARDFILE)); touchmap = new Touchmap (this); tictacicons_ = new Tictaciconset (touchmap); tictacicons_.enable (true); iconsets_ = new Touchiconset[1]; iconsets_[0] = tictacicons_; setouchandler (touchmap); } catch (Exception e) { Log.print ("TictagGui.ctor(): " + e); } } private void inserticonset (Touchiconset iconset, LayerManager manager) { Enumeration set; Icon icon; set = iconset.elements(); while (set.hasMoreElements()) { icon = (Icon) set.nextElement(); if (icon.isactive()) { manager.insert (icon.getsprite(), 0); } } } // Invoked by BTfollowerconnecter (on follower side) when follower is ACCEPTED/REJECTED public void followeraccepted (boolean accepted) { showconnectstatus(); } public void keypressed (Key key) { keyhandler_.keystroke (key); } public void keystroke (Key key) { switch (key.getgamekey()) { case CONNECT: // NOT presently connected, try to connect docommand (AppCommands.CONNECT); break; case DISCONNECT: // Presently connected, try to disocnnect docommand (AppCommands.DISCONNECT); showconnectstatus(); break; case EXIT: docommand (AppCommands.EXIT); break; case HELP: docommand (AppCommands.HELPORABOUT); break; case LOG: docommand (AppCommands.SHOWLOG); break; case REPLAY: whoseturn_ = Board.CROSS; showgameover (false); board_.clear(); break; case SETTINGS: docommand (AppCommands.SETUP); break; } } public void leaderdoneconnecting() { showconnectstatus(); } public Paramform makeparamform (String lang) { // return new Touchmeform (lang); return null; } public void newgame() { whoseturn_ = Board.CROSS; refresh(); } public void otherdisconnected (int playerid) { // NOTE: the Alert MUST be created after showconnectstatus is called // or the icons along the bottom of the display will disappear! showconnectstatus(); alert (Xlate.CONNECTION, Xlate.DISCONNECTREMOTE, Channelmanager.getinstance().getname (playerid)); } protected void pointerPressed (int x, int y) { int col, row; if (y < boardisplayer_.getop() || y > boardisplayer_.getbottom()) { super.pointerPressed (x, y); } else { row = boardisplayer_.calcrow (y); col = boardisplayer_.calcol (x); if (board_.isempty (row, col)) { if (Channelmanager.isconnected()) { Packethandler.getinstance().send (new Packetmark (Channelmanager.OTHER, row, col, whoseturn_)); } board_.mark (row, col, whoseturn_); whoseturn_ = whoseturn_ == Board.CIRCLE ? Board.CROSS : Board.CIRCLE; refresh(); } } } public void refresh() { stalelayers(); show(); } protected LayerManager setlayers() { int pos; Enumeration set; LayerManager layers; Touchiconset iconset; layers = super.setlayers(); layers.insert (background_, INFRONT); for (pos = 0; pos < iconsets_.length; pos++) { iconset = iconsets_[pos]; if (iconset.isenabled()) { inserticonset (iconset, layers); } } layers.insert (boardisplayer_.getlayer(), INFRONT); layers.insert (tictaclines_, INFRONT); layers.insert (gameover_, INFRONT); return layers; } public void setpreferences() { Image image; super.setpreferences(); try { image = Services.maketextimage (GAMEOVERFILE, Xlate.msg (Xlate.GAMEOVER), 0xB00000); image = Services.makealphaimage (image); gameover_ = new Blinkingsprite (image, 300, 100); gameover_.defineReferencePixel (image.getWidth() >> 1, 0); gameover_.setRefPixelPosition (getwidth() >> 1, getheight() - 100); // gameover_.setVisible (false); } catch (IOException e) { //#ifdef DEBUG new Soundonetone ("C5", 1000, 100).play(); //#endif } } public void setup() { super.setup(); setcommands (new AppCommands (this)); board_ = Board.getinstance(); boardisplayer_ = new Boardisplayer (getwidth(), getheight() - tictacicons_.getsetheight()); board_.addObserver (boardisplayer_); setpreferences(); setcurrent(); } public void showconnectstatus() { int pos; pos = Channelmanager.isconnected() ? 1 : 0; tictacicons_.select (Tictaciconset.CONNECTSTATUS, pos); tictacicons_.enable (true); refresh(); } public void showgameover (boolean over) { gameover_.setVisible (over); } }