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