Skip to content

Commit 83c0e3f

Browse files
committed
lcdui/mp.game: Improvements and cleanups.
Yet more code based on my work that's going into SquirrelJME. This time, those classes should now support transformed sprites where applicable, and be a bit more efficient overall as well.
1 parent 1fbceda commit 83c0e3f

File tree

9 files changed

+419
-698
lines changed

9 files changed

+419
-698
lines changed

src/com/siemens/mp/color_game/Layer.java

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ public abstract class Layer
3232

3333
protected int width;
3434

35-
protected Image image;
36-
3735
protected boolean visible = true;
3836

3937
public Layer() { x = 0; y = 0;}
@@ -46,33 +44,19 @@ public Layer(int width, int height)
4644
setHeight(height);
4745
}
4846

49-
public Layer(Image i) { setLayerImage(i); }
50-
51-
protected void setLayerImage(Image i)
52-
{
53-
image = i;
54-
x = 0;
55-
y = 0;
56-
width = i.getWidth();
57-
height = i.getHeight();
47+
protected void copyAllLayerVariables(Layer l)
48+
{
49+
l.x = this.x;
50+
l.y = this.y;
51+
l.height = this.height;
52+
l.width = this.width;
53+
l.visible = this.visible;
5854
}
5955

6056
public int getHeight() { return height; }
6157

6258
public int getWidth() { return width; }
6359

64-
public void setWidth(int width)
65-
{
66-
if (width < 0) { throw new IllegalArgumentException(); }
67-
this.width = width;
68-
}
69-
70-
public void setHeight(int height)
71-
{
72-
if (height < 0) { throw new IllegalArgumentException(); }
73-
this.height = height;
74-
}
75-
7660
public int getX() { return x; }
7761

7862
public int getY() { return y; }
@@ -87,8 +71,15 @@ public void setHeight(int height)
8771

8872
public void setVisible(boolean state) { visible = state; }
8973

90-
public Image getLayerImage() { return image; }
91-
92-
protected void copyAllLayerVariables(Layer l) { Mobile.log(Mobile.LOG_WARNING, Layer.class.getPackage().getName() + "." + Layer.class.getSimpleName() + ": " + "Siemens: color_game.Layer: copyAllLayerVariables()"); } // TODO
74+
protected void setWidth(int width)
75+
{
76+
if (width < 0) { throw new IllegalArgumentException(); }
77+
this.width = width;
78+
}
9379

80+
protected void setHeight(int height)
81+
{
82+
if (height < 0) { throw new IllegalArgumentException(); }
83+
this.height = height;
84+
}
9485
}

src/com/siemens/mp/color_game/LayerManager.java

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,105 +16,118 @@
1616
*/
1717
package com.siemens.mp.color_game;
1818

19+
import java.util.ArrayList;
20+
1921
import javax.microedition.lcdui.Graphics;
2022
import org.recompile.mobile.Mobile;
2123

