PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/android/Android(LGame-0.3.2&LAE-1.1)/LAE-1.1(Canvas)/src/core/org/loon/framework/android/game/core/graphics/LComponent.java

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