PageRenderTime 24ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://loon-simple.googlecode.com/
Java | 644 lines | 458 code | 115 blank | 71 comment | 124 complexity | 267e2e6c152fa8641c9c590da3a582bc MD5 | raw file
  1. package org.loon.framework.android.game.core.graphics;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.loon.framework.android.game.action.map.shapes.RectBox;
  5. import org.loon.framework.android.game.core.LInput;
  6. import org.loon.framework.android.game.core.LObject;
  7. import org.loon.framework.android.game.core.graphics.device.LGraphics;
  8. import org.loon.framework.android.game.core.graphics.window.UIFactory;
  9. import org.loon.framework.android.game.utils.GraphicsUtils;
  10. import android.graphics.Rect;
  11. import android.graphics.Bitmap.Config;
  12. import android.view.KeyEvent;
  13. /**
  14. *
  15. * Copyright 2008 - 2011
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  18. * use this file except in compliance with the License. You may obtain a copy of
  19. * the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  26. * License for the specific language governing permissions and limitations under
  27. * the License.
  28. *
  29. * @project loonframework
  30. * @author chenpeng
  31. * @email?ceponline@yahoo.com.cn
  32. * @version 0.1.1
  33. */
  34. public abstract class LComponent extends LObject {
  35. // ??
  36. private LContainer parent;
  37. // ???
  38. protected UIFactory factory;
  39. protected boolean elastic;
  40. protected LImage[] ui;
  41. private LImage[] imageUI;
  42. protected boolean isFull;
  43. private boolean processUI;
  44. // ????
  45. public boolean customRendering;
  46. // ???
  47. protected float alpha = 1.0f;
  48. // ???????
  49. private int width, height;
  50. // ????
  51. protected int screenX, screenY;
  52. // ????
  53. protected String tooltip;
  54. protected Rect oldClip;
  55. // ????
  56. protected boolean visible = true;
  57. protected boolean enabled = true;
  58. // ?????
  59. protected boolean focusable = true;
  60. // ?????
  61. protected boolean selected = false;
  62. // ????
  63. private final HashMap<Object, Object> UI_RES = new HashMap<Object, Object>();
  64. protected Desktop desktop = Desktop.EMPTY_DESKTOP;
  65. protected LInput input;
  66. protected LImage background;
  67. /**
  68. * ??????
  69. *
  70. * @param x
  71. * @param y
  72. * @param width
  73. * @param height
  74. */
  75. public LComponent(int x, int y, int width, int height) {
  76. this.setLocation(x, y);
  77. this.width = width;
  78. this.height = height;
  79. if (this.width == 0) {
  80. this.width = 10;
  81. }
  82. if (this.height == 0) {
  83. this.height = 10;
  84. }
  85. }
  86. /**
  87. * ?????????????
  88. *
  89. * @return
  90. */
  91. public boolean isContainer() {
  92. return false;
  93. }
  94. /**
  95. * ??????
  96. *
  97. */
  98. public void update(long timer) {
  99. }
  100. /**
  101. * ????????????????
  102. *
  103. * @param g
  104. */
  105. public void createUI(LGraphics g) {
  106. if (!this.visible) {
  107. return;
  108. }
  109. try {
  110. if (this.elastic) {
  111. this.oldClip = g.getClip();
  112. g.clipRect(this.getScreenX(), this.getScreenY(), this
  113. .getWidth(), this.getHeight());
  114. }
  115. // ?????
  116. if (alpha > 0.1 && alpha < 1.0) {
  117. g.setAlpha(alpha);
  118. if (background != null) {
  119. g.drawImage(background, this.screenX, this.screenY,
  120. this.width, this.height);
  121. }
  122. if (this.customRendering) {
  123. this.createCustomUI(g, this.screenX, this.screenY,
  124. this.width, this.height);
  125. } else {
  126. this.factory.createUI(g, this.screenX, this.screenY, this,
  127. this.ui);
  128. }
  129. g.setAlpha(1.0F);
  130. // ???
  131. } else {
  132. if (background != null) {
  133. g.drawImage(background, this.screenX, this.screenY,
  134. this.width, this.height);
  135. }
  136. if (this.customRendering) {
  137. this.createCustomUI(g, this.screenX, this.screenY,
  138. this.width, this.height);
  139. } else {
  140. this.factory.createUI(g, this.screenX, this.screenY, this,
  141. this.ui);
  142. }
  143. }
  144. if (this.elastic) {
  145. g.setClip(this.oldClip);
  146. }
  147. } catch (Exception ex) {
  148. }
  149. }
  150. /**
  151. * ???UI
  152. *
  153. * @param g
  154. * @param x
  155. * @param y
  156. * @param w
  157. * @param h
  158. */
  159. protected void createCustomUI(LGraphics g, int x, int y, int w, int h) {
  160. }
  161. public boolean intersects(int x1, int y1) {
  162. return (this.visible)
  163. && (x1 >= this.screenX && x1 <= this.screenX + this.width
  164. && y1 >= this.screenY && y1 <= this.screenY
  165. + this.height);
  166. }
  167. public boolean contains(int x, int y) {
  168. return (x >= this.screenX && y >= this.screenX
  169. && x < this.screenX + this.width && y < this.screenX
  170. + this.height);
  171. }
  172. public boolean intersects(LComponent comp) {
  173. return (this.visible)
  174. && (comp.isVisible())
  175. && (this.screenX + this.width >= comp.screenX
  176. && this.screenX <= comp.screenX + comp.width
  177. && this.screenY + this.height >= comp.screenY && this.screenY <= comp.screenY
  178. + comp.height);
  179. }
  180. public void dispose() {
  181. this.desktop.setComponentStat(this, false);
  182. if (this.parent != null) {
  183. this.parent.remove(this);
  184. }
  185. this.desktop = Desktop.EMPTY_DESKTOP;
  186. this.input = null;
  187. this.parent = null;
  188. if (ui != null) {
  189. for (int i = 0; i < ui.length; i++) {
  190. ui[i].dispose();
  191. }
  192. this.ui = null;
  193. }
  194. if (imageUI != null) {
  195. for (int i = 0; i < imageUI.length; i++) {
  196. imageUI[i].dispose();
  197. }
  198. this.imageUI = null;
  199. }
  200. if (background != null) {
  201. this.background.dispose();
  202. this.background = null;
  203. }
  204. this.factory = null;
  205. this.selected = false;
  206. this.visible = false;
  207. }
  208. public boolean isVisible() {
  209. return this.visible;
  210. }
  211. public void setVisible(boolean visible) {
  212. if (this.visible == visible) {
  213. return;
  214. }
  215. this.visible = visible;
  216. this.desktop.setComponentStat(this, this.visible);
  217. }
  218. public boolean isEnabled() {
  219. return (this.parent == null) ? this.enabled
  220. : (this.enabled && this.parent.isEnabled());
  221. }
  222. public void setEnabled(boolean b) {
  223. if (this.enabled == b) {
  224. return;
  225. }
  226. this.enabled = b;
  227. this.desktop.setComponentStat(this, this.enabled);
  228. }
  229. public boolean isSelected() {
  230. return this.selected;
  231. }
  232. final void setSelected(boolean b) {
  233. this.selected = b;
  234. }
  235. public boolean requestFocus() {
  236. return this.desktop.selectComponent(this);
  237. }
  238. public void transferFocus() {
  239. if (this.isSelected() && this.parent != null) {
  240. this.parent.transferFocus(this);
  241. }
  242. }
  243. public void transferFocusBackward() {
  244. if (this.isSelected() && this.parent != null) {
  245. this.parent.transferFocusBackward(this);
  246. }
  247. }
  248. public boolean isFocusable() {
  249. return this.focusable;
  250. }
  251. public void setFocusable(boolean b) {
  252. this.focusable = b;
  253. }
  254. public LContainer getContainer() {
  255. return this.parent;
  256. }
  257. final void setContainer(LContainer container) {
  258. this.parent = container;
  259. this.validatePosition();
  260. }
  261. final void setDesktop(Desktop desktop) {
  262. if (this.desktop == desktop) {
  263. return;
  264. }
  265. this.desktop = desktop;
  266. this.input = desktop.input;
  267. if (this.factory == null) {
  268. this.factory = desktop.getUIConfig().createUIFactory(
  269. this.getUIName());
  270. }
  271. this.createUI();
  272. }
  273. public void setBounds(double dx, double dy, int width, int height) {
  274. if (this.getX() != dx || this.getY() != dy) {
  275. super.setLocation(dx, dy);
  276. this.validatePosition();
  277. }
  278. if (this.width != width || this.height != height) {
  279. this.width = width;
  280. this.height = height;
  281. if (width == 0) {
  282. width = 1;
  283. }
  284. if (height == 0) {
  285. height = 1;
  286. }
  287. this.createUI();
  288. this.validateSize();
  289. }
  290. }
  291. public void setLocation(double dx, double dy) {
  292. if (this.getX() != dx || this.getY() != dy) {
  293. super.setLocation(dx, dy);
  294. this.validatePosition();
  295. }
  296. }
  297. public void move(double dx, double dy) {
  298. if (dx != 0 || dy != 0) {
  299. super.move(dx, dy);
  300. this.validatePosition();
  301. }
  302. }
  303. public void setSize(int w, int h) {
  304. if (this.width != w || this.height != h) {
  305. this.width = w;
  306. this.height = h;
  307. if (this.width == 0) {
  308. this.width = 1;
  309. }
  310. if (this.height == 0) {
  311. this.height = 1;
  312. }
  313. this.createUI();
  314. this.validateSize();
  315. }
  316. }
  317. protected void validateSize() {
  318. }
  319. public void validatePosition() {
  320. this.screenX = (int) ((this.parent == null) ? this.getX() : this.getX()
  321. + this.parent.getScreenX());
  322. this.screenY = (int) ((this.parent == null) ? this.getY() : this.getY()
  323. + this.parent.getScreenY());
  324. }
  325. public int getScreenX() {
  326. return this.screenX;
  327. }
  328. public int getScreenY() {
  329. return this.screenY;
  330. }
  331. protected void setHeight(int height) {
  332. this.height = height;
  333. }
  334. protected void setWidth(int width) {
  335. this.width = width;
  336. }
  337. public int getWidth() {
  338. return this.width;
  339. }
  340. public int getHeight() {
  341. return this.height;
  342. }
  343. public RectBox getCollisionBox() {
  344. return new RectBox(screenX, screenY, width, height);
  345. }
  346. public String getToolTipText() {
  347. return this.tooltip;
  348. }
  349. public void setToolTipText(String text) {
  350. this.tooltip = text;
  351. }
  352. // ????
  353. protected void processTouchPressed() {
  354. }
  355. protected void processTouchReleased() {
  356. }
  357. protected void processTouchClicked() {
  358. }
  359. protected void processTouchMoved() {
  360. }
  361. protected void processTouchDragged() {
  362. }
  363. protected void processTouchEntered() {
  364. }
  365. protected void processTouchExited() {
  366. }
  367. // ????
  368. protected void processKeyPressed() {
  369. }
  370. protected void processKeyReleased() {
  371. }
  372. void keyPressed() {
  373. this.checkFocusKey();
  374. this.processKeyPressed();
  375. }
  376. /**
  377. * ????????
  378. *
  379. */
  380. protected void checkFocusKey() {
  381. if (this.input.getKeyPressed() == KeyEvent.KEYCODE_ENTER) {
  382. if (this.input.isKeyDown(KeyEvent.KEYCODE_SHIFT_LEFT) == false
  383. && this.input.isKeyDown(KeyEvent.KEYCODE_SHIFT_RIGHT) == false) {
  384. this.transferFocus();
  385. } else {
  386. this.transferFocusBackward();
  387. }
  388. }
  389. }
  390. public float getAlpha() {
  391. return alpha;
  392. }
  393. public void setAlpha(float alpha) {
  394. this.alpha = alpha;
  395. }
  396. public LImage[] getImageUI() {
  397. return this.imageUI;
  398. }
  399. public void setImageUI(LImage[] imageUI, boolean processUI) {
  400. if (imageUI != null) {
  401. this.width = imageUI[0].getWidth();
  402. this.height = imageUI[0].getHeight();
  403. }
  404. this.imageUI = imageUI;
  405. this.processUI = processUI;
  406. this.createUI();
  407. }
  408. public void setImageUI(int index, LImage imageUI) {
  409. if (imageUI != null) {
  410. this.width = imageUI.getWidth();
  411. this.height = imageUI.getHeight();
  412. }
  413. this.imageUI[index] = imageUI;
  414. this.processUI = true;
  415. this.createUI();
  416. }
  417. public Map<Object, Object> getUIResource() {
  418. return this.UI_RES;
  419. }
  420. public UIFactory getUIRenderer() {
  421. return this.factory;
  422. }
  423. public void setUIRenderer(UIFactory factory) {
  424. this.factory = factory;
  425. this.createUI();
  426. }
  427. public abstract String getUIName();
  428. protected void createUI() {
  429. if (this.desktop == Desktop.EMPTY_DESKTOP) {
  430. return;
  431. }
  432. if (this.customRendering) {
  433. this.createCustomUI(this.width, this.height);
  434. this.processCustomUI();
  435. } else if (this.imageUI != null
  436. && this.factory.getUIDescription().length == this.imageUI.length) {
  437. if (this.width != this.imageUI[0].getWidth()
  438. || this.height != this.imageUI[0].getHeight()) {
  439. throw new RuntimeException(
  440. this
  441. + ("Invalid Operation! Can not change component size when using external UI size ("
  442. + this.width + "," + this.height + ") ")
  443. .intern());
  444. }
  445. if (!this.processUI) {
  446. this.ui = this.imageUI;
  447. } else {
  448. this.ui = new LImage[this.imageUI.length];
  449. for (int i = 0; i < this.ui.length; i++) {
  450. this.ui[i] = LImage.createImage(this.imageUI[i].getWidth(),
  451. this.imageUI[i].getHeight(), Config.ARGB_8888);
  452. LGraphics g = this.ui[i].getLGraphics();
  453. g.drawImage(this.imageUI[i], 0, 0);
  454. g.dispose();
  455. }
  456. this.processExternalUI();
  457. }
  458. } else {
  459. this.createRenderedUI(this.width, this.height);
  460. this.processRenderedUI();
  461. }
  462. }
  463. public LImage getBackground() {
  464. return background;
  465. }
  466. public void clearBackground() {
  467. this.setBackground(LImage.createImage(1, 1, Config.ARGB_4444));
  468. }
  469. public void setBackground(String fileName) {
  470. this.setBackground(GraphicsUtils.loadImage(fileName, false));
  471. }
  472. public void setBackground(String fileName, boolean t) {
  473. this.setBackground(GraphicsUtils.loadImage(fileName, t));
  474. }
  475. public void setBackground(LColor color) {
  476. LImage image = LImage.createImage(getWidth(), getHeight(),
  477. Config.RGB_565);
  478. LGraphics g = image.getLGraphics();
  479. g.setColorAll(color);
  480. g.dispose();
  481. setBackground(image);
  482. }
  483. public void setBackground(LImage background) {
  484. if (background == null) {
  485. return;
  486. }
  487. LImage oldImage = this.background;
  488. if (oldImage != null) {
  489. int a = background.hashCode();
  490. int b = oldImage.hashCode();
  491. if (a == b) {
  492. return;
  493. }
  494. synchronized (oldImage) {
  495. this.background = background;
  496. oldImage.dispose();
  497. oldImage = null;
  498. }
  499. } else {
  500. this.background = background;
  501. }
  502. this.setAlpha(1.0F);
  503. this.width = background.getWidth();
  504. this.height = background.getHeight();
  505. if (this.width == 0) {
  506. this.width = 10;
  507. }
  508. if (this.height == 0) {
  509. this.height = 10;
  510. }
  511. validateUI();
  512. }
  513. protected void createRenderedUI(int w, int h) {
  514. this.ui = this.factory.createUI(this, w, h);
  515. }
  516. protected void processRenderedUI() {
  517. this.factory.processUI(this, this.ui);
  518. }
  519. protected void createCustomUI(int w, int h) {
  520. }
  521. protected void processCustomUI() {
  522. }
  523. protected void processExternalUI() {
  524. this.factory.processUI(this, this.ui);
  525. }
  526. public void validateUI() {
  527. this.createUI();
  528. }
  529. }