MyGUI 3.5.3
MyGUI_InputManager.cpp
Go to the documentation of this file.
1/*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7#include "MyGUI_Precompiled.h"
9#include "MyGUI_Widget.h"
10#include "MyGUI_WidgetManager.h"
11#include "MyGUI_Gui.h"
12#include "MyGUI_Constants.h"
13
14namespace MyGUI
15{
16 namespace
17 {
18
19 IntPoint unrotateForWidget(const Widget* _widget, const IntPoint& _point)
20 {
21 if (_widget == nullptr)
22 return _point;
23 const FloatPoint point = _widget->unrotatePoint(FloatPoint((float)_point.left, (float)_point.top));
24 return IntPoint((int)std::lround(point.left), (int)std::lround(point.top));
25 }
26
27 }
28
29 // In seconds
30 const float INPUT_DELAY_FIRST_KEY = 0.4f;
31 const float INPUT_INTERVAL_KEY = 0.05f;
32
34
35 InputManager::InputManager() :
36 mSingletonHolder(this),
37 mTimerDoubleClick(mDoubleClickTime)
38 {
39 resetMouseCaptureWidget();
40 }
41
42 void InputManager::initialise()
43 {
44 MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
45 MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
46
47 mWidgetMouseFocus = nullptr;
48 mWidgetKeyFocus = nullptr;
49 mLayerMouseFocus = nullptr;
50 for (int i = MouseButton::Button0; i < MouseButton::MAX; ++i)
51 {
52 mMouseCapture[i] = false;
53 }
54 mIsShiftPressed = false;
55 mIsControlPressed = false;
56 mIsAltPressed = false;
57 mIsMetaPressed = false;
58
59 mHoldKey = KeyCode::None;
60 mHoldChar = 0;
61 mFirstPressKey = true;
62 mTimerKey = 0.0f;
63 mOldAbsZ = 0;
64
65 WidgetManager::getInstance().registerUnlinker(this);
66 Gui::getInstance().eventFrameStart += newDelegate(this, &InputManager::frameEntered);
67
68 MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
69 mIsInitialise = true;
70 }
71
72 void InputManager::shutdown()
73 {
74 MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
75 MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
76
77 Gui::getInstance().eventFrameStart -= newDelegate(this, &InputManager::frameEntered);
78 WidgetManager::getInstance().unregisterUnlinker(this);
79
80 MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
81 mIsInitialise = false;
82 }
83
84 bool InputManager::injectMouseMove(int _absx, int _absy, int _absz)
85 {
86 mMousePosition.set(_absx, _absy);
87
88 int relz = _absz - mOldAbsZ;
89 mOldAbsZ = _absz;
90
91 if (relz != 0)
92 {
93 bool isFocus = isFocusMouse();
94 if (isFocusMouse())
95 mWidgetMouseFocus->_riseMouseWheel(relz);
96 return isFocus;
97 }
98
99 if (isCaptureMouse())
100 {
101 bool isFocus = isFocusMouse();
102 if (isFocus)
103 {
104 if (mLayerMouseFocus != nullptr)
105 {
106 IntPoint point = unrotateForWidget(mWidgetMouseFocus, mLayerMouseFocus->getPosition(_absx, _absy));
107 for (int i = MouseButton::Button0; i < MouseButton::MAX; ++i)
108 {
109 if (mMouseCapture[i])
110 {
111 mWidgetMouseFocus->_riseMouseDrag(point.left, point.top, MouseButton::Enum(i));
112 }
113 }
114 }
115 }
116
117 return isFocus;
118 }
119
120 Widget* old_mouse_focus = mWidgetMouseFocus;
121
122 Widget* item = LayerManager::getInstance().getWidgetFromPoint(_absx, _absy);
123
124 // nothing changed
125 if (mWidgetMouseFocus == item)
126 {
127 bool isFocus = isFocusMouse();
128 if (isFocusMouse())
129 {
130 if (mLayerMouseFocus != nullptr)
131 {
132 IntPoint point = mLayerMouseFocus->getPosition(_absx, _absy);
133 point = unrotateForWidget(mWidgetMouseFocus, point);
134 mWidgetMouseFocus->_riseMouseMove(point.left, point.top);
135 }
136 }
137 return isFocus;
138 }
139
140 if (item)
141 {
142 Widget* root = item;
143 while (root->getParent())
144 root = root->getParent();
145
146 if (!mVectorModalRootWidget.empty())
147 {
148 if (root != mVectorModalRootWidget.back())
149 {
150 item = nullptr;
151 }
152 }
153
154 if (item != nullptr)
155 {
156 mLayerMouseFocus = root->getLayer();
157 }
158 }
159
160 // new type of root mouse focus
161 Widget* save_widget = nullptr;
162
163 // go down the new widget and set root focus
164 Widget* root_focus = item;
165 while (root_focus != nullptr)
166 {
167 if (root_focus->getRootMouseFocus())
168 {
169 save_widget = root_focus;
170 break;
171 }
172
173 root_focus->_setRootMouseFocus(true);
174 root_focus->_riseMouseChangeRootFocus(true);
175 root_focus = root_focus->getParent();
176 }
177
178 // go down the old widget and reset focus
179 root_focus = mWidgetMouseFocus;
180 while (root_focus != nullptr)
181 {
182 if (root_focus == save_widget)
183 break;
184
185 root_focus->_setRootMouseFocus(false);
186 root_focus->_riseMouseChangeRootFocus(false);
187 root_focus = root_focus->getParent();
188 }
189 //-------------------------------------------------------------------------------------//
190
191 // change focus, check widget availability
192 if (isFocusMouse() && mWidgetMouseFocus->getInheritedEnabled())
193 {
194 mWidgetMouseFocus->_riseMouseLostFocus(item);
195 }
196
197 if ((item != nullptr) && (item->getInheritedEnabled()))
198 {
199 MyGUI::IntPoint point(_absx, _absy);
200 if (mLayerMouseFocus != nullptr)
201 point = mLayerMouseFocus->getPosition(_absx, _absy);
202 point = unrotateForWidget(item, point);
203 item->_riseMouseMove(point.left, point.top);
204 item->_riseMouseSetFocus(mWidgetMouseFocus);
205 }
206
207 mWidgetMouseFocus = item;
208
209 if (old_mouse_focus != mWidgetMouseFocus)
210 {
211 // Reset double click timer, double clicks should only work when clicking on the *same* item twice
212 mTimerDoubleClick = mDoubleClickTime;
213 eventChangeMouseFocus(mWidgetMouseFocus);
214 }
215
216 return isFocusMouse();
217 }
218
219 bool InputManager::injectMousePress(int _absx, int _absy, MouseButton _id)
220 {
221 injectMouseMove(_absx, _absy, mOldAbsZ);
222
223 if (MouseButton::None != _id && MouseButton::MAX != _id)
224 {
225 // start capture
226 mMouseCapture[_id.getValue()] = true;
227 }
228
229 // clicked outside GUI
230 if (!isFocusMouse())
231 {
232 if (mVectorModalRootWidget.empty())
233 resetKeyFocusWidget();
234
235 return false;
236 }
237
238 // if active element is blocked
239 //FIXME
240 if (!mWidgetMouseFocus->getInheritedEnabled())
241 return true;
242
243 if (MouseButton::None != _id && MouseButton::MAX != _id)
244 {
245 // remember last pressed position
246 if (mLayerMouseFocus != nullptr)
247 {
248 IntPoint point = mLayerMouseFocus->getPosition(_absx, _absy);
249 mLastPressed[_id.getValue()] = point;
250 }
251 }
252
253 // search up for widget that can accept focus
254 Widget* item = mWidgetMouseFocus;
255 while ((item != nullptr) && (!item->getNeedKeyFocus()))
256 item = item->getParent();
257
258 // set before call as something inside might change focus
259 setKeyFocusWidget(item);
260
261 if (isFocusMouse())
262 {
263 IntPoint point(_absx, _absy);
264 if (mLayerMouseFocus != nullptr)
265 point = mLayerMouseFocus->getPosition(_absx, _absy);
266 point = unrotateForWidget(mWidgetMouseFocus, point);
267 mWidgetMouseFocus->_riseMouseButtonPressed(point.left, point.top, _id);
268
269 // might get reset after press
270 if (mWidgetMouseFocus)
271 {
272 // raise widget, consider what to do if key focus changed
273 LayerManager::getInstance().upLayerItem(mWidgetMouseFocus);
274
275 // raise picking of Overlapped windows
276 Widget* pick = mWidgetMouseFocus;
277 do
278 {
279 if (pick->getWidgetStyle() == WidgetStyle::Overlapped)
280 {
281 if (pick->getParent())
282 pick->getParent()->_forcePick(pick);
283 }
284
285 pick = pick->getParent();
286 } while (pick);
287 }
288 }
289
290 return true;
291 }
292
293 bool InputManager::injectMouseRelease(int _absx, int _absy, MouseButton _id)
294 {
295 if (_id != MouseButton::None && _id != MouseButton::MAX)
296 {
297 if (mMouseCapture[_id.getValue()])
298 {
299 // drop capture
300 mMouseCapture[_id.getValue()] = false;
301 }
302 }
303
304 if (isFocusMouse())
305 {
306 if (!mWidgetMouseFocus->getInheritedEnabled())
307 return true;
308
309 IntPoint point(_absx, _absy);
310 if (mLayerMouseFocus != nullptr)
311 point = mLayerMouseFocus->getPosition(_absx, _absy);
312 point = unrotateForWidget(mWidgetMouseFocus, point);
313 mWidgetMouseFocus->_riseMouseButtonReleased(point.left, point.top, _id);
314
315 // might get reset after release
316 if (nullptr != mWidgetMouseFocus)
317 {
318 if (MouseButton::Left == _id)
319 {
320 if (mTimerDoubleClick < mDoubleClickTime)
321 {
322 mWidgetMouseFocus->_riseMouseButtonClick();
323 // might be reset in the call above, so check again
324 if (nullptr != mWidgetMouseFocus)
325 mWidgetMouseFocus->_riseMouseButtonDoubleClick();
326 }
327 else
328 {
329 // check if we are over the same window as when pressed
330 Widget* item = LayerManager::getInstance().getWidgetFromPoint(_absx, _absy);
331 if (item == mWidgetMouseFocus)
332 {
333 mWidgetMouseFocus->_riseMouseButtonClick();
334 }
335 mTimerDoubleClick = 0;
336 }
337 }
338 }
339
340 injectMouseMove(_absx, _absy, mOldAbsZ);
341
342 return true;
343 }
344
345 return false;
346 }
347
348 void InputManager::setDoubleClickTime(float _value)
349 {
350 mDoubleClickTime = _value;
351 mTimerDoubleClick = mDoubleClickTime;
352 }
353
354 float InputManager::getDoubleClickTime() const
355 {
356 return mDoubleClickTime;
357 }
358
359 bool InputManager::injectKeyPress(KeyCode _key, Char _text)
360 {
361 firstEncoding(_key, true);
362
363 storeKey(_key, _text);
364
365 bool wasFocusKey = isFocusKey();
366
367 //Pass keystrokes to the current active text widget
368 if (isFocusKey())
369 {
370 mWidgetKeyFocus->_riseKeyButtonPressed(_key, _text);
371 }
372
373 return wasFocusKey;
374 }
375
376 bool InputManager::injectKeyRelease(KeyCode _key)
377 {
378 firstEncoding(_key, false);
379
380 resetKey();
381
382 bool wasFocusKey = isFocusKey();
383
384 if (isFocusKey())
385 mWidgetKeyFocus->_riseKeyButtonReleased(_key);
386
387 return wasFocusKey;
388 }
389
390 void InputManager::firstEncoding(KeyCode _key, bool bIsKeyPressed)
391 {
392 if ((_key == KeyCode::LeftShift) || (_key == KeyCode::RightShift))
393 mIsShiftPressed = bIsKeyPressed;
394 if ((_key == KeyCode::LeftControl) || (_key == KeyCode::RightControl))
395 mIsControlPressed = bIsKeyPressed;
396 if ((_key == KeyCode::LeftAlt) || (_key == KeyCode::RightAlt))
397 mIsAltPressed = bIsKeyPressed;
398 if ((_key == KeyCode::LeftWindows) || (_key == KeyCode::RightWindows))
399 mIsMetaPressed = bIsKeyPressed;
400 }
401
402 void InputManager::setKeyFocusWidget(Widget* _widget)
403 {
404 if (_widget == mWidgetKeyFocus)
405 return;
406
407 Widget* oldKeyFocus = mWidgetKeyFocus;
408 mWidgetKeyFocus = nullptr;
409
410 Widget* sharedRootFocus = nullptr; // possible shared parent for both old and new widget
411
412 // recursively set root key focus
413 Widget* rootFocus = _widget;
414 while (rootFocus != nullptr)
415 {
416 if (rootFocus->getRootKeyFocus())
417 {
418 sharedRootFocus = rootFocus;
419 break;
420 }
421
422 rootFocus->_setRootKeyFocus(true);
423 rootFocus->_riseKeyChangeRootFocus(true);
424 rootFocus = rootFocus->getParent();
425 }
426
427 // recursively reset root key focus
428 rootFocus = oldKeyFocus;
429 while (rootFocus != nullptr)
430 {
431 if (rootFocus == sharedRootFocus)
432 break;
433
434 rootFocus->_setRootKeyFocus(false);
435 rootFocus->_riseKeyChangeRootFocus(false);
436 rootFocus = rootFocus->getParent();
437 }
438 //-------------------------------------------------------------------------------------//
439
440 mWidgetKeyFocus = _widget;
441
442 if (oldKeyFocus)
443 {
444 oldKeyFocus->_riseKeyLostFocus(_widget);
445 }
446
447 if (_widget)
448 {
449 _widget->_riseKeySetFocus(oldKeyFocus);
450 }
451
452 eventChangeKeyFocus(mWidgetKeyFocus);
453 }
454
455 void InputManager::_resetMouseFocusWidget()
456 {
457 Widget* mouseFocus = mWidgetMouseFocus;
458 mWidgetMouseFocus = nullptr;
459
460 // recursively reset old widget focus
461 Widget* root_focus = mouseFocus;
462 while (root_focus != nullptr)
463 {
464 root_focus->_setRootMouseFocus(false);
465 root_focus->_riseMouseChangeRootFocus(false);
466 root_focus = root_focus->getParent();
467 }
468
469 for (int i = MouseButton::Button0; i < MouseButton::MAX; ++i)
470 {
471 if (mMouseCapture[i])
472 {
473 mMouseCapture[i] = false;
474 if (nullptr != mouseFocus)
475 {
476 mouseFocus->_riseMouseButtonReleased(
477 mLastPressed[i].left,
478 mLastPressed[i].top,
479 MouseButton::Enum(i));
480 }
481 }
482 }
483
484 if (nullptr != mouseFocus)
485 {
486 mouseFocus->_riseMouseLostFocus(nullptr);
487 }
488
489 if (mouseFocus != mWidgetMouseFocus)
490 eventChangeMouseFocus(mWidgetMouseFocus);
491 }
492
493 // remove the given widget from all possible places
494 void InputManager::_unlinkWidget(Widget* _widget)
495 {
496 if (nullptr == _widget)
497 return;
498
499 if (mWidgetMouseFocus == _widget)
500 _resetMouseFocusWidget();
501
502 if (_widget == mWidgetKeyFocus)
503 resetKeyFocusWidget();
504
505 // manually reset to not change focus
506 for (VectorWidgetPtr::iterator iter = mVectorModalRootWidget.begin(); iter != mVectorModalRootWidget.end();
507 ++iter)
508 {
509 if (*iter == _widget)
510 {
511 mVectorModalRootWidget.erase(iter);
512 break;
513 }
514 }
515 }
516
517 void InputManager::addWidgetModal(Widget* _widget)
518 {
519 if (nullptr == _widget)
520 return;
521 MYGUI_ASSERT(nullptr == _widget->getParent(), "Modal widget must be root");
522
523 _resetMouseFocusWidget();
524 removeWidgetModal(_widget);
525 mVectorModalRootWidget.push_back(_widget);
526
527 setKeyFocusWidget(_widget);
528 LayerManager::getInstance().upLayerItem(_widget);
529 }
530
531 void InputManager::removeWidgetModal(Widget* _widget)
532 {
533 resetKeyFocusWidget(_widget);
534 _resetMouseFocusWidget();
535
536 for (VectorWidgetPtr::iterator iter = mVectorModalRootWidget.begin(); iter != mVectorModalRootWidget.end();
537 ++iter)
538 {
539 if (*iter == _widget)
540 {
541 mVectorModalRootWidget.erase(iter);
542 break;
543 }
544 }
545 // if there are still modal widgets, focus and raise them
546 if (!mVectorModalRootWidget.empty())
547 {
548 setKeyFocusWidget(mVectorModalRootWidget.back());
549 LayerManager::getInstance().upLayerItem(mVectorModalRootWidget.back());
550 }
551 }
552
553 void InputManager::storeKey(KeyCode _key, Char _text)
554 {
555 mHoldKey = KeyCode::None;
556 mHoldChar = 0;
557
558 if (!isFocusKey())
559 return;
560 if ((_key == KeyCode::LeftShift) || (_key == KeyCode::RightShift) || (_key == KeyCode::LeftControl) ||
561 (_key == KeyCode::RightControl) || (_key == KeyCode::LeftAlt) || (_key == KeyCode::RightAlt))
562 return;
563
564 mFirstPressKey = true;
565 mHoldKey = _key;
566 mHoldChar = _text;
567 mTimerKey = 0.0f;
568 }
569
570 void InputManager::resetKey()
571 {
572 mHoldKey = KeyCode::None;
573 mHoldChar = 0;
574 }
575
576 void InputManager::frameEntered(float _frame)
577 {
578 mTimerDoubleClick += _frame;
579
580 if (mHoldKey == KeyCode::None)
581 return;
582
583 if (!isFocusKey())
584 {
585 mHoldKey = KeyCode::None;
586 mHoldChar = 0;
587 return;
588 }
589
590 mTimerKey += _frame;
591
592 if (mFirstPressKey)
593 {
594 if (mTimerKey > INPUT_DELAY_FIRST_KEY)
595 {
596 mFirstPressKey = false;
597 mTimerKey = 0.0f;
598 }
599 }
600 else
601 {
602 if (mTimerKey > INPUT_INTERVAL_KEY)
603 {
604 while (mTimerKey > INPUT_INTERVAL_KEY)
605 mTimerKey -= INPUT_INTERVAL_KEY;
606 mWidgetKeyFocus->_riseKeyButtonPressed(mHoldKey, mHoldChar);
607 // focus can be dropped in onKeyButtonPressed
608 if (isFocusKey())
609 mWidgetKeyFocus->_riseKeyButtonReleased(mHoldKey);
610 }
611 }
612 }
613
614 void InputManager::resetKeyFocusWidget(Widget* _widget)
615 {
616 if (mWidgetKeyFocus == _widget)
617 setKeyFocusWidget(nullptr);
618 }
619
620 IntPoint InputManager::getMousePositionByLayer() const
621 {
622 if (mLayerMouseFocus != nullptr)
623 return mLayerMouseFocus->getPosition(mMousePosition.left, mMousePosition.top);
624 return mMousePosition;
625 }
626
627 IntPoint InputManager::getMousePositionForWidget(const Widget* _widget) const
628 {
629 return unrotateForWidget(_widget, getMousePositionByLayer());
630 }
631
632 bool InputManager::isFocusMouse() const
633 {
634 return mWidgetMouseFocus != nullptr;
635 }
636
637 bool InputManager::isFocusKey() const
638 {
639 return mWidgetKeyFocus != nullptr;
640 }
641
642 bool InputManager::isCaptureMouse() const
643 {
644 for (int i = MouseButton::Button0; i < MouseButton::MAX; ++i)
645 {
646 if (mMouseCapture[i])
647 return true;
648 }
649 return false;
650 }
651
652 void InputManager::resetKeyFocusWidget()
653 {
654 setKeyFocusWidget(nullptr);
655 }
656
657 Widget* InputManager::getMouseFocusWidget() const
658 {
659 return mWidgetMouseFocus;
660 }
661
662 Widget* InputManager::getKeyFocusWidget() const
663 {
664 return mWidgetKeyFocus;
665 }
666
667 const IntPoint& InputManager::getLastPressedPosition(MouseButton _id) const
668 {
669 if (_id != MouseButton::None && _id != MouseButton::MAX)
670 {
671 return mLastPressed[_id.getValue()];
672 }
673 return Constants::getZeroIntPoint();
674 }
675
676 IntPoint InputManager::getLastPressedPositionForWidget(MouseButton _id, const Widget* _widget) const
677 {
678 return unrotateForWidget(_widget, getLastPressedPosition(_id));
679 }
680
681 const IntPoint& InputManager::getMousePosition() const
682 {
683 return mMousePosition;
684 }
685
686 bool InputManager::isModalAny() const
687 {
688 return !mVectorModalRootWidget.empty();
689 }
690
691 bool InputManager::isControlPressed() const
692 {
693 return mIsControlPressed;
694 }
695
696 bool InputManager::isShiftPressed() const
697 {
698 return mIsShiftPressed;
699 }
700
701 bool InputManager::isAltPressed() const
702 {
703 return mIsAltPressed;
704 }
705
706 bool InputManager::isMetaPressed() const
707 {
708 return mIsMetaPressed;
709 }
710
711 void InputManager::resetMouseCaptureWidget()
712 {
713 for (int i = MouseButton::Button0; i < MouseButton::MAX; ++i)
714 {
715 mMouseCapture[i] = false;
716 }
717 }
718
719 void InputManager::unlinkWidget(Widget* _widget)
720 {
721 _unlinkWidget(_widget);
722 }
723
724} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_LOG(level, text)
types::TPoint< int > IntPoint
Definition MyGUI_Types.h:26
types::TPoint< float > FloatPoint
Definition MyGUI_Types.h:27
delegates::DelegateFunction< Args... > * newDelegate(void(*_func)(Args... args))
MYGUI_SINGLETON_DEFINITION(ClipboardManager)
const float INPUT_INTERVAL_KEY
const float INPUT_DELAY_FIRST_KEY