2224
public class LayerManager
2325
{
2426

25-
private Layer component[] = new Layer[4];
27+
private ArrayList<Layer> layerList;
28+
29+
private int viewWindowX;
30+
31+
private int viewWindowY;
2632

27-
private int layers;
28-
private int x;
29-
private int y;
30-
private int width;
31-
private int height;
33+
private int viewWindowWidth;
3234

35+
private int viewWindowHeight;
3336

34-
public LayerManager() { setViewWindow(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE); }
37+
38+
public LayerManager()
39+
{
40+
setViewWindow(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
41+
this.layerList = new ArrayList<Layer>();
42+
}
3543

3644
// This is just an insert() call, but with the last layer pos as the index.
37-
public void append(Layer l) { insert(l, layers); }
45+
public void append(Layer layer) { insert(layer, layerList.size()); }
3846

3947
public Layer getLayerAt(int index)
4048
{
41-
if ((index < 0) || (index >= layers)) { throw new IndexOutOfBoundsException(); }
49+
if ((index < 0) || (index >= layerList.size()))
50+
{ throw new IndexOutOfBoundsException("Invalid layer index."); }
4251

43-
return component[index];
52+
return layerList.get(index);
4453
}
4554

46-
public int getSize() { return layers; }
55+
public int getSize() { return layerList.size(); }
4756

48-
public void insert(Layer l, int index)
49-
{
50-
if ((index < 0) || (index > layers) || (exist(l) && (index >= layers))) { throw new IndexOutOfBoundsException(); }
51-
52-
remove(l);
57+
public void insert(Layer layer, int index)
58+
{
59+
if(layer == null)
60+
{ throw new NullPointerException("Cannot insert a null layer"); }
5361

54-
if (layers == component.length)
55-
{
56-
Layer newcomponents[] = new Layer[layers + 4];
57-
System.arraycopy(component, 0, newcomponents, 0, layers);
58-
System.arraycopy(component, index, newcomponents, index + 1, layers - index);
59-
component = newcomponents;
60-
}
61-
else { System.arraycopy(component, index, component, index + 1, layers - index); }
62+
if ((index < 0) || (index > layerList.size()) ||
63+
(exist(layer) && (index >= layerList.size())))
64+
{ throw new IndexOutOfBoundsException("Layer index out of range"); }
65+
66+
remove(layer);
6267

63-
component[index] = l;
64-
layers++;
68+
layerList.add(index, layer);
6569
}
6670

67-
public void paint(Graphics g, int xdest, int ydest)
71+
public void paint(Graphics g, int dx, int dy)
6872
{
69-
int cx = g.getClipX();
70-
int cy = g.getClipY();
71-
int cw = g.getClipWidth();
72-
int ch = g.getClipHeight();
73-
74-
g.translate(xdest - x, ydest - y);
75-
// set the clip to view window
76-
g.clipRect(x, y, width, height);
77-
78-
for (int i = layers-1; i >= 0; i--)
73+
final int clipX = g.getClipX();
74+
final int clipY = g.getClipY();
75+
final int clipWidth = g.getClipWidth();
76+
final int clipHeight = g.getClipHeight();
77+
78+
// Translate and set Graphics clip to the current ViewWindow bounds
79+
g.translate(dx - this.viewWindowX, dy - this.viewWindowY);
80+
g.clipRect(this.viewWindowX, this.viewWindowY,
81+
this.viewWindowWidth, this.viewWindowHeight);
82+
83+
// Paint the given layer (if visible). The layer's index indicates its
84+
// z-order, meaning that index 0 is at the front of all others, which
85+
// means we have to draw from {@code layers} to {@code 0}.
86+
for (int i = layerList.size()-1; i >= 0; i--)
7987
{
80-
if (component[i].visible) { component[i].paint(g); }
88+
if (layerList.get(i).isVisible()) { layerList.get(i).paint(g); }
8189
}
90+
8291

83-
g.translate(-xdest + x, -ydest + y);
84-
g.setClip(cx, cy, cw, ch);
92+
// Restore the original translation and clip bounds after painting
93+
g.translate(-dx + this.viewWindowX, -dy + this.viewWindowY);
94+
g.setClip(clipX, clipY, clipWidth, clipHeight);
8595
}
8696

87-
public void remove(Layer l)
97+
public void remove(Layer layer)
8898
{
89-
if (l == null) { throw new NullPointerException(); }
99+
if (layer == null)
100+
throw new NullPointerException("Cannot remove a null layer.");
90101

91-
for (int i = layers-1; i >= 0; i--)
92-
{
93-
if (component[i] == l)
94-
{
95-
System.arraycopy(component, i + 1, component, i, layers - i - 1);
96-
component[--layers] = null;
97-
}
98-
}
102+
layerList.remove(layer);
99103
}
100104

101-
private boolean exist(Layer l)
105+
private boolean exist(Layer layer)
102106
{
103-
if (l == null) { return false; }
107+
if (layer == null) { return false; }
104108

105-
for (int i = layers; --i >= 0; ) { if (component[i] == l) { return true; } }
109+
for (int i = 0; i < layerList.size(); i++)
110+
{
111+
if (this.layerList.get(i) == layer) { return true; }
112+
}
113+
106114
return false;
107115
}
108116

109-
public void setViewWindow(int wx, int wy, int wwidth, int wheight)
117+
public void setViewWindow(int x, int y, int width, int height)
110118
{
111-
if (wwidth < 0 || wheight < 0) { throw new IllegalArgumentException(); }
119+
if (width < 0 || height < 0)
120+
{ throw new IllegalArgumentException("ViewWindow has invalid width and/or height."); }
112121

113-
x = wx;
114-
y = wy;
115-
width = wwidth;
116-
height = wheight;
122+
this.viewWindowX = x;
123+
this.viewWindowY = y;
124+
this.viewWindowWidth = width;
125+
this.viewWindowHeight = height;
117126
}
118127

119-
public void removeLayer(com.siemens.mp.misc.NativeMem NativeMemoryTable, Layer l) { Mobile.log(Mobile.LOG_WARNING, LayerManager.class.getPackage().getName() + "." + LayerManager.class.getSimpleName() + ": " + "Siemens: color_game.LayerManager: removeLayer()"); } // TODO
128+
public void removeLayer(com.siemens.mp.misc.NativeMem NativeMemoryTable, Layer l)
129+
{
130+
Mobile.log(Mobile.LOG_WARNING, LayerManager.class.getPackage().getName() + "." + LayerManager.class.getSimpleName() + ": " + "Siemens: color_game.LayerManager: removeLayer()");
131+
this.remove(l);
132+
}
120133
}

0 commit comments

Comments
 (0)