Mercurial > hg > ristipolku
comparison game/IDMContainer.java @ 75:b586ce4f6d97
Large GUI code cleanup.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 03 Mar 2011 04:04:57 +0200 |
parents | f81f76458b92 |
children | 4c0dec72e2f0 |
comparison
equal
deleted
inserted
replaced
74:b7adeae80363 | 75:b586ce4f6d97 |
---|---|
7 import java.awt.*; | 7 import java.awt.*; |
8 import java.awt.event.*; | 8 import java.awt.event.*; |
9 import java.util.*; | 9 import java.util.*; |
10 | 10 |
11 | 11 |
12 | |
12 public class IDMContainer extends IDMWidget | 13 public class IDMContainer extends IDMWidget |
13 { | 14 { |
14 ArrayList<IDMWidget> widgets; | 15 private ArrayList<IDMWidget> children, queue; |
16 private IDMWidget modal; | |
17 private int iterated; | |
18 private boolean modified; | |
15 | 19 |
16 public IDMContainer() | 20 public IDMContainer() |
17 { | 21 { |
18 widgets = new ArrayList<IDMWidget>(); | 22 children = new ArrayList<IDMWidget>(); |
23 iterated = 0; | |
24 modified = false; | |
19 } | 25 } |
20 | 26 |
21 public void add(IDMWidget widget) | 27 synchronized public void add(IDMWidget widget) |
22 { | 28 { |
23 widgets.add(widget); | 29 widget.setParent(this); |
30 if (iterated > 0) | |
31 { | |
32 queue.add(0, widget); | |
33 modified = true; | |
34 } | |
35 else | |
36 children.add(widget); | |
24 } | 37 } |
25 | 38 |
26 public void paint(Graphics2D g) | 39 synchronized public void remove(IDMWidget widget) |
27 { | 40 { |
28 for (IDMWidget widget : widgets) | 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(); | |
29 widget.paint(g); | 81 widget.paint(g); |
82 } | |
30 } | 83 } |
31 | 84 |
32 public void mousePressed(MouseEvent e) | 85 synchronized public boolean mousePressed(MouseEvent e) |
33 { | 86 { |
34 for (IDMWidget widget : widgets) | 87 try { |
35 { | 88 beginIteration(); |
36 if (widget.contains(e.getPoint())) | 89 for (IDMWidget widget : children) |
37 widget.mousePressed(e); | 90 { |
91 if (widget.contains(e.getPoint())) | |
92 { | |
93 if (widget.mousePressed(e)) | |
94 return true; | |
95 } | |
96 } | |
38 } | 97 } |
98 finally { endIteration(); } | |
99 return false; | |
39 } | 100 } |
40 | 101 |
41 public void mouseReleased(MouseEvent e) | 102 synchronized public boolean mouseReleased(MouseEvent e) |
42 { | 103 { |
43 for (IDMWidget widget : widgets) | 104 try { |
44 { | 105 beginIteration(); |
45 widget.mouseReleased(e); | 106 for (IDMWidget widget : children) |
107 { | |
108 if (widget.mouseReleased(e)) | |
109 return true; | |
110 } | |
46 } | 111 } |
112 finally { endIteration(); } | |
113 return false; | |
47 } | 114 } |
48 | 115 |
49 public void mouseExited(MouseEvent e) | 116 synchronized public boolean mouseExited(MouseEvent e) |
50 { | 117 { |
51 for (IDMWidget widget : widgets) | 118 try { |
52 { | 119 beginIteration(); |
53 widget.mouseExited(e); | 120 for (IDMWidget widget : children) |
121 { | |
122 if (widget.mouseExited(e)) | |
123 return true; | |
124 } | |
54 } | 125 } |
126 finally { endIteration(); } | |
127 return false; | |
55 } | 128 } |
56 | 129 |
57 public void mouseEntered(MouseEvent e) | 130 synchronized public boolean mouseEntered(MouseEvent e) |
58 { | 131 { |
59 for (IDMWidget widget : widgets) | 132 try { |
60 { | 133 beginIteration(); |
61 if (widget.contains(e.getPoint())) | 134 for (IDMWidget widget : children) |
62 widget.mouseEntered(e); | 135 { |
136 if (widget.contains(e.getPoint())) | |
137 { | |
138 if (widget.mouseEntered(e)) | |
139 return true; | |
140 } | |
141 } | |
63 } | 142 } |
143 finally { endIteration(); } | |
144 return false; | |
64 } | 145 } |
65 | 146 |
66 public boolean keyPressed(KeyEvent e) | 147 synchronized public boolean keyPressed(KeyEvent e) |
67 { | 148 { |
68 for (IDMWidget widget : widgets) | 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) | |
69 { | 165 { |
70 if (widget.keyPressed(e)) | 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) | |
71 return true; | 176 return true; |
72 } | 177 } |
73 return false; | 178 return false; |
74 } | 179 } |
75 | 180 |
76 public void setScale(IDMPoint scale) | 181 synchronized public boolean containsObject(Object o) |
77 { | 182 { |
78 for (IDMWidget widget : widgets) | 183 for (IDMWidget widget : children) |
79 { | 184 { |
80 widget.setScale(scale); | 185 if (widget == o) |
186 return true; | |
81 } | 187 } |
188 return false; | |
82 } | 189 } |
83 } | 190 } |