1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "GUI/BsGUIDropDownHitBox.h"
4#include "GUI/BsGUICommandEvent.h"
5#include "GUI/BsGUIMouseEvent.h"
6
7namespace bs
8{
9 const String& GUIDropDownHitBox::getGUITypeName()
10 {
11 static String name = "DropDownHitBox";
12 return name;
13 }
14
15 GUIDropDownHitBox* GUIDropDownHitBox::create(bool captureMouseOver, bool captureMousePresses)
16 {
17 return new (bs_alloc<GUIDropDownHitBox>())
18 GUIDropDownHitBox(captureMouseOver, captureMousePresses, GUIDimensions::create());
19 }
20
21 GUIDropDownHitBox* GUIDropDownHitBox::create(bool captureMouseOver, bool captureMousePresses, const GUIOptions& options)
22 {
23 return new (bs_alloc<GUIDropDownHitBox>())
24 GUIDropDownHitBox(captureMouseOver, captureMousePresses, GUIDimensions::create(options));
25 }
26
27 GUIDropDownHitBox::GUIDropDownHitBox(bool captureMouseOver,
28 bool captureMousePresses, const GUIDimensions& dimensions)
29 :GUIElementContainer(dimensions), mCaptureMouseOver(captureMouseOver),
30 mCaptureMousePresses(captureMousePresses)
31 {
32 mOptionFlags.set(GUIElementOption::ClickThrough);
33 }
34
35 void GUIDropDownHitBox::setBounds(const Rect2I& bounds)
36 {
37 mBounds.clear();
38 mBounds.push_back(bounds);
39
40 updateClippedBounds();
41 }
42
43 void GUIDropDownHitBox::setBounds(const Vector<Rect2I>& bounds)
44 {
45 mBounds = bounds;
46
47 updateClippedBounds();
48 }
49
50 void GUIDropDownHitBox::updateClippedBounds()
51 {
52 mClippedBounds = Rect2I();
53
54 if (mBounds.size() > 0)
55 {
56 mClippedBounds = mBounds[0];
57
58 for (UINT32 i = 1; i < (UINT32)mBounds.size(); i++)
59 mClippedBounds.encapsulate(mBounds[i]);
60 }
61 }
62
63 bool GUIDropDownHitBox::_commandEvent(const GUICommandEvent& ev)
64 {
65 bool processed = GUIElementContainer::_commandEvent(ev);
66
67 if(ev.getType() == GUICommandEventType::FocusGained)
68 {
69 if(!onFocusGained.empty())
70 onFocusGained();
71
72 return false;
73 }
74 else if(ev.getType() == GUICommandEventType::FocusLost)
75 {
76 if(!onFocusLost.empty())
77 onFocusLost();
78
79 return false;
80 }
81
82 return processed;
83 }
84
85 bool GUIDropDownHitBox::_mouseEvent(const GUIMouseEvent& ev)
86 {
87 bool processed = GUIElementContainer::_mouseEvent(ev);
88
89 if(mCaptureMouseOver)
90 {
91 if (ev.getType() == GUIMouseEventType::MouseOver)
92 {
93 return true;
94 }
95 else if (ev.getType() == GUIMouseEventType::MouseOut)
96 {
97 return true;
98 }
99 else if (ev.getType() == GUIMouseEventType::MouseMove)
100 {
101 return true;
102 }
103 }
104
105 if (mCaptureMousePresses)
106 {
107 if (ev.getType() == GUIMouseEventType::MouseUp)
108 {
109 return true;
110 }
111 else if (ev.getType() == GUIMouseEventType::MouseDown)
112 {
113 return true;
114 }
115 else if (ev.getType() == GUIMouseEventType::MouseDoubleClick)
116 {
117 return true;
118 }
119 }
120
121 return processed;
122 }
123
124 bool GUIDropDownHitBox::_isInBounds(const Vector2I position) const
125 {
126 for(auto& bound : mBounds)
127 {
128 if(bound.contains(position))
129 return true;
130 }
131
132 return false;
133 }
134};