40 #ifndef _uilistbox_cpp
41 #define _uilistbox_cpp
43 #include "UI/listbox.h"
44 #include "UI/uimanager.h"
45 #include "UI/screen.h"
46 #include "UI/scrollbar.h"
47 #include "Input/inputmanager.h"
48 #include "Input/metacode.h"
49 #include "Input/mouse.h"
79 Widget(RendName,RendRect,Parent)
150 Type = Widget::W_ListBox;
152 // Set some sane template defaults
153 SelectionTemplate.BackgroundColour = ColourValue(1.0,1.0,1.0,1.0);
154 SelectionTemplate.TextColour = ColourValue(0.0,0.0,0.0,1.0);
155 SelectionTemplate.TextScale = 1.0;
156 SelectionTemplate.CursorOffset = 0.0;
157 SelectionTemplate.HorizontalAlign = UI::Txt_Middle;
158 SelectionTemplate.VerticalAlign = UI::Txt_Center;
159 SelectionTemplate.Priority = UI::RP_Medium;
161 Rect ScrollRect, BoxRect;
162 const Vector2& WinDim = ParentScreen->GetViewportDimensions();
163 if(RendRect.Relative)
165 RelPosition = RendRect.Position;
166 RelSize = RendRect.Size;
168 SelectionTemplate.Size = RendRect.Size * WinDim;
170 ScrollRect.Position = Vector2((RelPosition.X + RelSize.X) - ((RendRect.Size.Y * WinDim.Y) / WinDim.X),RelPosition.Y);
171 ScrollRect.Size = Vector2((RendRect.Size.Y * WinDim.Y) / WinDim.X,RelSize.Y * MaxDisplay);
172 ScrollRect.Relative = RendRect.Relative;
174 RelPosition = RendRect.Position / WinDim;
175 RelSize = RendRect.Size / WinDim;
177 SelectionTemplate.Size = RendRect.Size;
179 ScrollRect.Position = Vector2((RendRect.Position.X + RendRect.Size.X) - RendRect.Size.Y,RendRect.Position.Y);
180 ScrollRect.Size = Vector2(RendRect.Size.Y,RendRect.Size.Y * MaxDisplay);
181 ScrollRect.Relative = RendRect.Relative;
183 BoxRect.Position = RendRect.Position;
184 BoxRect.Size.X = RendRect.Size.X;
185 BoxRect.Size.Y = RendRect.Size.Y * MaxDisplay;
186 BoxRect.Relative = RendRect.Relative;
188 BoxBack = ParentScreen->CreateRectangle(BoxRect);
189 VertScroll = ParentScreen->CreateScrollbar(Name+"Scr",ScrollRect,ScrollStyle);
192 AddSubRenderable(0,BoxBack);
193 AddSubRenderable(1,VertScroll);
198 ParentScreen->DestroyBasicRenderable(BoxBack);
199 if(!Selections.empty())
201 for( std::vector<Caption*>::iterator it = Selections.begin() ; it != Selections.end() ; it++ )
203 ParentScreen->DestroyBasicRenderable( *it );
207 VisibleSelections.clear();
210 void ListBox::ScrollerSizeCheck()
212 Real ScrollerSize = (Real)MaxDisplay / (Real)Selections.size();
213 VertScroll->SetScrollerSize(ScrollerSize);
216 void ListBox::ScrollHideCheck()
228 if(Selections.size() > MaxDisplay)
234 void ListBox::SelectionSizeCheck(UI::Caption* Selection)
236 const Vector2& WinDim = ParentScreen->GetViewportDimensions();
237 Vector2 CurrSize = Selection->GetActualSize();
239 if(VertScroll->IsVisible())
240 TargetSize = Vector2(SelectionTemplate.Size.X - VertScroll->GetActualSize().X,SelectionTemplate.Size.Y);
242 TargetSize = SelectionTemplate.Size;
243 if(CurrSize != TargetSize)
245 Selection->SetActualSize(TargetSize);
249 ListBox& ListBox::SetTemplateSize(const Vector2& Size, bool Relative)
251 const Vector2& WinDim = ParentScreen->GetViewportDimensions();
254 this->SelectionTemplate.Size = Size * WinDim;
255 Vector2 NewSize = Vector2(Size.X,Size.Y * MaxDisplay);
256 SetArea(NewSize * WinDim);
258 this->SelectionTemplate.Size = Size;
259 Vector2 NewSize = Vector2(Size.X,Size.Y * MaxDisplay);
265 ListBox& ListBox::SetTemplateGlyphIndex(const Whole& Glyph)
267 this->SelectionTemplate.GlyphIndex = Glyph;
271 ListBox& ListBox::SetTemplateTextColour(const ColourValue& TextColour)
273 this->SelectionTemplate.TextColour = TextColour;
277 ListBox& ListBox::SetTemplateTextScale(const Real& Scale)
279 this->SelectionTemplate.TextScale = Scale;
283 ListBox& ListBox::SetTemplateCursorOffset(const Whole& Offset)
285 this->SelectionTemplate.CursorOffset = Offset;
289 ListBox& ListBox::SetTemplateBackgroundColour(const ColourValue& BackgroundColour)
291 this->SelectionTemplate.BackgroundColour = BackgroundColour;
295 ListBox& ListBox::SetTemplateHorizontalAlign(const UI::TextHorizontalAlign& HorAlign)
297 this->SelectionTemplate.HorizontalAlign = HorAlign;
301 ListBox& ListBox::SetTemplateVerticalAlign(const UI::TextVerticalAlign& VertAlign)
303 this->SelectionTemplate.VerticalAlign = VertAlign;
307 ListBox& ListBox::SetTemplateRenderPriority(const UI::RenderPriority& Priority)
309 this->SelectionTemplate.Priority = Priority;
313 const UI::TemplateParams& ListBox::GetTemplateInfo()
315 return this->SelectionTemplate;
318 Caption* ListBox::AddSelection(ConstString& name, ConstString &Text, ConstString& BackgroundSprite)
321 Rect SelectionRect(RelPosition,SelectionTemplate.Size / ParentScreen->GetViewportDimensions(),true);
322 Caption* Select = ParentScreen->CreateCaption(name,SelectionRect,SelectionTemplate.GlyphIndex,Text);
323 if(!BackgroundSprite.empty())
324 Select->SetBackgroundSprite(BackgroundSprite);
325 if(SelectionTemplate.BackgroundColour != ColourValue(1.0,1.0,1.0,1.0))
326 Select->SetBackgroundColour(SelectionTemplate.BackgroundColour);
327 if(SelectionTemplate.CursorOffset != 0)
328 Select->SetCursorOffset(SelectionTemplate.CursorOffset);
329 if(SelectionTemplate.TextScale != 1)
330 Select->SetTextScale(SelectionTemplate.TextScale);
331 Select->SetTextColour(SelectionTemplate.TextColour);
332 Select->HorizontallyAlign(SelectionTemplate.HorizontalAlign);
333 Select->VerticallyAlign(SelectionTemplate.VerticalAlign);
334 Select->SetRenderPriority(SelectionTemplate.Priority);
336 Selections.push_back(Select);
337 AddSubRenderable(SelectionsAdded,Select);
343 Caption* ListBox::GetSelection(ConstString &Name)
345 for ( std::vector<Caption*>::iterator it = Selections.begin() ; it != Selections.end() ; it++ )
347 if ( Name == (*it)->GetName() )
349 UI::Caption* button = (*it);
356 void ListBox::DestroySelection(Caption* ToBeDestroyed)
358 for ( std::vector<Caption*>::iterator it = Selections.begin() ; it != Selections.end() ; it++ )
360 if ( ToBeDestroyed == (*it) )
362 Selections.erase(it);
366 for ( RenderableIterator it = SubRenderables.begin() ; it != SubRenderables.end() ; ++it )
368 if( (*it) == ToBeDestroyed )
370 SubRenderables.erase(it);
374 ParentScreen->DestroyBasicRenderable(ToBeDestroyed);