guiScrollContainer.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. Minetest
  3. Copyright (C) 2020 DS
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "guiScrollContainer.h"
  17. GUIScrollContainer::GUIScrollContainer(gui::IGUIEnvironment *env,
  18. gui::IGUIElement *parent, s32 id, const core::rect<s32> &rectangle,
  19. const std::string &orientation, f32 scrollfactor) :
  20. gui::IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rectangle),
  21. m_scrollbar(nullptr), m_scrollfactor(scrollfactor)
  22. {
  23. if (orientation == "vertical")
  24. m_orientation = VERTICAL;
  25. else if (orientation == "horizontal")
  26. m_orientation = HORIZONTAL;
  27. else
  28. m_orientation = UNDEFINED;
  29. }
  30. bool GUIScrollContainer::OnEvent(const SEvent &event)
  31. {
  32. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  33. event.MouseInput.Event == EMIE_MOUSE_WHEEL &&
  34. !event.MouseInput.isLeftPressed() && m_scrollbar) {
  35. Environment->setFocus(m_scrollbar);
  36. bool retval = m_scrollbar->OnEvent(event);
  37. // a hacky fix for updating the hovering and co.
  38. IGUIElement *hovered_elem = getElementFromPoint(core::position2d<s32>(
  39. event.MouseInput.X, event.MouseInput.Y));
  40. SEvent mov_event = event;
  41. mov_event.MouseInput.Event = EMIE_MOUSE_MOVED;
  42. Environment->postEventFromUser(mov_event);
  43. if (hovered_elem)
  44. hovered_elem->OnEvent(mov_event);
  45. return retval;
  46. }
  47. return IGUIElement::OnEvent(event);
  48. }
  49. void GUIScrollContainer::draw()
  50. {
  51. if (isVisible()) {
  52. for (auto child : Children)
  53. if (child->isNotClipped() ||
  54. AbsoluteClippingRect.isRectCollided(
  55. child->getAbsolutePosition()))
  56. child->draw();
  57. }
  58. }
  59. void GUIScrollContainer::updateScrolling()
  60. {
  61. s32 pos = m_scrollbar->getPos();
  62. core::rect<s32> rect = getRelativePosition();
  63. if (m_orientation == VERTICAL)
  64. rect.UpperLeftCorner.Y = pos * m_scrollfactor;
  65. else if (m_orientation == HORIZONTAL)
  66. rect.UpperLeftCorner.X = pos * m_scrollfactor;
  67. setRelativePosition(rect);
  68. }