1 /*!
2 	@file
3 	@author		Albert Semenov
4 	@date		10/2008
5 */
6 #include "Precompiled.h"
7 #include "InformationWindow.h"
8 
9 namespace demo
10 {
11 
InformationWindow(MyGUI::Widget * _parent)12 	InformationWindow::InformationWindow(MyGUI::Widget* _parent) :
13 		BaseLayout("InformationWindow.layout", _parent),
14 		mFocus(nullptr)
15 	{
16 		assignWidget(mInfo, "Info");
17 
18 		MyGUI::Gui::getInstance().eventFrameStart += MyGUI::newDelegate(this, &InformationWindow::notifyFrameStart);
19 
20 		if (_parent)
21 		{
22 			const MyGUI::IntCoord& coord = _parent->getClientCoord();
23 			const MyGUI::IntSize& size = mMainWidget->getSize();
24 			mMainWidget->setPosition(MyGUI::IntPoint(getRand(0, coord.width - size.width), getRand(0, coord.height - size.height)));
25 		}
26 	}
27 
~InformationWindow()28 	InformationWindow::~InformationWindow()
29 	{
30 		MyGUI::Gui::getInstance().eventFrameStart -= MyGUI::newDelegate(this, &InformationWindow::notifyFrameStart);
31 	}
32 
getRand(int _min,int _max)33 	int InformationWindow::getRand(int _min, int _max)
34 	{
35 		if (_max < _min)
36 			std::swap(_max, _min);
37 		int range = _max - _min;
38 		if (range == 0)
39 			return 0;
40 		int result = ::rand() % range;
41 		if (result < 0)
42 			result = -result;
43 		return _min + result;
44 	}
45 
notifyFrameStart(float _time)46 	void InformationWindow::notifyFrameStart(float _time)
47 	{
48 		MyGUI::Widget* focus = MyGUI::InputManager::getInstance().getMouseFocusWidget();
49 		if (focus == mFocus)
50 			return;
51 		mFocus = focus;
52 
53 		const std::string tag = "Info";
54 		std::string info;
55 		if (mFocus != nullptr)
56 		{
57 			while (true)
58 			{
59 				if (focus->isUserString(tag))
60 				{
61 					info = focus->getUserString(tag);
62 					break;
63 				}
64 				if (!focus->getParent())
65 				{
66 					break;
67 				}
68 				focus = focus->getParent();
69 			}
70 		}
71 
72 		mInfo->setCaption(info);
73 	}
74 
75 } // namespace demo
76