Source code: com/siemens/mp/color_game/LayerManager.java
1 /*
2 * Siemens API for MicroEmulator
3 * Copyright (C) 2003 Markus Heberling <markus@heberling.net>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 package com.siemens.mp.color_game;
20
21 import java.util.Vector;
22 import javax.microedition.lcdui.Graphics;
23
24 public class LayerManager {
25
26 public LayerManager() {
27 setViewWindow(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
28 layers=new Vector();
29 }
30
31 public void append(Layer l) {
32 layers.addElement(l);
33 }
34
35 public void insert(Layer l, int index) {
36 layers.insertElementAt(l,index);
37 }
38
39 public Layer getLayerAt(int index) {
40 return (Layer)layers.elementAt(index);
41 }
42
43 public int getSize() {
44 return layers.size();
45 }
46
47 public void remove(Layer l) {
48 layers.removeElement(l);
49 }
50
51
52 public void paint(Graphics g, int x, int y) {
53 // save the original clip
54 int clipX = g.getClipX();
55 int clipY = g.getClipY();
56 int clipW = g.getClipWidth();
57 int clipH = g.getClipHeight();
58
59
60 // translate the LayerManager co-ordinates to Screen co-ordinates
61 g.translate(x - viewX, y - viewY);
62 // set the clip to view window
63 g.setClip(viewX, viewY, viewWidth, viewHeight);
64 // draw last to first
65
66 for (int i = layers.size(); --i >= 0; ) {
67 Layer comp = (Layer)layers.elementAt(i);
68 if (comp.visible
69 &&comp.x+comp.width>=viewX
70 &&comp.x<=viewX+clipW
71 &&comp.y+comp.height>=viewY
72 &&comp.y<=viewY+clipH) {
73 comp.paint(g);
74 }
75 }
76
77 // restore Screen co-ordinates origin and clip
78
79 g.translate(-x + viewX, -y + viewY);
80 g.setClip(clipX, clipY, clipW, clipH);
81 }
82
83
84 public void setViewWindow(int x, int y, int width, int height) {
85
86 if (width < 0 || height < 0) {
87 throw new IllegalArgumentException();
88 }
89 viewX = x;
90 viewY = y;
91 viewWidth = width;
92 viewHeight = height;
93 }
94
95 private Vector layers;
96 private int viewX, viewY, viewWidth, viewHeight; // = 0;
97 }