Game Development Reference
In-Depth Information
}
Drawing is relatively straightforward, we start by drawing the background of the listbox,
next we begin drawing the items, starting at the top of the listbox.
Weusethescrollbartocalculatetheratio,apercentageofhowfardownthelistwearecur-
rently looking at. As we iterate over the list, we test if the item's index is below the visible
index, if so we flag the item as invisible and we keep iterating, otherwise we mark the item
visible and we draw it. If we draw an item we update the vertical position.
If we have more items than the size the listbox control can draw, then we will draw the
scrollbar.
void listbox::InternalDraw()
{
auto& textureView = *m_core->GetWhiteTexture()->GetView();
auto backRect = m_rectangle;
backRect.Top() = m_rectangle.Top();
backRect.Height() = m_font->GetLineSpacing() * (m_items.size()/2);
m_spriteBatch->Draw(textureView, backRect, nullptr, m_backgroundColor);
math::vector2 itemsPosition = m_rectangle.Position();
const float ratio = m_scrollBar.Ratio();
size_t cursor = ratio * (m_items.size() - 2 );
size_t i = 0;
for ( auto it : m_items )
{
if ( i < cursor )
{
it->Visible() = false;
++i;
continue;
}
it->Visible() = true;
it->Draw(itemsPosition, m_font, m_spriteBatch);
itemsPosition.y() += m_font->GetLineSpacing();
if ( itemsPosition.y() >= backRect.Bottom() )
break;
++i;
}
if ( m_items.size() * m_font->GetLineSpacing() > m_rectangle.Height() )
{
m_scrollBar.Draw();
}
}
Search WWH ::




Custom Search