|
16 | 16 | */ |
17 | 17 | package com.siemens.mp.color_game; |
18 | 18 |
|
| 19 | +import java.util.ArrayList; |
| 20 | + |
19 | 21 | import javax.microedition.lcdui.Graphics; |
20 | 22 | import org.recompile.mobile.Mobile; |
21 | 23 |
|
22 | 24 | public class LayerManager |
23 | 25 | { |
24 | 26 |
|
25 | | - private Layer component[] = new Layer[4]; |
| 27 | + private ArrayList<Layer> layerList; |
| 28 | + |
| 29 | + private int viewWindowX; |
| 30 | + |
| 31 | + private int viewWindowY; |
26 | 32 |
|
27 | | - private int layers; |
28 | | - private int x; |
29 | | - private int y; |
30 | | - private int width; |
31 | | - private int height; |
| 33 | + private int viewWindowWidth; |
32 | 34 |
|
| 35 | + private int viewWindowHeight; |
33 | 36 |
|
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 | + } |
35 | 43 |
|
36 | 44 | // 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()); } |
38 | 46 |
|
39 | 47 | public Layer getLayerAt(int index) |
40 | 48 | { |
41 | | - if ((index < 0) || (index >= layers)) { throw new IndexOutOfBoundsException(); } |
| 49 | + if ((index < 0) || (index >= layerList.size())) |
| 50 | + { throw new IndexOutOfBoundsException("Invalid layer index."); } |
42 | 51 |
|
43 | | - return component[index]; |
| 52 | + return layerList.get(index); |
44 | 53 | } |
45 | 54 |
|
46 | | - public int getSize() { return layers; } |
| 55 | + public int getSize() { return layerList.size(); } |
47 | 56 |
|
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"); } |
53 | 61 |
|
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); |
62 | 67 |
|
63 | | - component[index] = l; |
64 | | - layers++; |
| 68 | + layerList.add(index, layer); |
65 | 69 | } |
66 | 70 |
|
67 | | - public void paint(Graphics g, int xdest, int ydest) |
| 71 | + public void paint(Graphics g, int dx, int dy) |
68 | 72 | { |
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--) |
79 | 87 | { |
80 | | - if (component[i].visible) { component[i].paint(g); } |
| 88 | + if (layerList.get(i).isVisible()) { layerList.get(i).paint(g); } |
81 | 89 | } |
| 90 | + |
82 | 91 |
|
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); |
85 | 95 | } |
86 | 96 |
|
87 | | - public void remove(Layer l) |
| 97 | + public void remove(Layer layer) |
88 | 98 | { |
89 | | - if (l == null) { throw new NullPointerException(); } |
| 99 | + if (layer == null) |
| 100 | + throw new NullPointerException("Cannot remove a null layer."); |
90 | 101 |
|
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); |
99 | 103 | } |
100 | 104 |
|
101 | | - private boolean exist(Layer l) |
| 105 | + private boolean exist(Layer layer) |
102 | 106 | { |
103 | | - if (l == null) { return false; } |
| 107 | + if (layer == null) { return false; } |
104 | 108 |
|
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 | + |
106 | 114 | return false; |
107 | 115 | } |
108 | 116 |
|
109 | | - public void setViewWindow(int wx, int wy, int wwidth, int wheight) |
| 117 | + public void setViewWindow(int x, int y, int width, int height) |
110 | 118 | { |
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."); } |
112 | 121 |
|
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; |
117 | 126 | } |
118 | 127 |
|
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 | + } |
120 | 133 | } |
0 commit comments