/android/LGame-Android-0.3.1/src/org/loon/framework/android/game/core/graphics/Desktop.java

http://loon-simple.googlecode.com/ · Java · 503 lines · 353 code · 71 blank · 79 comment · 126 complexity · 7e63474e3d2d3ac99322d1bf84a13426 MD5 · raw file

  1. package org.loon.framework.android.game.core.graphics;
  2. import java.util.ArrayList;
  3. import org.loon.framework.android.game.core.graphics.component.LPanel;
  4. import org.loon.framework.android.game.core.graphics.component.Layer;
  5. import org.loon.framework.android.game.core.graphics.opengl.GLEx;
  6. import org.loon.framework.android.game.core.input.LInput;
  7. import org.loon.framework.android.game.core.input.LInputFactory.Touch;
  8. /**
  9. *
  10. * Copyright 2008 - 2009
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  13. * use this file except in compliance with the License. You may obtain a copy of
  14. * the License at
  15. *
  16. * http://www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  21. * License for the specific language governing permissions and limitations under
  22. * the License.
  23. *
  24. * @project loonframework
  25. * @author chenpeng
  26. * @email?ceponline@yahoo.com.cn
  27. * @version 0.1
  28. */
  29. public class Desktop {
  30. // ?????
  31. public static final Desktop EMPTY_DESKTOP = new Desktop();
  32. // ??????
  33. protected final LInput input;
  34. private LContainer contentPane;
  35. private LComponent modal;
  36. private LComponent hoverComponent;
  37. private LComponent selectedComponent;
  38. private LComponent[] clickComponent = new LComponent[1];
  39. /**
  40. * ????????
  41. *
  42. * @param input
  43. * @param width
  44. * @param height
  45. */
  46. public Desktop(LInput input, int width, int height) {
  47. this.contentPane = new LPanel(0, 0, width, height);
  48. this.input = input;
  49. this.setDesktop(this.contentPane);
  50. }
  51. /**
  52. * ?????
  53. *
  54. */
  55. private Desktop() {
  56. this.contentPane = new LPanel(0, 0, 1, 1);
  57. this.input = null;
  58. this.setDesktop(this.contentPane);
  59. }
  60. public int size() {
  61. if (contentPane == null) {
  62. return 0;
  63. }
  64. return contentPane.getComponentCount();
  65. }
  66. public void add(LComponent comp) {
  67. if (comp == null) {
  68. return;
  69. }
  70. if (comp.isFull) {
  71. this.input.setRepaintMode(Screen.SCREEN_NOT_REPAINT);
  72. }
  73. this.contentPane.add(comp);
  74. this.processTouchMotionEvent();
  75. }
  76. public int remove(LComponent comp) {
  77. int removed = this.removeComponent(this.contentPane, comp);
  78. if (removed != -1) {
  79. this.processTouchMotionEvent();
  80. }
  81. return removed;
  82. }
  83. public int remove(Class<? extends LComponent> clazz) {
  84. int removed = this.removeComponent(this.contentPane, clazz);
  85. if (removed != -1) {
  86. this.processTouchMotionEvent();
  87. }
  88. return removed;
  89. }
  90. private int removeComponent(LContainer container, LComponent comp) {
  91. int removed = container.remove(comp);
  92. LComponent[] components = container.getComponents();
  93. int i = 0;
  94. while (removed == -1 && i < components.length - 1) {
  95. if (components[i].isContainer()) {
  96. removed = this
  97. .removeComponent((LContainer) components[i], comp);
  98. }
  99. i++;
  100. }
  101. return removed;
  102. }
  103. private int removeComponent(LContainer container,
  104. Class<? extends LComponent> clazz) {
  105. int removed = container.remove(clazz);
  106. LComponent[] components = container.getComponents();
  107. int i = 0;
  108. while (removed == -1 && i < components.length - 1) {
  109. if (components[i].isContainer()) {
  110. removed = this.removeComponent((LContainer) components[i],
  111. clazz);
  112. }
  113. i++;
  114. }
  115. return removed;
  116. }
  117. /**
  118. * ??????
  119. *
  120. */
  121. public void update(long timer) {
  122. if (!this.contentPane.isVisible()) {
  123. return;
  124. }
  125. this.processEvents();
  126. // ??????????
  127. this.contentPane.update(timer);
  128. }
  129. public void createUI(GLEx g) {
  130. this.contentPane.createUI(g);
  131. }
  132. /**
  133. * ????
  134. *
  135. */
  136. private void processEvents() {
  137. // ????
  138. this.processTouchMotionEvent();
  139. // ????
  140. if (this.hoverComponent != null && this.hoverComponent.isEnabled()) {
  141. this.processTouchEvent();
  142. }
  143. // ????
  144. if (this.selectedComponent != null
  145. && this.selectedComponent.isEnabled()) {
  146. this.processKeyEvent();
  147. }
  148. }
  149. /**
  150. * ??????
  151. *
  152. */
  153. private void processTouchMotionEvent() {
  154. if (this.hoverComponent != null && this.hoverComponent.isEnabled()
  155. && this.input.isMoving()) {
  156. if (this.input.getTouchDY() != 0 || this.input.getTouchDY() != 0) {
  157. this.hoverComponent.processTouchDragged();
  158. }
  159. } else {
  160. // ???????????
  161. LComponent comp = this.findComponent(this.input.getTouchX(),
  162. this.input.getTouchY());
  163. if (comp != null) {
  164. if (this.input.getTouchDX() != 0
  165. || this.input.getTouchDY() != 0) {
  166. comp.processTouchMoved();
  167. }
  168. if (this.hoverComponent == null) {
  169. comp.processTouchEntered();
  170. } else if (comp != this.hoverComponent) {
  171. this.hoverComponent.processTouchExited();
  172. comp.processTouchEntered();
  173. }
  174. } else {
  175. if (this.hoverComponent != null) {
  176. this.hoverComponent.processTouchExited();
  177. }
  178. }
  179. this.hoverComponent = comp;
  180. }
  181. }
  182. /**
  183. * ??????
  184. *
  185. */
  186. private void processTouchEvent() {
  187. int pressed = this.input.getTouchPressed(), released = this.input
  188. .getTouchReleased();
  189. if (pressed > LInput.NO_BUTTON) {
  190. this.hoverComponent.processTouchPressed();
  191. this.clickComponent[0] = this.hoverComponent;
  192. if (this.hoverComponent.isFocusable()) {
  193. if ((pressed == Touch.TOUCH_DOWN || pressed == Touch.TOUCH_UP)
  194. && this.hoverComponent != this.selectedComponent) {
  195. this.selectComponent(this.hoverComponent);
  196. }
  197. }
  198. }
  199. if (released > LInput.NO_BUTTON) {
  200. this.hoverComponent.processTouchReleased();
  201. // ?????????????
  202. if (this.clickComponent[0] == this.hoverComponent) {
  203. this.hoverComponent.processTouchClicked();
  204. }
  205. }
  206. }
  207. /**
  208. * ??????
  209. *
  210. */
  211. private void processKeyEvent() {
  212. if (this.input.getKeyPressed() != LInput.NO_KEY) {
  213. this.selectedComponent.keyPressed();
  214. }
  215. if (this.input.getKeyReleased() != LInput.NO_KEY
  216. && this.selectedComponent != null) {
  217. this.selectedComponent.processKeyReleased();
  218. }
  219. }
  220. /**
  221. * ?????????
  222. *
  223. * @param x
  224. * @param y
  225. * @return
  226. */
  227. private LComponent findComponent(int x, int y) {
  228. if (this.modal != null && !this.modal.isContainer()) {
  229. return null;
  230. }
  231. // ?????
  232. LContainer panel = (this.modal == null) ? this.contentPane
  233. : ((LContainer) this.modal);
  234. LComponent comp = panel.findComponent(x, y);
  235. return comp;
  236. }
  237. /**
  238. * ??????
  239. */
  240. public void clearFocus() {
  241. this.deselectComponent();
  242. }
  243. void deselectComponent() {
  244. if (this.selectedComponent == null) {
  245. return;
  246. }
  247. this.selectedComponent.setSelected(false);
  248. this.selectedComponent = null;
  249. }
  250. /**
  251. * ??????
  252. *
  253. * @param comp
  254. * @return
  255. */
  256. boolean selectComponent(LComponent comp) {
  257. if (!comp.isVisible() || !comp.isEnabled() || !comp.isFocusable()) {
  258. return false;
  259. }
  260. // ??????
  261. this.deselectComponent();
  262. // ??????
  263. comp.setSelected(true);
  264. this.selectedComponent = comp;
  265. return true;
  266. }
  267. void setDesktop(LComponent comp) {
  268. if (comp.isContainer()) {
  269. LComponent[] child = ((LContainer) comp).getComponents();
  270. for (int i = 0; i < child.length; i++) {
  271. this.setDesktop(child[i]);
  272. }
  273. }
  274. comp.setDesktop(this);
  275. }
  276. void setComponentStat(LComponent comp, boolean active) {
  277. if (this == Desktop.EMPTY_DESKTOP) {
  278. return;
  279. }
  280. if (active == false) {
  281. if (this.hoverComponent == comp) {
  282. this.processTouchMotionEvent();
  283. }
  284. if (this.selectedComponent == comp) {
  285. this.deselectComponent();
  286. }
  287. this.clickComponent[0] = null;
  288. if (this.modal == comp) {
  289. this.modal = null;
  290. }
  291. } else {
  292. this.processTouchMotionEvent();
  293. }
  294. if (comp.isContainer()) {
  295. LComponent[] components = ((LContainer) comp).getComponents();
  296. int size = ((LContainer) comp).getComponentCount();
  297. for (int i = 0; i < size; i++) {
  298. this.setComponentStat(components[i], active);
  299. }
  300. }
  301. }
  302. void clearComponentsStat(LComponent[] comp) {
  303. if (this == Desktop.EMPTY_DESKTOP) {
  304. return;
  305. }
  306. boolean checkTouchMotion = false;
  307. for (int i = 0; i < comp.length; i++) {
  308. if (this.hoverComponent == comp[i]) {
  309. checkTouchMotion = true;
  310. }
  311. if (this.selectedComponent == comp[i]) {
  312. this.deselectComponent();
  313. }
  314. this.clickComponent[0] = null;
  315. }
  316. if (checkTouchMotion) {
  317. this.processTouchMotionEvent();
  318. }
  319. }
  320. public void validateUI() {
  321. this.validateContainer(this.contentPane);
  322. }
  323. final void validateContainer(LContainer container) {
  324. LComponent[] components = container.getComponents();
  325. int size = container.getComponentCount();
  326. for (int i = 0; i < size; i++) {
  327. if (components[i].isContainer()) {
  328. this.validateContainer((LContainer) components[i]);
  329. }
  330. }
  331. }
  332. public ArrayList<LComponent> getComponents(Class<? extends LComponent> clazz) {
  333. if (clazz == null) {
  334. return null;
  335. }
  336. LComponent[] components = contentPane.getComponents();
  337. int size = components.length;
  338. ArrayList<LComponent> l = new ArrayList<LComponent>(size);
  339. for (int i = size; i > 0; i--) {
  340. LComponent comp = (LComponent) components[i - 1];
  341. Class<? extends LComponent> cls = comp.getClass();
  342. if (clazz == null || clazz == cls || clazz.isInstance(comp)
  343. || clazz.equals(cls)) {
  344. l.add(comp);
  345. }
  346. }
  347. return l;
  348. }
  349. public LComponent getTopComponent() {
  350. LComponent[] components = contentPane.getComponents();
  351. int size = components.length;
  352. if (size > 1) {
  353. return components[1];
  354. }
  355. return null;
  356. }
  357. public LComponent getBottomComponent() {
  358. LComponent[] components = contentPane.getComponents();
  359. int size = components.length;
  360. if (size > 0) {
  361. return components[size - 1];
  362. }
  363. return null;
  364. }
  365. public Layer getTopLayer() {
  366. LComponent[] components = contentPane.getComponents();
  367. int size = components.length;
  368. Class<Layer> clazz = Layer.class;
  369. for (int i = 0; i < size; i++) {
  370. LComponent comp = (LComponent) components[i];
  371. Class<? extends LComponent> cls = comp.getClass();
  372. if (clazz == null || clazz == cls || clazz.isInstance(comp)
  373. || clazz.equals(cls)) {
  374. return (Layer) comp;
  375. }
  376. }
  377. return null;
  378. }
  379. public Layer getBottomLayer() {
  380. LComponent[] components = contentPane.getComponents();
  381. int size = components.length;
  382. Class<Layer> clazz = Layer.class;
  383. for (int i = size; i > 0; i--) {
  384. LComponent comp = (LComponent) components[i - 1];
  385. Class<? extends LComponent> cls = comp.getClass();
  386. if (clazz == null || clazz == cls || clazz.isInstance(comp)
  387. || clazz.equals(cls)) {
  388. return (Layer) comp;
  389. }
  390. }
  391. return null;
  392. }
  393. public int getWidth() {
  394. return this.contentPane.getWidth();
  395. }
  396. public int getHeight() {
  397. return this.contentPane.getHeight();
  398. }
  399. public void setSize(int w, int h) {
  400. this.contentPane.setSize(w, h);
  401. }
  402. public LContainer getContentPane() {
  403. return this.contentPane;
  404. }
  405. public void setContentPane(LContainer pane) {
  406. pane.setBounds(0, 0, this.getWidth(), this.getHeight());
  407. this.contentPane = pane;
  408. this.setDesktop(this.contentPane);
  409. }
  410. public LComponent getHoverComponent() {
  411. return this.hoverComponent;
  412. }
  413. public LComponent getSelectedComponent() {
  414. return this.selectedComponent;
  415. }
  416. public LComponent getModal() {
  417. return this.modal;
  418. }
  419. public void setModal(LComponent comp) {
  420. if (comp != null && !comp.isVisible()) {
  421. throw new RuntimeException(
  422. "Can't set invisible component as modal component!");
  423. }
  424. this.modal = comp;
  425. }
  426. public LComponent get() {
  427. return this.contentPane.get();
  428. }
  429. protected void finalize() throws Throwable {
  430. super.finalize();
  431. }
  432. }