The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
vertical_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 
30 tplacer_vertical_list::tplacer_vertical_list(const unsigned maximum_columns)
31  : maximum_columns_(maximum_columns)
32  , rows_(1, std::make_pair(0, 0))
33  , columns_(maximum_columns, 0)
34  , row_(0)
35  , column_(0)
36 {
37  assert(maximum_columns_ > 0);
38 }
39 
41 {
42  rows_.clear();
43  rows_.push_back(std::make_pair(0, 0));
44  std::fill(columns_.begin(), columns_.end(), 0);
45  row_ = 0;
46  column_ = 0;
47 }
48 
50 {
51  if(size.x > columns_[column_]) {
52  columns_[column_] = size.x;
53  }
54 
55  if(size.y > rows_[row_].second) {
56  rows_[row_].second = size.y;
57  }
58 
59  ++column_;
60  if(column_ == maximum_columns_) {
61  column_ = 0;
62  ++row_;
63 
64  const int origin = rows_.back().first + rows_.back().second;
65  rows_.push_back(std::make_pair(origin, 0));
66  }
67 }
68 
70 {
71  const int width = std::accumulate(columns_.begin(), columns_.end(), 0);
72  const int height = rows_.back().first + rows_.back().second;
73  return tpoint(width, height);
74 }
75 
77 {
78  const unsigned row = index / maximum_columns_;
79  const unsigned column = index % maximum_columns_;
80 
81  const int width = column == 0 ? 0
82  : std::accumulate(columns_.begin(),
83  columns_.begin() + column,
84  0);
85 
86  return tpoint(width, rows_[row].first);
87 }
88 
89 } // namespace implementation
90 
91 } // namespace gui2
virtual tpoint get_origin(const unsigned index) const
Gets the origin for an item.
tplacer_vertical_list(const unsigned maximum_columns)
std::vector< int > columns_
Holds the widths of the columns.
Placement helper for the vertical list.
STL namespace.
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
virtual tpoint get_size() const
Gets the required size of all items.
int y
y coordinate.
Definition: point.hpp:34
int x
x coordinate.
Definition: point.hpp:31
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
unsigned maximum_columns_
The maximum number of columns to use.
GLuint index
Definition: glew.h:1782
Holds a 2D point.
Definition: point.hpp:24
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
std::vector< std::pair< int, int > > rows_
Holds the row sizes.
unsigned row_
The row to add an item to.
GLsizeiptr size
Definition: glew.h:1649
GLenum GLenum GLvoid * row
Definition: glew.h:3805
virtual void initialise()
Initialises the placer.
GLint * first
Definition: glew.h:1496
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
virtual void add_item(const tpoint &size)
Adds a item to be placed.
unsigned column_
The column to add an item to.
Contains the implementation details for lexical_cast and shouldn't be used directly.