4#include "sdl_buttons.hpp"
6static const Uint32 hpadding = 10;
8SdlButtonList::~SdlButtonList() =
default;
10bool 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() * (
static_cast<size_t>(width) + hpadding) + hpadding;
21 size_t offsetX =
static_cast<size_t>(total_width) -
22 std::min<size_t>(
static_cast<size_t>(total_width), button_width);
23 for (
size_t x = 0; x < ids.size(); x++)
25 const size_t curOffsetX = offsetX + x * (
static_cast<size_t>(width) + hpadding);
26 const SDL_FRect rect = {
static_cast<float>(curOffsetX),
static_cast<float>(offsetY),
27 static_cast<float>(width),
static_cast<float>(height) };
28 _list.emplace_back(renderer, labels[x], ids[x], rect);
33SdlButton* SdlButtonList::get_selected(
const SDL_MouseButtonEvent& button)
35 const auto x = button.x;
36 const auto y = button.y;
38 return get_selected(x, y);
41SdlButton* SdlButtonList::get_selected(
float x,
float y)
43 for (
auto& btn : _list)
46 if ((x >= r.x) && (x <= r.x + r.w) && (y >= r.y) && (y <= r.y + r.h))
52bool SdlButtonList::set_highlight_next(
bool reset)
55 _highlighted =
nullptr;
58 auto next = _highlight_index++;
59 _highlight_index %= _list.size();
60 auto& element = _list[next];
61 _highlighted = &element;
66bool SdlButtonList::set_highlight(
size_t index)
68 if (index >= _list.size())
70 _highlighted =
nullptr;
73 auto& element = _list[index];
74 _highlighted = &element;
75 _highlight_index = ++index % _list.size();
79bool SdlButtonList::set_mouseover(
float x,
float y)
81 _mouseover = get_selected(x, y);
82 return _mouseover !=
nullptr;
85void SdlButtonList::clear()
89 _highlighted =
nullptr;
93bool SdlButtonList::update(SDL_Renderer* renderer)
97 for (
auto& btn : _list)
99 if (!btn.update(renderer))
104 _highlighted->highlight(renderer);
107 _mouseover->mouseover(renderer);