Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
EventDispatch.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: EventDispatch.cpp
7  AUTHOR: John DiCamillo
8 
9 */
10 
11 #include "MemDebug.h"
12 #include "EventDispatch.h"
13 #include "Mouse.h"
14 #include "Keyboard.h"
15 
16 // +--------------------------------------------------------------------+
17 
18 int GetKeyPlus(int& key, int& shift);
19 
20 // +--------------------------------------------------------------------+
21 
23 
24 // +--------------------------------------------------------------------+
25 
27 : capture(0), current(0), focus(0), click_tgt(0)
28 , mouse_x(0), mouse_y(0), mouse_l(0), mouse_r(0)
29 { }
30 
32 { }
33 
34 // +--------------------------------------------------------------------+
35 
36 void
38 {
39  dispatcher = new(__FILE__,__LINE__) EventDispatch;
40 }
41 
42 // +--------------------------------------------------------------------+
43 
44 void
46 {
47  delete dispatcher;
48  dispatcher = 0;
49 }
50 
51 // +--------------------------------------------------------------------+
52 
53 void
55 {
56  int ml = Mouse::LButton();
57  int mr = Mouse::RButton();
58  int mx = Mouse::X();
59  int my = Mouse::Y();
60  int mw = Mouse::Wheel();
61 
62  EventTarget* mouse_tgt = capture;
63  EventTarget* key_tgt = focus;
64  EventTarget* do_click = 0;
65 
66  if (!mouse_tgt) {
68  while (++iter) {
69  EventTarget* test = iter.value();
70  if (test->IsFormActive()) {
71  if (test->TargetRect().Contains(mx,my))
72  mouse_tgt = test;
73 
74  if (test->HasFocus())
75  key_tgt = test;
76  }
77  }
78  }
79 
80  // Mouse Events:
81 
82  if (mouse_tgt != current) {
83  if (current && current->IsEnabled() && current->IsVisible())
84  current->OnMouseExit(mx,my);
85 
86  current = mouse_tgt;
87 
88  if (current && current->IsEnabled() && current->IsVisible())
89  current->OnMouseEnter(mx,my);
90  }
91 
92  if (mouse_tgt && mouse_tgt->IsEnabled()) {
93  if (mx != mouse_x || my != mouse_y)
94  mouse_tgt->OnMouseMove(mx,my);
95 
96  if (mw != 0)
97  mouse_tgt->OnMouseWheel(mw);
98 
99  if (ml != mouse_l) {
100  if (ml) {
101  mouse_tgt->OnLButtonDown(mx,my);
102  click_tgt = mouse_tgt;
103  }
104  else {
105  mouse_tgt->OnLButtonUp(mx,my);
106 
107  if (click_tgt == mouse_tgt) {
108  if (click_tgt->TargetRect().Contains(mx,my))
109  do_click = click_tgt;
110  click_tgt = 0;
111  }
112  }
113  }
114 
115  if (mr != mouse_r) {
116  if (mr)
117  mouse_tgt->OnRButtonDown(mx,my);
118  else
119  mouse_tgt->OnRButtonUp(mx,my);
120  }
121  }
122 
123  mouse_l = ml;
124  mouse_r = mr;
125  mouse_x = mx;
126  mouse_y = my;
127 
128  // Keyboard Events:
129 
130  if (click_tgt && click_tgt != key_tgt) {
131  if (key_tgt) key_tgt->KillFocus();
132  key_tgt = click_tgt;
133 
134  if (key_tgt != focus) {
135  if (focus) focus->KillFocus();
136 
137  if (key_tgt && key_tgt->IsEnabled() && key_tgt->IsVisible())
138  focus = key_tgt;
139  else
140  key_tgt = 0;
141 
142  if (focus) focus->SetFocus();
143  }
144  }
145 
146  if (key_tgt && key_tgt->IsEnabled()) {
147  int key = 0;
148  int shift = 0;
149 
150  while (GetKeyPlus(key, shift)) {
151  if (key == VK_ESCAPE) {
152  key_tgt->KillFocus();
153  focus = 0;
154  break;
155  }
156 
157  else if (key == VK_TAB && focus) {
158  int key_index = clients.index(focus) + 1;
159 
160  if (shift & 1) key_index -= 2;
161 
162  if (key_index >= clients.size())
163  key_index = 0;
164  else if (key_index < 0)
165  key_index = clients.size()-1;
166 
167  if (focus) focus->KillFocus();
168  focus = clients[key_index];
169  if (focus) focus->SetFocus();
170 
171  break;
172  }
173 
174  key_tgt->OnKeyDown(key, shift);
175  }
176  }
177 
178  if (do_click)
179  do_click->OnClick();
180 }
181 
182 // +--------------------------------------------------------------------+
183 
184 void
186 {
187  if (mouse_tgt != current) {
188  if (current) current->OnMouseExit(0,0);
189  current = mouse_tgt;
190  if (current) current->OnMouseEnter(0,0);
191  }
192 }
193 
194 // +--------------------------------------------------------------------+
195 
196 void
198 {
199  if (!clients.contains(tgt))
200  clients.append(tgt);
201 }
202 
203 // +--------------------------------------------------------------------+
204 
205 void
207 {
208  clients.remove(tgt);
209 
210  if (capture == tgt) capture = 0;
211  if (current == tgt) current = 0;
212  if (focus == tgt) focus = 0;
213  if (click_tgt == tgt) click_tgt = 0;
214 }
215 
216 // +--------------------------------------------------------------------+
217 
220 {
221  return capture;
222 }
223 
224 int
226 {
227  if (tgt) {
228  capture = tgt;
229  return 1;
230  }
231 
232  return 0;
233 }
234 
235 int
237 {
238  if (capture == tgt) {
239  capture = 0;
240  return 1;
241  }
242 
243  return 0;
244 }
245 
246 // +--------------------------------------------------------------------+
247 
250 {
251  return focus;
252 }
253 
254 void
256 {
257  if (focus != tgt) {
258  if (focus)
259  focus->KillFocus();
260 
261  focus = tgt;
262 
263  if (focus && focus->IsEnabled() && focus->IsVisible())
264  focus->SetFocus();
265  }
266 }
267 
268 void
270 {
271  if (focus && focus == tgt) {
272  focus = 0;
273  tgt->KillFocus();
274  }
275 }