Mercurial > hg > ristipolku
comparison src/IDMContainer.java @ 161:fb33d3796942
Rename source directory.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 21 Jun 2016 12:53:53 +0300 |
parents | game/IDMContainer.java@d6d92845d6a2 |
children |
comparison
equal
deleted
inserted
replaced
160:d5fbad999841 | 161:fb33d3796942 |
---|---|
1 /* | |
2 * Ristipolku IDM widget container | |
3 * (C) Copyright 2011 Matti 'ccr' Hämäläinen <ccr@tnsp.org> | |
4 */ | |
5 package game; | |
6 | |
7 import java.awt.*; | |
8 import java.awt.event.*; | |
9 import java.util.*; | |
10 | |
11 | |
12 | |
13 public class IDMContainer extends IDMWidget | |
14 { | |
15 private ArrayList<IDMWidget> children, queue; | |
16 private IDMWidget modal; | |
17 private int iterated; | |
18 private boolean modified; | |
19 | |
20 public IDMContainer() | |
21 { | |
22 children = new ArrayList<IDMWidget>(); | |
23 iterated = 0; | |
24 modified = false; | |
25 } | |
26 | |
27 synchronized public void add(IDMWidget widget) | |
28 { | |
29 widget.setParent(this); | |
30 if (iterated > 0) | |
31 { | |
32 queue.add(0, widget); | |
33 modified = true; | |
34 } | |
35 else | |
36 children.add(widget); | |
37 } | |
38 | |
39 synchronized public void remove(IDMWidget widget) | |
40 { | |
41 widget.setParent(null); | |
42 if (iterated > 0) | |
43 { | |
44 queue.remove(widget); | |
45 modified = true; | |
46 } | |
47 else | |
48 children.remove(widget); | |
49 } | |
50 | |
51 synchronized private void beginIteration() | |
52 { | |
53 modified = false; | |
54 if (iterated == 0) | |
55 { | |
56 queue = new ArrayList<IDMWidget>(); | |
57 for (IDMWidget widget : children) | |
58 queue.add(widget); | |
59 } | |
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--; | |
71 } | |
72 | |
73 synchronized public void paint(Graphics2D g) | |
74 { | |
75 // Paint in reverse order | |
76 ListIterator<IDMWidget> iter = children.listIterator(children.size()); | |
77 | |
78 while (iter.hasPrevious()) | |
79 { | |
80 final IDMWidget widget = iter.previous(); | |
81 widget.paint(g); | |
82 } | |
83 } | |
84 | |
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 } | |
101 | |
102 synchronized public boolean mouseReleased(MouseEvent e) | |
103 { | |
104 try { | |
105 beginIteration(); | |
106 for (IDMWidget widget : children) | |
107 { | |
108 if (widget.mouseReleased(e)) | |
109 return true; | |
110 } | |
111 } | |
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; | |
128 } | |
129 | |
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) | |
148 { | |
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) | |
165 { | |
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) | |
176 return true; | |
177 } | |
178 return false; | |
179 } | |
180 | |
181 synchronized public boolean containsObject(Object o) | |
182 { | |
183 for (IDMWidget widget : children) | |
184 { | |
185 if (widget == o) | |
186 return true; | |
187 } | |
188 return false; | |
189 } | |
190 } |