WindowsManager.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "WindowsManager.h"
  23. #include "Device.h"
  24. #include <GL/glew.h> // FIXME
  25. #include "Types.h"
  26. #include "Themes.h"
  27. #include "LunaTheme.h"
  28. #include "Log.h"
  29. #include "Scene.h"
  30. #include "Renderer.h"
  31. #include "RenderWindow.h"
  32. #include "InputManager.h"
  33. namespace Crown
  34. {
  35. WindowsManager::WindowsManager(Scene* creator):
  36. SceneNode(creator, 0, Vec3(0, 0, 0), Angles(0, 0, 0), true),
  37. mMouseCapturedWidget(NULL), mWidgetUnderMouse(NULL), mWidgetMouseDown(NULL),
  38. mInputFocusWidget(NULL), mModalWindow(NULL), mMousePosition(-10000, -10000)
  39. {
  40. Device* mDevice = GetDevice();
  41. mDevice->GetInputManager()->RegisterMouseListener(this);
  42. mDevice->GetInputManager()->RegisterKeyboardListener(this);
  43. Themes::LoadTheme(new LunaTheme());
  44. //Register the default actions
  45. RegisterAction("QuitApplication", CreateDelegate(this, &WindowsManager::QuitApplicationAction));
  46. RegisterAction("CopyProperty", CreateDelegate(this, &WindowsManager::CopyProperty));
  47. if (creator)
  48. creator->GetRootSceneNode()->AddChild(this);
  49. }
  50. WindowsManager::~WindowsManager()
  51. {
  52. for (int i=0; i<mWindows.GetSize(); i++)
  53. {
  54. delete mWindows[i];
  55. }
  56. ActionDictionary::Enumerator e = mRegisteredActions.getBegin();
  57. while (e.next())
  58. {
  59. delete e.current().value;
  60. }
  61. }
  62. bool WindowsManager::DoModalWindow(Window* window)
  63. {
  64. if (mModalWindow != NULL)
  65. return false;
  66. mModalWindow = window;
  67. //Perform a layout of the window to obtain actual size and place it at the center of the screen
  68. RenderWindow* renderWindow = GetDevice()->GetMainWindow();
  69. window->_PerformLayout();
  70. uint rwWidth, rwHeight;
  71. renderWindow->GetMetrics(rwWidth, rwHeight);
  72. window->SetDesiredPosition((int)Math::Floor(((float)rwWidth - window->GetSize().x)/2.0f),
  73. (int)Math::Floor(((float)rwHeight - window->GetSize().y)/2.0f));
  74. return true;
  75. }
  76. void WindowsManager::EndModal(Window* window)
  77. {
  78. if (mModalWindow == window)
  79. {
  80. mModalWindow = NULL;
  81. }
  82. }
  83. void WindowsManager::RegisterAction(const Str& name, ActionDelegate* slot)
  84. {
  85. if (mRegisteredActions.Contains(name))
  86. {
  87. Log::E("WindowsManager already has an Action registered with name '" + name + "'");
  88. }
  89. else
  90. {
  91. mRegisteredActions[name] = slot;
  92. }
  93. }
  94. ActionDelegate* WindowsManager::GetRegisteredAction(const Str& name)
  95. {
  96. if (mRegisteredActions.Contains(name))
  97. {
  98. //Return a duplicate because the caller will usually pass the delegate to an event, which transfers ownership
  99. return (ActionDelegate*)mRegisteredActions[name]->Duplicate();
  100. }
  101. return NULL;
  102. }
  103. void WindowsManager::ButtonPressed(const MouseEvent& event)
  104. {
  105. if (mWidgetUnderMouse != NULL)
  106. {
  107. mWidgetMouseDown = mWidgetUnderMouse;
  108. BringWindowToFront(mWidgetMouseDown->GetWindow());
  109. MouseDownHelper(mWidgetMouseDown);
  110. }
  111. else if (mMouseCapturedWidget != NULL)
  112. {
  113. BringWindowToFront(mMouseCapturedWidget->GetWindow());
  114. MouseDownHelper(mMouseCapturedWidget);
  115. }
  116. }
  117. void WindowsManager::ButtonReleased(const MouseEvent& event)
  118. {
  119. if (mWidgetUnderMouse != NULL)
  120. {
  121. bool isClick = mWidgetUnderMouse == mWidgetMouseDown || (mMouseCapturedWidget != NULL && mWidgetUnderMouse->IsChildOf(mMouseCapturedWidget));
  122. MouseUpHelper(mWidgetUnderMouse, isClick);
  123. mWidgetMouseDown = NULL;
  124. }
  125. else if (mMouseCapturedWidget != NULL)
  126. {
  127. MouseUpHelper(mMouseCapturedWidget, false);
  128. }
  129. }
  130. void WindowsManager::CursorMoved(const MouseEvent& event)
  131. {
  132. Point2 newMousePosition(event.x, event.y);
  133. if (mMousePosition.x == -10000)
  134. {
  135. mMousePosition = newMousePosition;
  136. }
  137. //Check for the widget under mouse.
  138. Point2 point(newMousePosition);
  139. Widget* wdg = NULL;
  140. Widget* wdgToMove = NULL;
  141. if (mMouseCapturedWidget != NULL)
  142. {
  143. wdgToMove = mMouseCapturedWidget;
  144. Widget* parent = mMouseCapturedWidget->GetParent();
  145. if (parent != NULL)
  146. {
  147. point -= parent->GetScreenPosition();
  148. }
  149. if (mMouseCapturedWidget->IsPointInside(point))
  150. {
  151. wdg = mMouseCapturedWidget;
  152. }
  153. }
  154. else
  155. {
  156. for (int i=mWindows.GetSize()-1; i>=0; i--)
  157. {
  158. Window* w = mWindows[i];
  159. if (w->IsMouseSensible() && w->IsPointInside(point))
  160. {
  161. point -= w->GetPosition() + w->GetTranslation();
  162. wdg = FindWidgetAt(w, point);
  163. break;
  164. }
  165. }
  166. if (mModalWindow != NULL && wdg != NULL && wdg->GetWindow() != mModalWindow)
  167. {
  168. return;
  169. }
  170. }
  171. if (wdg == NULL)
  172. {
  173. //Mouse is not inside any window
  174. if (mWidgetUnderMouse != NULL)
  175. {
  176. MouseOutHelper(mWidgetUnderMouse);
  177. mWidgetUnderMouse = NULL;
  178. }
  179. mWidgetMouseDown = NULL;
  180. }
  181. else if (mWidgetUnderMouse != wdg)
  182. {
  183. if (mWidgetUnderMouse != NULL)
  184. {
  185. MouseOutHelper(mWidgetUnderMouse);
  186. }
  187. mWidgetUnderMouse = wdg;
  188. MouseInHelper(mWidgetUnderMouse);
  189. wdgToMove = wdg;
  190. }
  191. if (wdgToMove)
  192. {
  193. point -= wdgToMove->GetPosition();
  194. MouseMoveHelper(wdgToMove, newMousePosition - mMousePosition);
  195. }
  196. mMousePosition = newMousePosition;
  197. }
  198. void WindowsManager::KeyPressed(const KeyboardEvent& event)
  199. {
  200. if (event.key == KC_F1)
  201. {
  202. Widget::SetDrawDebugBorder(true);
  203. }
  204. if (mInputFocusWidget != NULL)
  205. {
  206. KeyDownHelper(mInputFocusWidget, event.key);
  207. }
  208. }
  209. void WindowsManager::KeyReleased(const KeyboardEvent& event)
  210. {
  211. if (event.key == KC_F1)
  212. {
  213. Widget::SetDrawDebugBorder(false);
  214. }
  215. if (mInputFocusWidget != NULL)
  216. {
  217. KeyUpHelper(mInputFocusWidget, event.key);
  218. }
  219. }
  220. void WindowsManager::TextInput(const KeyboardEvent& event)
  221. {
  222. if (mInputFocusWidget != NULL)
  223. {
  224. TextInputHelper(mInputFocusWidget, event.text);
  225. }
  226. }
  227. Widget* WindowsManager::FindWidgetAt(Widget* parent, Point2& point) const
  228. {
  229. Widget* widgetInside = parent;
  230. for (int i=parent->mChildren.GetSize()-1; i>=0; i--)
  231. {
  232. Widget* w = parent->mChildren[i];
  233. if (w->IsPointInside(point))
  234. {
  235. point -= w->GetPosition() + w->GetTranslation();
  236. widgetInside = FindWidgetAt(w, point);
  237. break;
  238. }
  239. }
  240. if (!widgetInside->IsMouseSensible())
  241. {
  242. return parent;
  243. }
  244. return widgetInside;
  245. }
  246. //Propagate the event in the widgets hierarchy. delegate contains the pointer to function and the
  247. //parameters that need to be passed to it.
  248. void WindowsManager::LaunchEvent(Widget* targetWidget, IDelegate1<void, bool>* onEventHelper, WindowingEventArgs* args)
  249. {
  250. if (!onEventHelper || !targetWidget || !args)
  251. return;
  252. List<Widget*> ancestors;
  253. Widget* w = targetWidget;
  254. while (w != NULL)
  255. {
  256. ancestors.Append(w);
  257. w = w->GetParent();
  258. }
  259. //Tunneling
  260. args->StopPropagation(false);
  261. for(int i = ancestors.GetSize()-1; i >= 0; i--)
  262. {
  263. onEventHelper->SetCalledObject(ancestors[i]);
  264. onEventHelper->Invoke(true);
  265. if (args->IsPropagationStopped())
  266. break;
  267. }
  268. //Bubbling
  269. args->StopPropagation(false);
  270. for(int i = 0; i < ancestors.GetSize(); i++)
  271. {
  272. onEventHelper->SetCalledObject(ancestors[i]);
  273. onEventHelper->Invoke(false);
  274. if (args->IsPropagationStopped())
  275. break;
  276. }
  277. }
  278. /*
  279. * Widget event helpers
  280. */
  281. void WindowsManager::MouseInHelper(Widget* targetWidget)
  282. {
  283. MouseEventArgs args(targetWidget);
  284. Delegate2<Widget, void, bool, MouseEventArgs*> d(NULL, &Widget::OnMouseInHelper, true, &args);
  285. LaunchEvent(targetWidget, &d, &args);
  286. }
  287. void WindowsManager::MouseOutHelper(Widget* targetWidget)
  288. {
  289. MouseEventArgs args(targetWidget);
  290. Delegate2<Widget, void, bool, MouseEventArgs*> d(NULL, &Widget::OnMouseOutHelper, true, &args);
  291. LaunchEvent(targetWidget, &d, &args);
  292. }
  293. void WindowsManager::MouseMoveHelper(Widget* targetWidget, const Point2& delta)
  294. {
  295. MouseMoveEventArgs args(targetWidget, delta);
  296. Delegate2<Widget, void, bool, MouseMoveEventArgs*> d(NULL, &Widget::OnMouseMoveHelper, true, &args);
  297. LaunchEvent(targetWidget, &d, &args);
  298. }
  299. void WindowsManager::MouseUpHelper(Widget* targetWidget, bool isClick)
  300. {
  301. MouseButtonEventArgs args(targetWidget, MB_LEFT, isClick);
  302. Delegate2<Widget, void, bool, MouseButtonEventArgs*> d(NULL, &Widget::OnMouseUpHelper, true, &args);
  303. LaunchEvent(targetWidget, &d, &args);
  304. }
  305. void WindowsManager::MouseDownHelper(Widget* targetWidget)
  306. {
  307. MouseButtonEventArgs args(targetWidget, MB_LEFT, false);
  308. Delegate2<Widget, void, bool, MouseButtonEventArgs*> d(NULL, &Widget::OnMouseDownHelper, true, &args);
  309. LaunchEvent(targetWidget, &d, &args);
  310. }
  311. void WindowsManager::TextInputHelper(Widget* targetWidget, const Str& text)
  312. {
  313. TextInputEventArgs args(targetWidget, text);
  314. Delegate2<Widget, void, bool, TextInputEventArgs*> d(NULL, &Widget::OnTextInputHelper, true, &args);
  315. LaunchEvent(targetWidget, &d, &args);
  316. }
  317. void WindowsManager::GotFocusHelper(Widget* targetWidget)
  318. {
  319. WindowingEventArgs args(targetWidget);
  320. Delegate2<Widget, void, bool, WindowingEventArgs*> d(NULL, &Widget::OnGotFocusHelper, true, &args);
  321. LaunchEvent(targetWidget, &d, &args);
  322. }
  323. void WindowsManager::LostFocusHelper(Widget* targetWidget)
  324. {
  325. WindowingEventArgs args(targetWidget);
  326. Delegate2<Widget, void, bool, WindowingEventArgs*> d(NULL, &Widget::OnLostFocusHelper, true, &args);
  327. LaunchEvent(targetWidget, &d, &args);
  328. }
  329. void WindowsManager::KeyDownHelper(Widget* targetWidget, Key key)
  330. {
  331. KeyboardEventArgs args(targetWidget, key);
  332. Delegate2<Widget, void, bool, KeyboardEventArgs*> d(NULL, &Widget::OnKeyDownHelper, true, &args);
  333. LaunchEvent(targetWidget, &d, &args);
  334. }
  335. void WindowsManager::KeyUpHelper(Widget* targetWidget, Key key)
  336. {
  337. KeyboardEventArgs args(targetWidget, key);
  338. Delegate2<Widget, void, bool, KeyboardEventArgs*> d(NULL, &Widget::OnKeyUpHelper, true, &args);
  339. LaunchEvent(targetWidget, &d, &args);
  340. }
  341. void WindowsManager::SizeChangedHelper(Widget* targetWidget)
  342. {
  343. WindowingEventArgs args(targetWidget);
  344. Delegate2<Widget, void, bool, WindowingEventArgs*> d(NULL, &Widget::OnSizeChangedHelper, true, &args);
  345. LaunchEvent(targetWidget, &d, &args);
  346. }
  347. bool WindowsManager::MouseCapture(Widget* w)
  348. {
  349. if (mMouseCapturedWidget != NULL || w == NULL)
  350. {
  351. return false;
  352. }
  353. if (!w->IsMouseSensible())
  354. {
  355. return false;
  356. }
  357. mMouseCapturedWidget = w;
  358. return true;
  359. }
  360. void WindowsManager::MouseRelease()
  361. {
  362. mMouseCapturedWidget = NULL;
  363. }
  364. void WindowsManager::_SetTextInputFocusWidget(Widget* widget)
  365. {
  366. if (mInputFocusWidget != NULL)
  367. {
  368. LostFocusHelper(mInputFocusWidget);
  369. }
  370. mInputFocusWidget = widget;
  371. GotFocusHelper(mInputFocusWidget);
  372. }
  373. void WindowsManager::ReleaseWidget(Widget* w)
  374. {
  375. if (mMouseCapturedWidget != NULL && mMouseCapturedWidget->IsChildOf(w))
  376. {
  377. MouseRelease();
  378. }
  379. if (mWidgetUnderMouse != NULL && mWidgetUnderMouse->IsChildOf(w))
  380. {
  381. mWidgetUnderMouse = NULL;
  382. }
  383. if (mWidgetMouseDown != NULL && mWidgetMouseDown->IsChildOf(w))
  384. {
  385. mWidgetMouseDown = NULL;
  386. }
  387. if (mInputFocusWidget != NULL && mInputFocusWidget->IsChildOf(w))
  388. {
  389. mInputFocusWidget = NULL;
  390. }
  391. }
  392. void WindowsManager::BringWindowToFront(Window* window)
  393. {
  394. if (mWindows[mWindows.GetSize() - 1] != window)
  395. {
  396. mWindows.Remove(mWindows.Find(window));
  397. mWindows.Append(window);
  398. }
  399. }
  400. void WindowsManager::OnRegisterForRendering()
  401. {
  402. if (mCreator)
  403. mCreator->RegisterNodeForRendering(this, RP_GUI);
  404. }
  405. void WindowsManager::Render()
  406. {
  407. Renderer* renderer = GetDevice()->GetRenderer();
  408. Mat4 ortho;
  409. RenderWindow* rWindow = GetDevice()->GetMainWindow();
  410. uint rwWidth, rwHeight;
  411. rWindow->GetMetrics(rwWidth, rwHeight);
  412. ortho.BuildProjectionOrtho2dRH((float)rwWidth, (float)rwHeight, -1.0f, 1.0f);
  413. renderer->SetMatrix(MT_PROJECTION, ortho);
  414. renderer->SetMatrix(MT_VIEW, Mat4::IDENTITY);
  415. renderer->SelectMatrix(MT_MODEL);
  416. renderer->_SetScissor(true);
  417. renderer->_SetBlending(true);
  418. renderer->_SetBlendingParams(BE_FUNC_ADD, BF_SRC_ALPHA, BF_ONE_MINUS_SRC_ALPHA, Color4::BLACK);
  419. renderer->_SetLighting(false);
  420. for (int i=0; i<mWindows.GetSize(); i++)
  421. {
  422. renderer->PushMatrix();
  423. Window* wnd = mWindows[i];
  424. wnd->_PerformLayout();
  425. if (wnd == mModalWindow)
  426. {
  427. RenderWindow* renderWindow = GetDevice()->GetMainWindow();
  428. //Draw a dark-gray overlay before drawing the modal window
  429. //TODO: Adjust this behaviour by introducing windows z-ordering and foremost windows
  430. Color4 fillColor(0.0f, 0.0f, 0.0f, 0.3f);
  431. renderer->_SetScissor(false);
  432. renderer->DrawRectangle(Point2::ZERO, Point2(rwWidth, rwHeight), DM_FILL, fillColor, fillColor);
  433. renderer->_SetScissor(true);
  434. }
  435. Point2 pos = wnd->GetPosition();
  436. Point2 size = wnd->GetSize();
  437. glTranslatef((float)pos.x, (float)pos.y, 0.0f);
  438. DrawingClipInfo clipInfo;
  439. clipInfo.screenX = pos.x;
  440. clipInfo.screenY = pos.y;
  441. clipInfo.sx = pos.x;
  442. clipInfo.sy = pos.y;
  443. clipInfo.sw = size.x;
  444. clipInfo.sh = size.y;
  445. renderer->SetScissorBox(pos.x, pos.y, size.x, size.y);
  446. wnd->ApplyBinds();
  447. wnd->OnDraw(clipInfo);
  448. renderer->PopMatrix();
  449. }
  450. renderer->_SetScissor(false);
  451. }
  452. /*
  453. * Predefined Actions
  454. */
  455. void WindowsManager::QuitApplicationAction(Widget* /*src*/, List<Str>* /*args*/)
  456. {
  457. GetDevice()->StopRunning();
  458. }
  459. void WindowsManager::CopyProperty(Widget* src, List<Str>* args)
  460. {
  461. //Input parameters:
  462. //args[0]: Source of the copy
  463. //args[1]: Destination of the copy
  464. //TODO: Parse the source and destination to locate the properties to copy
  465. List<Str> srcField;
  466. List<Str> dstField;
  467. (*args)[0].Split('.', srcField);
  468. (*args)[1].Split('.', dstField);
  469. Window* window = src->GetWindow();
  470. Widget* srcWidget = window->FindChildByName(srcField[0]);
  471. Widget* dstWidget = window->FindChildByName(dstField[0]);
  472. if (srcWidget == NULL)
  473. {
  474. Log::E("CopyProperty action: Source widget '" + srcField[0] + "' could not be found");
  475. return;
  476. }
  477. if (dstWidget == NULL)
  478. {
  479. Log::E("CopyProperty action: Destination widget '" + dstField[0] + "' could not be found");
  480. return;
  481. }
  482. dstWidget->SetPropertyValue(dstField[1], srcWidget->GetPropertyValue(srcField[1]));
  483. }
  484. } //namespace Crown