The Battle for Wesnoth  1.17.0-dev
horizontal_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "sdl/point.hpp"
21 
22 #include <cassert>
23 #include <numeric>
24 
25 namespace gui2
26 {
27 
28 namespace implementation
29 {
30 
32  : maximum_rows_(maximum_rows)
33  , rows_(maximum_rows, 0)
34  , columns_(1, std::pair(0, 0))
35  , row_(0)
36  , column_(0)
37 {
38  assert(maximum_rows_ > 0);
39 }
40 
42 {
43  std::fill(rows_.begin(), rows_.end(), 0);
44  columns_.clear();
45  columns_.emplace_back(0, 0);
46  row_ = 0;
47  column_ = 0;
48 }
49 
51 {
52  if(size.x > columns_[column_].second) {
53  columns_[column_].second = size.x;
54  }
55 
56  if(size.y > rows_[row_]) {
57  rows_[row_] = size.y;
58  }
59 
60  ++row_;
61  if(row_ == maximum_rows_) {
62  row_ = 0;
63  ++column_;
64 
65  const int origin = columns_.back().first + columns_.back().second;
66  columns_.emplace_back(origin, 0);
67  }
68 }
69 
71 {
72  const int width = columns_.back().first + columns_.back().second;
73  const int height = std::accumulate(rows_.begin(), rows_.end(), 0);
74  return point(width, height);
75 }
76 
78 {
79  const unsigned row = index % maximum_rows_;
80  const unsigned column = index / maximum_rows_;
81 
82  const int height
83  = row == 0 ? 0
84  : std::accumulate(rows_.begin(), rows_.begin() + row, 0);
85 
86  return point(columns_[column].first, height);
87 }
88 
89 } // namespace implementation
90 
91 } // namespace gui2
Placement helper for the horizontal list.
unsigned column_
The column to add an item to.
virtual void initialize()
Initialises the placer.
virtual point get_size() const
Gets the required size of all items.
unsigned maximum_rows_
The maximum number of rows to use.
placer_horizontal_list(const unsigned maximum_rows)
STL namespace.
virtual void add_item(const point &size)
Adds a item to be placed.
int x
x coordinate.
Definition: point.hpp:45
Generic file dialog.
Definition: field-fwd.hpp:23
std::vector< std::pair< int, int > > columns_
Holds the column sizes.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
virtual point get_origin(const unsigned index) const
Gets the origin for an item.
std::vector< int > rows_
Holds the heights of the rows.
Holds a 2D point.
Definition: point.hpp:24
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.
Definition: unicode.cpp:72
unsigned row_
The row to add an item to.
int y
y coordinate.
Definition: point.hpp:48
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.