MezzanineEngine 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dropdownlist.cpp
1 // © Copyright 2010 - 2014 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 #ifndef _uidropdownlist_cpp
41 #define _uidropdownlist_cpp
42 
43 #include "UI/uimanager.h"
44 #include "UI/dropdownlist.h"
45 #include "UI/listbox.h"
46 #include "UI/button.h"
47 #include "UI/scrollbar.h"
48 #include "UI/screen.h"
49 #include "Input/inputmanager.h"
50 #include "Input/mouse.h"
51 
52 namespace Mezzanine
53 {
54  namespace UI
55  {
56  /*DropDownList::DropDownList(const String& name, const Rect& RendRect, const Real& LineHeight, const UI::ScrollbarStyle& ScrollStyle, Screen* parent)
57  : Widget(name,parent),
58  ToggleActivated(false)
59  {
60  const Vector2& WinDim = ParentScreen->GetViewportDimensions();
61  std::pair<Whole,Real> Result;
62  if(Rect.Relative) Result = Manager->SuggestGlyphIndex((Whole)(LineHeight * WinDim.Y),ParentScreen->GetPrimaryAtlas());
63  else Result = Manager->SuggestGlyphIndex((Whole)LineHeight,ParentScreen->GetPrimaryAtlas());
64 
65  ConstructDropDownList(RendRect,Result.first,ScrollStyle);
66 
67  if(1.f != Result.second)
68  {
69  Selection->SetTextScale(Result.second);
70  SelectionList->SetTemplateTextScale(Result.second);
71  }
72  }
73 
74  DropDownList::DropDownList(const String& name, const Rect& RendRect, const Whole& Glyph, const UI::ScrollbarStyle& ScrollStyle, Screen* parent)
75  : Widget(name,parent),
76  ToggleActivated(false)
77  {
78  ConstructDropDownList(RendRect,Glyph,ScrollStyle);
79  }
80 
81  DropDownList::~DropDownList()
82  {
83  ParentScreen->DestroyBasicRenderable(Selection);
84  ParentScreen->DestroyWidget(ListToggle);
85  ParentScreen->DestroyWidget(SelectionList);
86  }
87 
88  void DropDownList::ConstructDropDownList(const Rect& RendRect, const Whole& Glyph, const UI::ScrollbarStyle& ScrollStyle)
89  {
90  Type = Widget::W_DropDownList;
91  Rect SelectionRect, ListToggleRect, SelectionListRect;
92  Real ScrollbarWidth;
93  const Vector2& WinDim = ParentScreen->GetViewportDimensions();
94  if(RendRect.Relative)
95  {
96  RelPosition = RendRect.Position;
97  RelSize = RendRect.Size;
98 
99  SelectionRect.Position = RendRect.Position;
100  SelectionRect.Size.X = RendRect.Size.X - ((RendRect.Size.Y * WinDim.Y) / WinDim.X);
101  SelectionRect.Size.Y = RendRect.Size.Y;
102  SelectionRect.Relative = RendRect.Relative;
103 
104  ListToggleRect.Position.X = RendRect.Position.X + SelectionRect.Size.X;
105  ListToggleRect.Position.Y = RendRect.Position.Y;
106  ListToggleRect.Size.X = (RendRect.Size.Y * WinDim.Y) / WinDim.X;
107  ListToggleRect.Size.Y = RendRect.Size.Y;
108  ListToggleRect.Relative = RendRect.Relative;
109 
110  ScrollbarWidth = (RendRect.Size.Y * WinDim.Y) / WinDim.X;
111  }else{
112  RelPosition = RendRect.Position / WinDim;
113  RelSize = RendRect.Size / WinDim;
114 
115  SelectionRect.Position = RendRect.Position;
116  SelectionRect.Size.X = RendRect.Size.X - Rect.Size.Y;
117  SelectionRect.Size.Y = RendRect.Size.Y;
118  SelectionRect.Relative = RendRect.Relative;
119 
120  ListToggleRect.Position.X = RendRect.Position.X + SelectionRect.Size.X;
121  ListToggleRect.Position.Y = RendRect.Position.Y;
122  ListToggleRect.Size.X = RendRect.Size.Y;
123  ListToggleRect.Size.Y = RendRect.Size.Y;
124  ListToggleRect.Relative = RendRect.Relative;
125 
126  ScrollbarWidth = RendRect.Size.Y;
127  }
128  SelectionListRect.Position.X = RendRect.Position.X;
129  SelectionListRect.Position.Y = RendRect.Position.Y + SelectionRect.Size.Y;
130  SelectionListRect.Size = RendRect.Size;
131  SelectionListRect.Relative = RendRect.Relative;
132 
133  Selection = ParentScreen->CreateCaption(Name+"Select",SelectionRect,Glyph,"");
134  ListToggle = ParentScreen->CreateButton(Name+"Toggle",ListToggleRect);
135  SelectionList = ParentScreen->CreateListBox(Name+"List",SelectionListRect,ScrollStyle);
136 
137  AddSubRenderable(0,Selection);
138  AddSubRenderable(1,ListToggle);
139  AddSubRenderable(2,SelectionList);
140 
141  SelectionList->SetTemplateGlyphIndex(Glyph);
142  SelectionList->SetTemplateRenderPriority(UI::RP_High);
143  SelectionList->Hide();
144  SelectionList->GetBoxBack()->SetRenderPriority(UI::RP_High);
145  SelectionList->GetVertScroll()->GetScrollBack()->SetRenderPriority(UI::RP_High);
146  SelectionList->GetVertScroll()->GetScroller()->SetRenderPriority(UI::RP_High);
147  SelectionList->GetVertScroll()->GetUpLeftButton()->SetRenderPriority(UI::RP_High);
148  SelectionList->GetVertScroll()->GetDownRightButton()->SetRenderPriority(UI::RP_High);
149  }
150 
151  void DropDownList::UpdateImpl(bool Force)
152  {
153  Input::ButtonState State = InputManager::GetSingletonPtr()->GetSystemMouse()->GetButtonState(1);
154  if(HoveredSubWidget == ListToggle)
155  {
156  if(Input::BUTTON_PRESSING == State)
157  {
158  if(!ToggleActivated)
159  {
160  SelectionList->Show();
161  ToggleActivated = true;
162  }else{
163  SelectionList->Hide();
164  ToggleActivated = false;
165  }
166  }
167  }
168  else if(HoveredSubWidget == SelectionList)
169  {
170  if(Input::BUTTON_PRESSING == State && !SelectionList->GetHoveredSubWidget())
171  {
172  Selection->SetText(SelectionList->GetSelected()->GetText());
173  SelectionList->Hide();
174  ToggleActivated = false;
175  }
176  }
177  }
178 
179  void DropDownList::SetVisibleImpl(bool visible)
180  {
181  Selection->SetVisible(visible);
182  ListToggle->SetVisible(visible);
183  if(ToggleActivated)
184  SelectionList->SetVisible(visible);
185  }
186 
187  bool DropDownList::CheckMouseHoverImpl()
188  {
189  if(Selection->CheckMouseHover())
190  {
191  HoveredSubWidget = NULL;
192  return true;
193  }
194  else if(ListToggle->CheckMouseHover())
195  {
196  HoveredSubWidget = ListToggle;
197  return true;
198  }
199  else if(SelectionList->CheckMouseHover())
200  {
201  HoveredSubWidget = SelectionList;
202  return true;
203  }
204  return false;
205  }
206 
207  void DropDownList::SetSelection(Caption* ToBeSelected)
208  {
209  Selection->SetText(ToBeSelected->GetText());
210  SelectionList->SetSelected(ToBeSelected);
211  }
212 
213  void DropDownList::SetSelection(const String& ToBeSelected)
214  {
215  SetSelection(SelectionList->GetSelection(ToBeSelected));
216  }
217 
218  void DropDownList::SetPosition(const Vector2& Position)
219  {
220  RelPosition = Position;
221  const Vector2& WinDim = ParentScreen->GetViewportDimensions();
222  Selection->SetPosition(Position);
223  ListToggle->SetPosition(Position + Vector2(RelSize.X - ((RelSize.Y * WinDim.Y) / WinDim.X),0));
224  SelectionList->SetPosition(Position + Vector2(0,RelSize.Y));
225  }
226 
227  void DropDownList::SetActualPosition(const Vector2& Position)
228  {
229  RelPosition = Position / ParentScreen->GetViewportDimensions();
230  Vector2 CurrSize = GetActualSize();
231  Selection->SetActualPosition(Position);
232  ListToggle->SetActualPosition(Position + Vector2(CurrSize.X - CurrSize.Y,0));
233  SelectionList->SetActualPosition(Position + Vector2(0,CurrSize.Y));
234  }
235 
236  void DropDownList::SetSize(const Vector2& Size)
237  {
238  RelSize = Size;
239  const Vector2& WinDim = ParentScreen->GetViewportDimensions();
240  Selection->SetSize(Vector2(Size.X - ((Size.Y * WinDim.Y) / WinDim.X),Size.Y));
241  ListToggle->SetSize(Vector2((Size.Y * WinDim.Y) / WinDim.X,Size.Y));
242  SelectionList->SetTemplateSize(Size);
243  SetPosition(RelPosition);
244  }
245 
246  void DropDownList::SetActualSize(const Vector2& Size)
247  {
248  RelSize = Size / ParentScreen->GetViewportDimensions();
249  Selection->SetActualSize(Vector2(Size.X - Size.Y,Size.Y));
250  ListToggle->SetActualSize(Vector2(Size.Y,Size.Y));
251  SelectionList->SetTemplateSize(Size,false);
252  SetActualPosition(GetActualPosition());
253  }
254 
255  void DropDownList::UpdateDimensions()
256  {
257  WidgetResult Result = ViewportUpdateTool::UpdateWidget(this);
258  RelPosition = Result.first / ViewportUpdateTool::GetNewSize();
259  RelSize = Result.second / ViewportUpdateTool::GetNewSize();
260  Selection->UpdateDimensions();
261  ListToggle->UpdateDimensions();
262  SelectionList->UpdateDimensions();
263  SetPosition(RelPosition);
264  }
265 
266  Caption* DropDownList::GetSelection()
267  {
268  return Selection;
269  }
270 
271  Button* DropDownList::GetListToggle()
272  {
273  return ListToggle;
274  }
275 
276  UI::ListBox* DropDownList::GetSelectionList()
277  {
278  return SelectionList;
279  }//*/
280  }//UI
281 }//Mezzanine
282 
283 #endif