Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
OptionsBox API facelift
Browse files Browse the repository at this point in the history
* Close #359: OptionsBox starts with the last added item. Should start
  with the first!
* Close #360: OptionsBox set(T which) and select(T which)
* Close #361: OptionsBox: Still no get()!
* Removed arcane non-const currentRef() (actually renamed to be just an
  overload of `current()`)
  • Loading branch information
xparq committed Dec 6, 2023
1 parent e7b9b19 commit 570956c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
18 changes: 12 additions & 6 deletions include/sfw/Widgets/OptionsBox.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,20 @@ class OptionsBox: public InputWidget<OptionsBox<T>>
auto assign(const std::string& label, const T& value);

// Change the current item
auto set (size_t index);
auto set (const std::string& label);
auto set(size_t index);
auto set(const std::string& label);
auto set(const T& value); // Would call T::op==!

OptionsBox<T>* setTextColor(const sf::Color& color);
OptionsBox<T>* setFillColor(const sf::Color& color);

// -------- Actions...

// Select item (by index or label)
auto select(size_t index); // Same as update(...), just a more natural verb
auto select(const std::string& label); // Same as update(...), just a more natural verb
// (Synonym to update(...), which is pretty awkward here.)
auto select(size_t index);
auto select(const std::string& label);
auto select(const T& value);

// Actions for Up/Down, Home/End...
auto selectNext();
Expand All @@ -62,8 +65,11 @@ class OptionsBox: public InputWidget<OptionsBox<T>>
// -------- Queries...

// Accessing the selected item
const T& current() const; // For value & copy semantics (still a const ref for efficiency)
T& currentRef(); // For in-place modification
const T& get() const; // Const ref. instead of val., as T can be big!
T& get(); // Non-const, for in-place modification
// Just a synonym (for the symmetry with 'select'...):
const T& current() const { return get(); }
T& current() { return get(); }

size_t currentIndex() const;
const std::string& currentLabel() const;
Expand Down
17 changes: 14 additions & 3 deletions include/sfw/Widgets/OptionsBox.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace sfw
{

template <class T> OptionsBox<T>::OptionsBox():
m_currentIndex((size_t)-1),
m_currentIndex(0), // Used to start with the last: (size_t)-1, but #359...
m_box(Box::Input),
m_arrowLeft(Arrow(Arrow::Left)),
m_arrowRight(Arrow(Arrow::Right))
Expand Down Expand Up @@ -80,6 +80,17 @@ template <class T> auto OptionsBox<T>::set(const std::string& label)
return this;
}

template <class T> auto OptionsBox<T>::set(const T& value)
{
for (size_t i = 0; i < m_items.size(); ++i)
{
if (value == m_items[i].value)
{
return set(i);
}
}
return this;
}

template <class T> auto OptionsBox<T>::select(size_t index)
{
Expand Down Expand Up @@ -114,12 +125,12 @@ template <class T> auto OptionsBox<T>::selectLast()
}


template <class T> const T& OptionsBox<T>::current() const
template <class T> const T& OptionsBox<T>::get() const
{
return m_items[m_currentIndex].value;
}

template <class T> T& OptionsBox<T>::currentRef()
template <class T> T& OptionsBox<T>::get()
{
return m_items[m_currentIndex].value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/examples/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ int main()
right_bar->add(sfw::Slider({.range = {8, 18}}, 100), "font size slider")
//!! Would be tempting to sync the initial font size directly with
//!! the theme selector widget -- but can't: the GUI is not up yet, so:
//!! ->set(w->getWidget<OBTheme>("theme-selector")->currentRef().textSize) //! This would fail here!
//!! ->set(w->getWidget<OBTheme>("theme-selector")->current().textSize) //! This would fail here!
->set((float)themes[DEFAULT_THEME].textSize)
->setCallback([&] (auto* w){
assert(sfw::getWidget("theme-selector", w));
auto& themecfg = sfw::getWidget<OBTheme>("theme-selector", w)->currentRef();
auto& themecfg = sfw::getWidget<OBTheme>("theme-selector", w)->current();
themecfg.textSize = (size_t)w->get();
cerr << "font size: "<< themecfg.textSize << endl; //!!#196
demo.setTheme(themecfg);
Expand Down Expand Up @@ -423,7 +423,7 @@ cerr << "font size: "<< themecfg.textSize << endl; //!!#196
->set(demo.getWallpaper().getColor().a)
->setCallback([&](auto* w) {
assert(sfw::getWidget("theme-selector", w));
auto& themecfg = sfw::getWidget<OBTheme>("theme-selector", w)->currentRef();
auto& themecfg = sfw::getWidget<OBTheme>("theme-selector", w)->current();
themecfg.wallpaper.tint = {themecfg.wallpaper.tint.r,
themecfg.wallpaper.tint.g,
themecfg.wallpaper.tint.b,
Expand Down
4 changes: 2 additions & 2 deletions src/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ int main()
->set((float)themes[DEFAULT_THEME].textSize)
->setCallback([&] (auto* w){
assert(getWidget("theme-selector"));
auto& themecfg = getWidget<OBTheme>("theme-selector")->currentRef();
auto& themecfg = getWidget<OBTheme>("theme-selector")->current();
themecfg.textSize = (size_t)w->get();
cerr << "font size: "<< themecfg.textSize << endl; //!!#196
demo.setTheme(themecfg);
Expand Down Expand Up @@ -469,7 +469,7 @@ cerr << "font size: "<< themecfg.textSize << endl; //!!#196
->set(demo.getWallpaper().getColor().a)
->setCallback([&](auto* w) {
assert(getWidget("theme-selector"));
auto& themecfg = getWidget<OBTheme>("theme-selector")->currentRef();
auto& themecfg = getWidget<OBTheme>("theme-selector")->current();
themecfg.wallpaper.tint = {themecfg.wallpaper.tint.r,
themecfg.wallpaper.tint.g,
themecfg.wallpaper.tint.b,
Expand Down

0 comments on commit 570956c

Please sign in to comment.