The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
horizontal_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gui/core/point.hpp"
20 
21 #include <cassert>
22 #include <numeric>
23 
24 namespace gui2
25 {
26 
27 namespace implementation
28 {
29 
31  : maximum_rows_(maximum_rows)
32  , rows_(maximum_rows, 0)
33  , columns_(1, std::make_pair(0, 0))
34  , row_(0)
35  , column_(0)
36 {
37  assert(maximum_rows_ > 0);
38 }
39 
41 {
42  std::fill(rows_.begin(), rows_.end(), 0);
43  columns_.clear();
44  columns_.push_back(std::make_pair(0, 0));
45  row_ = 0;
46  column_ = 0;
47 }
48 
50 {
51  if(size.x > columns_[column_].second) {
52  columns_[column_].second = size.x;
53  }
54 
55  if(size.y > rows_[row_]) {
56  rows_[row_] = size.y;
57  }
58 
59  ++row_;
60  if(row_ == maximum_rows_) {
61  row_ = 0;
62  ++column_;
63 
64  const int origin = columns_.back().first + columns_.back().second;
65  columns_.push_back(std::make_pair(origin, 0));
66  }
67 }
68 
70 {
71  const int width = columns_.back().first + columns_.back().second;
72  const int height = std::accumulate(rows_.begin(), rows_.end(), 0);
73  return tpoint(width, height);
74 }
75 
77 {
78  const unsigned row = index % maximum_rows_;
79  const unsigned column = index / maximum_rows_;
80 
81  const int height
82  = row == 0 ? 0
83  : std::accumulate(rows_.begin(), rows_.begin() + row, 0);
84 
85  return tpoint(columns_[column].first, height);
86 }
87 
88 } // namespace implementation
89 
90 } // namespace gui2
unsigned maximum_rows_
The maximum number of rows to use.
Placement helper for the horizontal list.
std::vector< int > rows_
Holds the heights of the rows.
tplacer_horizontal_list(const unsigned maximum_rows)
STL namespace.
unsigned column_
The column to add an item to.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
int y
y coordinate.
Definition: point.hpp:34
virtual tpoint get_size() const
Gets the required size of all items.
unsigned row_
The row to add an item to.
virtual tpoint get_origin(const unsigned index) const
Gets the origin for an item.
virtual void initialise()
Initialises the placer.
int x
x coordinate.
Definition: point.hpp:31
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
GLuint index
Definition: glew.h:1782
virtual void add_item(const tpoint &size)
Adds a item to be placed.
Holds a 2D point.
Definition: point.hpp:24
std::vector< std::pair< int, int > > columns_
Holds the column sizes.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
GLsizeiptr size
Definition: glew.h:1649
GLenum GLenum GLvoid * row
Definition: glew.h:3805
GLint * first
Definition: glew.h:1496
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
Contains the implementation details for lexical_cast and shouldn't be used directly.