6#include "sdl_buttons.hpp"
8static const Uint32 hpadding = 10;
10SdlButtonList::~SdlButtonList() =
default;
12bool SdlButtonList::populate(SDL_Renderer* renderer,
const std::vector<std::string>& labels,
13 const std::vector<int>& ids, Sint32 total_width, Sint32 offsetY,
14 Sint32 width, Sint32 height)
19 assert(labels.size() == ids.size());
23 ids.size() * (WINPR_ASSERTING_INT_CAST(uint32_t, width) + hpadding) + hpadding;
25 WINPR_ASSERTING_INT_CAST(uint32_t, total_width) -
26 std::min<size_t>(WINPR_ASSERTING_INT_CAST(uint32_t, total_width), button_width);
27 for (
size_t x = 0; x < ids.size(); x++)
29 const size_t curOffsetX = offsetX + x * (
static_cast<size_t>(width) + hpadding);
30 const SDL_Rect rect = {
static_cast<int>(curOffsetX), offsetY, width, height };
31 _list.emplace_back(renderer, labels[x], ids[x], rect);
36SdlButton* SdlButtonList::get_selected(
const SDL_MouseButtonEvent& button)
38 const Sint32 x = button.x;
39 const Sint32 y = button.y;
41 return get_selected(x, y);
44SdlButton* SdlButtonList::get_selected(Sint32 x, Sint32 y)
46 for (
auto& btn : _list)
49 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
55bool SdlButtonList::set_highlight_next(
bool reset)
58 _highlighted =
nullptr;
61 auto next = _highlight_index++;
62 _highlight_index %= _list.size();
63 auto& element = _list[next];
64 _highlighted = &element;
69bool SdlButtonList::set_highlight(
size_t index)
71 if (index >= _list.size())
73 _highlighted =
nullptr;
76 auto& element = _list[index];
77 _highlighted = &element;
78 _highlight_index = ++index % _list.size();
82bool SdlButtonList::set_mouseover(Sint32 x, Sint32 y)
84 _mouseover = get_selected(x, y);
85 return _mouseover !=
nullptr;
88void SdlButtonList::clear()
92 _highlighted =
nullptr;
96bool SdlButtonList::update(SDL_Renderer* renderer)
100 for (
auto& btn : _list)
102 if (!btn.update(renderer))
107 _highlighted->highlight(renderer);
110 _mouseover->mouseover(renderer);