The Battle for Wesnoth  1.19.5+dev
vertical_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2024
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 
31 placer_vertical_list::placer_vertical_list(const unsigned maximum_columns)
32  : maximum_columns_(maximum_columns)
33  , rows_(1, std::pair(0, 0))
34  , columns_(maximum_columns, 0)
35  , row_(0)
36  , column_(0)
37 {
38  assert(maximum_columns_ > 0);
39 }
40 
42 {
43  rows_.clear();
44  rows_.emplace_back(0, 0);
45  std::fill(columns_.begin(), columns_.end(), 0);
46  row_ = 0;
47  column_ = 0;
48 }
49 
51 {
52  if(size.x > columns_[column_]) {
53  columns_[column_] = size.x;
54  }
55 
56  if(size.y > rows_[row_].second) {
57  rows_[row_].second = size.y;
58  }
59 
60  ++column_;
61  if(column_ == maximum_columns_) {
62  column_ = 0;
63  ++row_;
64 
65  const int origin = rows_.back().first + rows_.back().second;
66  rows_.emplace_back(origin, 0);
67  }
68 }
69 
71 {
72  const int width = std::accumulate(columns_.begin(), columns_.end(), 0);
73  const int height = rows_.back().first + rows_.back().second;
74  return point(width, height);
75 }
76 
78 {
79  const unsigned row = index / maximum_columns_;
80  const unsigned column = index % maximum_columns_;
81 
82  const int width = column == 0 ? 0
83  : std::accumulate(columns_.begin(),
84  columns_.begin() + column,
85  0);
86 
87  return point(width, rows_[row].first);
88 }
89 
90 } // namespace implementation
91 
92 } // namespace gui2
std::vector< std::pair< int, int > > rows_
Holds the row sizes.
std::vector< int > columns_
Holds the widths of the columns.
unsigned row_
The row to add an item to.
virtual void add_item(const point &size)
Adds a item to be placed.
virtual point get_origin(const unsigned index) const
Gets the origin for an item.
virtual void initialize()
Initialises the placer.
unsigned column_
The column to add an item to.
unsigned maximum_columns_
The maximum number of columns to use.
virtual point get_size() const
Gets the required size of all items.
placer_vertical_list(const unsigned maximum_columns)
void fill(const SDL_Rect &rect, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Fill an area with the given colour.
Definition: draw.cpp:50
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:202
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
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:70
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
Holds a 2D point.
Definition: point.hpp:25
Placement helper for the vertical list.