MCPcopy Create free account
hub / github.com/Siv3D/OpenSiv3D / ListBoxAt

Function ListBoxAt

Siv3D/src/Siv3D/SimpleGUI/SivSimpleGUI.cpp:1826–2051  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1824 }
1825
1826 bool ListBoxAt(ListBoxState& state, const Vec2& center, const double width, const double height, const bool enabled)
1827 {
1828 const Optional<size_t> oldState = state.selectedItemIndex;
1829
1830 const Font& font = SimpleGUI::GetFont();
1831 const int32 maxLines = (static_cast<int32>(height) - (ListBoxFrameThickness * 2)) / font.height();
1832 const bool hasScrollBar = (maxLines < static_cast<int32>(state.items.size()));
1833 const RectF rect{ Arg::center(center), Max(width, 40.0), Max<double>(height, font.height() + (ListBoxFrameThickness * 2)) };
1834 const Vec2 pos = rect.pos;
1835
1836 const int32 fontHeight = font.height();
1837 const int32 lines = Min(static_cast<int32>(state.items.size()), maxLines);
1838 const int32 itemWidth = static_cast<int32>(rect.w - (ListBoxFrameThickness * 2) - (hasScrollBar ? ScrollBarWidth : 0));
1839
1840 if (state.items.isEmpty())
1841 {
1842 state.selectedItemIndex.reset();
1843 }
1844 else if (state.items.size() <= state.selectedItemIndex)
1845 {
1846 state.selectedItemIndex = (state.items.size() - 1);
1847 }
1848
1849 // update
1850 {
1851 if (enabled)
1852 {
1853 for (int32 i = 0; i < lines; ++i)
1854 {
1855 const size_t itemIndex = (state.scroll + i);
1856 const RectF itemRect(pos.x + ListBoxFrameThickness, pos.y + ListBoxFrameThickness + (fontHeight * i), itemWidth, fontHeight);
1857
1858 if (itemRect.leftClicked())
1859 {
1860 state.active = true;
1861 state.selectedItemIndex = itemIndex;
1862 }
1863 }
1864 }
1865
1866 if ((not enabled) || (MouseL.down() && not rect.mouseOver()))
1867 {
1868 state.active = false;
1869 }
1870
1871 if (state.active && state.selectedItemIndex)
1872 {
1873 if ((0 < *state.selectedItemIndex) &&
1874 (KeyUp.down() || ((SecondsF{ 0.33 } < KeyUp.pressedDuration()) && (SecondsF{ 0.06 } < state.upPressStopwatch))))
1875 {
1876 --(*state.selectedItemIndex);
1877 state.upPressStopwatch.restart();
1878
1879 if (hasScrollBar && (static_cast<int32>(*state.selectedItemIndex) < state.scroll))
1880 {
1881 state.scroll = Max(state.scroll - 1, 0);
1882 }
1883 }

Callers 1

ListBoxFunction · 0.85

Calls 15

centerFunction · 0.85
WheelFunction · 0.85
PosFunction · 0.85
GetTextColorFunction · 0.85
centerMethod · 0.80
MaxFunction · 0.50
MinFunction · 0.50
ClampFunction · 0.50
ColorFClass · 0.50
heightMethod · 0.45
sizeMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected