From ca5ac9bc9abef367159a80a4b9b65af2a2acbd1b Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Fri, 25 Aug 2017 12:19:11 +1100 Subject: [PATCH] GUI2/Generator: formatting cleanup --- src/gui/widgets/generator.cpp | 294 +++++++++++--------------- src/gui/widgets/generator_private.hpp | 197 ++++++++--------- 2 files changed, 213 insertions(+), 278 deletions(-) diff --git a/src/gui/widgets/generator.cpp b/src/gui/widgets/generator.cpp index 06ce5947d86a3..f1e189f1ebb2d 100644 --- a/src/gui/widgets/generator.cpp +++ b/src/gui/widgets/generator.cpp @@ -23,15 +23,12 @@ namespace gui2 { - namespace policy { - /***** ***** ***** ***** Minimum selection ***** ***** ***** *****/ namespace minimum_selection { - void one_item::set_item_shown(const unsigned index, const bool show) { if(show && get_selected_item_count() == 0) { @@ -62,6 +59,7 @@ bool one_item::deselect_item(const unsigned index) do_deselect_item(index); return true; } + return false; } @@ -73,10 +71,10 @@ void one_item::delete_item(const unsigned index) do_deselect_item(index); if(get_selected_item_count() == 0) { - // Are there items left? const unsigned item_count = get_item_count(); const unsigned visible_index = get_ordered_index(index); + if(item_count > 1) { // Is the last item deselected? if(visible_index == item_count - 1) { @@ -104,8 +102,8 @@ void no_item::set_item_shown(const unsigned index, const bool show) namespace placement { - -horizontal_list::horizontal_list() : placed_(false) +horizontal_list::horizontal_list() + : placed_(false) { } @@ -123,10 +121,9 @@ point horizontal_list::calculate_best_size() const { // The best size is the sum of the widths and the greatest height. point result(0, 0); - for(size_t i = 0; i < get_item_count(); ++i) { + for(size_t i = 0; i < get_item_count(); ++i) { if(!get_item_shown(i)) { - continue; } @@ -153,16 +150,16 @@ void horizontal_list::place(const point& origin, const point& size) */ point current_origin = origin; - for(size_t i = 0; i < get_item_count(); ++i) { + for(size_t i = 0; i < get_item_count(); ++i) { if(!get_item_shown(get_item_at_ordered(i))) { - continue; } grid& grid = item_ordered(i); point best_size = grid.get_best_size(); assert(best_size.y <= size.y); + // FIXME should we look at grow factors??? best_size.y = size.y; @@ -182,14 +179,13 @@ void horizontal_list::set_origin(const point& origin) { point current_origin = origin; for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } grid& grid = item_ordered(i); grid.set_origin(current_origin); + current_origin.x += grid.get_width(); } } @@ -203,21 +199,17 @@ void horizontal_list::set_visible_rectangle(const SDL_Rect& rectangle) * placement functions. */ for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item_ordered(i); grid.set_visible_rectangle(rectangle); } } -widget* horizontal_list::find_at(const point& coordinate, - const bool must_be_active) +widget* horizontal_list::find_at(const point& coordinate, const bool must_be_active) { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -227,18 +219,16 @@ widget* horizontal_list::find_at(const point& coordinate, return widget; } } + return nullptr; } -const widget* horizontal_list::find_at(const point& coordinate, - const bool must_be_active) const +const widget* horizontal_list::find_at(const point& coordinate, const bool must_be_active) const { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -248,6 +238,7 @@ const widget* horizontal_list::find_at(const point& coordinate, return widget; } } + return nullptr; } @@ -266,6 +257,7 @@ void horizontal_list::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handl break; } } + return; } @@ -273,9 +265,7 @@ void horizontal_list::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handl handled = true; for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -289,8 +279,7 @@ void horizontal_list::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handl } } -void horizontal_list::handle_key_right_arrow(SDL_Keymod /*modifier*/, - bool& handled) +void horizontal_list::handle_key_right_arrow(SDL_Keymod /*modifier*/, bool& handled) { if(get_item_count() == 0) { return; @@ -312,9 +301,7 @@ void horizontal_list::handle_key_right_arrow(SDL_Keymod /*modifier*/, handled = true; for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -328,7 +315,8 @@ void horizontal_list::handle_key_right_arrow(SDL_Keymod /*modifier*/, } } -vertical_list::vertical_list() : placed_(false) +vertical_list::vertical_list() + : placed_(false) { } @@ -347,9 +335,7 @@ point vertical_list::calculate_best_size() const // The best size is the sum of the heights and the greatest width. point result(0, 0); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -377,15 +363,14 @@ void vertical_list::place(const point& origin, const point& size) point current_origin = origin; for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } grid& grid = item_ordered(i); point best_size = grid.get_best_size(); assert(best_size.x <= size.x); + // FIXME should we look at grow factors??? best_size.x = size.x; @@ -405,14 +390,13 @@ void vertical_list::set_origin(const point& origin) { point current_origin = origin; for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } grid& grid = item_ordered(i); grid.set_origin(current_origin); + current_origin.y += grid.get_height(); } } @@ -426,25 +410,20 @@ void vertical_list::set_visible_rectangle(const SDL_Rect& rectangle) * placement functions. */ for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.set_visible_rectangle(rectangle); } } -widget* vertical_list::find_at(const point& coordinate, - const bool must_be_active) +widget* vertical_list::find_at(const point& coordinate, const bool must_be_active) { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } - widget* widget = item(i).find_at(coordinate, must_be_active); if(widget) { @@ -454,15 +433,12 @@ widget* vertical_list::find_at(const point& coordinate, return nullptr; } -const widget* vertical_list::find_at(const point& coordinate, - const bool must_be_active) const +const widget* vertical_list::find_at(const point& coordinate, const bool must_be_active) const { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -497,9 +473,7 @@ void vertical_list::handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) handled = true; for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -535,9 +509,7 @@ void vertical_list::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled handled = true; for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -551,7 +523,8 @@ void vertical_list::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled } } -table::table() : placed_(false)//, n_cols_(2) +table::table() + : placed_(false) //, n_cols_(2) { } @@ -567,59 +540,85 @@ void table::create_item(const unsigned /*index*/) point table::calculate_best_size() const { - // The best size is the one that minimizes aspect ratio of the enclosing rect - // We first calculate the best size of each item, - // then find the number of rows that minimizes the aspect ratio - // We try a number of columns from 1 up to sqrt(visible_items) + 2 + /* The best size is that which minimizes the aspect ratio of the enclosing rect. + * We first calculate the best size of each item, then find the number of rows + * that minimizes the aspect ratio. We try a number of columns from 1 up to + * sqrt(visible_items) + 2. + * + * @todo these calculations need rethinking since the grid layout doesn't work + * properly as of now. + * + * - vultraz, 2017-08-25 + */ + size_t n_items = get_item_count(); size_t max_cols = sqrt(n_items) + 2; + std::vector item_sizes; for(size_t i = 0; i < n_items; i++) { if(get_item_shown(i)) { item_sizes.push_back(item(i).get_best_size()); } } + if(item_sizes.empty()) { return point(); } + std::vector best_sizes(1); - best_sizes[0] = std::accumulate(item_sizes.begin(), item_sizes.end(), point(), [](point a, point b) { - return point(std::max(a.x, b.x), a.y + b.y); - }); - int max_xtra = std::min_element(item_sizes.begin(), item_sizes.end(), [](point a, point b) { - return a.x < b.x; - })->x / 2; + + best_sizes[0] = std::accumulate(item_sizes.begin(), item_sizes.end(), point(), + [](point a, point b) { return point(std::max(a.x, b.x), a.y + b.y); } + ); + + int max_xtra = std::min_element(item_sizes.begin(), item_sizes.end(), + [](point a, point b) { return a.x < b.x; } + )->x / 2; + for(size_t cells_in_1st_row = 2; cells_in_1st_row <= max_cols; cells_in_1st_row++) { - int row_min_width = std::accumulate(item_sizes.begin(), item_sizes.begin() + cells_in_1st_row, 0, [](int a, point b) { - return a + b.x; - }); + int row_min_width = std::accumulate(item_sizes.begin(), item_sizes.begin() + cells_in_1st_row, 0, + [](int a, point b) { return a + b.x; } + ); + int row_max_width = row_min_width + max_xtra; int row = 0; + point row_size, total_size; + for(size_t n = 0; n < item_sizes.size(); n++) { if(row_size.x + item_sizes[n].x > row_max_width) { // Start new row row++; + total_size.y += row_size.y; + if(total_size.x < row_size.x) { total_size.x = row_size.x; } + row_size = point(); } + row_size.x += item_sizes[n].x; + if(row_size.y < item_sizes[n].y) { row_size.y = item_sizes[n].y; } } + total_size.y += row_size.y; + if(total_size.x < row_size.x) { total_size.x = row_size.x; } + best_sizes.push_back(total_size); } return *std::min_element(best_sizes.begin(), best_sizes.end(), [](point p1, point p2) { - return std::max(p1.x, p1.y) / std::min(p1.x, p1.y) < std::max(p2.x, p2.y) / std::min(p2.x, p2.y); + return + std::max(p1.x, p1.y) / std::min(p1.x, p1.y) < + std::max(p2.x, p2.y) / std::min(p2.x, p2.y); }); } @@ -637,9 +636,7 @@ void table::place(const point& origin, const point& size) point current_origin = origin; int row_height = 0; for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -676,9 +673,7 @@ void table::set_origin(const point& origin) point current_origin = origin; size_t row_height = 0; for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -707,21 +702,17 @@ void table::set_visible_rectangle(const SDL_Rect& rectangle) * placement functions. */ for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.set_visible_rectangle(rectangle); } } -widget* table::find_at(const point& coordinate, - const bool must_be_active) +widget* table::find_at(const point& coordinate, const bool must_be_active) { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -734,15 +725,12 @@ widget* table::find_at(const point& coordinate, return nullptr; } -const widget* table::find_at(const point& coordinate, - const bool must_be_active) const +const widget* table::find_at(const point& coordinate, const bool must_be_active) const { assert(get_window()); for(size_t i = 0; i < get_item_count(); ++i) { - if(!get_item_shown(i)) { - continue; } @@ -752,6 +740,7 @@ const widget* table::find_at(const point& coordinate, return widget; } } + return nullptr; } @@ -770,6 +759,7 @@ void table::handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) break; } } + return; } @@ -777,9 +767,7 @@ void table::handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& handled) handled = true; for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -808,6 +796,7 @@ void table::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) break; } } + return; } @@ -815,9 +804,7 @@ void table::handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& handled) handled = true; for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -846,6 +833,7 @@ void table::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handled) break; } } + return; } @@ -853,9 +841,7 @@ void table::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handled) handled = true; for(int i = get_ordered_index(get_selected_item()) - 1; i >= 0; --i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -869,8 +855,7 @@ void table::handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& handled) } } -void table::handle_key_right_arrow(SDL_Keymod /*modifier*/, - bool& handled) +void table::handle_key_right_arrow(SDL_Keymod /*modifier*/, bool& handled) { if(get_item_count() == 0) { return; @@ -885,6 +870,7 @@ void table::handle_key_right_arrow(SDL_Keymod /*modifier*/, break; } } + return; } @@ -892,9 +878,7 @@ void table::handle_key_right_arrow(SDL_Keymod /*modifier*/, handled = true; for(size_t i = get_ordered_index(get_selected_item()) + 1; i < get_item_count(); ++i) { - if(!get_item_shown(get_item_at_ordered(i))) { - continue; } @@ -911,7 +895,6 @@ void table::handle_key_right_arrow(SDL_Keymod /*modifier*/, void independent::request_reduce_width(const unsigned maximum_width) { for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.request_reduce_width(maximum_width); } @@ -920,7 +903,6 @@ void independent::request_reduce_width(const unsigned maximum_width) void independent::request_reduce_height(const unsigned maximum_height) { for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.request_reduce_height(maximum_height); } @@ -933,8 +915,8 @@ point independent::calculate_best_size() const * height. */ point result(0, 0); - for(size_t i = 0; i < get_item_count(); ++i) { + for(size_t i = 0; i < get_item_count(); ++i) { const grid& grid = item(i); const point best_size = grid.get_best_size(); @@ -954,7 +936,6 @@ point independent::calculate_best_size() const void independent::place(const point& origin, const point& size) { for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.place(origin, size); } @@ -969,14 +950,12 @@ void independent::set_origin(const point& origin) * and what the consequences are. */ for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.set_origin(origin); } } -widget* independent::find_at(const point& coordinate, - const bool must_be_active) +widget* independent::find_at(const point& coordinate, const bool must_be_active) { assert(get_window()); @@ -989,8 +968,7 @@ widget* independent::find_at(const point& coordinate, return grid.find_at(coordinate, must_be_active); } -const widget* independent::find_at(const point& coordinate, - const bool must_be_active) const +const widget* independent::find_at(const point& coordinate, const bool must_be_active) const { assert(get_window()); @@ -1012,20 +990,20 @@ widget* independent::find(const std::string& id, const bool must_be_active) } } } + return nullptr; } -const widget* independent::find(const std::string& id, - const bool must_be_active) const +const widget* independent::find(const std::string& id, const bool must_be_active) const { for(size_t i = 0; i < get_item_count(); ++i) { if(is_selected(i)) { if(const widget* widget = item(i).find(id, must_be_active)) { - return widget; } } } + return nullptr; } @@ -1038,7 +1016,6 @@ void independent::set_visible_rectangle(const SDL_Rect& rectangle) * and what the consequences are. */ for(size_t i = 0; i < get_item_count(); ++i) { - grid& grid = item(i); grid.set_visible_rectangle(rectangle); } @@ -1050,7 +1027,6 @@ void independent::set_visible_rectangle(const SDL_Rect& rectangle) namespace select_action { - void selection::select(grid& grid, const bool select) { selectable_item* selectable = dynamic_cast(grid.get_widget(0, 0)); @@ -1059,10 +1035,9 @@ void selection::select(grid& grid, const bool select) selectable->set_value(select); } -void -selection::init(grid* g, - const std::map& data, - const std::function& callback) +void selection::init(grid* g, + const std::map& data, + const std::function& callback) { for(unsigned row = 0; row < g->get_rows(); ++row) { for(unsigned col = 0; col < g->get_cols(); ++col) { @@ -1076,8 +1051,7 @@ selection::init(grid* g, if(btn) { connect_signal_notify_modified(*btn, std::bind(callback, _1)); - std::map::const_iterator itor - = data.find(btn->id()); + std::map::const_iterator itor = data.find(btn->id()); if(itor == data.end()) { itor = data.find(""); @@ -1099,26 +1073,22 @@ selection::init(grid* g, } void show::init(grid* grid, - const std::map& data, - const std::function& callback) + const std::map& data, + const std::function& callback) { assert(!callback); - for(const auto & item : data) - { + for(const auto& item : data) { if(item.first.empty()) { for(unsigned row = 0; row < grid->get_rows(); ++row) { for(unsigned col = 0; col < grid->get_cols(); ++col) { - if(styled_widget* control - = dynamic_cast(grid->get_widget(row, col))) { - + if(styled_widget* control = dynamic_cast(grid->get_widget(row, col))) { control->set_members(item.second); } } } } else { - styled_widget* control - = dynamic_cast(grid->find(item.first, false)); + styled_widget* control = dynamic_cast(grid->find(item.first, false)); if(control) { control->set_members(item.second); } @@ -1135,80 +1105,66 @@ void show::init(grid* grid, #ifdef GENERATE_PLACEMENT static_assert(false, "GUI2/Generator: GENERATE_PLACEMENT already defined!"); #else -#define GENERATE_PLACEMENT \ - switch(placement) { \ - case generator_base::horizontal_list: \ - result = new generator; \ - break; \ - case generator_base::vertical_list: \ - result = new generator; \ - break; \ - case generator_base::table: \ - result = new generator; \ - break; \ - case generator_base::independent: \ - result = new generator; \ - break; \ - default: \ - assert(false); \ +#define GENERATE_PLACEMENT \ + switch(placement) { \ + case generator_base::horizontal_list: \ + result = new generator; \ + break; \ + case generator_base::vertical_list: \ + result = new generator; \ + break; \ + case generator_base::table: \ + result = new generator; \ + break; \ + case generator_base::independent: \ + result = new generator; \ + break; \ + default: \ + assert(false); \ } #endif #ifdef GENERATE_SELECT static_assert(false, "GUI2/Generator: GENERATE_SELECT already defined!"); #else -#define GENERATE_SELECT \ - if(select) { \ - typedef policy::select_action::selection select_action; \ - GENERATE_PLACEMENT \ - } else { \ - typedef policy::select_action::show select_action; \ - GENERATE_PLACEMENT \ +#define GENERATE_SELECT \ + if(select) { \ + typedef policy::select_action::selection select_action; \ + GENERATE_PLACEMENT \ + } else { \ + typedef policy::select_action::show select_action; \ + GENERATE_PLACEMENT \ } #endif #ifdef GENERATE_MAXIMUM static_assert(false, "GUI2/Generator: GENERATE_MAXIMUM already defined!"); #else -#define GENERATE_MAXIMUM \ - if(has_maximum) { \ - typedef policy::maximum_selection::one_item maximum; \ - GENERATE_SELECT \ - } else { \ - typedef policy::maximum_selection::many_items maximum; \ - GENERATE_SELECT \ +#define GENERATE_MAXIMUM \ + if(has_maximum) { \ + typedef policy::maximum_selection::one_item maximum; \ + GENERATE_SELECT \ + } else { \ + typedef policy::maximum_selection::many_items maximum; \ + GENERATE_SELECT \ } #endif #ifdef GENERATE_BODY static_assert(false, "GUI2/Generator: GENERATE_BODY already defined!"); #else -#define GENERATE_BODY \ - if(has_minimum) { \ - typedef policy::minimum_selection::one_item minimum; \ - GENERATE_MAXIMUM \ - } else { \ - typedef policy::minimum_selection::no_item minimum; \ - GENERATE_MAXIMUM \ +#define GENERATE_BODY \ + if(has_minimum) { \ + typedef policy::minimum_selection::one_item minimum; \ + GENERATE_MAXIMUM \ + } else { \ + typedef policy::minimum_selection::no_item minimum; \ + GENERATE_MAXIMUM \ } #endif -generator_base* generator_base::build(const bool has_minimum, - const bool has_maximum, - const placement placement, - const bool select) +generator_base* generator_base::build( + const bool has_minimum, const bool has_maximum, const placement placement, const bool select) { generator_base* result = nullptr; GENERATE_BODY; @@ -1221,7 +1177,6 @@ namespace { void pointer_test() { - generator_base *a = generator_base::build( true, true, generator_base::horizontal_list, true); @@ -1279,7 +1234,6 @@ void direct_test() b.clear(); c.clear(); d.clear(); - } } // namespace diff --git a/src/gui/widgets/generator_private.hpp b/src/gui/widgets/generator_private.hpp index dc9cde153f3b6..28bee5f73a36e 100644 --- a/src/gui/widgets/generator_private.hpp +++ b/src/gui/widgets/generator_private.hpp @@ -16,7 +16,6 @@ #include "gui/widgets/generator.hpp" -#include #include "gui/widgets/grid.hpp" #include "gui/widgets/selectable_item.hpp" #include "gui/widgets/toggle_button.hpp" @@ -24,21 +23,20 @@ #include "gui/widgets/window.hpp" // For widget::visibility #include "wml_exception.hpp" +#include + namespace gui2 { - /** * Contains the policies for the generator class. */ namespace policy { - /***** ***** ***** ***** Minimum selection ***** ***** ***** *****/ /** Contains the policy for the minimum number of selected items. */ namespace minimum_selection { - /** Must select at least one item. */ struct one_item : public virtual generator_base { @@ -64,7 +62,7 @@ struct one_item : public virtual generator_base * Called when the users wants to deselect an item. * * If the item can be deselected this function should call - * do_deselect_item() to make the deslection happen. If not allowed no + * do_deselect_item() to make the deselection happen. If not allowed no * action needs to be taken. * * @param index The index of the item to deselect. @@ -91,7 +89,6 @@ struct one_item : public virtual generator_base /** No minimum selection. */ struct no_item : public virtual generator_base { - /** See @ref minimum_selection::one_item::set_item_shown(). */ void set_item_shown(const unsigned index, const bool show); @@ -126,7 +123,6 @@ struct no_item : public virtual generator_base /** Contains the policy for the maximum number of selected items. */ namespace maximum_selection { - /** May select only one item. */ struct one_item : public virtual generator_base { @@ -152,6 +148,7 @@ struct one_item : public virtual generator_base // deselect current. do_deselect_item(get_selected_item()); } + // select new. do_select_item(index); } @@ -176,7 +173,6 @@ struct many_items : public virtual generator_base /** Controls how new items are placed. */ namespace placement { - /** Places the items in a horizontal row. */ struct horizontal_list : public virtual generator_base { @@ -223,25 +219,23 @@ struct horizontal_list : public virtual generator_base void set_visible_rectangle(const SDL_Rect& rectangle) override; /** See @ref widget::find_at. */ - virtual widget* find_at(const point& coordinate, - const bool must_be_active) override; + virtual widget* find_at(const point& coordinate, const bool must_be_active) override; /** See @ref widget::find_at. */ - virtual const widget* find_at(const point& coordinate, - const bool must_be_active) const override; + virtual const widget* find_at(const point& coordinate, const bool must_be_active) const override; /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/ /** Inherited from generator_base. */ void handle_key_up_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override { - /* do nothing */ + /* DO NOTHING */ } /** Inherited from generator_base. */ void handle_key_down_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override { - /* do nothing */ + /* DO NOTHING */ } /** Inherited from generator_base. */ @@ -297,12 +291,10 @@ struct vertical_list : public virtual generator_base void set_visible_rectangle(const SDL_Rect& rectangle) override; /** See @ref widget::find_at. */ - virtual widget* find_at(const point& coordinate, - const bool must_be_active) override; + virtual widget* find_at(const point& coordinate, const bool must_be_active) override; /** See @ref widget::find_at. */ - virtual const widget* find_at(const point& coordinate, - const bool must_be_active) const override; + virtual const widget* find_at(const point& coordinate, const bool must_be_active) const override; /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/ @@ -314,12 +306,14 @@ struct vertical_list : public virtual generator_base /** Inherited from generator_base. */ void handle_key_left_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override - { /* do nothing */ + { + /* DO NOTHING */ } /** Inherited from generator_base. */ void handle_key_right_arrow(SDL_Keymod /*modifier*/, bool& /*handled*/) override - { /* do nothing */ + { + /* DO NOTHING */ } // FIXME we need a delete handler as well, @@ -372,8 +366,7 @@ struct table : public virtual generator_base virtual point calculate_best_size() const override; /** See @ref widget::place. */ - virtual void place(const point& /*origin*/ - , const point& /*size*/) override; + virtual void place(const point& /*origin*/, const point& /*size*/) override; /** See @ref widget::set_origin. */ virtual void set_origin(const point& /*origin*/) override; @@ -383,11 +376,13 @@ struct table : public virtual generator_base /** See @ref widget::find_at. */ virtual widget* find_at(const point& /*coordinate*/ - , const bool /*must_be_active*/) override; + , + const bool /*must_be_active*/) override; /** See @ref widget::find_at. */ virtual const widget* find_at(const point& /*coordinate*/ - , const bool /*must_be_active*/) const override; + , + const bool /*must_be_active*/) const override; /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/ @@ -402,6 +397,7 @@ struct table : public virtual generator_base /** Inherited from generator_base. */ void handle_key_right_arrow(SDL_Keymod, bool&) override; + private: /** * Has the grid already been placed? @@ -450,19 +446,16 @@ struct independent : public virtual generator_base void set_visible_rectangle(const SDL_Rect& rectangle) override; /** See @ref widget::find_at. */ - virtual widget* find_at(const point& coordinate, - const bool must_be_active) override; + virtual widget* find_at(const point& coordinate, const bool must_be_active) override; /** See @ref widget::find_at. */ - virtual const widget* find_at(const point& coordinate, - const bool must_be_active) const override; + virtual const widget* find_at(const point& coordinate, const bool must_be_active) const override; /** See @ref widget::find. */ widget* find(const std::string& id, const bool must_be_active) override; /** See @ref widget::find. */ - const widget* find(const std::string& id, - const bool must_be_active) const override; + const widget* find(const std::string& id, const bool must_be_active) const override; /***** ***** ***** ***** keyboard functions ***** ***** ***** *****/ @@ -501,7 +494,6 @@ struct independent : public virtual generator_base */ namespace select_action { - /** Select the item, this requires the grid to contain a selectable_item. */ struct selection : public virtual generator_base { @@ -517,8 +509,8 @@ struct selection : public virtual generator_base * in the grid is (de)selected. */ void init(grid* grid, - const std::map& data, - const std::function& callback); + const std::map& data, + const std::function& callback); }; /** Show the item. */ @@ -526,8 +518,7 @@ struct show : public virtual generator_base { void select(grid& grid, const bool show) { - grid.set_visible(show ? widget::visibility::visible - : widget::visibility::hidden); + grid.set_visible(show ? widget::visibility::visible : widget::visibility::hidden); } /** @@ -541,8 +532,8 @@ struct show : public virtual generator_base * should be nullptr. */ void init(grid* grid, - const std::map& data, - const std::function& callback); + const std::map& data, + const std::function& callback); }; } // namespace select_action @@ -556,14 +547,14 @@ struct show : public virtual generator_base * * The class is policy based so the behavior can be selected. */ -template +template class generator : public minimum_selection, - public maximum_selection, - public my_placement, - public select_action + public maximum_selection, + public my_placement, + public select_action { public: generator() @@ -623,7 +614,7 @@ class generator : public minimum_selection, last_selected_item_ = index; } else if(is_selected(index)) { if(!minimum_selection::deselect_item(index)) { - // Some items might have deseleted themselves so + // Some items might have deselected themselves so // make sure they do get selected again. select_action::select(item(index), true); } @@ -642,12 +633,9 @@ class generator : public minimum_selection, { assert(index < items_.size()); if(items_[index]->shown != show) { - /*** Set the proper visible state. ***/ items_[index]->shown = show; - items_[index] - ->child_grid.set_visible(show ? widget::visibility::visible - : widget::visibility::invisible); + items_[index]->child_grid.set_visible(show ? widget::visibility::visible : widget::visibility::invisible); /*** Update the selection. ***/ minimum_selection::set_item_shown(index, show); @@ -661,7 +649,6 @@ class generator : public minimum_selection, return items_[index]->shown && items_[index]->child_grid.get_visible() != widget::visibility::invisible; } - /** Inherited from generator_base. */ unsigned get_item_count() const override { @@ -677,23 +664,21 @@ class generator : public minimum_selection, /** Inherited from generator_base. */ int get_selected_item() const override { - if(selected_item_count_ == 0) { return -1; - } else if(last_selected_item_ != -1 - && last_selected_item_ < static_cast(items_.size()) - && (*items_[last_selected_item_]).selected) { - + } else if(last_selected_item_ != -1 && last_selected_item_ < static_cast(items_.size()) + && (*items_[last_selected_item_]).selected) + { return last_selected_item_; - } else { for(size_t i = 0; i < items_.size(); ++i) { if((*items_[i]).selected) { return i; } } - FAIL_WITH_DEV_MESSAGE("No item selected.", - "selected_item_count_ was non-zero, yet no selected item was found."); + + FAIL_WITH_DEV_MESSAGE( + "No item selected.", "selected_item_count_ was non-zero, yet no selected item was found."); } } @@ -727,12 +712,11 @@ class generator : public minimum_selection, return items_[order_[index]]->child_grid; } - /** Inherited from generator_base. */ grid& create_item(const int index, - builder_grid_const_ptr list_builder, - const string_map& item_data, - const std::function& callback) override + builder_grid_const_ptr list_builder, + const string_map& item_data, + const std::function& callback) override { std::map data; @@ -741,8 +725,7 @@ class generator : public minimum_selection, } /** Inherited from generator_base. */ - grid& create_item( - const int index, + grid& create_item(const int index, builder_grid_const_ptr list_builder, const std::map& item_data, const std::function& callback) override @@ -752,26 +735,30 @@ class generator : public minimum_selection, child* item = new child; list_builder->build(&item->child_grid); + init(&item->child_grid, item_data, callback); const unsigned item_index = index == -1 ? items_.size() : index; items_.emplace(items_.begin() + item_index, item); + order_dirty_ = true; + minimum_selection::create_item(item_index); + my_placement::create_item(item_index); + if(!is_selected(item_index)) { select_action::select(item->child_grid, false); } + return item->child_grid; } /** Inherited from generator_base. */ - virtual void create_items( - const int index, + virtual void create_items(const int index, builder_grid_const_ptr list_builder, - const std::vector >& - data, + const std::vector>& data, const std::function& callback) override { impl_create_items(index, list_builder, data, callback); @@ -779,9 +766,9 @@ class generator : public minimum_selection, /** Inherited from generator_base. */ virtual void create_items(const int index, - builder_grid_const_ptr list_builder, - const std::vector& data, - const std::function& callback) override + builder_grid_const_ptr list_builder, + const std::vector& data, + const std::function& callback) override { impl_create_items(index, list_builder, data, callback); } @@ -789,11 +776,8 @@ class generator : public minimum_selection, /** See @ref widget::layout_initialize. */ virtual void layout_initialize(const bool full_initialization) override { - for(auto& item : items_) - { - if(item->child_grid.get_visible() != widget::visibility::invisible - && item->shown) { - + for(auto& item : items_) { + if(item->child_grid.get_visible() != widget::visibility::invisible && item->shown) { item->child_grid.layout_initialize(full_initialization); } } @@ -842,45 +826,38 @@ class generator : public minimum_selection, } /** See @ref widget::impl_draw_children. */ - virtual void impl_draw_children(surface& frame_buffer, - int x_offset, - int y_offset) override + virtual void impl_draw_children(surface& frame_buffer, int x_offset, int y_offset) override { assert(this->get_visible() == widget::visibility::visible); + calculate_order(); - for(auto index : order_) - { + + for(auto index : order_) { child* item = items_[index].get(); - if(item->child_grid.get_visible() == widget::visibility::visible - && item->shown) { + if(item->child_grid.get_visible() == widget::visibility::visible && item->shown) { item->child_grid.draw_children(frame_buffer, x_offset, y_offset); } } } /** See @ref widget::child_populate_dirty_list. */ - virtual void - child_populate_dirty_list(window& caller, - const std::vector& call_stack) override + virtual void child_populate_dirty_list(window& caller, const std::vector& call_stack) override { - for(auto& item : items_) - { + for(auto& item : items_) { std::vector child_call_stack = call_stack; item->child_grid.populate_dirty_list(caller, child_call_stack); } } /** See @ref widget::find_at. */ - virtual widget* find_at(const point& coordinate, - const bool must_be_active) override + virtual widget* find_at(const point& coordinate, const bool must_be_active) override { return my_placement::find_at(coordinate, must_be_active); } /** See @ref widget::find_at. */ - virtual const widget* find_at(const point& coordinate, - const bool must_be_active) const override + virtual const widget* find_at(const point& coordinate, const bool must_be_active) const override { return my_placement::find_at(coordinate, must_be_active); } @@ -888,12 +865,12 @@ class generator : public minimum_selection, /** See @ref widget::disable_click_dismiss. */ bool disable_click_dismiss() const override { - for(auto& item : items_) - { + for(auto& item : items_) { if(item->child_grid.disable_click_dismiss()) { return true; } } + return false; } @@ -956,8 +933,11 @@ class generator : public minimum_selection, /** Definition of an item. */ struct child { - - child() : child_grid(), selected(false), shown(true), ordered_index(0) + child() + : child_grid() + , selected(false) + , shown(true) + , ordered_index(0) { } @@ -996,10 +976,9 @@ class generator : public minimum_selection, /** whether need to recalculate order_dirty_ */ mutable bool order_dirty_; - typedef std::function order_func; + typedef std::function order_func; order_func order_func_; - virtual void set_order(const order_func& order) override { order_func_ = order; @@ -1043,20 +1022,22 @@ class generator : public minimum_selection, if(order_dirty_) { if(order_.size() != items_.size()) { order_.resize(items_.size()); + for(size_t i = 0; i < items_.size(); ++i) { order_[i] = i; } } + if(order_func_) { std::stable_sort(order_.begin(), order_.end(), calculate_order_helper(order_func_, items_)); } + for(size_t i = 0; i < order_.size(); ++i) { items_[order_[i]]->ordered_index = i; } order_dirty_ = false; - } - else { + } else { assert(order_.size() == items_.size()); } } @@ -1089,15 +1070,15 @@ class generator : public minimum_selection, * @param callback The callback function to call when an item * in the grid is (de)selected. */ - template + template void impl_create_items(const int index, - builder_grid_const_ptr list_builder, - const std::vector& data, - const std::function& callback) + builder_grid_const_ptr list_builder, + const std::vector& data, + const std::function& callback) { int i = index; - for(const auto & item_data : data) - { + + for(const auto& item_data : data) { create_item(i, list_builder, item_data, callback); if(i != -1) { ++i; @@ -1118,8 +1099,8 @@ class generator : public minimum_selection, * in the grid is (de)selected. */ void init(grid* grid, - const std::map& data, - const std::function& callback) + const std::map& data, + const std::function& callback) { assert(grid); grid->set_parent(this);