PageRenderTime 29ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/engine_java/000_Engine_Core/gfx/com/cell/gfx/AScreen.java

http://cellengine.googlecode.com/
Java | 1457 lines | 928 code | 242 blank | 287 comment | 144 complexity | 7cff37c76235940cdd66812c88601623 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, AGPL-3.0, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0
  1. package com.cell.gfx;
  2. import java.util.Stack;
  3. import java.util.Vector;
  4. import com.cell.CObject;
  5. import com.cell.CUtil;
  6. /**
  7. * SubScreen</br>
  8. * @author yifeizhang
  9. * @since 2006-11-30
  10. * @version 1.0
  11. */
  12. abstract public class AScreen extends CObject
  13. {
  14. private static IGfxBridge GfxAdapter;
  15. public static void initGfxSystem(IGfxBridge gfxAdapter) {
  16. GfxAdapter = gfxAdapter;
  17. System.out.println(
  18. "AScreen : Gfx System initialized !\n" +
  19. "\tIGfxBridge = " + GfxAdapter + "\n" +
  20. "");
  21. }
  22. public static IGfxBridge getGfxAdapter() {
  23. if (GfxAdapter == null) {
  24. if (CObject.getAppBridge() instanceof IGfxBridge) {
  25. initGfxSystem((IGfxBridge)CObject.getAppBridge());
  26. }
  27. }
  28. return GfxAdapter;
  29. }
  30. // -------------------------------------------------------------------------------------------------------
  31. // game refer
  32. /**GameOver Flag*/
  33. volatile static public boolean ExitGame = false;
  34. /**Current AScreen Instance*/
  35. volatile static private AScreen CurSubScreen = null;
  36. /**Current IGraphics Instance*/
  37. volatile static public IGraphics CurGraphics = null;
  38. /**Screen Width*/
  39. static public int SCREEN_WIDTH = 176;
  40. /**Screen Height*/
  41. static public int SCREEN_HEIGHT = 208;
  42. /**Screen Width/2*/
  43. static public int SCREEN_HCENTER = 176/2;
  44. /**Screen Height/2*/
  45. static public int SCREEN_VCENTER = 208/2;
  46. /**Delay per Frame FPS=30 FrameDelay = 1000/30 = 33*/
  47. static public int FrameDelay = 25;//default fps = 50
  48. /**Game Render Enable*/
  49. static public boolean RenderEnable = true;
  50. /**Screen Transition Enable*/
  51. static public boolean TransitionEnable = true;
  52. /**Game Key Enable*/
  53. static public boolean KeyEnable = true;
  54. /**Game Logic Enable*/
  55. static private boolean LogicEnable = true;
  56. // ------------------------------------------------------------------------------------------------------
  57. static public int FrameImageDrawed = 0;
  58. static public boolean IsDebug = false;
  59. static private int Timer = 1;
  60. /**
  61. * tick frame timer
  62. */
  63. static public void tickTimer() {
  64. Timer++;
  65. }
  66. /**
  67. * reset frame timer
  68. */
  69. static public void resetTimer() {
  70. Timer = 1;
  71. }
  72. /**
  73. * get current frame timer
  74. * @return
  75. */
  76. static public int getTimer() {
  77. return Timer;
  78. }
  79. // ------------------------------------------------------------------------------------------------------
  80. static public void clearCurScreen(){
  81. if (CurSubScreen!=null){
  82. try{
  83. CurSubScreen.notifyDestroy();
  84. }catch(Exception err){
  85. err.printStackTrace();
  86. }
  87. }
  88. CurSubScreen = null;
  89. }
  90. static public AScreen getCurScreen(){
  91. return CurSubScreen;
  92. }
  93. // ------------------------------------------------------------------------------------------------------
  94. private boolean m_IsLoading = true;
  95. /**
  96. * canvas notifyHide call back </br>
  97. */
  98. public void notifyPause(){}
  99. /**
  100. * canvas notifyShow call back</br>
  101. */
  102. public void notifyResume(){}
  103. public void notifyRenderPause(IGraphics g){}
  104. /***
  105. * render loading
  106. * @param g
  107. * @return is loading complete
  108. */
  109. public boolean notifyRenderLoading(IGraphics g){return true;}
  110. /**
  111. * main game logic call back</br>
  112. */
  113. abstract public void notifyLogic();
  114. /**
  115. * main render call back : canvas paint call back</br>
  116. * @param g
  117. */
  118. abstract public void notifyRender(IGraphics g);
  119. /**
  120. * main release call back : call back when screen is disposing </br>
  121. * @param g
  122. */
  123. abstract public void notifyDestroy();
  124. //--------------------------------------------------------------------------------------------------------------------
  125. /**Current Time Transition Flag*/
  126. static private boolean Transition = false;
  127. static private int TransitionMaxTime = 0 ;
  128. static private int TransitionTime = 0 ;
  129. static private boolean TransitionIn = false;
  130. static private boolean TransitionOut = false;
  131. static private String TransitionText[] = new String[]{"Loading..."};
  132. static private IImage TransitionImage = null;
  133. static private IImage TransitionTagImage = null;
  134. static private String TipText[] = null;
  135. static private int tipIndex = 0;
  136. //
  137. static public int TransitionType = 0;
  138. final static public int TRANSITION_STRIP_H = 0;
  139. final static public int TRANSITION_ALPHA = 1;
  140. // type refer
  141. static public int TransitionStripCellW = 16;
  142. static public int TransitionStripDelta = 2;
  143. static public int TransitionAlphaColor = 0;
  144. static public int TransitionAlphaMaxTime = 16;
  145. static public void setTransitionImage(IImage img) {
  146. TransitionImage = img;
  147. }
  148. static public IImage getTransistionImage(){
  149. return TransitionImage;
  150. }
  151. static public void setTransitionTagImage(IImage img){
  152. TransitionTagImage = img;
  153. }
  154. static public IImage getTransistionTagImage(){
  155. return TransitionTagImage;
  156. }
  157. static public void setTipText(String[] tag){
  158. TipText = tag;
  159. }
  160. static public boolean isHaveTipText(){
  161. return TipText!=null&&TipText.length>1;
  162. }
  163. /**
  164. * If Current Screen is Transition
  165. * @return
  166. */
  167. static public boolean isTransition(){
  168. return Transition;
  169. }
  170. /**
  171. * Set Current In TransitionIn
  172. */
  173. static private void setTransitionIn(){
  174. Transition = true;
  175. if (TransitionType==TRANSITION_STRIP_H){
  176. TransitionMaxTime = SCREEN_WIDTH / TransitionStripCellW + TransitionStripCellW/TransitionStripDelta;
  177. }else{
  178. TransitionMaxTime = TransitionAlphaMaxTime;
  179. }
  180. TransitionTime = 0;
  181. TransitionIn = true;
  182. TransitionOut = false;
  183. }
  184. /**
  185. * Set Current In TransitionOut
  186. */
  187. static private void setTransitionOut(){
  188. Transition = true;
  189. if (TransitionType==TRANSITION_STRIP_H){
  190. TransitionMaxTime = SCREEN_WIDTH / TransitionStripCellW + TransitionStripCellW/TransitionStripDelta;
  191. }else{
  192. TransitionMaxTime = TransitionAlphaMaxTime;
  193. }
  194. TransitionTime = 0;
  195. TransitionIn = false;
  196. TransitionOut = true;
  197. }
  198. /**
  199. * @return
  200. */
  201. static private boolean isTransitionOut(){
  202. if(AScreen.TransitionEnable==false)return true;
  203. if(!TransitionOut || TransitionTime>TransitionMaxTime)return true;
  204. return false;
  205. }
  206. /**
  207. * @return
  208. */
  209. static private boolean isTransitionIn(){
  210. if(AScreen.TransitionEnable==false)return true;
  211. if(!TransitionIn || TransitionTime>TransitionMaxTime)return true;
  212. return false;
  213. }
  214. /**
  215. * Render Transition Effect
  216. * @param g
  217. * @param w
  218. * @param h
  219. */
  220. static private void Transition(IGraphics g,int w,int h)
  221. {
  222. w += TransitionStripCellW*2;
  223. TransitionTime++;
  224. if(TransitionIn && TransitionTime>TransitionMaxTime){
  225. Transition = false;
  226. }
  227. if(Transition==false){
  228. return;
  229. }
  230. //System.out.println("Transition Screen : " + TransitionTime);
  231. switch(TransitionType)
  232. {
  233. case TRANSITION_STRIP_H:
  234. {
  235. g.setClip(0, 0, w, h);
  236. g.setColor(0xff000000);
  237. int count = w/TransitionStripCellW+1;
  238. if(TransitionIn){// black -> clean
  239. for(int i=0;i<count;i++){
  240. g.fillRect(
  241. i*TransitionStripCellW , 0,
  242. TransitionStripCellW-(TransitionTime-i)*TransitionStripDelta, h);
  243. }
  244. }
  245. if(TransitionOut){//clean -> black
  246. for(int i=0;i<count;i++){
  247. g.fillRect(
  248. i*TransitionStripCellW - (TransitionTime-i)*TransitionStripDelta, 0,
  249. (TransitionTime-i)*TransitionStripDelta, h);
  250. }
  251. }
  252. }
  253. break;
  254. case TRANSITION_ALPHA:
  255. {
  256. float rate = (float)Math.min((TransitionTime+1), TransitionMaxTime) / TransitionMaxTime;
  257. if(TransitionIn){
  258. g.setClip(0, 0, w, h);
  259. g.setColor(CUtil.toColor((int)(0xff * (1-rate)), TransitionAlphaColor));
  260. g.fillRect(0, 0, w, h);
  261. }
  262. if(TransitionOut){
  263. g.setClip(0, 0, w, h);
  264. g.setColor(CUtil.toColor((int)(0xff * (rate)), TransitionAlphaColor));
  265. g.fillRect(0, 0, w, h);
  266. }
  267. }
  268. break;
  269. }
  270. if( (TransitionIn && TransitionTime<=1) ||
  271. (TransitionOut && TransitionTime>=TransitionMaxTime-1))
  272. {
  273. // draw splash
  274. if(TransitionImage!=null)
  275. {
  276. g.drawImage(
  277. TransitionImage,
  278. SCREEN_HCENTER - TransitionImage.getWidth()/2,
  279. SCREEN_VCENTER - TransitionImage.getHeight()/2,
  280. 0);
  281. if (TransitionTagImage!=null)
  282. {
  283. int x = SCREEN_HCENTER - TransitionTagImage.getWidth()/2;
  284. int y = SCREEN_VCENTER - TransitionImage.getHeight()/2+ 180;
  285. g.drawImage(TransitionTagImage, x, y, 0);
  286. if (TipText!=null&&TipText.length>0){
  287. drawString(g, TipText[tipIndex],
  288. SCREEN_WIDTH/2 - g.getStringWidth(TipText[tipIndex])/2 ,
  289. y + (TransitionTagImage.getHeight() - g.getStringHeight())/2,
  290. 0xffffffff);
  291. }
  292. }
  293. }
  294. // draw string
  295. else if (TransitionText != null)
  296. {
  297. int th = (g.getStringHeight() + 1) * TransitionText.length;
  298. int ty = SCREEN_HEIGHT/2 - th/2;
  299. for(int i=0;i<TransitionText.length;i++)
  300. {
  301. drawString(g, TransitionText[i],
  302. SCREEN_WIDTH/2 - g.getStringWidth(TransitionText[i])/2 ,
  303. ty + (g.getStringHeight() + 1) * i ,
  304. 0xffffffff);
  305. }
  306. }
  307. }
  308. }
  309. // ---------------------------------------------------------------------------------
  310. volatile static private Class NextScreenClassName = null;
  311. /**
  312. * Change Current To NextScreen
  313. * @param screenClassName Your extends AScreen Class Name
  314. */
  315. static public void ChangeSubScreen(Class screenClassName)
  316. {
  317. TransitionText = new String[]{"Loading..."};
  318. NextScreenClassName = screenClassName;
  319. KeyEnable = false;
  320. LogicEnable = false;
  321. if (TipText!=null&&TipText.length>0){
  322. tipIndex = CUtil.getRandom(0, TipText.length-1);
  323. }
  324. setTransitionOut();
  325. }
  326. /**
  327. * Change Current To NextScreen
  328. * @param screenClassName Your extends AScreen Class Name
  329. * @param transitionText Show Text On Transition Screen SingleLine
  330. */
  331. static public void ChangeSubScreen(Class screenClassName,String transitionText)
  332. {
  333. TransitionText = new String[]{transitionText};
  334. NextScreenClassName = screenClassName;
  335. KeyEnable = false;
  336. LogicEnable = false;
  337. if (TipText!=null&&TipText.length>0){
  338. tipIndex = CUtil.getRandom(0, TipText.length-1);
  339. }
  340. setTransitionOut();
  341. }
  342. /**
  343. * Change Current To NextScreen
  344. * @param screenClassName Your extends AScreen Class Name
  345. * @param transitionText Show Text On Transition Screen MultiLine
  346. */
  347. static public void ChangeSubScreen(Class screenClassName,String[] transitionText)
  348. {
  349. TransitionText = transitionText;
  350. NextScreenClassName = screenClassName;
  351. KeyEnable = false;
  352. LogicEnable = false;
  353. if (TipText!=null&&TipText.length>0){
  354. tipIndex = CUtil.getRandom(0, TipText.length-1);
  355. }
  356. setTransitionOut();
  357. }
  358. // --------------------------------------------------------------------------------------------------------------
  359. /**
  360. * Change Current To NextScreen
  361. * @param screenClassName Your extends AScreen Class Name
  362. */
  363. static public void ChangeSubScreen(String screenClassName)
  364. {
  365. try{
  366. NextScreenClassName = Class.forName(screenClassName);
  367. TransitionText = new String[]{"Loading..."};
  368. KeyEnable = false;
  369. LogicEnable = false;
  370. if (TipText!=null&&TipText.length>0){
  371. tipIndex = CUtil.getRandom(0, TipText.length-1);
  372. }
  373. setTransitionOut();
  374. } catch (ClassNotFoundException e) {
  375. e.printStackTrace();
  376. }
  377. }
  378. /**
  379. * Change Current To NextScreen
  380. * @param screenClassName Your extends AScreen Class Name
  381. * @param transitionText Show Text On Transition Screen SingleLine
  382. */
  383. static public void ChangeSubScreen(String screenClassName,String transitionText)
  384. {
  385. try {
  386. NextScreenClassName = Class.forName(screenClassName);
  387. TransitionText = new String[]{transitionText};
  388. KeyEnable = false;
  389. LogicEnable = false;
  390. if (TipText!=null&&TipText.length>0){
  391. tipIndex = CUtil.getRandom(0, TipText.length-1);
  392. }
  393. setTransitionOut();
  394. } catch (ClassNotFoundException e) {
  395. e.printStackTrace();
  396. }
  397. }
  398. /**
  399. * Change Current To NextScreen
  400. * @param screenClassName Your extends AScreen Class Name
  401. * @param transitionText Show Text On Transition Screen MultiLine
  402. */
  403. static public void ChangeSubScreen(String screenClassName,String[] transitionText)
  404. {
  405. try {
  406. NextScreenClassName = Class.forName(screenClassName);
  407. TransitionText = transitionText;
  408. KeyEnable = false;
  409. LogicEnable = false;
  410. if (TipText!=null&&TipText.length>0){
  411. tipIndex = CUtil.getRandom(0, TipText.length-1);
  412. }
  413. setTransitionOut();
  414. } catch (ClassNotFoundException e) {
  415. e.printStackTrace();
  416. }
  417. }
  418. static public void ChangeSubScreen(String screenClassName, IImage image)
  419. {
  420. try {
  421. NextScreenClassName = Class.forName(screenClassName);
  422. TransitionImage = image;
  423. KeyEnable = false;
  424. LogicEnable = false;
  425. if (TipText!=null&&TipText.length>0){
  426. tipIndex = CUtil.getRandom(0, TipText.length-1);
  427. }
  428. setTransitionOut();
  429. } catch (ClassNotFoundException e) {
  430. e.printStackTrace();
  431. }
  432. }
  433. // --------------------------------------------------------------------------------------------------------------
  434. /**
  435. * Check when ChangeSubScreen Event are happen
  436. */
  437. static private void TryChangeSubSreen()
  438. {
  439. if (NextScreenClassName!=null && isTransitionOut())
  440. {
  441. try
  442. {
  443. if(CurSubScreen!=null)
  444. {
  445. CurSubScreen.notifyDestroy();
  446. CurSubScreen = null;
  447. System.gc();
  448. Thread.yield();
  449. }
  450. System.out.println(
  451. "\n"+
  452. "ChangeSubScreen -> "+NextScreenClassName.getName()+"\n"+
  453. "Memory Status : "+
  454. (Runtime.getRuntime().freeMemory()/1024) +
  455. "/" +
  456. (Runtime.getRuntime().totalMemory()/1024) +
  457. "(K byte)" +
  458. "\n"
  459. );
  460. CurSubScreen = (AScreen)(NextScreenClassName.newInstance());
  461. System.gc();
  462. Thread.yield();
  463. KeyEnable = true;
  464. LogicEnable = true;
  465. setTransitionIn();
  466. NextScreenClassName = null;
  467. }
  468. catch (Exception e)
  469. {
  470. e.printStackTrace();
  471. ExitGame = true;
  472. }
  473. }
  474. }
  475. // -------------------------------------------------------------------------------------------------------
  476. // advance key mask
  477. private static boolean[] VKDState;
  478. private static boolean[] VKUState;
  479. private static boolean[] VKHState;
  480. // -------------------------------------------------------------------------------------------------------
  481. // key refer
  482. /**Key Input Enable*/
  483. static private boolean KeyInput = false;
  484. // key constance
  485. static final public int KEY_ANY = 0xffffffff;
  486. static final public int KEY_0 = 0x00000001;
  487. static final public int KEY_1 = 0x00000002;
  488. static final public int KEY_2 = 0x00000004;
  489. static final public int KEY_3 = 0x00000008;
  490. static final public int KEY_4 = 0x00000010;
  491. static final public int KEY_5 = 0x00000020;
  492. static final public int KEY_6 = 0x00000040;
  493. static final public int KEY_7 = 0x00000080;
  494. static final public int KEY_8 = 0x00000100;
  495. static final public int KEY_9 = 0x00000200;
  496. static final public int KEY_TAB = 0x00000400;
  497. static final public int KEY_SHARP = 0x00000800;
  498. static final public int KEY_STAR = 0x00001000;
  499. static final public int KEY_UP = 0x00002000;
  500. static final public int KEY_DOWN = 0x00004000;
  501. static final public int KEY_LEFT = 0x00008000;
  502. static final public int KEY_RIGHT = 0x00010000;
  503. static final public int KEY_A = 0x00020000;
  504. static final public int KEY_B = 0x00040000;
  505. static final public int KEY_C = 0x00080000;
  506. //
  507. // key
  508. static private int KeyState = 0;
  509. static private int KeyDownState = 0;
  510. static private int KeyUpState = 0;
  511. static private boolean PointerState = false;
  512. static private boolean PointerDragState = false;
  513. static private boolean PointerDownState = false;
  514. static private boolean PointerUpState = false;
  515. static private int CurKeyState = 0;
  516. static private int CurKeyDownState = 0;
  517. static private int CurKeyUpState = 0;
  518. static private boolean CurKeyDownEnable = true;
  519. static private boolean CurKeyUpEnable = true;
  520. static private boolean CurPointerState = false;
  521. static private boolean CurPointerDragState = false;
  522. static private boolean CurPointerDownState = false;
  523. static private boolean CurPointerUpState = false;
  524. static private int PointerX;
  525. static private int PointerY;
  526. static private int PointerUX;
  527. static private int PointerUY;
  528. static private int PointerDX;
  529. static private int PointerDY;
  530. static private boolean CurPointerDownEnable = true;
  531. static private boolean CurPointerUpEnable = true;
  532. static private int[] TGQueryKeys = new int[8];
  533. static private int TGQueryIndex = 0;
  534. static private byte[] TGCharE = new byte[]{67,111,112,121,114,105,103,104,116,32,84,105,108,101,114,71,97,109,101,115,};
  535. static private Vector<IInputListener> InputListeners = new Vector<IInputListener>();
  536. static public int HoldEventTime = 0;
  537. static public int getKeyState(){
  538. return KeyState;
  539. }
  540. static public void appendInputListener(IInputListener listener){
  541. InputListeners.addElement(listener);
  542. }
  543. static public void removeInputListener(IInputListener listener){
  544. InputListeners.removeElement(listener);
  545. }
  546. static public IInputListener getCurrentInputListener(){
  547. if(!InputListeners.isEmpty())
  548. return InputListeners.lastElement();
  549. return null;
  550. }
  551. /**screen call*/
  552. static public void keyPressed(int keyCode)
  553. {
  554. // if(CurKeyDownEnable)
  555. {
  556. AScreen.KeyDownState |= keyCode;
  557. AScreen.KeyState |= keyCode;
  558. HoldEventTime = 0;
  559. CurKeyDownEnable = false;
  560. }
  561. if(isKeyDownState(KEY_A)){
  562. TGQueryIndex = 0;
  563. }else{
  564. if(TGQueryIndex<TGQueryKeys.length){
  565. TGQueryKeys[TGQueryIndex] = keyCode;
  566. TGQueryIndex++;
  567. }
  568. }
  569. }
  570. static public void keyTyped(char keyChar)
  571. {
  572. if (!InputListeners.isEmpty()){
  573. InputListeners.lastElement().charTyped(keyChar);
  574. }
  575. /*
  576. int count = InputListeners.size();
  577. for(int i=count-1; i>=0; i--){
  578. ((IInputListener)InputListeners.elementAt(i)).charTyped(keyChar);
  579. }*/
  580. }
  581. /**screen call*/
  582. static public void keyReleased(int keyCode)
  583. {
  584. // if(CurKeyUpEnable)
  585. {
  586. AScreen.KeyUpState |= keyCode;
  587. AScreen.KeyState &= (~keyCode);
  588. HoldEventTime = 0;
  589. CurKeyUpEnable = false;
  590. }
  591. }
  592. /**screen call*/
  593. static public void pointerPressed(int x, int y)
  594. {
  595. // if(CurPointerDownEnable)
  596. {
  597. AScreen.PointerState = true;
  598. AScreen.PointerDownState = true;
  599. AScreen.PointerDragState = false;
  600. AScreen.PointerX = x;
  601. AScreen.PointerY = y;
  602. PointerDX = x;
  603. PointerDY = y;
  604. HoldEventTime = 0;
  605. CurPointerDownEnable = false;
  606. if(PointerX < 0)PointerX = 0;
  607. if(PointerX >= SCREEN_WIDTH)PointerX = SCREEN_WIDTH-1;
  608. if(PointerY < 0)PointerY = 0;
  609. if(PointerY >= SCREEN_HEIGHT)PointerY = SCREEN_HEIGHT-1;
  610. }
  611. }
  612. /**screen call*/
  613. static public void pointerReleased(int x, int y)
  614. {
  615. // if(CurPointerUpEnable)
  616. {
  617. AScreen.PointerState = false;
  618. AScreen.PointerUpState = true;
  619. AScreen.PointerDragState = false;
  620. AScreen.PointerX = x;
  621. AScreen.PointerY = y;
  622. PointerUX = x;
  623. PointerUY = y;
  624. HoldEventTime = 0;
  625. CurPointerUpEnable = false;
  626. if(PointerX < 0)PointerX = 0;
  627. if(PointerX >= SCREEN_WIDTH)PointerX = SCREEN_WIDTH-1;
  628. if(PointerY < 0)PointerY = 0;
  629. if(PointerY >= SCREEN_HEIGHT)PointerY = SCREEN_HEIGHT-1;
  630. }
  631. }
  632. /**screen call*/
  633. static public void pointerDragged(int x, int y)
  634. {
  635. AScreen.PointerDragState = true;
  636. AScreen.PointerX = x;
  637. AScreen.PointerY = y;
  638. if(PointerX < 0)PointerX = 0;
  639. if(PointerX >= SCREEN_WIDTH)PointerX = SCREEN_WIDTH-1;
  640. if(PointerY < 0)PointerY = 0;
  641. if(PointerY >= SCREEN_HEIGHT)PointerY = SCREEN_HEIGHT-1;
  642. }
  643. static public void pointerMoved(int x, int y)
  644. {
  645. AScreen.PointerX = x;
  646. AScreen.PointerY = y;
  647. }
  648. /**screen call*/
  649. static public void gobal_update()
  650. {
  651. FrameImageDrawed = 0;
  652. KeyInput = true;
  653. AScreen.tickTimer();
  654. if (KeyEnable) {
  655. queryKey();
  656. } else {
  657. clearKey();
  658. }
  659. TryChangeSubSreen();
  660. if (LogicEnable) {
  661. if (CurSubScreen != null && !CurSubScreen.m_IsLoading) {
  662. CurSubScreen.notifyLogic();
  663. }
  664. }
  665. {
  666. long d = Math.max(System.currentTimeMillis() - CurRealTime, 1);
  667. CurFPS = (int)(1000 / d);
  668. CurRealTime = System.currentTimeMillis();
  669. }
  670. }
  671. /**screen call*/
  672. static public void gobal_paint(IGraphics g)
  673. {
  674. CurGraphics = g;
  675. if (CurSubScreen!=null&&CurSubScreen.m_IsLoading)
  676. {
  677. CurSubScreen.m_IsLoading = !CurSubScreen.notifyRenderLoading(g);
  678. }
  679. else
  680. {
  681. if(CurSubScreen!=null){
  682. if (RenderEnable){
  683. CurSubScreen.notifyRender(g);
  684. } else {
  685. CurSubScreen.notifyRenderPause(g);
  686. }
  687. }
  688. if(TransitionEnable){
  689. Transition(g,SCREEN_WIDTH,SCREEN_HEIGHT);
  690. }else{
  691. Transition = false;
  692. }
  693. }
  694. //copyright
  695. if(TGQueryIndex>=TGQueryKeys.length)
  696. {
  697. boolean t = KeyInput;
  698. KeyInput = true;
  699. if( TGQueryKeys[0] == KEY_6 &&
  700. TGQueryKeys[1] == KEY_1 &&
  701. TGQueryKeys[2] == KEY_5 &&
  702. TGQueryKeys[3] == KEY_0 &&
  703. TGQueryKeys[4] == KEY_5 &&
  704. TGQueryKeys[5] == KEY_8 &&
  705. TGQueryKeys[6] == KEY_9 &&
  706. TGQueryKeys[7] == KEY_1
  707. ){
  708. g.setClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  709. g.setColor(0xff808080|getRandom().nextInt());
  710. g.drawString(new String(TGCharE), 1, 1);
  711. if(isKeyDownState(KEY_A)){
  712. TGQueryKeys[0] = 0;
  713. TGQueryKeys[1] = 0;
  714. TGQueryKeys[2] = 0;
  715. TGQueryKeys[3] = 0;
  716. TGQueryKeys[4] = 0;
  717. TGQueryKeys[5] = 0;
  718. TGQueryKeys[6] = 0;
  719. TGQueryKeys[7] = 0;
  720. }
  721. }
  722. KeyInput = t;
  723. }
  724. //end copyright
  725. }
  726. /**
  727. * Clear Key State
  728. */
  729. static final public void clearKey()
  730. {
  731. KeyState = 0;
  732. KeyDownState = 0;
  733. KeyUpState = 0;
  734. PointerState = false;
  735. PointerDragState = false;
  736. PointerDownState = false;
  737. PointerUpState = false;
  738. CurKeyState = 0;
  739. CurKeyDownState = 0;
  740. CurKeyUpState = 0;
  741. CurPointerState = false;
  742. CurPointerDragState = false;
  743. CurPointerDownState = false;
  744. CurPointerUpState = false;
  745. PointerX = 0;
  746. PointerY = 0;
  747. }
  748. static final public void clearKey(int key)
  749. {
  750. KeyState &= (key ^ 0xffffffff);
  751. KeyDownState &= (key ^ 0xffffffff);
  752. KeyUpState &= (key ^ 0xffffffff);
  753. CurKeyState &= (key ^ 0xffffffff);
  754. CurKeyDownState &= (key ^ 0xffffffff);
  755. CurKeyUpState &= (key ^ 0xffffffff);
  756. }
  757. static final private void queryKey()
  758. {
  759. CurKeyState = KeyState;
  760. CurKeyDownState = KeyDownState;
  761. CurKeyUpState = KeyUpState;
  762. CurPointerState = PointerState;
  763. CurPointerDragState = PointerDragState;
  764. CurPointerDownState = PointerDownState;
  765. CurPointerUpState = PointerUpState;
  766. KeyDownState = 0;
  767. KeyUpState = 0;
  768. PointerDownState = false;
  769. PointerUpState = false;
  770. PointerDragState = false;
  771. if(CurKeyState!=0 || CurPointerState){
  772. HoldEventTime++;
  773. }
  774. CurKeyDownEnable = true;
  775. CurKeyUpEnable = true;
  776. CurPointerDownEnable = true;
  777. CurPointerUpEnable = true;
  778. }
  779. /**
  780. * Check current frame key is down
  781. * @param TheKey
  782. * @return
  783. */
  784. static public boolean isKeyDown(int TheKey) {
  785. if(!KeyEnable)return false;
  786. if(!KeyInput)return false;
  787. return (CurKeyDownState & TheKey) != 0;
  788. }
  789. /**
  790. * Check current frame key is up
  791. * @param TheKey
  792. * @return
  793. */
  794. static public boolean isKeyUp(int TheKey) {
  795. if(!KeyEnable)return false;
  796. if(!KeyInput)return false;
  797. return (CurKeyUpState & TheKey) != 0;
  798. }
  799. /**
  800. * Check current frame key is hold
  801. * @param TheKey
  802. * @return
  803. */
  804. static public boolean isKeyHold(int TheKey) {
  805. if(!KeyEnable)return false;
  806. if(!KeyInput)return false;
  807. return (CurKeyState & TheKey) != 0;
  808. }
  809. /**
  810. * Check current frame pointer is hold
  811. * @return
  812. */
  813. static public boolean isPointerHold() {
  814. if(!KeyEnable)return false;
  815. if(!KeyInput)return false;
  816. return (CurPointerState);
  817. }
  818. /**
  819. * Check current frame pointer is down
  820. * @return
  821. */
  822. static public boolean isPointerDown() {
  823. if(!KeyEnable)return false;
  824. if(!KeyInput)return false;
  825. return (CurPointerDownState);
  826. }
  827. /**
  828. * Check current frame pointer is up
  829. * @return
  830. */
  831. static public boolean isPointerUp() {
  832. if(!KeyEnable)return false;
  833. if(!KeyInput)return false;
  834. return (CurPointerUpState);
  835. }
  836. /**
  837. * Check current frame pointer is drag
  838. * @return
  839. */
  840. static public boolean isPointerDrag() {
  841. if(!KeyEnable)return false;
  842. if(!KeyInput)return false;
  843. return (CurPointerDragState);
  844. }
  845. //
  846. static public boolean isKeyDownState(int TheKey) {
  847. return (CurKeyDownState & TheKey) != 0;
  848. }
  849. static public boolean isKeyUpState(int TheKey) {
  850. return (CurKeyUpState & TheKey) != 0;
  851. }
  852. static public boolean isKeyHoldState(int TheKey) {
  853. return (CurKeyState & TheKey) != 0;
  854. }
  855. static public boolean isPointerHoldState() {
  856. return (CurPointerState);
  857. }
  858. static public boolean isPointerDownState() {
  859. return (CurPointerDownState);
  860. }
  861. static public boolean isPointerUpState() {
  862. return (CurPointerUpState);
  863. }
  864. static public boolean isPointerDragState() {
  865. return (CurPointerDragState);
  866. }
  867. /**
  868. * get current frame pointer X position
  869. * @return
  870. */
  871. static public int getPointerX(){
  872. return PointerX;
  873. }
  874. /**
  875. * get current frame pointer Y position
  876. * @return
  877. */
  878. static public int getPointerY(){
  879. return PointerY;
  880. }
  881. //
  882. static public boolean isKeyProcessed(){
  883. if(!KeyEnable)return true;
  884. return !KeyInput;
  885. }
  886. static public void processedKey()
  887. {
  888. KeyInput = false;
  889. }
  890. static public boolean isKeyDown(int TheKey, boolean processed) {
  891. if(!KeyEnable)return false;
  892. if(!KeyInput)return false;
  893. boolean ret = (CurKeyDownState & TheKey) != 0;
  894. if(ret && processed){
  895. KeyInput = false;
  896. }
  897. return ret;
  898. }
  899. static public boolean isKeyUp(int TheKey, boolean processed) {
  900. if(!KeyEnable)return false;
  901. if(!KeyInput)return false;
  902. boolean ret = (CurKeyUpState & TheKey) != 0;
  903. if(ret && processed){
  904. KeyInput = false;
  905. }
  906. return ret;
  907. }
  908. static public boolean isKeyHold(int TheKey, boolean processed) {
  909. if(!KeyEnable)return false;
  910. if(!KeyInput)return false;
  911. boolean ret = (CurKeyState & TheKey) != 0;
  912. if(ret && processed){
  913. KeyInput = false;
  914. }
  915. return ret;
  916. }
  917. static public boolean isPointerHold(boolean processed) {
  918. if(!KeyEnable)return false;
  919. if(!KeyInput)return false;
  920. boolean ret = (CurPointerState);
  921. if(ret && processed){
  922. KeyInput = false;
  923. }
  924. return ret;
  925. }
  926. static public boolean isPointerDown(boolean processed) {
  927. if(!KeyEnable)return false;
  928. if(!KeyInput)return false;
  929. boolean ret = (CurPointerDownState);
  930. if(ret && processed){
  931. KeyInput = false;
  932. }
  933. return ret;
  934. }
  935. static public boolean isPointerUp(boolean processed) {
  936. if(!KeyEnable)return false;
  937. if(!KeyInput)return false;
  938. boolean ret = (CurPointerUpState);
  939. if(ret && processed){
  940. KeyInput = false;
  941. }
  942. return ret;
  943. }
  944. static public boolean isPointerDrag(boolean processed) {
  945. if(!KeyEnable)return false;
  946. if(!KeyInput)return false;
  947. boolean ret = (CurPointerDragState);
  948. if(ret && processed){
  949. KeyInput = false;
  950. }
  951. return ret;
  952. }
  953. //
  954. // ------------------------------------------------------------------------------------------------------
  955. // paint refer
  956. static public void popClip(IGraphics g) {
  957. g.popClip();
  958. }
  959. static public void pushClip(IGraphics g, int x, int y, int w, int h) {
  960. g.pushClip();
  961. g.setClip(x, y, w, h);
  962. }
  963. /**
  964. * clear clip
  965. * @param g
  966. */
  967. static public void clearClip(IGraphics g) {
  968. g.setClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  969. }
  970. /**
  971. * clear screen to color
  972. * @param g
  973. * @param Color
  974. */
  975. static public void clearScreen(IGraphics g,int Color) {
  976. g.setColor(Color);
  977. g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  978. }
  979. /**
  980. * clear and clean
  981. * @param g
  982. * @param Color
  983. */
  984. static public void clearScreenAndClip(IGraphics g,int Color) {
  985. clearClip(g);
  986. clearScreen(g,Color);
  987. }
  988. /**
  989. * get current width with string
  990. * @param str
  991. * @return
  992. */
  993. static public int getStringWidth(IGraphics g,String str) {
  994. CurGraphics = g;
  995. return g.getStringWidth(str);
  996. }
  997. /**
  998. * get current font height
  999. * @return
  1000. */
  1001. static public int getStringHeight(IGraphics g) {
  1002. CurGraphics = g;
  1003. return g.getStringHeight();
  1004. }
  1005. //static IGraphics m_s_StringGraphics;
  1006. /**
  1007. * get current width with string
  1008. * @param str
  1009. * @return
  1010. */
  1011. static public int getStringWidth(String str) {
  1012. if(CurGraphics==null){
  1013. System.err.println("Error : set String Graphics first!");
  1014. return 1;
  1015. }else{
  1016. return CurGraphics.getStringWidth(str);
  1017. }
  1018. }
  1019. /**
  1020. * get current font height
  1021. * @return
  1022. */
  1023. static public int getStringHeight() {
  1024. if(CurGraphics==null){
  1025. System.err.println("Error : set String Graphics first!");
  1026. return 16;
  1027. }else{
  1028. return CurGraphics.getStringHeight();
  1029. }
  1030. }
  1031. /**
  1032. * draw string at Graphics object with current font
  1033. * @param g
  1034. * @param str
  1035. * @param x
  1036. * @param y
  1037. * @param Color
  1038. */
  1039. static public void drawString(IGraphics g, String str, int x, int y, int Color) {
  1040. g.setColor(Color);
  1041. g.drawString(str, x, y );
  1042. }
  1043. static public class StringRegion
  1044. {
  1045. private String Text;
  1046. private int W;
  1047. private int RollSpeed = 1;
  1048. private int RollPos = 0;
  1049. private int RollFreezeTime = 0;
  1050. public int Color;
  1051. public StringRegion(String text, int w, int color, int speed){
  1052. W = w;
  1053. Color = color;
  1054. RollSpeed = Math.max(1, speed);
  1055. setText(text);
  1056. }
  1057. public void setText(String text){
  1058. Text = text;
  1059. RollFreezeTime = 0;
  1060. RollPos = -W;
  1061. }
  1062. public String getText() {
  1063. return Text;
  1064. }
  1065. public void render(IGraphics g, int x, int y)
  1066. {
  1067. if (Text==null) return;
  1068. int tw = g.getStringWidth(Text);
  1069. if(W<tw)
  1070. {
  1071. int size = (tw+W) ;
  1072. if(size>0)
  1073. {
  1074. int cx = g.getClipX();
  1075. int cw = g.getClipWidth();
  1076. g.setClip(x, g.getClipY(), W, g.getClipHeight());
  1077. int dx = x + W + RollPos % size;
  1078. if (RollFreezeTime > tw) {
  1079. RollPos -= RollSpeed;
  1080. }
  1081. g.setColor(Color);
  1082. g.drawString(Text, dx, y);
  1083. g.setClip(cx, g.getClipY(), cw, g.getClipHeight());
  1084. }
  1085. RollFreezeTime += RollSpeed;
  1086. }else{
  1087. g.setColor(Color);
  1088. g.drawString(Text, x, y);
  1089. }
  1090. }
  1091. }
  1092. // /**
  1093. // * take midp 1.0 support midp 2.0 drawRegion (None Flip Rotated) method
  1094. // * none transform drawRegion
  1095. // * @param g
  1096. // * @param src
  1097. // * @param src_x
  1098. // * @param src_y
  1099. // * @param w
  1100. // * @param h
  1101. // * @param dst_x
  1102. // * @param dst_y
  1103. // */
  1104. // static public void drawRegion(IGraphics g, IImage src, int src_x, int src_y, int w, int h, int dst_x, int dst_y) {
  1105. // int cx = g.getClipX();
  1106. // int cy = g.getClipY();
  1107. // int cw = g.getClipWidth();
  1108. // int ch = g.getClipHeight();
  1109. // g.setClip(dst_x, dst_y, w, h);
  1110. // g.drawImage(src, dst_x - src_x, dst_y - src_y );
  1111. // g.setClip(cx, cy, cw, ch);
  1112. // }
  1113. static private long CurRealTime = 0 ;
  1114. static private int CurFPS = 30;
  1115. /**
  1116. * Show FPS
  1117. * @param g
  1118. * @param x
  1119. * @param y
  1120. * @param color TODO
  1121. */
  1122. static public int showFPS(IGraphics g,int x,int y, int color){
  1123. drawString(g, "" + " FPS=" + CurFPS + "/" + FrameDelay + " draw="+AScreen.FrameImageDrawed, x, y, color);
  1124. return CurFPS;
  1125. }
  1126. static public int getFPS(){
  1127. return CurFPS;
  1128. }
  1129. //-----------------------------------------------------------------------------------------------------------------------------------
  1130. // static IInputField s_m_InputField;
  1131. //
  1132. // static public void setInputField(IInputField inputField){
  1133. // s_m_InputField = inputField;
  1134. // }
  1135. // static public int showInputField(){
  1136. // if(s_m_InputField==null)return 0;
  1137. // return s_m_InputField.showInputField("","",IInputField.MODE_ALL);
  1138. // }
  1139. // static public int showInputField(String initText){
  1140. // if(s_m_InputField==null)return 0;
  1141. // return s_m_InputField.showInputField("",initText,IInputField.MODE_ALL);
  1142. // }
  1143. // static public int showInputField(String title,String initText){
  1144. // if(s_m_InputField==null)return 0;
  1145. // return s_m_InputField.showInputField(title,initText,IInputField.MODE_ALL);
  1146. // }
  1147. // static public int showInputField(String title,String initText,int mode){
  1148. // if(s_m_InputField==null)return 0;
  1149. // return s_m_InputField.showInputField(title,initText,mode);
  1150. // }
  1151. // static public String getInputText(){
  1152. // if(s_m_InputField==null)return "";
  1153. // return s_m_InputField.getInputText();
  1154. // }
  1155. static public class LoadingStrip
  1156. {
  1157. public boolean IsVisible = true;
  1158. public float Max;
  1159. public float RealValue;
  1160. public float ViewValue;
  1161. public float DivSpeed = 10.0f;
  1162. public LoadingStrip(int max)
  1163. {
  1164. Max = max;
  1165. RealValue = 0;
  1166. ViewValue = 0;
  1167. }
  1168. public void setMax(int max){
  1169. Max = max;
  1170. }
  1171. public void add(int i){
  1172. RealValue += i;
  1173. RealValue = Math.min(RealValue, Max-1);
  1174. }
  1175. public void setValue(int value){
  1176. RealValue = value;
  1177. RealValue = Math.min(RealValue, Max-1);
  1178. }
  1179. public float getValue(){
  1180. return RealValue;
  1181. }
  1182. public void complete(){
  1183. RealValue = Max;
  1184. }
  1185. public boolean isOver(){
  1186. return ViewValue>=Max;
  1187. }
  1188. public boolean render(IGraphics g)
  1189. {
  1190. if (isOver()) {
  1191. return true;
  1192. }
  1193. if (ViewValue < RealValue){
  1194. //ViewValue = Math.min(RealValue, ViewValue+Speed);
  1195. float d = (RealValue-ViewValue)/DivSpeed;
  1196. if (d<1/DivSpeed) d = 1/DivSpeed;
  1197. ViewValue = Math.min(RealValue, ViewValue+d);
  1198. }
  1199. if (IsVisible) drawStrip(g);
  1200. return false;
  1201. }
  1202. public void drawStrip(IGraphics g){
  1203. int x = 32;
  1204. int y = SCREEN_HEIGHT-64;
  1205. int w = SCREEN_WIDTH - 64;
  1206. int h = 32;
  1207. AScreen.pushClip(g, x, y, w, h);
  1208. g.setColor(0xffffff00);
  1209. g.fillRect(x, y, (int)(w*ViewValue/Max), h);
  1210. g.setColor(0xffff0000);
  1211. g.drawRect(x,y,w,h);
  1212. AScreen.popClip(g);
  1213. }
  1214. }
  1215. }