|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui.game; |
|
18 |
|
19 import javax.microedition.lcdui.Graphics; |
|
20 import javax.microedition.lcdui.Image; |
|
21 |
|
22 /** |
|
23 * LCDUI Layer class. |
|
24 */ |
|
25 public abstract class Layer |
|
26 { |
|
27 |
|
28 int x; |
|
29 int y; |
|
30 boolean visible; |
|
31 |
|
32 Image tileImage; |
|
33 |
|
34 int tileWidth; |
|
35 int tileHeight; |
|
36 |
|
37 int tileCount; |
|
38 int tileColumns; |
|
39 |
|
40 /** |
|
41 * Package level constructor prevents Layer being subclassed. This is |
|
42 * consistent with the MIDP 2.0 RI. |
|
43 */ |
|
44 Layer(Image tileImage, int frameWidth, int frameHeight) |
|
45 { |
|
46 visible = true; |
|
47 setTileImage(tileImage, frameWidth, frameHeight); |
|
48 } |
|
49 |
|
50 Layer(Layer from) |
|
51 { |
|
52 x = from.x; |
|
53 y = from.y; |
|
54 visible = from.visible; |
|
55 tileImage = from.tileImage; |
|
56 tileWidth = from.tileWidth; |
|
57 tileHeight = from.tileHeight; |
|
58 tileCount = from.tileCount; |
|
59 tileColumns = from.tileColumns; |
|
60 } |
|
61 |
|
62 public void setPosition(int x, int y) |
|
63 { |
|
64 this.x = x; |
|
65 this.y = y; |
|
66 } |
|
67 |
|
68 public void move(int deltaX, int deltaY) |
|
69 { |
|
70 x += deltaX; |
|
71 y += deltaY; |
|
72 } |
|
73 |
|
74 public final int getX() |
|
75 { |
|
76 return x; |
|
77 } |
|
78 |
|
79 public final int getY() |
|
80 { |
|
81 return y; |
|
82 } |
|
83 |
|
84 public final int getWidth() |
|
85 { |
|
86 return getLayerWidth(); |
|
87 } |
|
88 |
|
89 public final int getHeight() |
|
90 { |
|
91 return getLayerHeight(); |
|
92 } |
|
93 |
|
94 public void setVisible(boolean visible) |
|
95 { |
|
96 this.visible = visible; |
|
97 } |
|
98 |
|
99 public final boolean isVisible() |
|
100 { |
|
101 return visible; |
|
102 } |
|
103 |
|
104 public abstract void paint(Graphics graphics); |
|
105 |
|
106 abstract int getLayerWidth(); |
|
107 |
|
108 abstract int getLayerHeight(); |
|
109 |
|
110 /** |
|
111 * Set the source image and tile size. checks that the image is completely |
|
112 * tiled. computes the tile count. |
|
113 */ |
|
114 void setTileImage(Image tileImage, int tileWidth, int tileHeight) |
|
115 { |
|
116 if(tileWidth > 0 && tileHeight > 0) |
|
117 { |
|
118 final int imageWidth = tileImage.getWidth(); |
|
119 final int imageHeight = tileImage.getHeight(); |
|
120 |
|
121 int rows = imageHeight / tileHeight; |
|
122 int cols = imageWidth / tileWidth; |
|
123 |
|
124 int totalHeight = rows * tileHeight; |
|
125 int totalWidth = cols * tileWidth; |
|
126 |
|
127 if(totalHeight == imageHeight && totalWidth == imageWidth) |
|
128 { |
|
129 this.tileImage = tileImage; |
|
130 this.tileWidth = tileWidth; |
|
131 this.tileHeight = tileHeight; |
|
132 this.tileCount = rows * cols; |
|
133 this.tileColumns = cols; |
|
134 return; |
|
135 } |
|
136 } |
|
137 throw new IllegalArgumentException( |
|
138 MsgRepository.LAYER_EXCEPTION_INVALID_TILE_WIDTH_HEIGHT); |
|
139 } |
|
140 |
|
141 /** |
|
142 * Is Layer visible in Graphics clip region. |
|
143 */ |
|
144 final boolean isVisible(Graphics graphics, int pX, int pY) |
|
145 { |
|
146 int x1 = x + pX; |
|
147 int y1 = y + pY; |
|
148 int x2 = x1 + getWidth(); |
|
149 int y2 = y1 + getHeight(); |
|
150 int clipX1 = graphics.getClipX(); |
|
151 int clipY1 = graphics.getClipY(); |
|
152 int clipX2 = clipX1 + graphics.getClipWidth(); |
|
153 int clipY2 = clipY1 + graphics.getClipHeight(); |
|
154 |
|
155 return !(clipX2 <= x1 || clipY2 <= y1 || clipX1 >= x2 || clipY1 >= y2); |
|
156 } |
|
157 |
|
158 } |