BsGUIDropDownHitBox.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsGUIDropDownHitBox.h"
  4. #include "BsGUICommandEvent.h"
  5. #include "BsGUIMouseEvent.h"
  6. namespace BansheeEngine
  7. {
  8. const String& GUIDropDownHitBox::getGUITypeName()
  9. {
  10. static String name = "DropDownHitBox";
  11. return name;
  12. }
  13. GUIDropDownHitBox* GUIDropDownHitBox::create(bool captureMouseOver, bool captureMousePresses)
  14. {
  15. return new (bs_alloc<GUIDropDownHitBox>())
  16. GUIDropDownHitBox(captureMouseOver, captureMousePresses, GUIDimensions::create());
  17. }
  18. GUIDropDownHitBox* GUIDropDownHitBox::create(bool captureMouseOver, bool captureMousePresses, const GUIOptions& options)
  19. {
  20. return new (bs_alloc<GUIDropDownHitBox>())
  21. GUIDropDownHitBox(captureMouseOver, captureMousePresses, GUIDimensions::create(options));
  22. }
  23. GUIDropDownHitBox::GUIDropDownHitBox(bool captureMouseOver,
  24. bool captureMousePresses, const GUIDimensions& dimensions)
  25. :GUIElementContainer(dimensions), mCaptureMouseOver(captureMouseOver),
  26. mCaptureMousePresses(captureMousePresses)
  27. {
  28. }
  29. void GUIDropDownHitBox::setBounds(const Rect2I& bounds)
  30. {
  31. mBounds.clear();
  32. mBounds.push_back(bounds);
  33. updateClippedBounds();
  34. }
  35. void GUIDropDownHitBox::setBounds(const Vector<Rect2I>& bounds)
  36. {
  37. mBounds = bounds;
  38. updateClippedBounds();
  39. }
  40. void GUIDropDownHitBox::updateClippedBounds()
  41. {
  42. mClippedBounds = Rect2I();
  43. if (mBounds.size() > 0)
  44. {
  45. mClippedBounds = mBounds[0];
  46. for (UINT32 i = 1; i < (UINT32)mBounds.size(); i++)
  47. mClippedBounds.encapsulate(mBounds[i]);
  48. }
  49. }
  50. bool GUIDropDownHitBox::_commandEvent(const GUICommandEvent& ev)
  51. {
  52. bool processed = GUIElementContainer::_commandEvent(ev);
  53. if(ev.getType() == GUICommandEventType::FocusGained)
  54. {
  55. if(!onFocusGained.empty())
  56. onFocusGained();
  57. return true;
  58. }
  59. else if(ev.getType() == GUICommandEventType::FocusLost)
  60. {
  61. if(!onFocusLost.empty())
  62. onFocusLost();
  63. return true;
  64. }
  65. return processed;
  66. }
  67. bool GUIDropDownHitBox::_mouseEvent(const GUIMouseEvent& ev)
  68. {
  69. bool processed = GUIElementContainer::_mouseEvent(ev);
  70. if(mCaptureMouseOver)
  71. {
  72. if (ev.getType() == GUIMouseEventType::MouseOver)
  73. {
  74. return true;
  75. }
  76. else if (ev.getType() == GUIMouseEventType::MouseOut)
  77. {
  78. return true;
  79. }
  80. else if (ev.getType() == GUIMouseEventType::MouseMove)
  81. {
  82. return true;
  83. }
  84. }
  85. if (mCaptureMousePresses)
  86. {
  87. if (ev.getType() == GUIMouseEventType::MouseUp)
  88. {
  89. return true;
  90. }
  91. else if (ev.getType() == GUIMouseEventType::MouseDown)
  92. {
  93. return true;
  94. }
  95. else if (ev.getType() == GUIMouseEventType::MouseDoubleClick)
  96. {
  97. return true;
  98. }
  99. }
  100. return processed;
  101. }
  102. bool GUIDropDownHitBox::_isInBounds(const Vector2I position) const
  103. {
  104. for(auto& bound : mBounds)
  105. {
  106. if(bound.contains(position))
  107. return true;
  108. }
  109. return false;
  110. }
  111. };