Stacked Widget: added support for selecting more than one layer, but fewer than all layers

This commit is contained in:
Charles Dang 2017-03-28 02:17:00 +11:00
parent 396b33c8a9
commit 1c1caffe52
2 changed files with 50 additions and 6 deletions

View file

@ -21,6 +21,7 @@
#include "gui/widgets/settings.hpp"
#include "gui/widgets/generator.hpp"
#include "gettext.hpp"
#include "utils/general.hpp"
#include "utils/functional.hpp"
@ -117,14 +118,13 @@ void stacked_widget::set_self_active(const bool /*active*/)
/* DO NOTHING */
}
void stacked_widget::select_layer(const int layer)
void stacked_widget::select_layer_impl(std::function<bool(unsigned int i)> display_condition)
{
const unsigned int num_layers = generator_->get_item_count();
selected_layer_ = std::max(-1, std::min<int>(layer, num_layers - 1));
const unsigned int num_layers = get_layer_count();
// Deselect all layers except the chosen one.
// Deselect all layers except the chosen ones.
for(unsigned int i = 0; i < num_layers; ++i) {
const bool selected = i == static_cast<unsigned int>(selected_layer_);
const bool selected = display_condition(i);
/* Selecting a previously selected item will deselect it, regardless of the what is passed to
* select_item. This causes issues if this function is called when all layers are visible (for
@ -140,7 +140,7 @@ void stacked_widget::select_layer(const int layer)
}
}
// If we already have our chosen layer, exit.
// If we already have our chosen layers, exit.
if(selected_layer_ >= 0) {
return;
}
@ -155,6 +155,35 @@ void stacked_widget::select_layer(const int layer)
}
}
void stacked_widget::update_selected_layer_index(const int i)
{
selected_layer_ = util::clamp<int>(i, -1, get_layer_count() - 1);
}
void stacked_widget::select_layer(const int layer)
{
update_selected_layer_index(layer);
select_layer_impl([this](unsigned int i)
{
return i == static_cast<unsigned int>(selected_layer_);
});
}
void stacked_widget::select_layers(const boost::dynamic_bitset<>& mask)
{
assert(mask.size() == get_layer_count());
select_layer_impl([&](unsigned int i)
{
if(mask[i]) {
update_selected_layer_index(i);
}
return mask[i];
});
}
unsigned int stacked_widget::get_layer_count() const
{
return generator_->get_item_count();

View file

@ -20,6 +20,8 @@
#include "gui/core/widget_definition.hpp"
#include "gui/core/window_builder.hpp"
#include <boost/dynamic_bitset.hpp>
namespace gui2
{
@ -57,6 +59,9 @@ public:
* The current layer number will be -1 if all layers are currently visible.
* In this case, only the topmost (highest-numbered) layer will receive
* events.
*
* If more than one but not all layers are visible, this will be the number of
* the last one made visible.
*/
int current_layer() const { return selected_layer_; }
@ -68,6 +73,11 @@ public:
*/
void select_layer(const int layer);
/**
* Selects and displays multiple layers based on the state of the provided dynamic_bitset.
*/
void select_layers(const boost::dynamic_bitset<>& mask);
/**
* Gets the total number of layers.
*/
@ -109,6 +119,11 @@ private:
*/
int selected_layer_;
void update_selected_layer_index(const int i);
/** Internal implementation detail for selecting layers. */
void select_layer_impl(std::function<bool(unsigned int i)> display_condition);
/** See @ref styled_widget::get_control_type. */
virtual const std::string& get_control_type() const override;