Simple GUI Library
layered_container.cpp
Go to the documentation of this file.
1/**
2 * @author Nikita Mochalov (github.com/tralf-strues)
3 * @file layered_container.cpp
4 * @date 2021-12-12
5 *
6 * @copyright Copyright (c) 2021
7 */
8
10
11using namespace Sgl;
12
13class FocusReceivedFilter : public FocusListener<LayeredContainer>
14{
15public:
17
18 virtual void onFocusReceived(FocusReceivedEvent* event) override
19 {
20 if (!getComponent()->isAutoAdjust())
21 {
22 return;
23 }
24
25 for (auto child : getComponent()->getChildren())
26 {
27 if (child->containsComponent(event->getNewFocus()))
28 {
29 getComponent()->moveToFront(child);
30 break;
31 }
32 }
33 }
34};
35
36LayeredContainer::LayeredContainer()
37{
38 getEventDispatcher()->attachFilter(FocusReceivedFilter::EVENT_TYPES, new FocusReceivedFilter(this));
39}
40
41bool LayeredContainer::isAutoAdjust() const { return m_AutoAdjust; }
42void LayeredContainer::setAutoAdjust(bool autoAdjust) { m_AutoAdjust = autoAdjust; }
43
44void LayeredContainer::moveToFront(Component* child)
45{
46 assert(child);
47
48 for (auto it = m_Children.begin(); it != m_Children.end(); ++it)
49 {
50 if (*it == child)
51 {
52 m_Children.erase(it);
53 m_Children.push_back(child);
54 break;
55 }
56 }
57}
58
59void LayeredContainer::moveToBack(Component* child)
60{
61 assert(child);
62
63 for (auto it = m_Children.begin(); it != m_Children.end(); ++it)
64 {
65 if (*it == child)
66 {
67 m_Children.erase(it);
68 m_Children.push_front(child);
69 break;
70 }
71 }
72}
73
74Component* LayeredContainer::getFront()
75{
76 return m_Children.empty() ? nullptr : m_Children.back();
77}
78
79Component* LayeredContainer::getBack()
80{
81 return m_Children.empty() ? nullptr : m_Children.front();
82}