MyGUI  3.2.1
MyGUI_ToolTipManager.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"
8 #include "MyGUI_ToolTipManager.h"
9 #include "MyGUI_Gui.h"
10 #include "MyGUI_InputManager.h"
11 #include "MyGUI_WidgetManager.h"
12 
13 namespace MyGUI
14 {
15 
16  template <> ToolTipManager* Singleton<ToolTipManager>::msInstance = nullptr;
17  template <> const char* Singleton<ToolTipManager>::mClassTypeName = "ToolTipManager";
18 
20  mDelayVisible(0.5f),
21  mOldFocusWidget(nullptr),
22  mToolTipVisible(false),
23  mCurrentTime(0),
24  mOldIndex(ITEM_NONE),
25  mNeedToolTip(false),
26  mIsInitialise(false)
27  {
28  }
29 
31  {
32  MYGUI_ASSERT(!mIsInitialise, getClassTypeName() << " initialised twice");
33  MYGUI_LOG(Info, "* Initialise: " << getClassTypeName());
34 
35  mDelayVisible = 0.5f;
36  mOldFocusWidget = nullptr;
37  mToolTipVisible = false;
38  mCurrentTime = 0;
39  mOldIndex = ITEM_NONE;
40  mNeedToolTip = false;
41 
42  Gui::getInstance().eventFrameStart += newDelegate(this, &ToolTipManager::notifyEventFrameStart);
44 
45  MYGUI_LOG(Info, getClassTypeName() << " successfully initialized");
46  mIsInitialise = true;
47  }
48 
50  {
51  MYGUI_ASSERT(mIsInitialise, getClassTypeName() << " is not initialised");
52  MYGUI_LOG(Info, "* Shutdown: " << getClassTypeName());
53 
55  Gui::getInstance().eventFrameStart -= newDelegate(this, &ToolTipManager::notifyEventFrameStart);
56 
57  MYGUI_LOG(Info, getClassTypeName() << " successfully shutdown");
58  mIsInitialise = false;
59  }
60 
61  void ToolTipManager::notifyEventFrameStart(float _time)
62  {
64  if (mOldFocusWidget != widget)
65  {
66  if (mToolTipVisible)
67  {
68  mToolTipVisible = false;
69  hideToolTip(mOldFocusWidget);
70  }
71  mOldFocusWidget = widget;
72  mNeedToolTip = false;
73 
74  if (mOldFocusWidget != nullptr)
75  {
76  mCurrentTime = 0;
78  mOldIndex = getToolTipIndex(mOldFocusWidget);
79  mNeedToolTip = isNeedToolTip(mOldFocusWidget);
80  }
81  }
82  else if (mNeedToolTip)
83  {
84  bool capture = InputManager::getInstance().isCaptureMouse();
85  if (capture)
86  {
87  if (mToolTipVisible)
88  {
89  mToolTipVisible = false;
90  hideToolTip(mOldFocusWidget);
91  }
92  }
93  else
94  {
96  if (!mToolTipVisible && point != mOldMousePoint)
97  {
98  if (mToolTipVisible)
99  {
100  mToolTipVisible = false;
101  hideToolTip(mOldFocusWidget);
102  }
103  mCurrentTime = 0;
104  mOldMousePoint = point;
105  mOldIndex = getToolTipIndex(mOldFocusWidget);
106  }
107  else
108  {
109  size_t index = getToolTipIndex(mOldFocusWidget);
110  if (mOldIndex != index)
111  {
112  if (mToolTipVisible)
113  {
114  mToolTipVisible = false;
115  hideToolTip(mOldFocusWidget);
116  }
117  mCurrentTime = 0;
118  mOldIndex = index;
119  }
120  else
121  {
122  if (!mToolTipVisible)
123  {
124  mCurrentTime += _time;
125  if (mCurrentTime >= mDelayVisible)
126  {
127  mToolTipVisible = true;
128  showToolTip(mOldFocusWidget, mOldIndex, point);
129  }
130  }
131  else if (point != mOldMousePoint)
132  {
133  moveToolTip(mOldFocusWidget, mOldIndex, point);
134  }
135  }
136  }
137  }
138  }
139  }
140 
142  {
143  if (mOldFocusWidget == _widget)
144  {
145  if (mToolTipVisible)
146  {
147  mToolTipVisible = false;
148  hideToolTip(mOldFocusWidget);
149  }
150  mOldFocusWidget = nullptr;
151  mNeedToolTip = false;
152  }
153  }
154 
155  void ToolTipManager::hideToolTip(Widget* _widget)
156  {
157  Widget* container = _widget->_getContainer();
158  if (container != nullptr)
159  container->eventToolTip(container, ToolTipInfo(ToolTipInfo::Hide));
160  else
161  _widget->eventToolTip(_widget, ToolTipInfo(ToolTipInfo::Hide));
162  }
163 
164  void ToolTipManager::showToolTip(Widget* _widget, size_t _index, const IntPoint& _point)
165  {
166  Widget* container = _widget->_getContainer();
167  if (container != nullptr)
168  container->eventToolTip(container, ToolTipInfo(ToolTipInfo::Show, _index, _point));
169  else
170  _widget->eventToolTip(_widget, ToolTipInfo(ToolTipInfo::Show, ITEM_NONE, _point));
171  }
172 
173  void ToolTipManager::moveToolTip(Widget* _widget, size_t _index, const IntPoint& _point)
174  {
175  Widget* container = _widget->_getContainer();
176  if (container != nullptr)
177  container->eventToolTip(container, ToolTipInfo(ToolTipInfo::Move, _index, _point));
178  else
179  _widget->eventToolTip(_widget, ToolTipInfo(ToolTipInfo::Move, ITEM_NONE, _point));
180  }
181 
182  bool ToolTipManager::isNeedToolTip(Widget* _widget)
183  {
184  Widget* container = _widget->_getContainer();
185  if (container != nullptr)
186  return container->getNeedToolTip();
187  return _widget->getNeedToolTip();
188  }
189 
190  size_t ToolTipManager::getToolTipIndex(Widget* _widget) const
191  {
192  Widget* container = _widget->_getContainer();
193  if (container != nullptr)
194  return container->_getItemIndex(_widget);
195  return ITEM_NONE;
196  }
197 
199  {
200  mDelayVisible = _value;
201  }
202 
204  {
205  return mDelayVisible;
206  }
207 
208 } // namespace MyGUI