Mercurial > hg > ristipolku
annotate game/IDMContainer.java @ 189:189cd8fe2304
Bump copyrights.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 20 Mar 2017 12:23:02 +0200 |
parents | 4568c8016377 |
children | 20231e8bbc43 |
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 |
189 | 3 * (C) Copyright 2011-2017 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 int iterated; | |
17 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
|
18 |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 public IDMContainer() |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 { |
75 | 21 children = new ArrayList<IDMWidget>(); |
22 iterated = 0; | |
23 modified = false; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
25 |
75 | 26 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
|
27 { |
75 | 28 widget.setParent(this); |
29 if (iterated > 0) | |
30 { | |
31 queue.add(0, widget); | |
32 modified = true; | |
33 } | |
34 else | |
35 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
|
36 } |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
37 |
75 | 38 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
|
39 { |
75 | 40 widget.setParent(null); |
41 if (iterated > 0) | |
42 { | |
43 queue.remove(widget); | |
44 modified = true; | |
45 } | |
46 else | |
47 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
|
48 } |
75 | 49 |
50 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
|
51 { |
75 | 52 modified = false; |
53 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
|
54 { |
75 | 55 queue = new ArrayList<IDMWidget>(); |
56 for (IDMWidget widget : children) | |
57 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
|
58 } |
75 | 59 iterated++; |
60 } | |
61 | |
62 synchronized private void endIteration() | |
63 { | |
64 if (modified && iterated == 1) | |
65 { | |
66 children = queue; | |
67 queue = null; | |
68 } | |
69 iterated--; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
75 | 72 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
|
73 { |
75 | 74 // Paint in reverse order |
75 ListIterator<IDMWidget> iter = children.listIterator(children.size()); | |
76 | |
77 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
|
78 { |
75 | 79 final IDMWidget widget = iter.previous(); |
80 widget.paint(g); | |
50 | 81 } |
82 } | |
134
4c0dec72e2f0
Whitespace cosmetic cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
75
diff
changeset
|
83 |
75 | 84 synchronized public boolean mousePressed(MouseEvent e) |
85 { | |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
86 IDMWidget modal = getActiveModalWidget(); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
87 if (modal != null) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
88 return modal.mousePressed(e); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
89 |
75 | 90 try { |
91 beginIteration(); | |
92 for (IDMWidget widget : children) | |
93 { | |
94 if (widget.contains(e.getPoint())) | |
95 { | |
96 if (widget.mousePressed(e)) | |
97 return true; | |
98 } | |
99 } | |
100 } | |
101 finally { endIteration(); } | |
102 return false; | |
103 } | |
50 | 104 |
75 | 105 synchronized public boolean mouseReleased(MouseEvent e) |
50 | 106 { |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
107 IDMWidget modal = getActiveModalWidget(); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
108 if (modal != null) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
109 return modal.mouseReleased(e); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
110 |
75 | 111 try { |
112 beginIteration(); | |
113 for (IDMWidget widget : children) | |
114 { | |
115 if (widget.mouseReleased(e)) | |
116 return true; | |
117 } | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
118 } |
75 | 119 finally { endIteration(); } |
120 return false; | |
121 } | |
122 | |
123 synchronized public boolean mouseExited(MouseEvent e) | |
124 { | |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
125 IDMWidget modal = getActiveModalWidget(); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
126 if (modal != null) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
127 return modal.mouseExited(e); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
128 |
75 | 129 try { |
130 beginIteration(); | |
131 for (IDMWidget widget : children) | |
132 { | |
133 if (widget.mouseExited(e)) | |
134 return true; | |
135 } | |
136 } | |
137 finally { endIteration(); } | |
138 return false; | |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 |
75 | 141 synchronized public boolean mouseEntered(MouseEvent e) |
142 { | |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
143 IDMWidget modal = getActiveModalWidget(); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
144 if (modal != null) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
145 return modal.mouseEntered(e); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
146 |
75 | 147 try { |
148 beginIteration(); | |
149 for (IDMWidget widget : children) | |
150 { | |
151 if (widget.contains(e.getPoint())) | |
152 { | |
153 if (widget.mouseEntered(e)) | |
154 return true; | |
155 } | |
156 } | |
157 } | |
158 finally { endIteration(); } | |
159 return false; | |
160 } | |
161 | |
162 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
|
163 { |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
164 IDMWidget modal = getActiveModalWidget(); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
165 if (modal != null) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
166 return modal.keyPressed(e); |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
167 |
75 | 168 try { |
169 beginIteration(); | |
170 for (IDMWidget widget : children) | |
171 { | |
172 if (widget.keyPressed(e)) | |
173 return true; | |
174 } | |
175 } | |
176 finally { endIteration(); } | |
177 return false; | |
178 } | |
179 | |
180 synchronized public void setScale(IDMPoint scale) | |
181 { | |
188
4568c8016377
Apply scale to IDMContainers also, not only to child widgets.
Matti Hamalainen <ccr@tnsp.org>
parents:
175
diff
changeset
|
182 super.setScale(scale); |
75 | 183 beginIteration(); |
184 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
|
185 { |
75 | 186 widget.setScale(scale); |
187 } | |
188 endIteration(); | |
189 } | |
190 | |
191 synchronized public boolean hasObject(Class c) | |
192 { | |
193 for (IDMWidget widget : children) | |
194 { | |
195 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
|
196 return true; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
197 } |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
198 return false; |
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 } |
51 | 200 |
75 | 201 synchronized public boolean containsObject(Object o) |
51 | 202 { |
75 | 203 for (IDMWidget widget : children) |
51 | 204 { |
75 | 205 if (widget == o) |
206 return true; | |
51 | 207 } |
75 | 208 return false; |
51 | 209 } |
175
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
210 |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
211 synchronized public IDMWidget getActiveModalWidget() |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
212 { |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
213 for (IDMWidget widget : children) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
214 { |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
215 if (widget.isModal()) |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
216 return widget; |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
217 } |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
218 return null; |
55ea5821c802
Implement "real" widget modality.
Matti Hamalainen <ccr@tnsp.org>
parents:
172
diff
changeset
|
219 } |
49
e6da5c71be28
Add more code to IDM widgets, do preliminary work for integrating them.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 } |