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