📄 heclcanvas.java
字号:
/* * Copyright 2005-2007 * Wolfgang S. Kechel, data2c GmbH (www.data2c.com) * * Author: Wolfgang S. Kechel - wolfgang.kechel@data2c.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.hecl.midp20.lcdui;import java.util.Enumeration;import java.util.Vector;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Command;import javax.microedition.lcdui.CommandListener;import javax.microedition.lcdui.Display;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Font;import javax.microedition.lcdui.Graphics;import javax.microedition.lcdui.List;import javax.microedition.lcdui.game.GameCanvas;import org.awt.Color;import org.awt.Rectangle;import org.graphics.Drawable;import org.hecl.midp20.MidletCmd;public class HeclCanvas extends GameCanvas implements CommandListener { // We treat the left/right softkey on our own and provide two additional // gamekeys to simplify the programming. public static final int GAME_LEFT_SK = -1; public static final int GAME_RIGHT_SK = -2; // Fallback keycodes, valid for: // Nokia, Samsung (SGH-D500) and SonyEricsson (K608i, K700i and V800) // and probably many more public static int KEYCODE_LEFT_SK = -6; public static int KEYCODE_RIGHT_SK = -7; public HeclCanvas(boolean suppressKeyEvents) { super(suppressKeyEvents); this.nokeyevents = !suppressKeyEvents; //de.enough.polish.ui.game.GameCanvas has no getGraphics method//#ifdef polish.blackberry this.mygraphics = super.getPolishGraphics();//#else this.mygraphics = super.getGraphics();//#endif calcScreenWidth(); this.drawable = new Drawable(this.mygraphics,this.drawwidth,this.drawheight); // Force initial draw showCommands(mygraphics); flushGraphics(); } public void addCommand(Command cmd) { for (int i=0; i<this.cmds.size(); i++) { if (cmd == (Command)this.cmds.elementAt(i)) { // Its the same just return return; } } if(!isfullscreen || SettingsCmd.cvkeepcmdsinfullscreen) super.addCommand(cmd); // Now insert it in order (according to priority) boolean inserted = false; for (int i=0; i<this.cmds.size(); i++) { if (cmd.getPriority() < ((Command)this.cmds.elementAt(i)).getPriority()) { this.cmds.insertElementAt(cmd,i); inserted = true; break; } } if (inserted == false) { // Not inserted just place it at the end this.cmds.addElement(cmd); } updateCommands(); } public void commandAction(Command c,Displayable d) { // command handler for the menu if(c == CMD_BACK) { MidletCmd.getDisplay().setCurrent(this); return; } if(c == List.SELECT_COMMAND) { int n = menulist.getSelectedIndex(); if(n == -1) return; ++n; if(n >= 1 && n < cmds.size()) { Command mycmd = (Command)cmds.elementAt(n); MidletCmd.getDisplay().setCurrent(this); handleCommand(mycmd); } } } public void handleCommand(Command c) { if(savecmdlistener != null) savecmdlistener.commandAction(c,this); } public void flushGraphics() {//#ifdef DEBUG Rectangle r = new Rectangle(mygraphics.getClipX(), mygraphics.getClipY(), mygraphics.getClipWidth(), mygraphics.getClipHeight()); System.err.println("HeclCanvas.flushgraphics()"); System.err.println("clip is: "+r.x +", "+r.y +", "+r.width +", "+r.height); mygraphics.setClip(0,0,240,309);//#endif super.flushGraphics();//#ifdef DEBUG mygraphics.setClip(r.x,r.y,r.width,r.height);//#endif } public Color getCmdBgColor() {return cmdbgcolor;} public Color getCmdFgColor() {return cmdfgcolor;} public Vector getCommands() {return cmds;} public int getFullWidth() {return fullwidth;} public int getFullHeight() {return fullheight;} public int getDrawWidth() {return drawwidth;} public int getDrawHeight() {return drawheight;} public int getGameAction(int keyCode) { if(keyCode == KEYCODE_LEFT_SK) return GAME_LEFT_SK; if(keyCode == KEYCODE_RIGHT_SK) return GAME_RIGHT_SK; return super.getGameAction(keyCode); } public boolean getFullScreenMode() {return isfullscreen;} public Graphics getGraphics() {return mygraphics;} public int getKeyCode(int gameAction) { if(gameAction == GAME_LEFT_SK) return KEYCODE_LEFT_SK; if(gameAction == GAME_RIGHT_SK) return KEYCODE_RIGHT_SK; return super.getKeyCode(gameAction); } public String getKeyName(int keyCode) { if(keyCode == KEYCODE_LEFT_SK) return "LEFT_SK"; if(keyCode == KEYCODE_RIGHT_SK) return "RIGHT_SK"; return super.getKeyName(keyCode); } public void hideNotify() { callEventHandler(CanvasEvent.E_HIDE,0,0,drawwidth,drawheight,0); } public void setCmdBgColor(Color c) { cmdbgcolor = c; showCommands(mygraphics); } public void setCmdFgColor(Color c) { cmdfgcolor = c; showCommands(mygraphics); } public void setCommandListener(CommandListener l) { // we need to take care of the listener here since we override the // default behavior in fullscreen mode! cmdlistener = l; if(!isfullscreen) { // Just do it for non-fullscreen super.setCommandListener(cmdlistener); } else { // do not set, this will be done when fullscreen mode is turned off savecmdlistener = cmdlistener; } } public void setEventHandler(EventHandler eventHandler) { evh = eventHandler; } public void setFullScreenMode(boolean b) { // Commented out due to problem with WTK2.5.2 that forces us to // resitore fullscreen mode of canvas after performing setcurrent to a // non-canvas displayable. // //if(b == isfullscreen) // return; // ignore request for fullscreen canvas when disabled if(b && !SettingsCmd.cvallowfullscreen) return; isfullscreen = b; super.setFullScreenMode(isfullscreen); if(SettingsCmd.cvkeepcmdsinfullscreen) return; int n = cmds.size(); if(isfullscreen) { // disable commands savecmdlistener = cmdlistener; super.setCommandListener(null); for(int i=0; i<n; ++i) { super.removeCommand((Command)cmds.elementAt(i)); } showCommands(mygraphics); } else { // enable commands for(int i=0; i<n; ++i) { super.addCommand((Command)cmds.elementAt(i)); } cmdlistener = savecmdlistener; super.setCommandListener(cmdlistener); } } protected void showCommands(Graphics g) {//#ifdef DEBUG System.err.println("showCommands, fullscreen="+isfullscreen+", isshown="+isShown());//#endif // Unfortunately, isShown does not work on some emulators, therefor we // simply draw the commands in any case here. if(SettingsCmd.cvdocmds && isfullscreen && cmds.size() > 0 && isShown()) { int oldcol = g.getColor(); Font oldfont = g.getFont(); int oldcx = g.getClipX(); int oldcy = g.getClipY(); int oldcw = g.getClipWidth(); int oldch = g.getClipHeight(); // Nokia 6630 fullscreen bug calcScreenWidth(); //#ifdef DEBUG System.err.println("drawing rect: y="+drawheight+", h="+CMDBARHEIGHT); System.err.println("size - w="+getWidth()+" h="+getHeight()); System.err.println("draw - w"+getDrawWidth()+" h="+getDrawHeight()); System.err.println("oldclip("+oldcx+","+oldcy+","+oldcw+","+oldch+")"); System.err.println("setclip(0,"+drawheight+","+drawwidth+","+CMDBARHEIGHT+")"); g.setColor(0xff0000); g.drawLine(0,0,getWidth(),getHeight());//#endif // set clipping region to area where commands are shown g.setClip(0,drawheight,drawwidth,CMDBARHEIGHT); // clear rect in bg color g.setColor(cmdbgcolor.getRGB()); g.fillRect(0,drawheight,drawwidth,CMDBARHEIGHT); //g.drawLine(0,drawheight,drawwidth-1,drawheight+3); // Draw the labels g.setColor(cmdfgcolor.getRGB()); g.setFont(CMDFONT); int ypos = drawheight+1; Command c = buttons[0].getCommand(); if(c != null) { String l = c.getLabel(); g.drawString(l != null ? l : "Left", 1,ypos,Graphics.TOP|Graphics.LEFT); } c = buttons[1].getCommand(); if(c != null) { String l = c.getLabel(); g.drawString(l != null ? l : "Right", drawwidth-1,ypos,Graphics.TOP|Graphics.RIGHT); } c = buttons[2].getCommand(); if(c != null) { String l = c.getLabel(); g.drawString(l != null ? l : "Center", drawwidth/2,ypos,Graphics.TOP|Graphics.HCENTER); } g.setColor(oldcol); g.setFont(oldfont); //#ifdef DEBUG System.err.println("flushing: 0, "+drawheight +", "+drawwidth+", "+CMDBARHEIGHT); //#endif flushGraphics(0,drawheight,drawwidth,CMDBARHEIGHT); g.setClip(oldcx,oldcy,oldcw,oldch); } else { //System.err.println("no commands"); } } public void paint(Graphics g) { //#ifdef notdef System.err.println("HeclCanvas.paint called"); if(this.drawable != null) System.err.println("NEEDSFLUSH="+this.drawable.needsFlush()); //#endif callEventHandler(CanvasEvent.E_PAINT,0,0,drawwidth,drawheight,0); if(this.drawable != null && this.drawable.needsFlush()) { showCommands(mygraphics); flushGraphics(); } super.paint(g); } public void pointerPressed(int x,int y) { callEventHandler(CanvasEvent.E_PPRESS,x,y,drawwidth,drawheight,0); } public void pointerReleased(int x,int y) { callEventHandler(CanvasEvent.E_PRELEASE,x,y,drawwidth,drawheight,0); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -