/android/LGame-Android-0.2.94/LGame/org/loon/framework/android/game/core/graphics/Screen.java

http://loon-simple.googlecode.com/ · Java · 1425 lines · 968 code · 184 blank · 273 comment · 206 complexity · 5a1f4a9fae322a778eedd50b31d5a6ff MD5 · raw file

  1. package org.loon.framework.android.game.core.graphics;
  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.concurrent.TimeUnit;
  5. import org.loon.framework.android.game.IAndroid2DHandler;
  6. import org.loon.framework.android.game.action.map.shapes.RectBox;
  7. import org.loon.framework.android.game.action.sprite.ISprite;
  8. import org.loon.framework.android.game.action.sprite.ISpriteListener;
  9. import org.loon.framework.android.game.action.sprite.Sprites;
  10. import org.loon.framework.android.game.core.EmulatorButtons;
  11. import org.loon.framework.android.game.core.EmulatorListener;
  12. import org.loon.framework.android.game.core.LInput;
  13. import org.loon.framework.android.game.core.LObject;
  14. import org.loon.framework.android.game.core.LSystem;
  15. import org.loon.framework.android.game.core.graphics.device.LGraphics;
  16. import org.loon.framework.android.game.core.graphics.window.actor.Layer;
  17. import org.loon.framework.android.game.core.timer.LTimerContext;
  18. import org.loon.framework.android.game.utils.GraphicsUtils;
  19. import org.loon.framework.android.game.utils.MultitouchUtils;
  20. import android.app.AlertDialog;
  21. import android.graphics.Bitmap;
  22. import android.graphics.Point;
  23. import android.hardware.Sensor;
  24. import android.hardware.SensorEvent;
  25. import android.hardware.SensorManager;
  26. import android.util.Log;
  27. import android.view.KeyEvent;
  28. import android.view.Menu;
  29. import android.view.MenuItem;
  30. import android.view.MotionEvent;
  31. /**
  32. *
  33. * Copyright 2008 - 2011
  34. *
  35. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  36. * use this file except in compliance with the License. You may obtain a copy of
  37. * the License at
  38. *
  39. * http://www.apache.org/licenses/LICENSE-2.0
  40. *
  41. * Unless required by applicable law or agreed to in writing, software
  42. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  43. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  44. * License for the specific language governing permissions and limitations under
  45. * the License.
  46. *
  47. * @project loonframework
  48. * @author chenpeng
  49. * @email ceponline@yahoo.com.cn
  50. * @version 0.1.3
  51. */
  52. public abstract class Screen implements IScreen, LInput {
  53. // private LTimer adTimer;
  54. // ??????
  55. private final ArrayList<Runnable> runnables;
  56. private int touchX, touchY, lastTouchX, lastTouchY, touchDX, touchDY,
  57. touchDirection;
  58. private float offsetTouchX, offsetMoveX, offsetTouchY, offsetMoveY;
  59. protected long elapsedTime;
  60. private final static boolean[] touchDown, keyDown;
  61. private final static int[] touchPressed, touchReleased;
  62. private int pressedTouch, releasedTouch;
  63. private final static int[] keyPressed, keyReleased;
  64. private final static float ACCEL_OFFEST = 3;
  65. private final static double LANDSCAPE_OFFSET = 0.2;
  66. private double landscapeUpdate = 0;
  67. private int pressedKey, releasedKey;
  68. private double sensorInclination;
  69. private float accelX, accelY, accelZ;
  70. boolean isNext, isMoving, isGravityToKey;
  71. private int mode;
  72. private long lastUpdate;
  73. private Bitmap currentScreen;
  74. private IAndroid2DHandler handler;
  75. private int width, height, halfWidth, halfHeight;
  76. private SensorDirection direction = SensorDirection.NONE;
  77. private LInput baseInput;
  78. // ????
  79. private Sprites sprites;
  80. // ????
  81. private Desktop desktop;
  82. private Point touch = new Point(0, 0);
  83. static {
  84. keyDown = new boolean[128];
  85. keyPressed = keyReleased = new int[128];
  86. touchDown = new boolean[10];
  87. touchPressed = touchReleased = new int[10];
  88. }
  89. public Screen() {
  90. LSystem.AUTO_REPAINT = true;
  91. this.mode = SCREEN_CANVAS_REPAINT;
  92. this.setFPS(getMaxFPS());
  93. this.runnables = new ArrayList<Runnable>(1);
  94. this.handler = (IAndroid2DHandler) LSystem.getSystemHandler();
  95. this.width = handler.getWidth();
  96. this.height = handler.getHeight();
  97. this.halfWidth = getWidth() / 2;
  98. this.halfHeight = getHeight() / 2;
  99. this.currentScreen = Bitmap.createBitmap(width, height,
  100. Bitmap.Config.RGB_565);
  101. this.baseInput = this;
  102. this.touchX = touchY = lastTouchX = lastTouchY = touchDX = touchDY = 0;
  103. this.sprites = new Sprites(width, height);
  104. this.desktop = new Desktop(baseInput, width, height);
  105. this.isNext = true;
  106. this.isMoving = false;
  107. }
  108. /**
  109. * ?????????
  110. */
  111. public void onLoad() {
  112. }
  113. /**
  114. * ??????
  115. *
  116. * @return
  117. */
  118. public ISpriteListener getSprListerner() {
  119. if (sprites == null) {
  120. return null;
  121. }
  122. return sprites.getSprListerner();
  123. }
  124. /**
  125. * ??Screen???
  126. *
  127. * @param sprListerner
  128. */
  129. public void setSprListerner(ISpriteListener sprListerner) {
  130. if (sprites == null) {
  131. return;
  132. }
  133. sprites.setSprListerner(sprListerner);
  134. }
  135. /**
  136. * ????Screen??
  137. */
  138. public String getName() {
  139. return getClass().getSimpleName();
  140. }
  141. /**
  142. * ?????????
  143. */
  144. public void setEmulatorListener(EmulatorListener emulator) {
  145. if (LSystem.getSurface2D() != null) {
  146. LSystem.getSurface2D().setEmulatorListener(emulator);
  147. }
  148. }
  149. /**
  150. * ????????
  151. *
  152. * @return
  153. */
  154. public EmulatorButtons getEmulatorButtons() {
  155. if (LSystem.getSurface2D() != null) {
  156. return LSystem.getSurface2D().getEmulatorButtons();
  157. }
  158. return null;
  159. }
  160. /**
  161. * ???????????
  162. *
  163. * @param visible
  164. */
  165. public void emulatorButtonsVisible(boolean visible) {
  166. if (LSystem.getSurface2D() != null) {
  167. try {
  168. EmulatorButtons es = LSystem.getSurface2D()
  169. .getEmulatorButtons();
  170. es.setVisible(visible);
  171. } catch (Exception e) {
  172. }
  173. }
  174. }
  175. /**
  176. * ????????
  177. *
  178. * @param delay
  179. */
  180. /*
  181. * public void setAdTimer(long delay) { adTimer.setDelay(delay); adDelay =
  182. * delay; }
  183. */
  184. /**
  185. * ????????
  186. *
  187. * @return
  188. */
  189. // public long getAdTimer() {
  190. // return adTimer.getDelay();
  191. // }
  192. /**
  193. * ?????????
  194. *
  195. * @param url
  196. */
  197. public void openBrowser(final String url) {
  198. if (handler != null) {
  199. handler.getLGameActivity().openBrowser(url);
  200. }
  201. }
  202. /**
  203. * ??????
  204. *
  205. * @return
  206. */
  207. public boolean isVisibleAD() {
  208. if (handler != null) {
  209. return handler.getLGameActivity().isVisibleAD();
  210. }
  211. return false;
  212. }
  213. /**
  214. * ????
  215. */
  216. public void hideAD() {
  217. if (handler != null) {
  218. handler.getLGameActivity().hideAD();
  219. }
  220. }
  221. /**
  222. * ????
  223. */
  224. public void showAD() {
  225. if (handler != null) {
  226. handler.getLGameActivity().showAD();
  227. }
  228. }
  229. /**
  230. * ?????
  231. *
  232. * @return
  233. */
  234. public int getAndroidSelect() {
  235. if (handler != null) {
  236. return handler.getLGameActivity().getAndroidSelect();
  237. }
  238. return -2;
  239. }
  240. /**
  241. * ????Android?????????????
  242. *
  243. * @param title
  244. * @param message
  245. * @return
  246. */
  247. public AlertDialog showAndroidProgress(final String title,
  248. final String message) {
  249. if (handler != null) {
  250. return handler.getLGameActivity().showAndroidProgress(title,
  251. message);
  252. }
  253. return null;
  254. }
  255. /**
  256. * ????Android????????????
  257. *
  258. * @param message
  259. */
  260. public void showAndroidAlert(final String title, final String message) {
  261. if (handler != null) {
  262. handler.getLGameActivity().showAndroidAlert(title, message);
  263. }
  264. }
  265. /**
  266. * ????Android??????????????
  267. *
  268. * @param ex
  269. */
  270. public void showAndroidExceptionMessageBox(final Exception ex) {
  271. if (handler != null) {
  272. handler.getLGameActivity().showAndroidException(ex);
  273. }
  274. }
  275. /**
  276. * ????Android???????????
  277. *
  278. * @param text
  279. * @return
  280. */
  281. public int showAndroidSelectMessageBox(final String title,
  282. final String[] text) {
  283. if (handler != null) {
  284. return handler.getLGameActivity().showAndroidSelect(title, text);
  285. }
  286. return -1;
  287. }
  288. /**
  289. * ??Assets??????
  290. *
  291. * @param file
  292. * @param loop
  293. */
  294. public void playtAssetsMusic(final String file, final boolean loop) {
  295. if (handler != null) {
  296. handler.getAssetsSound().playSound(file, loop);
  297. }
  298. }
  299. /**
  300. * ??Assets????????
  301. *
  302. * @param vol
  303. */
  304. public void resetAssetsMusic(final int vol) {
  305. if (handler != null) {
  306. handler.getAssetsSound().setSoundVolume(vol);
  307. }
  308. }
  309. /**
  310. * ??Assets??????
  311. *
  312. */
  313. public void resetAssetsMusic() {
  314. if (handler != null) {
  315. handler.getAssetsSound().resetSound();
  316. }
  317. }
  318. /**
  319. * ??Assets??????
  320. */
  321. public void stopAssetsMusic() {
  322. if (handler != null) {
  323. handler.getAssetsSound().stopSound();
  324. }
  325. }
  326. /**
  327. * ??Assets??????????
  328. */
  329. public void stopAssetsMusic(int index) {
  330. if (handler != null) {
  331. handler.getAssetsSound().stopSound(index);
  332. }
  333. }
  334. /**
  335. * ??????
  336. *
  337. * @param screen
  338. */
  339. public void setBackground(Bitmap screen) {
  340. if (screen != null) {
  341. setRepaintMode(SCREEN_BITMAP_REPAINT);
  342. if (screen.getWidth() != getWidth()
  343. || screen.getHeight() != getHeight()) {
  344. screen = GraphicsUtils.getResize(screen, getWidth(),
  345. getHeight());
  346. }
  347. Bitmap tmp = currentScreen;
  348. currentScreen = screen;
  349. if (tmp != null) {
  350. tmp.recycle();
  351. tmp = null;
  352. }
  353. }
  354. }
  355. /**
  356. * ??????
  357. *
  358. * @param screen
  359. */
  360. public void setBackground(LImage screen) {
  361. setBackground(screen.getBitmap());
  362. }
  363. /**
  364. * ??????
  365. */
  366. public void setBackground(String fileName, boolean transparency) {
  367. this.setBackground(getImage(fileName, transparency));
  368. }
  369. /**
  370. * ??????
  371. *
  372. * @param fileName
  373. */
  374. public void setBackground(String fileName) {
  375. this.setBackground(getImage(fileName, false));
  376. }
  377. /**
  378. * ??????
  379. *
  380. * @return
  381. */
  382. public Bitmap getBackground() {
  383. return currentScreen;
  384. }
  385. /**
  386. * ?????
  387. *
  388. * @param fps
  389. */
  390. public void setFPS(long fps) {
  391. LSystem.setFPS(fps);
  392. }
  393. /**
  394. * ???????
  395. *
  396. * @return
  397. */
  398. public long getFPS() {
  399. return LSystem.getFPS();
  400. }
  401. /**
  402. * ???????
  403. *
  404. * @return
  405. */
  406. public long getMaxFPS() {
  407. return LSystem.getMaxFPS();
  408. }
  409. public void setBackground(LColor color) {
  410. setRepaintMode(SCREEN_BITMAP_REPAINT);
  411. LImage image = new LImage(currentScreen);
  412. LGraphics g = image.getLGraphics();
  413. g.setBackground(color);
  414. g.dispose();
  415. }
  416. public void destroy() {
  417. synchronized (this) {
  418. callEvents(false);
  419. isNext = false;
  420. sprites = null;
  421. desktop = null;
  422. currentScreen = null;
  423. dispose();
  424. }
  425. }
  426. /**
  427. * ???????
  428. *
  429. */
  430. public void dispose() {
  431. }
  432. public Desktop getDesktop() {
  433. return desktop;
  434. }
  435. public Sprites getSprites() {
  436. return sprites;
  437. }
  438. /**
  439. * ???????????
  440. */
  441. public ArrayList<LComponent> getComponents(Class<? extends LComponent> clazz) {
  442. if (desktop != null) {
  443. return desktop.getComponents(clazz);
  444. }
  445. return null;
  446. }
  447. /**
  448. * ???????????
  449. *
  450. * @return
  451. */
  452. public LComponent getTopComponent() {
  453. if (desktop != null) {
  454. return desktop.getTopComponent();
  455. }
  456. return null;
  457. }
  458. /**
  459. * ???????????
  460. *
  461. * @return
  462. */
  463. public LComponent getBottomComponent() {
  464. if (desktop != null) {
  465. return desktop.getBottomComponent();
  466. }
  467. return null;
  468. }
  469. /**
  470. * ???????????
  471. */
  472. public Layer getTopLayer() {
  473. if (desktop != null) {
  474. return desktop.getTopLayer();
  475. }
  476. return null;
  477. }
  478. /**
  479. * ???????????
  480. */
  481. public Layer getBottomLayer() {
  482. if (desktop != null) {
  483. return desktop.getBottomLayer();
  484. }
  485. return null;
  486. }
  487. /**
  488. * ????????????
  489. *
  490. */
  491. public ArrayList<ISprite> getSprites(Class<? extends ISprite> clazz) {
  492. if (sprites != null) {
  493. return sprites.getSprites(clazz);
  494. }
  495. return null;
  496. }
  497. /**
  498. * ???????????
  499. *
  500. */
  501. public ISprite getTopSprite() {
  502. if (sprites != null) {
  503. return sprites.getTopSprite();
  504. }
  505. return null;
  506. }
  507. /**
  508. * ???????????
  509. *
  510. */
  511. public ISprite getBottomSprite() {
  512. if (sprites != null) {
  513. return sprites.getBottomSprite();
  514. }
  515. return null;
  516. }
  517. /**
  518. * ??????
  519. *
  520. * @param sprite
  521. */
  522. public void add(ISprite sprite) {
  523. if (sprites != null) {
  524. sprites.add(sprite);
  525. }
  526. }
  527. /**
  528. * ??????
  529. *
  530. * @param comp
  531. */
  532. public void add(LComponent comp) {
  533. if (desktop != null) {
  534. desktop.add(comp);
  535. }
  536. }
  537. public void remove(ISprite sprite) {
  538. if (sprites != null) {
  539. sprites.remove(sprite);
  540. }
  541. }
  542. public void removeSprite(Class<? extends ISprite> clazz) {
  543. if (sprites != null) {
  544. sprites.remove(clazz);
  545. }
  546. }
  547. public void remove(LComponent comp) {
  548. if (desktop != null) {
  549. desktop.remove(comp);
  550. }
  551. }
  552. public void removeComponent(Class<? extends LComponent> clazz) {
  553. if (desktop != null) {
  554. desktop.remove(clazz);
  555. }
  556. }
  557. public void removeAll() {
  558. if (sprites != null) {
  559. sprites.removeAll();
  560. }
  561. if (desktop != null) {
  562. desktop.getContentPane().clear();
  563. }
  564. }
  565. /**
  566. * ??????????
  567. *
  568. * @param sprite
  569. * @return
  570. */
  571. public boolean onClick(ISprite sprite) {
  572. if (sprite == null) {
  573. return false;
  574. }
  575. if (sprite.isVisible()) {
  576. RectBox rect = sprite.getCollisionBox();
  577. if (rect.contains(touchX, touchY)
  578. || rect.intersects(touchX, touchY)) {
  579. return true;
  580. }
  581. }
  582. return false;
  583. }
  584. /**
  585. * ??????????
  586. *
  587. * @param component
  588. * @return
  589. */
  590. public boolean onClick(LComponent component) {
  591. if (component == null) {
  592. return false;
  593. }
  594. if (component.isVisible()) {
  595. RectBox rect = component.getCollisionBox();
  596. if (rect.contains(touchX, touchY)
  597. || rect.intersects(touchX, touchY)) {
  598. return true;
  599. }
  600. }
  601. return false;
  602. }
  603. public void centerOn(final LObject object) {
  604. object.setLocation(getWidth() / 2 - object.getWidth() / 2, getHeight()
  605. / 2 - object.getHeight() / 2);
  606. }
  607. public void topOn(final LObject object) {
  608. object.setLocation(getWidth() / 2 - object.getWidth() / 2, 0);
  609. }
  610. public void leftOn(final LObject object) {
  611. object.setLocation(0, getHeight() / 2 - object.getHeight() / 2);
  612. }
  613. public void rightOn(final LObject object) {
  614. object.setLocation(getWidth() - object.getWidth(), getHeight() / 2
  615. - object.getHeight() / 2);
  616. }
  617. public void bottomOn(final LObject object) {
  618. object.setLocation(getWidth() / 2 - object.getWidth() / 2, getHeight()
  619. - object.getHeight());
  620. }
  621. /**
  622. * ????????
  623. */
  624. public int getRepaintMode() {
  625. return mode;
  626. }
  627. /**
  628. * ????????
  629. *
  630. * @param mode
  631. */
  632. public void setRepaintMode(int mode) {
  633. this.mode = mode;
  634. }
  635. /**
  636. * ????????
  637. *
  638. * @param runnable
  639. */
  640. public final void callEvent(Runnable runnable) {
  641. synchronized (runnables) {
  642. runnables.add(runnable);
  643. }
  644. }
  645. /**
  646. * ?????????
  647. *
  648. * @param runnable
  649. */
  650. public final void callEventWait(Runnable runnable) {
  651. synchronized (runnable) {
  652. synchronized (runnables) {
  653. runnables.add(runnable);
  654. }
  655. try {
  656. runnable.wait();
  657. } catch (InterruptedException ex) {
  658. }
  659. }
  660. }
  661. /**
  662. * ????????
  663. *
  664. */
  665. public final void callEventInterrupt() {
  666. synchronized (runnables) {
  667. for (Iterator<Runnable> it = runnables.iterator(); it.hasNext();) {
  668. Object running = it.next();
  669. synchronized (running) {
  670. if (running instanceof Thread) {
  671. ((Thread) running).setPriority(Thread.MIN_PRIORITY);
  672. ((Thread) running).interrupt();
  673. }
  674. }
  675. }
  676. }
  677. }
  678. /**
  679. * ??????
  680. *
  681. */
  682. public final void callEvents() {
  683. callEvents(true);
  684. }
  685. /**
  686. * ????????????
  687. *
  688. * @param execute
  689. */
  690. public final void callEvents(boolean execute) {
  691. if (!execute) {
  692. synchronized (runnables) {
  693. runnables.clear();
  694. }
  695. return;
  696. }
  697. if (runnables.size() == 0) {
  698. return;
  699. }
  700. ArrayList<Runnable> runnableList;
  701. synchronized (runnables) {
  702. runnableList = new ArrayList<Runnable>(runnables);
  703. runnables.clear();
  704. }
  705. for (Iterator<Runnable> it = runnableList.iterator(); it.hasNext();) {
  706. Object running = it.next();
  707. synchronized (running) {
  708. try {
  709. if (running instanceof Thread) {
  710. Thread thread = (Thread) running;
  711. if (!thread.isAlive()) {
  712. thread.start();
  713. }
  714. } else {
  715. ((Runnable) running).run();
  716. }
  717. } catch (Exception ex) {
  718. }
  719. running.notifyAll();
  720. }
  721. }
  722. runnableList = null;
  723. }
  724. public LImage getWebImage(String url, boolean transparency) {
  725. return GraphicsUtils.loadWebImage(url, transparency);
  726. }
  727. public LImage getImage(String fileName, boolean transparency) {
  728. return GraphicsUtils.loadImage(fileName, transparency);
  729. }
  730. public LImage[] getSplitImages(String fileName, int row, int col,
  731. boolean transparency) {
  732. return getSplitImages(getImage(fileName, transparency), row, col);
  733. }
  734. public LImage[] getSplitImages(LImage image, int row, int col) {
  735. return GraphicsUtils.getSplitImages(image, row, col);
  736. }
  737. public synchronized void createUI(LGraphics g) {
  738. draw(g);
  739. if (sprites != null) {
  740. sprites.createUI(g);
  741. }
  742. if (desktop != null) {
  743. desktop.createUI(g);
  744. }
  745. }
  746. public abstract void draw(LGraphics g);
  747. public void runTimer(final LTimerContext timer) {
  748. this.elapsedTime = timer.getTimeSinceLastUpdate();
  749. if (sprites != null && this.sprites.size() > 0) {
  750. this.sprites.update(elapsedTime);
  751. }
  752. if (desktop != null
  753. && this.desktop.getContentPane().getComponentCount() > 0) {
  754. this.desktop.update(elapsedTime);
  755. }
  756. this.pressedTouch = releasedTouch = pressedKey = releasedKey = 0;
  757. this.touchDX = touchX - lastTouchX;
  758. this.touchDY = touchY - lastTouchY;
  759. this.lastTouchX = touchX;
  760. this.lastTouchY = touchY;
  761. this.alter(timer);
  762. }
  763. public boolean next() {
  764. return isNext;
  765. }
  766. public void setNext(boolean next) {
  767. this.isNext = next;
  768. }
  769. public void alter(LTimerContext timer) {
  770. }
  771. /**
  772. * ??????
  773. */
  774. public void setScreen(IScreen screen) {
  775. if (handler != null) {
  776. this.handler.setScreen(screen);
  777. }
  778. }
  779. public int getWidth() {
  780. return width;
  781. }
  782. public int getHeight() {
  783. return height;
  784. }
  785. /**
  786. * ??????
  787. */
  788. public void refresh() {
  789. for (int i = 0; i < touchDown.length; i++) {
  790. touchDown[i] = false;
  791. }
  792. touchDX = touchDY = 0;
  793. for (int i = 0; i < keyDown.length; i++) {
  794. keyDown[i] = false;
  795. }
  796. }
  797. /**
  798. * ????????
  799. *
  800. * @param timeMillis
  801. */
  802. public void pause(long timeMillis) {
  803. try {
  804. TimeUnit.MILLISECONDS.sleep(timeMillis);
  805. } catch (InterruptedException e) {
  806. throw new RuntimeException("Interrupted in pause !");
  807. }
  808. }
  809. public Point getTouch() {
  810. touch.set(touchX, touchY);
  811. return touch;
  812. }
  813. public boolean isPaused() {
  814. return LSystem.isPaused;
  815. }
  816. public boolean isClick() {
  817. return touchDown[MotionEvent.ACTION_DOWN];
  818. }
  819. public boolean isTouchClick() {
  820. return baseInput.isTouchPressed(MotionEvent.ACTION_DOWN);
  821. }
  822. public boolean isTouchClickUp() {
  823. return baseInput.isTouchPressed(MotionEvent.ACTION_UP);
  824. }
  825. public int getTouchPressed() {
  826. return (pressedTouch > 0) ? touchPressed[pressedTouch - 1]
  827. : LInput.NO_BUTTON;
  828. }
  829. public int getTouchReleased() {
  830. return (releasedTouch > 0) ? touchReleased[releasedTouch - 1]
  831. : LInput.NO_BUTTON;
  832. }
  833. public boolean isTouchPressed(int button) {
  834. if (!isTouchReleased(button)) {
  835. return false;
  836. }
  837. try {
  838. for (int i = 0; i < this.pressedTouch; i++) {
  839. if (touchPressed[i] == button) {
  840. return true;
  841. }
  842. }
  843. return touchPressed[0] == button;
  844. } catch (Exception e) {
  845. return false;
  846. }
  847. }
  848. public boolean isTouchReleased(int button) {
  849. try {
  850. for (int i = 0; i < this.releasedTouch; i++) {
  851. if (touchReleased[i] == button) {
  852. return true;
  853. }
  854. }
  855. return touchReleased[0] == button;
  856. } catch (Exception e) {
  857. return false;
  858. }
  859. }
  860. public boolean getKeyDown(int keyCode) {
  861. return baseInput.isKeyDown(keyCode);
  862. }
  863. public boolean getKeyPressed(int keyCode) {
  864. return baseInput.isKeyPressed(keyCode);
  865. }
  866. public int getTouchX() {
  867. return touchX;
  868. }
  869. public int getTouchY() {
  870. return touchY;
  871. }
  872. public int getTouchDX() {
  873. return touchDX;
  874. }
  875. public int getTouchDY() {
  876. return touchDY;
  877. }
  878. public boolean[] getTouchDown() {
  879. return touchDown;
  880. }
  881. public boolean isTouchDown(int button) {
  882. return touchDown[button];
  883. }
  884. public int getKeyPressed() {
  885. return (pressedKey > 0) ? keyPressed[pressedKey - 1] : LInput.NO_KEY;
  886. }
  887. public boolean isKeyPressed(int keyCode) {
  888. for (int i = 0; i < pressedKey; i++) {
  889. if (keyPressed[i] == keyCode) {
  890. return true;
  891. }
  892. }
  893. return false;
  894. }
  895. public int getKeyReleased() {
  896. return (releasedKey > 0) ? keyReleased[releasedKey - 1] : LInput.NO_KEY;
  897. }
  898. public boolean isKeyReleased(int keyCode) {
  899. for (int i = 0; i < releasedKey; i++) {
  900. if (keyReleased[i] == keyCode) {
  901. return true;
  902. }
  903. }
  904. return false;
  905. }
  906. public boolean[] getKeyDown() {
  907. return keyDown;
  908. }
  909. public boolean isKeyDown(int keyCode) {
  910. return keyDown[keyCode];
  911. }
  912. public boolean onTrackballEvent(MotionEvent e) {
  913. return false;
  914. }
  915. public boolean onKeyDownEvent(int keyCode, KeyEvent e) {
  916. try {
  917. keyDown[keyCode] = true;
  918. keyPressed[pressedKey] = keyCode;
  919. pressedKey++;
  920. } catch (Exception ex) {
  921. pressedKey = 0;
  922. Log.d(getName(), "on Key Down !", ex);
  923. }
  924. return onKeyDown(keyCode, e);
  925. }
  926. public boolean onKeyDown(int keyCode, KeyEvent e) {
  927. return true;
  928. }
  929. public void setKeyDown(int code) {
  930. try {
  931. keyDown[code] = true;
  932. keyPressed[this.pressedKey] = code;
  933. pressedKey++;
  934. } catch (Exception e) {
  935. }
  936. }
  937. public boolean onKeyUpEvent(int keyCode, KeyEvent e) {
  938. try {
  939. keyDown[keyCode] = false;
  940. keyReleased[releasedKey] = keyCode;
  941. releasedKey++;
  942. } catch (Exception ex) {
  943. releasedKey = 0;
  944. Log.d(getName(), "on Key Up !", ex);
  945. }
  946. return onKeyUp(keyCode, e);
  947. }
  948. public boolean onKeyUp(int keyCode, KeyEvent e) {
  949. return true;
  950. }
  951. public void setKeyUp(int code) {
  952. try {
  953. keyDown[code] = false;
  954. keyReleased[this.releasedKey] = code;
  955. releasedKey++;
  956. } catch (Exception e) {
  957. }
  958. }
  959. public boolean onTouchEvent(MotionEvent e) {
  960. try {
  961. int code = e.getAction();
  962. int pointerCount = MultitouchUtils.getPointerCount(e);
  963. if (MultitouchUtils.isMultitouch() && pointerCount > 1) {
  964. int id = 0;
  965. for (int idx = 0; idx < pointerCount; idx++) {
  966. id = MultitouchUtils.getPointerId(e, idx);
  967. touchX = Math.round(MultitouchUtils.getX(e, id));
  968. touchY = Math.round(MultitouchUtils.getY(e, id));
  969. switch (code) {
  970. case MotionEvent.ACTION_DOWN:
  971. if (idx == 0) {
  972. offsetTouchX = touchX;
  973. offsetTouchY = touchY;
  974. isMoving = false;
  975. touchDown[code] = true;
  976. touchPressed[pressedTouch] = code;
  977. touchDX = lastTouchX = touchX;
  978. touchDY = lastTouchY = touchY;
  979. pressedTouch++;
  980. if ((touchX < halfWidth) && (touchY < halfHeight)) {
  981. this.touchDirection = LInput.UPPER_LEFT;
  982. } else if ((touchX >= halfWidth)
  983. && (touchY < halfHeight)) {
  984. this.touchDirection = LInput.UPPER_RIGHT;
  985. } else if ((touchX < halfWidth)
  986. && (touchY >= halfHeight)) {
  987. this.touchDirection = LInput.LOWER_LEFT;
  988. } else {
  989. this.touchDirection = LInput.LOWER_RIGHT;
  990. }
  991. onTouchDown(e);
  992. return true;
  993. }
  994. break;
  995. case MotionEvent.ACTION_UP:
  996. case MotionEvent.ACTION_CANCEL:
  997. if (idx == 0) {
  998. isMoving = false;
  999. touchDown[code] = false;
  1000. touchReleased[releasedTouch] = code;
  1001. touchDX = lastTouchX = touchX;
  1002. touchDY = lastTouchY = touchY;
  1003. releasedTouch++;
  1004. onTouchUp(e);
  1005. return true;
  1006. }
  1007. break;
  1008. case MotionEvent.ACTION_MOVE:
  1009. if (idx == 0) {
  1010. offsetMoveX = touchX;
  1011. offsetMoveY = touchY;
  1012. if (Math.abs(offsetTouchX - offsetMoveX) > 5
  1013. || Math.abs(offsetTouchY - offsetMoveY) > 5) {
  1014. isMoving = true;
  1015. onTouchMove(e);
  1016. return true;
  1017. }
  1018. }
  1019. break;
  1020. case MultitouchUtils.ACTION_POINTER_1_DOWN:
  1021. if (idx == 0) {
  1022. onTouchDown(e);
  1023. return true;
  1024. }
  1025. break;
  1026. case MultitouchUtils.ACTION_POINTER_1_UP:
  1027. if (idx == 0) {
  1028. onTouchUp(e);
  1029. return true;
  1030. }
  1031. break;
  1032. case MultitouchUtils.ACTION_POINTER_2_DOWN:
  1033. if (idx == 1) {
  1034. onTouchDown(e);
  1035. return true;
  1036. }
  1037. break;
  1038. case MultitouchUtils.ACTION_POINTER_2_UP:
  1039. if (idx == 1) {
  1040. onTouchUp(e);
  1041. return true;
  1042. }
  1043. break;
  1044. case MultitouchUtils.ACTION_POINTER_3_DOWN:
  1045. if (idx == 2) {
  1046. onTouchDown(e);
  1047. return true;
  1048. }
  1049. break;
  1050. case MultitouchUtils.ACTION_POINTER_3_UP:
  1051. if (idx == 2) {
  1052. onTouchUp(e);
  1053. return true;
  1054. }
  1055. break;
  1056. }
  1057. }
  1058. } else {
  1059. touchX = Math.round(e.getX());
  1060. touchY = Math.round(e.getY());
  1061. switch (code) {
  1062. case MotionEvent.ACTION_DOWN:
  1063. offsetTouchX = touchX;
  1064. offsetTouchY = touchY;
  1065. isMoving = false;
  1066. touchDown[code] = true;
  1067. touchPressed[pressedTouch] = code;
  1068. touchDX = lastTouchX = touchX;
  1069. touchDY = lastTouchY = touchY;
  1070. pressedTouch++;
  1071. if ((touchX < halfWidth) && (touchY < halfHeight)) {
  1072. this.touchDirection = LInput.UPPER_LEFT;
  1073. } else if ((touchX >= halfWidth) && (touchY < halfHeight)) {
  1074. this.touchDirection = LInput.UPPER_RIGHT;
  1075. } else if ((touchX < halfWidth) && (touchY >= halfHeight)) {
  1076. this.touchDirection = LInput.LOWER_LEFT;
  1077. } else {
  1078. this.touchDirection = LInput.LOWER_RIGHT;
  1079. }
  1080. onTouchDown(e);
  1081. return true;
  1082. case MotionEvent.ACTION_UP:
  1083. case MotionEvent.ACTION_CANCEL:
  1084. isMoving = false;
  1085. touchDown[code] = false;
  1086. touchReleased[releasedTouch] = code;
  1087. touchDX = lastTouchX = touchX;
  1088. touchDY = lastTouchY = touchY;
  1089. releasedTouch++;
  1090. onTouchUp(e);
  1091. return true;
  1092. case MotionEvent.ACTION_MOVE:
  1093. offsetMoveX = touchX;
  1094. offsetMoveY = touchY;
  1095. if (Math.abs(offsetTouchX - offsetMoveX) > 5
  1096. || Math.abs(offsetTouchY - offsetMoveY) > 5) {
  1097. isMoving = true;
  1098. onTouchMove(e);
  1099. return true;
  1100. }
  1101. }
  1102. }
  1103. } catch (Exception ex) {
  1104. pressedTouch = 0;
  1105. releasedTouch = 0;
  1106. Log.d(getName(), "on Touch !", ex);
  1107. }
  1108. return false;
  1109. }
  1110. public boolean onCreateOptionsMenu(Menu menu) {
  1111. return false;
  1112. }
  1113. public boolean onOptionsItemSelected(MenuItem item) {
  1114. return false;
  1115. }
  1116. public void onOptionsMenuClosed(Menu menu) {
  1117. }
  1118. public void onResume() {
  1119. }
  1120. public void onPause() {
  1121. }
  1122. private void gravityToKey(SensorDirection d) {
  1123. setKeyUp(KeyEvent.KEYCODE_DPAD_LEFT);
  1124. setKeyUp(KeyEvent.KEYCODE_DPAD_RIGHT);
  1125. setKeyUp(KeyEvent.KEYCODE_DPAD_DOWN);
  1126. setKeyUp(KeyEvent.KEYCODE_DPAD_UP);
  1127. if (d == SensorDirection.LEFT) {
  1128. setKeyDown(KeyEvent.KEYCODE_DPAD_LEFT);
  1129. } else if (d == SensorDirection.RIGHT) {
  1130. setKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT);
  1131. } else if (d == SensorDirection.UP) {
  1132. setKeyDown(KeyEvent.KEYCODE_DPAD_UP);
  1133. } else if (d == SensorDirection.DOWN) {
  1134. setKeyDown(KeyEvent.KEYCODE_DPAD_DOWN);
  1135. }
  1136. }
  1137. public void onSensorChanged(SensorEvent e) {
  1138. try {
  1139. accelX = e.values[SensorManager.DATA_X];
  1140. accelY = e.values[SensorManager.DATA_Y];
  1141. accelZ = e.values[SensorManager.DATA_Z];
  1142. double speed = Math.sqrt(accelX * accelX + accelY * accelY + accelZ
  1143. * accelZ);
  1144. sensorInclination = -Math.asin(accelZ / speed);
  1145. } catch (Exception ex) {
  1146. return;
  1147. }
  1148. long curTime = System.currentTimeMillis();
  1149. if (LSystem.SCREEN_LANDSCAPE) {
  1150. if ((curTime - lastUpdate) > 100) {
  1151. lastUpdate = curTime;
  1152. double move = Math.atan(accelY / accelZ);
  1153. if (move <= -LANDSCAPE_OFFSET) {
  1154. if (landscapeUpdate > -LANDSCAPE_OFFSET) {
  1155. direction = SensorDirection.LEFT;
  1156. }
  1157. } else if (move >= LANDSCAPE_OFFSET) {
  1158. if (landscapeUpdate < LANDSCAPE_OFFSET) {
  1159. direction = SensorDirection.RIGHT;
  1160. }
  1161. } else {
  1162. if (landscapeUpdate <= -LANDSCAPE_OFFSET) {
  1163. direction = SensorDirection.LEFT;
  1164. } else if (landscapeUpdate >= LANDSCAPE_OFFSET) {
  1165. direction = SensorDirection.RIGHT;
  1166. }
  1167. }
  1168. landscapeUpdate = move;
  1169. }
  1170. } else {
  1171. if ((curTime - lastUpdate) > 150) {
  1172. lastUpdate = curTime;
  1173. if (accelX > 0 && accelY <= 0) {
  1174. direction = SensorDirection.RIGHT;
  1175. } else if (Math.abs(accelX) > Math.abs(accelY)) {
  1176. if (accelX < -ACCEL_OFFEST) {
  1177. direction = SensorDirection.LEFT;
  1178. }
  1179. if (accelX > ACCEL_OFFEST) {
  1180. direction = SensorDirection.RIGHT;
  1181. }
  1182. } else {
  1183. if (accelY < -ACCEL_OFFEST) {
  1184. direction = SensorDirection.DOWN;
  1185. }
  1186. if (accelY > ACCEL_OFFEST) {
  1187. direction = SensorDirection.UP;
  1188. }
  1189. }
  1190. }
  1191. }
  1192. if (isGravityToKey) {
  1193. gravityToKey(direction);
  1194. }
  1195. onDirection(direction, accelX, accelY, accelZ);
  1196. }
  1197. public void onDirection(SensorDirection direction, float x, float y, float z) {
  1198. }
  1199. public void onAccuracyChanged(Sensor sensor, int accuracy) {
  1200. }
  1201. public void onTouch(float x, float y, MotionEvent e, int pointerCount,
  1202. int pointerId) {
  1203. }
  1204. public void move(double x, double y) {
  1205. this.touchX = (int) x;
  1206. this.touchY = (int) y;
  1207. }
  1208. public boolean isMoving() {
  1209. return isMoving;
  1210. }
  1211. public int getHalfWidth() {
  1212. return halfWidth;
  1213. }
  1214. public int getHalfHeight() {
  1215. return halfHeight;
  1216. }
  1217. public int getTouchDirection() {
  1218. return touchDirection;
  1219. }
  1220. public SensorDirection getSensorDirection() {
  1221. return direction;
  1222. }
  1223. public double getSensorInclination() {
  1224. return sensorInclination;
  1225. }
  1226. public float getAccelX() {
  1227. return accelX;
  1228. }
  1229. public float getAccelY() {
  1230. return accelY;
  1231. }
  1232. public float getAccelZ() {
  1233. return accelZ;
  1234. }
  1235. public boolean isGravityToKey() {
  1236. return isGravityToKey;
  1237. }
  1238. public void setGravityToKey(boolean isGravityToKey) {
  1239. this.isGravityToKey = isGravityToKey;
  1240. }
  1241. }