4 #include "sdl_buttons.hpp"
6 static const Uint32 hpadding = 10;
8 SdlButtonList::~SdlButtonList() =
default;
10 bool SdlButtonList::populate(SDL_Renderer* renderer,
const std::vector<std::string>& labels,
11 const std::vector<int>& ids, Sint32 total_width, Sint32 offsetY,
12 Sint32 width, Sint32 height)
17 assert(labels.size() == ids.size());
20 size_t button_width = ids.size() * (width + hpadding) + hpadding;
21 size_t offsetX = total_width - std::min<size_t>(total_width, button_width);
22 for (
size_t x = 0; x < ids.size(); x++)
24 const size_t curOffsetX = offsetX + x * (
static_cast<size_t>(width) + hpadding);
25 const SDL_Rect rect = {
static_cast<int>(curOffsetX), offsetY, width, height };
26 _list.emplace_back(renderer, labels[x], ids[x], rect);
31 SdlButton* SdlButtonList::get_selected(
const SDL_MouseButtonEvent& button)
33 const Sint32 x = button.x;
34 const Sint32 y = button.y;
36 return get_selected(x, y);
39 SdlButton* SdlButtonList::get_selected(Sint32 x, Sint32 y)
41 for (
auto& btn : _list)
44 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
50 bool SdlButtonList::set_highlight_next(
bool reset)
53 _highlighted =
nullptr;
56 auto next = _highlight_index++;
57 _highlight_index %= _list.size();
58 auto& element = _list[next];
59 _highlighted = &element;
64 bool SdlButtonList::set_highlight(
size_t index)
66 if (index >= _list.size())
68 _highlighted =
nullptr;
71 auto& element = _list[index];
72 _highlighted = &element;
73 _highlight_index = ++index % _list.size();
77 bool SdlButtonList::set_mouseover(Sint32 x, Sint32 y)
79 _mouseover = get_selected(x, y);
80 return _mouseover !=
nullptr;
83 void SdlButtonList::clear()
87 _highlighted =
nullptr;
91 bool SdlButtonList::update(SDL_Renderer* renderer)
95 for (
auto& btn : _list)
97 if (!btn.update(renderer))
102 _highlighted->highlight(renderer);
105 _mouseover->mouseover(renderer);