📄 maincanvas.java
字号:
package com.ismyway.fairyui;import javax.microedition.lcdui.Canvas;import javax.microedition.lcdui.Displayable;import javax.microedition.lcdui.Graphics;import com.ismyway.anyview.Anyview;import com.ismyway.anyview.others.Configure;import com.ismyway.anyview.others.Settings;import com.ismyway.util.ArrayList;import com.ismyway.util.Theme;public final class MainCanvas extends Canvas implements Runnable { private static MainCanvas instance = null; /** Support Vars */ private Panel panel = null; private ArrayList popups = null; private boolean alive = false; private boolean panelAnimation = false; //private boolean repaint = false; //是否需要重绘 private boolean forcerepaint = false; //是否需要全屏重绘 private static boolean busymode = false; //是否处于忙模式 private static int busyx, busyy, busyframe; //protected long pointerPressedtime = 0; //触摸屏按下时的时间 protected long lastActionTime = 0; private boolean processUiEvent = true; public static int screenWidth, screenHeight; public static MainCanvas getInstance() { if (instance == null) { instance = new MainCanvas(); } return instance; } private MainCanvas() { setFullScreenMode(true); popups = new ArrayList(); alive = true; Settings.SCROLL_HEIGHT = getHeight() - getBottomOffset() - getTopOffset() - 10; Settings.SCROLL_STEP = Settings.SCROLL_HEIGHT / Settings.SCROLL_COUNT; busyx = (getWidth() - 20) >> 1; busyy = (getHeight() - 20) >> 1; busyframe = 0; lastActionTime = System.currentTimeMillis(); Anyview.hasPointerEvents = hasPointerEvents(); new Thread(this).start(); } private int getTopOffset() { return 20; } protected void paint(Graphics g) { if (busymode) { Theme.paintBusyFrame(g, busyframe, busyx, busyy); return; } if (forcerepaint) { if (null != panel) { panel.paint(g); } for (int i = 0; i < popups.size(); i++) { ((Panel) popups.get(i)).paint(g); } } else { //顶层窗口 if (popups.size() > 0) { ((Panel) popups.get(popups.size() - 1)).paint(g); } } } public void repaintAll() { forcerepaint = true; repaint(); } /** * Sets Displayable c as the Current display. * @param c a displayable */ public void setCurrent(Displayable c) { Handset.getDisplay().setCurrent(c); } /* * (non-Javadoc) * * @see java.lang.Runnable#run() */ public void run() { while (alive) { long startTime = System.currentTimeMillis(); //UI处理 if (processUiEvent) { try { boolean repaint = false; // check for animation if (busymode) { repaint(); busyframe++; if (busyframe == 4) { busyframe = 0; } continue; } if (popups.size() > 0) { Panel p = (Panel) popups.get(popups.size() - 1); if (p.isAnimated()) { if (p.clock()) { panelAnimation = true; } } } else if (panel != null && panel.isAnimated()) { boolean panelrepaint = panel.clock(); if (panelrepaint) { panelAnimation = true; } } /*if (true) { System.out .println("repaint: " + (repaint || animationImage != null || panelAnimation)); }*/ if (repaint || panelAnimation) { repaint(); //serviceRepaints(); repaint = false; panelAnimation = false; } } catch (Throwable e) { //e.printStackTrace(); System.out.println("Error: " + e.getClass().getName() + ". Msg: " + e.getMessage()); } } //背景常亮 if (Configure.flashBackLight) { //if (System.currentTimeMillis() - backlightTimmer > 10000) { Handset.flashBacklight(120000); Handset.setLight(Configure.lightValue); //backlightTimmer = System.currentTimeMillis(); //} } //自动关机 if (Configure.shutDownTime > 0 && System.currentTimeMillis() - lastActionTime > Configure.shutDownTime) { destroy(); Anyview.exit(); } long delay = Configure.delayTime - (System.currentTimeMillis() - startTime); if (delay > 0) { try { Thread.sleep(delay); } catch (InterruptedException e) { } } } } /** * This method is called by the vm every time the with and the height of the screen change. The parameters are the real width and height of the screen. * @see javax.microedition.lcdui.Displayable#sizeChanged(int, int) */ protected void sizeChanged(int ww, int hh) { // force recalculation of backgrounds. setFullScreenMode(true); /*screenSize++; int w, h; if (Settings.Orientation == Settings.NORMAL) // keep in mind the screen orientation. { w = ww; h = hh; } else { w = hh; h = ww; } Settings.SCROLL_HEIGHT = h - getBottomOffset() - getTopOffset(); Settings.SCROLL_STEP = Settings.SCROLL_HEIGHT / Settings.SCROLL_COUNT; bgImage = prepareBgImage(w, h); offscreen = null; if (panel != null) { panel.setWidth(w); panel.setHeight(h); panel.validate(); } for (int i = 0; i < popups.size(); ++i) { Window popup = (Window) popups.get(i); popup.setWidth(w - Settings.SCROLLBAR_WIDTH); // den tha fenete oraio, alla toulaxiston tha mini to functionallity tou popup. popup.validate(); } try { System.gc(); // now its a very good time for garbage collection } catch (Throwable e) { System.out.println("Failed to call garbage collector." + e.getMessage()); e.printStackTrace(); }*/ } private int getBottomOffset() { return 20; } public void setContainerCurrent() { Handset.getDisplay().setCurrent(this); } protected void pointerPressed(int x, int y) { lastActionTime = System.currentTimeMillis(); //pointerPressedtime = System.currentTimeMillis(); if (busymode) { return; } int tmp = y; switch (Configure.rotateScreen) { case 0: break; case 1:// translate joystick for left handed land scape. y = getHeight() - x; x = tmp; break; case 2:// translate joystick for right handed land scape. y = getHeight() - y; x = getWidth() - x; break; case 3:// translate joystick for right handed land scape. y = x; x = getWidth() - tmp; break; } if (popups.size() > 0) { if (((Panel) popups.get(popups.size() - 1)).pointerPressed(x, y)) { repaint(); } return; } if (panel.pointerPressed(x, y)) { repaint(); } } protected void pointerDragged(int x, int y) { lastActionTime = System.currentTimeMillis(); if (busymode) { return; } int tmp = y; switch (Configure.rotateScreen) { case 0: break; case 1:// translate joystick for left handed land scape. y = getHeight() - x; x = tmp; break; case 2:// translate joystick for right handed land scape. y = getHeight() - y; x = getWidth() - x; break; case 3:// translate joystick for right handed land scape. y = x; x = getWidth() - tmp; break; } if (popups.size() > 0) { if (((Panel) popups.get(popups.size() - 1)).pointerDragged(x, y)) { repaint(); } } if (panel.pointerDragged(x, y)) { repaint(); } } protected void pointerReleased(int x, int y) { lastActionTime = System.currentTimeMillis(); /*if (busymode || animationImage != null) { return; }*/ int tmp = y; switch (Configure.rotateScreen) { case 0: break; case 1:// translate joystick for left handed land scape. y = getHeight() - x; x = tmp; break; case 2:// translate joystick for right handed land scape. y = getHeight() - y; x = getWidth() - x; break; case 3:// translate joystick for right handed land scape. y = x; x = getWidth() - tmp; break; } if (popups.size() > 0) { if (((Panel) popups.get(popups.size() - 1)).pointerReleased(x, y)) { repaint(); } return; } if (panel.pointerReleased(x, y)) { repaint(); } } protected void keyRepeated(int keyCode) { lastActionTime = System.currentTimeMillis(); System.out.println("keyRepeated = " + keyCode); if (busymode) { return; // ignore events while in animation. } boolean repaint; if (popups.size() > 0) { Panel p = (Panel) popups.get(popups.size() - 1); repaint = p.keyRepeated(keyCode); if (repaint) { repaint(); } } else if (panel != null) { repaint = panel.keyRepeated(keyCode); if (repaint) { repaint(); } } } protected void keyReleased(int keyCode) { lastActionTime = System.currentTimeMillis(); System.out.println("keyReleased = " + keyCode); if (busymode) { return; // ignore events while in animation. } boolean repaint; if (popups.size() > 0) { Panel p = (Panel) popups.get(popups.size() - 1); repaint = p.keyReleased(keyCode); if (repaint) { repaint(); } } else if (panel != null) { repaint = panel.keyReleased(keyCode); if (repaint) { repaint(); } } } /** * Returns the current panel set on the FireScreen. * @return */ public Panel getCurrentPanel() { return panel; } /** * Set a panel to the FireScreen. * @param p */ public void setCurrent(Panel p) { setCurrent(p, Settings.ANIMATE_NONE); } public void setCurrent(Panel p, boolean update) { if (update) { setCurrent(p, LEFT); } else { Handset.getDisplay().setCurrent(this); } } /** * Shows the panel p on the screen with the supplied animation direction. * @param p * @param animDirection */ public void setCurrent(Panel p, int animDirection) { setCurrent(p, animDirection, false); } public void setCurrent(Panel p, int animDirection, boolean animate) { popups.clear(); p.setWidth(getWidth()); p.setHeight(getHeight()); p.validate(); forcerepaint = true; if (!animate) { panel = p; repaint(); } else { panel = p; repaint(); } if (null != p) { p.afterActive(); } } /** * Shows a popup panel on the screen. The popups can stuck on each other. * @param popup */ public void showPopup(Panel popup) { showPopup(popup, Settings.ANIMATE_NONE); } public Component getTopComponent() { if (popups.size() == 0) { return null; } return (Component)popups.get(popups.size() - 1); } public void showPopup(Panel popup, int method) { //deactive if (popups.size() > 0) { ((Panel) popups.get(popups.size() - 1)).deActive(); } else { if (null != panel) { panel.deActive(); } } popup.validate(); popups.add(popup); repaint(); } /** * Closes the top-level popup, shown on the FireScreen * @return */ synchronized public void closePopup() { if (popups.size() > 0) { //System.out.println("closepopup"); forcerepaint = true; popups.remove(popups.size() - 1); if (popups.size() > 0) { ((Panel) popups.get(popups.size() - 1)).reActive(); } else { if (null != panel) { panel.reActive(); } } repaint(); } } /** * 关闭所有的弹出菜单 * */ synchronized public void closePopMenu() { if (popups.size() < 1) { return; } while (true) { int pop = findPopmenuIndex(); if (pop > -1) { popups.remove(pop); continue; } break; } if (popups.size() > 0) { ((Panel) popups.get(popups.size() - 1)).reActive(); } else { if (null != panel) { panel.reActive(); } } repaint(); } synchronized private int findPopmenuIndex() { if (popups.size() < 1) { return -1; } for (int i = 0; i < popups.size(); i++) { if (popups.get(i) instanceof PopMenu) { return i; } } return -1; } /** * Destroys the FireScreen instance. This method should be called on clean-up * in order to stop the animation thread inside the FireScreen Singleton. * */ public void destroy() { if (alive) { try { alive = false; } catch (Throwable e) { } } instance = null; } public void setOrientation(byte o) { if (o > -1 && 0 < 4) { Configure.rotateScreen = o; sizeChanged(super.getWidth(), super.getHeight()); repaint(); } } /** * Returns the width of this FireScreen. If the screen is in landscape mode, it will return the real height of the screen. * @see javax.microedition.lcdui.Displayable#getWidth() */ public int getWidth() { /*if (Configure.rotateScreen == 0 || Configure.rotateScreen == 2) { return super.getWidth(); } return super.getHeight();*/ if (Configure.rotateScreen == 0 || Configure.rotateScreen == 2) { return screenWidth; } return screenHeight; } /** * Returns the height of this FireScreen. If the screen is in landscape mode, it will return the real width of the screen. * @see javax.microedition.lcdui.Displayable#getHeight() */ public int getHeight() { if (Configure.rotateScreen == 0 || Configure.rotateScreen == 2) { return screenHeight; } return screenWidth; } public void showNotify() { lastActionTime = System.currentTimeMillis(); processUiEvent = true; setFullScreenMode(true); } public void hideNotify() { processUiEvent = false; } synchronized public void closeAllPopup() { popups.clear(); if (null != panel) { panel.reActive(); } repaint(); } public static boolean isBusymode() { return busymode; } public static void setBusymode(boolean busymode) { busyx = (getInstance().getWidth() - 20) >> 1; busyy = (getInstance().getHeight() - 20) >> 1; MainCanvas.busymode = busymode; } public static void setBusymode(boolean busymode, int x, int y) { busyx = x; busyy = y; if (x < 0) { busyx = 0; } if (x > getInstance().getWidth() - 20) { busyx = getInstance().getWidth() - 20; } if (y < 0) { busyy = 0; } if (y > getInstance().getHeight() - 20) { busyy = getInstance().getHeight() - 20; } MainCanvas.busymode = busymode; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -