PageRenderTime 2ms CodeModel.GetById 37ms app.highlight 31ms RepoModel.GetById 0ms app.codeStats 1ms

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