16 #define GETTEXT_DOMAIN "wesnoth-lib"
31 namespace minimum_selection
40 bool found_new_item =
false;
44 for(
unsigned i = ordered_index + 1;
i < item_count; ++
i) {
48 found_new_item =
true;
54 for(
signed i =
static_cast<signed>(ordered_index) - 1;
i >= 0; --
i) {
130 result.x += best_size.x;
132 if(best_size.y > result.y) {
133 result.y = best_size.y;
150 point current_origin = origin;
159 assert(best_size.y <=
size.y);
162 best_size.y =
size.y;
166 current_origin.x += best_size.x;
169 if(current_origin.x != origin.x +
size.x) {
170 ERR_GUI_L <<
"Failed to fit horizontal list to requested rect; expected right edge was " << origin.x +
size.x
171 <<
", actual right edge was " << current_origin.x
172 <<
" (left edge is " << origin.x <<
")\n";
178 point current_origin = origin;
342 if(best_size.x > result.x) {
343 result.x = best_size.x;
346 result.y += best_size.y;
362 point current_origin = origin;
370 assert(best_size.x <=
size.x);
373 best_size.x =
size.x;
377 current_origin.y += best_size.y;
380 if(current_origin.y != origin.y +
size.y) {
381 ERR_GUI_L <<
"Failed to fit vertical list to requested rect; expected bottom edge was " << origin.y +
size.y
382 <<
", actual bottom edge was " << current_origin.y
383 <<
" (top edge is " << origin.y <<
")\n";
389 point current_origin = origin;
553 std::size_t max_cols = std::sqrt(n_items) + 2;
555 std::vector<point> item_sizes;
556 for(std::size_t
i = 0;
i < n_items;
i++) {
562 if(item_sizes.empty()) {
566 std::vector<point> best_sizes(1);
568 best_sizes[0] = std::accumulate(item_sizes.begin(), item_sizes.end(),
point(),
572 int max_xtra = std::min_element(item_sizes.begin(), item_sizes.end(),
576 for(std::size_t cells_in_1st_row = 2; cells_in_1st_row <= max_cols; cells_in_1st_row++) {
577 int row_min_width = std::accumulate(item_sizes.begin(), item_sizes.begin() + cells_in_1st_row, 0,
578 [](
int a,
point b) { return a + b.x; }
581 int row_max_width = row_min_width + max_xtra;
583 point row_size, total_size;
585 for(std::size_t
n = 0;
n < item_sizes.size();
n++) {
586 if(row_size.x + item_sizes[
n].x > row_max_width) {
588 total_size.y += row_size.y;
590 if(total_size.x < row_size.x) {
591 total_size.x = row_size.x;
597 row_size.x += item_sizes[
n].x;
599 if(row_size.y < item_sizes[
n].y) {
600 row_size.y = item_sizes[
n].y;
604 total_size.y += row_size.y;
606 if(total_size.x < row_size.x) {
607 total_size.x = row_size.x;
610 best_sizes.push_back(total_size);
613 return *std::min_element(best_sizes.begin(), best_sizes.end(), [](
point p1,
point p2) {
615 std::max<double>(p1.x, p1.y) / std::min<double>(p1.x, p1.y) <
616 std::max<double>(p2.x, p2.y) / std::min<double>(p2.x, p2.y);
631 point current_origin = origin;
642 if(current_origin.x + best_size.x > origin.x +
size.x) {
643 current_origin.x = origin.x;
644 current_origin.y += row_height;
650 current_origin.x += best_size.x;
651 if(best_size.y > row_height) {
652 row_height = best_size.y;
659 if(current_origin.y + row_height != origin.y +
size.y) {
661 better_size.y -= current_origin.y + row_height - origin.y;
668 point current_origin = origin;
669 std::size_t row_height = 0;
677 current_origin.x = origin.x;
678 current_origin.y += row_height;
919 if(best_size.x > result.x) {
920 result.x = best_size.x;
923 if(best_size.y > result.y) {
924 result.y = best_size.y;
958 if(selected_item < 0) {
971 if(selected_item < 0) {
1023 namespace select_action
1029 VALIDATE(selectable,
"Only toggle buttons and panels are allowed as the cells of a list definition.");
1036 const std::function<
void(
widget&)>& callback)
1038 for(
unsigned row = 0; row <
g->get_rows(); ++row) {
1039 for(
unsigned col = 0; col <
g->get_cols(); ++col) {
1050 widget_data::const_iterator itor =
data.find(btn->
id());
1052 if(itor ==
data.end()) {
1053 itor =
data.find(
"");
1055 if(itor !=
data.end()) {
1062 }
else if(child_grid) {
1065 FAIL(
"Only toggle buttons and panels are allowed as the cells of a list definition.");
1073 const std::function<
void(
widget&)>& )
1076 if(
item.first.empty()) {
1080 control->set_members(
item.second);
1099 #ifdef GENERATE_PLACEMENT
1100 static_assert(
false,
"GUI2/Generator: GENERATE_PLACEMENT already defined!");
1102 #define GENERATE_PLACEMENT \
1103 switch(placement) { \
1104 case generator_base::horizontal_list: \
1105 result = std::make_unique<generator<minimum, maximum, policy::placement::horizontal_list, select_action>>(); \
1107 case generator_base::vertical_list: \
1108 result = std::make_unique<generator<minimum, maximum, policy::placement::vertical_list, select_action>>(); \
1110 case generator_base::table: \
1111 result = std::make_unique<generator<minimum, maximum, policy::placement::table, select_action>>(); \
1113 case generator_base::independent: \
1114 result = std::make_unique<generator<minimum, maximum, policy::placement::independent, select_action>>(); \
1121 #ifdef GENERATE_SELECT
1122 static_assert(
false,
"GUI2/Generator: GENERATE_SELECT already defined!");
1124 #define GENERATE_SELECT \
1126 typedef policy::select_action::selection select_action; \
1127 GENERATE_PLACEMENT \
1129 typedef policy::select_action::show select_action; \
1130 GENERATE_PLACEMENT \
1134 #ifdef GENERATE_MAXIMUM
1135 static_assert(
false,
"GUI2/Generator: GENERATE_MAXIMUM already defined!");
1137 #define GENERATE_MAXIMUM \
1139 typedef policy::maximum_selection::one_item maximum; \
1142 typedef policy::maximum_selection::many_items maximum; \
1147 #ifdef GENERATE_BODY
1148 static_assert(
false,
"GUI2/Generator: GENERATE_BODY already defined!");
1150 #define GENERATE_BODY \
1152 typedef policy::minimum_selection::one_item minimum; \
1155 typedef policy::minimum_selection::no_item minimum; \
1161 const bool has_minimum,
const bool has_maximum,
const placement placement,
const bool select)
1163 std::unique_ptr<generator_base> result =
nullptr;
1195 < policy::minimum_selection::one_item
1196 , policy::maximum_selection::one_item
1197 , policy::placement::vertical_list
1198 , policy::select_action::selection
1202 < policy::minimum_selection::one_item
1203 , policy::maximum_selection::many_items
1204 , policy::placement::vertical_list
1205 , policy::select_action::selection
1209 < policy::minimum_selection::no_item
1210 , policy::maximum_selection::one_item
1211 , policy::placement::vertical_list
1212 , policy::select_action::selection
1216 < policy::minimum_selection::no_item
1217 , policy::maximum_selection::many_items
1218 , policy::placement::vertical_list
1219 , policy::select_action::selection
virtual unsigned get_ordered_index(unsigned index) const =0
If a sort-order is being applied, maps from unsorted to sorted indicies.
virtual unsigned get_selected_item_count() const =0
Returns the number of selected items.
virtual grid & item_ordered(const unsigned index)=0
Gets the grid of an item.
virtual void select_item(const unsigned index, const bool select)=0
(De)selects an item.
virtual grid & item(const unsigned index)=0
Gets the grid of an item.
virtual unsigned get_item_count() const =0
Returns the number of items.
static std::unique_ptr< generator_base > build(const bool has_minimum, const bool has_maximum, const placement placement, const bool select)
Create a new generator.
virtual bool is_selected(const unsigned index) const =0
Returns whether the item is selected.
virtual bool get_item_shown(const unsigned index) const =0
Returns whether the item is shown.
placement
Determines how the items are placed.
virtual unsigned get_item_at_ordered(unsigned index_ordered) const =0
If a sort-order is being applied, maps from sorted to unsorted indicies.
virtual void do_select_item(const unsigned index)=0
Selects a not selected item.
virtual void do_deselect_item(const unsigned index)=0
Deselects a selected item.
virtual int get_selected_item() const =0
Returns the selected item.
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual void set_visible_rectangle(const SDL_Rect &rectangle) override
See widget::set_visible_rectangle.
const widget * get_widget(const unsigned row, const unsigned col) const
Returns the widget in the selected cell.
unsigned int get_rows() const
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
unsigned int get_cols() const
virtual void set_origin(const point &origin) override
See widget::set_origin.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Small abstract helper class.
virtual void set_value(unsigned value, bool fire_event=false)=0
Select the styled_widget.
void point(int x, int y)
Draw a single point.
void show(const std::string &window_id, const t_string &message, const point &mouse, const SDL_Rect &source_rect)
Shows a tip.
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification &signal)
Connects a signal handler for getting a notification upon modification.
std::map< std::string, widget_item > widget_data
rng * generator
This generator is automatically synced during synced context.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
void set_item_shown(const unsigned index, const bool show)
See minimum_selection::one_item::set_item_shown().
void delete_item(const unsigned index)
Called just before an item is deleted.
bool deselect_item(const unsigned index)
Called when the users wants to deselect an item.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
void set_item_shown(const unsigned index, const bool show)
Called when an item is shown or hidden.
virtual void set_origin(const point &origin) override
See widget::set_origin.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
void handle_key_left_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from generator_base.
void handle_key_right_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from generator_base.
virtual void place(const point &origin, const point &size) override
See widget::place.
bool placed_
Has the grid already been placed?
void set_visible_rectangle(const SDL_Rect &rectangle) override
Sets the visible rectangle of the generator.
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
void set_visible_rectangle(const SDL_Rect &rectangle) override
See horizontal_list::set_visible_rectangle().
virtual void request_reduce_height(const unsigned maximum_height) override
See horizontal_list::request_reduce_height.
virtual void place(const point &origin, const point &size) override
See widget::place.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
virtual void set_origin(const point &origin) override
See widget::set_origin.
virtual void place(const point &, const point &) override
See widget::place.
bool placed_
Has the grid already been placed?
void handle_key_up_arrow(SDL_Keymod, bool &) override
Inherited from generator_base.
void handle_key_right_arrow(SDL_Keymod, bool &) override
Inherited from generator_base.
virtual void set_origin(const point &) override
See widget::set_origin.
void handle_key_down_arrow(SDL_Keymod, bool &) override
Inherited from generator_base.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
void handle_key_left_arrow(SDL_Keymod, bool &) override
Inherited from generator_base.
virtual widget * find_at(const point &, const bool) override
See widget::find_at.
void set_visible_rectangle(const SDL_Rect &) override
See horizontal_list::set_visible_rectangle().
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
virtual void set_origin(const point &origin) override
See widget::set_origin.
void handle_key_up_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from generator_base.
virtual grid & create_item(const int index, const builder_grid &list_builder, const widget_item &item_data, const std::function< void(widget &)> &callback)=0
Creates a new item.
virtual void place(const point &origin, const point &size) override
See widget::place.
bool placed_
Has the grid already been placed?
void set_visible_rectangle(const SDL_Rect &rectangle) override
See horizontal_list::set_visible_rectangle().
virtual point calculate_best_size() const override
See widget::calculate_best_size.
void handle_key_down_arrow(SDL_Keymod modifier, bool &handled) override
Inherited from generator_base.
void init(grid *grid, const widget_data &data, const std::function< void(widget &)> &callback)
Helper function to initialize a grid.
void select(grid &grid, const bool select)
void init(grid *grid, const widget_data &data, const std::function< void(widget &)> &callback)
Helper function to initialize a grid.
static map_location::direction n
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE(cond, message)
The macro to use for the validation of WML.