The Battle for Wesnoth  1.17.4+dev
viewport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2022
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 
18 #include "gui/widgets/viewport.hpp"
19 
21 #include "gui/core/log.hpp"
22 #include "config.hpp"
23 #include "utils/const_clone.hpp"
24 
25 #define LOG_SCOPE_HEADER "viewport [" + id() + "] " + __func__
26 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
27 
28 namespace gui2
29 {
30 
31 // ------------ WIDGET -----------{
32 
33 /**
34  * Helper to implement private functions without modifying the header.
35  *
36  * The class is a helper to avoid recompilation and only has static
37  * functions. It also facilitates to create duplicates of functions for a const
38  * and a non-const member function.
39  */
41 {
42  /**
43  * Implementation for the wrappers for
44  * [const] widget* pane::find_at(const point&, const bool) [const].
45  *
46  * @tparam W A pointer to the pane.
47  */
48  template <class W>
50  find_at(W viewport, point coordinate, const bool must_be_active)
51  {
52 
53  /*
54  * First test whether the mouse is at the pane.
55  */
56  if(viewport->widget::find_at(coordinate, must_be_active) != viewport) {
57  return nullptr;
58  }
59 
60  /*
61  * The widgets are placed at coordinate 0,0 so adjust the offset to
62  * that coordinate system.
63  */
64  coordinate.x -= viewport->get_x();
65  coordinate.y -= viewport->get_y();
66 
67  return viewport->widget_->find_at(coordinate, must_be_active);
68  }
69 
70  template <class W>
72  find(W viewport, const std::string& id, const bool must_be_active)
73  {
74  if(viewport->widget::find(id, must_be_active)) {
75  return viewport;
76  } else {
77  return viewport->widget_->find(id, must_be_active);
78  }
79  }
80 };
81 
83  const builder_widget::replacements_map& replacements)
84  : widget(builder)
85  , widget_(builder.widget_->build(replacements))
86 {
87  widget_->set_parent(this);
88 }
89 
91 {
92 }
93 
94 void viewport::place(const point& origin, const point& size)
95 {
96  widget::place(origin, size);
97 
98  widget_->place(point(), widget_->get_best_size());
99 }
100 
101 void viewport::layout_initialize(const bool full_initialization)
102 {
103  widget::layout_initialize(full_initialization);
104 
105  if(widget_->get_visible() != widget::visibility::invisible) {
106  widget_->layout_initialize(full_initialization);
107  }
108 }
109 
110 void viewport::impl_draw_children(int x_offset, int y_offset)
111 {
112  x_offset += get_x();
113  y_offset += get_y();
114 
115  if(widget_->get_visible() != widget::visibility::invisible) {
116  widget_->draw_background(x_offset, y_offset);
117  widget_->draw_children(x_offset, y_offset);
118  widget_->draw_foreground(x_offset, y_offset);
119  widget_->set_is_dirty(false);
120  }
121 }
122 
123 void
125  const std::vector<widget*>& call_stack)
126 {
127  std::vector<widget*> child_call_stack = call_stack;
128  widget_->populate_dirty_list(caller, child_call_stack);
129 }
130 
131 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
132 {
133 }
134 
135 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
136 {
137  return viewport_implementation::find_at(this, coordinate, must_be_active);
138 }
139 
141  const bool must_be_active) const
142 {
143  return viewport_implementation::find_at(this, coordinate, must_be_active);
144 }
145 
146 widget* viewport::find(const std::string& id, const bool must_be_active)
147 {
148  return viewport_implementation::find(this, id, must_be_active);
149 }
150 
151 const widget* viewport::find(const std::string& id, const bool must_be_active)
152  const
153 {
154  return viewport_implementation::find(this, id, must_be_active);
155 }
156 
158 {
159  return widget_->get_best_size();
160 }
161 
163 {
164  return false;
165 }
166 
168 {
169  /**
170  * @todo Implement properly.
171  */
172  return nullptr;
173 }
174 
175 // }---------- BUILDER -----------{
176 
177 namespace implementation
178 {
179 
180 builder_viewport::builder_viewport(const config& cfg)
181  : builder_widget(cfg)
182  , widget_(create_widget_builder(cfg.child("widget", "[viewport]")))
183 {
184 }
185 
186 std::unique_ptr<widget> builder_viewport::build() const
187 {
188  return build(replacements_map());
189 }
190 
191 std::unique_ptr<widget> builder_viewport::build(const replacements_map& replacements) const
192 {
193  return std::make_unique<viewport>(*this, replacements);
194 }
195 
196 } // namespace implementation
197 
198 // }------------ END --------------
199 
200 } // namespace gui2
Define the common log macros for the gui toolkit.
Contains the info needed to instantiate a widget.
int get_x() const
Definition: widget.cpp:316
std::unique_ptr< class walker_base > walker_ptr
Definition: widget.hpp:39
std::unique_ptr< widget > widget_
Definition: viewport.hpp:99
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:168
virtual std::unique_ptr< widget > build() const override
Definition: viewport.cpp:186
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:40
Base class for all widgets.
Definition: widget.hpp:49
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
Definitions for the interface to Wesnoth Markup Language (WML).
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: viewport.cpp:131
virtual void impl_draw_children(int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: viewport.cpp:110
int x
x coordinate.
Definition: point.hpp:45
Generic file dialog.
Definition: field-fwd.hpp:23
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:239
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:146
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:94
static thread_local std::deque< std::string > call_stack
For printing error messages when WFL parsing or evaluation fails, this contains the names of the WFL ...
Definition: function.cpp:47
virtual widget * find_at(const point &coordinate, const bool must_be_active) override
See widget::find_at.
Definition: viewport.cpp:135
int get_y() const
Definition: widget.cpp:321
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:66
A viewport is an special widget used to view only a part of the widget it &#39;holds&#39;.
Definition: viewport.hpp:45
The user set the widget invisible, that means:
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:82
Holds a 2D point.
Definition: point.hpp:24
static utils::const_clone_ptr< widget, W > find(W viewport, const std::string &id, const bool must_be_active)
Definition: viewport.cpp:72
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: viewport.cpp:101
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:162
virtual iteration::walker_ptr create_walker() override
See widget::create_walker.
Definition: viewport.cpp:167
static utils::const_clone_ptr< widget, W > find_at(W viewport, point coordinate, const bool must_be_active)
Implementation for the wrappers for [const] widget* pane::find_at(const point&, const bool) [const]...
Definition: viewport.cpp:50
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: viewport.cpp:124
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
map_location coordinate
Contains an x and y coordinate used for starting positions in maps.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60
int y
y coordinate.
Definition: point.hpp:48
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:66
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:157
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
builder_widget_ptr create_widget_builder(const config &cfg)
Create a widget builder.