The Battle for Wesnoth  1.15.1+dev
placer.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 /**
16  * @file
17  * Base class for the placement helper.
18  *
19  * Some items can create new child items and these items are placed in some way
20  * this code contains helpers for the placement by calculating the positions
21  * for the items and the best size for the children.
22  */
23 
24 #pragma once
25 
26 #include "utils/make_enum.hpp"
27 
28 struct point;
29 
30 namespace gui2
31 {
32 
33 /**
34  * Base class for the placement helper.
35  *
36  * The normal operation for the usage of the class is:
37  * * Call @ref initialize().
38  * * For every visible child item call @ref add_item() with the wanted size of
39  * the widget.
40  * Once this is done the required size for all children can be retrieved with
41  * @ref get_size(). It is also possible to place all children now. In order to
42  * do so loop again over all children in the same order as @ref add_item() and
43  * call @ref get_origin(). The @p index parameter is an increasing counter.
44  *
45  * @note The origins can only be retrieved after all items are added since the
46  * adding of a later item may influence a previous item. E.g. in a vertical
47  * list with two columns the position of the second column depends on the width
48  * of the first and a later row may have a wider column 1 as an earlier row.
49  */
51 {
52 public:
53  /***** ***** Types. ***** *****/
54 
55  /** The direction the placer should grow towards. */
56  MAKE_ENUM(tgrow_direction,
57  (horizontal, "horizontal")
58  (vertical, "vertical")
59  )
60 
61 
62  /***** ***** Constructor, destructor, assignment. ***** *****/
63 
64 public:
65  /**
66  * Builder function.
67  *
68  * @pre @p parallel_items > 0
69  *
70  * @param grow_direction The direction in which the items will be
71  * added.
72  * @param parallel_items The direction perpendicular towards the grow
73  * direction has a fixed number of items. This
74  * value sets that limit. For a list containing
75  * only horizontally or vertically placed items
76  * the value should be 1.
77  */
78  static placer_base* build(const tgrow_direction grow_direction,
79  const unsigned parallel_items);
80 
81  virtual ~placer_base();
82 
83 
84  /***** ***** Operations. ***** *****/
85 
86  /**
87  * Initialises the placer.
88  *
89  * When the placement needs to be calculated the state often needs to be
90  * reset, items are placed, removed or changed visibility causing the old
91  * placement to be invalid.
92  */
93  virtual void initialize() = 0;
94 
95  /**
96  * Adds a item to be placed.
97  *
98  * @param size The required size for the item.
99  */
100  virtual void add_item(const point& size) = 0;
101 
102  /**
103  * Gets the required size of all items.
104  *
105  * @returns The required size.
106  */
107  virtual point get_size() const = 0;
108 
109  /**
110  * Gets the origin for an item.
111  *
112  * @param index The index of the item whose origin to return.
113  * The index is the index of the call to
114  * @ref add_item().
115  *
116  * @returns The origin where to place the widget.
117  */
118  virtual point get_origin(const unsigned index) const = 0;
119 };
120 
121 } // namespace gui2
Base class for the placement helper.
Definition: placer.hpp:50
virtual point get_size() const =0
Gets the required size of all items.
Generic file dialog.
Definition: field-fwd.hpp:22
virtual MAKE_ENUM(tgrow_direction,(horizontal, "horizontal")(vertical, "vertical")) public ~placer_base()
The direction the placer should grow towards.
Definition: placer.cpp:40
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
window * build(const builder_window::window_resolution *definition)
Builds a window.
Holds a 2D point.
Definition: point.hpp:23
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:71
virtual void initialize()=0
Initialises the placer.
MAKE_ENUM(ALIGNMENT_FEMALE_VARIATION,(LAWFUL, N_("female^lawful"))(FEMALE_NEUTRAL, N_("female^neutral"))(CHAOTIC, N_("female^chaotic"))(LIMINAL, N_("female^liminal"))) std
Implementation detail of unit_type::alignment_description.
Definition: types.cpp:922
virtual void add_item(const point &size)=0
Adds a item to be placed.
virtual point get_origin(const unsigned index) const =0
Gets the origin for an item.
Defines the MAKE_ENUM macro.