Mercurial > hg > ristipolku
annotate game/IDMContainer.java @ 152:64b04c0eccce
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 06 Feb 2013 10:24:47 +0200 |
parents | d6d92845d6a2 |
children |
rev | line source |
---|---|
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * Ristipolku IDM widget container |
151 | 3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 */ |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 package game; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 import java.awt.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 import java.awt.event.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 import java.util.*; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 |
75 | 12 |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
13 public class IDMContainer extends IDMWidget |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
14 { |
75 | 15 private ArrayList<IDMWidget> children, queue; |
16 private IDMWidget modal; | |
17 private int iterated; | |
18 private boolean modified; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 public IDMContainer() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 { |
75 | 22 children = new ArrayList<IDMWidget>(); |
23 iterated = 0; | |
24 modified = false; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
26 |
75 | 27 synchronized public void add(IDMWidget widget) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 { |
75 | 29 widget.setParent(this); |
30 if (iterated > 0) | |
31 { | |
32 queue.add(0, widget); | |
33 modified = true; | |
34 } | |
35 else | |
36 children.add(widget); | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
38 |
75 | 39 synchronized public void remove(IDMWidget widget) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 { |
75 | 41 widget.setParent(null); |
42 if (iterated > 0) | |
43 { | |
44 queue.remove(widget); | |
45 modified = true; | |
46 } | |
47 else | |
48 children.remove(widget); | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 } |
75 | 50 |
51 synchronized private void beginIteration() | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 { |
75 | 53 modified = false; |
54 if (iterated == 0) | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 { |
75 | 56 queue = new ArrayList<IDMWidget>(); |
57 for (IDMWidget widget : children) | |
58 queue.add(widget); | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 } |
75 | 60 iterated++; |
61 } | |
62 | |
63 synchronized private void endIteration() | |
64 { | |
65 if (modified && iterated == 1) | |
66 { | |
67 children = queue; | |
68 queue = null; | |
69 } | |
70 iterated--; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 |
75 | 73 synchronized public void paint(Graphics2D g) |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 { |
75 | 75 // Paint in reverse order |
76 ListIterator<IDMWidget> iter = children.listIterator(children.size()); | |
77 | |
78 while (iter.hasPrevious()) | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 { |
75 | 80 final IDMWidget widget = iter.previous(); |
81 widget.paint(g); | |
50 | 82 } |
83 } | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
84 |
75 | 85 synchronized public boolean mousePressed(MouseEvent e) |
86 { | |
87 try { | |
88 beginIteration(); | |
89 for (IDMWidget widget : children) | |
90 { | |
91 if (widget.contains(e.getPoint())) | |
92 { | |
93 if (widget.mousePressed(e)) | |
94 return true; | |
95 } | |
96 } | |
97 } | |
98 finally { endIteration(); } | |
99 return false; | |
100 } | |
50 | 101 |
75 | 102 synchronized public boolean mouseReleased(MouseEvent e) |
50 | 103 { |
75 | 104 try { |
105 beginIteration(); | |
106 for (IDMWidget widget : children) | |
107 { | |
108 if (widget.mouseReleased(e)) | |
109 return true; | |
110 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
111 } |
75 | 112 finally { endIteration(); } |
113 return false; | |
114 } | |
115 | |
116 synchronized public boolean mouseExited(MouseEvent e) | |
117 { | |
118 try { | |
119 beginIteration(); | |
120 for (IDMWidget widget : children) | |
121 { | |
122 if (widget.mouseExited(e)) | |
123 return true; | |
124 } | |
125 } | |
126 finally { endIteration(); } | |
127 return false; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
128 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
129 |
75 | 130 synchronized public boolean mouseEntered(MouseEvent e) |
131 { | |
132 try { | |
133 beginIteration(); | |
134 for (IDMWidget widget : children) | |
135 { | |
136 if (widget.contains(e.getPoint())) | |
137 { | |
138 if (widget.mouseEntered(e)) | |
139 return true; | |
140 } | |
141 } | |
142 } | |
143 finally { endIteration(); } | |
144 return false; | |
145 } | |
146 | |
147 synchronized public boolean keyPressed(KeyEvent e) | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 { |
75 | 149 try { |
150 beginIteration(); | |
151 for (IDMWidget widget : children) | |
152 { | |
153 if (widget.keyPressed(e)) | |
154 return true; | |
155 } | |
156 } | |
157 finally { endIteration(); } | |
158 return false; | |
159 } | |
160 | |
161 synchronized public void setScale(IDMPoint scale) | |
162 { | |
163 beginIteration(); | |
164 for (IDMWidget widget : children) | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
165 { |
75 | 166 widget.setScale(scale); |
167 } | |
168 endIteration(); | |
169 } | |
170 | |
171 synchronized public boolean hasObject(Class c) | |
172 { | |
173 for (IDMWidget widget : children) | |
174 { | |
175 if (widget.getClass() == c) | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 return true; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
177 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
178 return false; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 } |
51 | 180 |
75 | 181 synchronized public boolean containsObject(Object o) |
51 | 182 { |
75 | 183 for (IDMWidget widget : children) |
51 | 184 { |
75 | 185 if (widget == o) |
186 return true; | |
51 | 187 } |
75 | 188 return false; |
51 | 189 } |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
190 } |