|
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 java.util.Vector; |
|
20 |
|
21 import javax.microedition.lcdui.Graphics; |
|
22 |
|
23 /** |
|
24 * LayerManager class. |
|
25 */ |
|
26 public class LayerManager |
|
27 { |
|
28 |
|
29 private Vector layers; |
|
30 |
|
31 private int viewX; |
|
32 private int viewY; |
|
33 |
|
34 private int viewWidth = Integer.MAX_VALUE; |
|
35 private int viewHeight = Integer.MAX_VALUE; |
|
36 |
|
37 public LayerManager() |
|
38 { |
|
39 layers = new Vector(); |
|
40 } |
|
41 |
|
42 public void append(Layer aLayer) |
|
43 { |
|
44 remove(aLayer); |
|
45 layers.addElement(aLayer); |
|
46 } |
|
47 |
|
48 public void insert(Layer layer, int index) |
|
49 { |
|
50 if(layer == null) |
|
51 { |
|
52 throw new NullPointerException( |
|
53 MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL); |
|
54 } |
|
55 if(index < 0 || index > layers.size()) |
|
56 { |
|
57 throw new IndexOutOfBoundsException( |
|
58 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX); |
|
59 } |
|
60 if(index > layers.size() - 1 && layers.contains(layer)) |
|
61 { |
|
62 throw new IndexOutOfBoundsException( |
|
63 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_LAYER_INDEX); |
|
64 } |
|
65 remove(layer); |
|
66 layers.insertElementAt(layer, index); |
|
67 } |
|
68 |
|
69 public void remove(Layer layer) |
|
70 { |
|
71 if(layer == null) |
|
72 { |
|
73 throw new NullPointerException( |
|
74 MsgRepository.LAYERMANAGER_EXCEPTION_LAYER_NULL); |
|
75 } |
|
76 layers.removeElement(layer); |
|
77 } |
|
78 |
|
79 public Layer getLayerAt(int index) |
|
80 { |
|
81 return (Layer) layers.elementAt(index); |
|
82 } |
|
83 |
|
84 public int getSize() |
|
85 { |
|
86 return layers.size(); |
|
87 } |
|
88 |
|
89 public void setViewWindow(int x, int y, int width, int height) |
|
90 { |
|
91 if(width < 0 || height < 0) |
|
92 { |
|
93 throw new IllegalArgumentException( |
|
94 MsgRepository.LAYERMANAGER_EXCEPTION_INVALID_WIDTH_HEIGHT); |
|
95 } |
|
96 viewX = x; |
|
97 viewY = y; |
|
98 viewWidth = width; |
|
99 viewHeight = height; |
|
100 } |
|
101 |
|
102 public void paint(Graphics graphics, int x, int y) |
|
103 { |
|
104 // save Graphics clip and translate |
|
105 final int oldClipX = graphics.getClipX(); |
|
106 final int oldClipY = graphics.getClipY(); |
|
107 final int oldClipW = graphics.getClipWidth(); |
|
108 final int oldClipH = graphics.getClipHeight(); |
|
109 final int oldTransX = graphics.getTranslateX(); |
|
110 final int oldTransY = graphics.getTranslateY(); |
|
111 |
|
112 // intersect Graphics clip region with View Window |
|
113 graphics.clipRect(x, y, viewWidth, viewHeight); |
|
114 /* |
|
115 * translate the graphics object so the point (aX,aY) corresponds to the |
|
116 * location of the viewWindow in the coordinate system of the |
|
117 * LayerManager. |
|
118 */ |
|
119 graphics.translate(-viewX, -viewY); |
|
120 |
|
121 // paint visible Layers |
|
122 Layer layer = null; |
|
123 for(int i = layers.size() - 1; i >= 0; i--) |
|
124 { |
|
125 layer = getLayerAt(i); |
|
126 if(layer.isVisible(graphics, x, y)) |
|
127 { |
|
128 graphics.translate(x, y); |
|
129 layer.paint(graphics); |
|
130 graphics.translate(-x, -y); |
|
131 } |
|
132 } |
|
133 |
|
134 // restore Graphics translation because translation is concatenated |
|
135 // need to translate back to 0,0 first |
|
136 graphics.translate(-graphics.getTranslateX(), |
|
137 -graphics.getTranslateY()); |
|
138 |
|
139 // restore Graphics translate and clip |
|
140 graphics.translate(oldTransX, oldTransY); |
|
141 graphics.setClip(oldClipX, oldClipY, oldClipW, oldClipH); |
|
142 } |
|
143 |
|
144 } |