The Battle for Wesnoth  1.17.0-dev
viewport.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2012 - 2021
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 
20 #include "gui/core/log.hpp"
21 #include "config.hpp"
22 #include "utils/const_clone.hpp"
23 
24 #define LOG_SCOPE_HEADER "viewport [" + id() + "] " + __func__
25 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
26 
27 namespace gui2
28 {
29 
30 // ------------ WIDGET -----------{
31 
32 /**
33  * Helper to implement private functions without modifying the header.
34  *
35  * The class is a helper to avoid recompilation and only has static
36  * functions. It also facilitates to create duplicates of functions for a const
37  * and a non-const member function.
38  */
40 {
41  /**
42  * Implementation for the wrappers for
43  * [const] widget* pane::find_at(const point&, const bool) [const].
44  *
45  * @tparam W A pointer to the pane.
46  */
47  template <class W>
49  find_at(W viewport, point coordinate, const bool must_be_active)
50  {
51 
52  /*
53  * First test whether the mouse is at the pane.
54  */
55  if(viewport->widget::find_at(coordinate, must_be_active) != viewport) {
56  return nullptr;
57  }
58 
59  /*
60  * The widgets are placed at coordinate 0,0 so adjust the offset to
61  * that coordinate system.
62  */
63  coordinate.x -= viewport->get_x();
64  coordinate.y -= viewport->get_y();
65 
66  return viewport->widget_.find_at(coordinate, must_be_active);
67  }
68 
69  template <class W>
71  find(W viewport, const std::string& id, const bool must_be_active)
72  {
73  if(viewport->widget::find(id, must_be_active)) {
74  return viewport;
75  } else {
76  return viewport->widget_.find(id, must_be_active);
77  }
78  }
79 };
80 
82  const builder_widget::replacements_map& replacements)
83  : widget(builder)
84  , widget_(*builder.widget_->build(replacements))
85  , owns_widget_(true)
86 {
87  widget_.set_parent(this);
88 }
89 
91 {
92  if(owns_widget_) {
93  delete &widget_;
94  }
95 }
96 
98  const builder_widget::replacements_map& replacements)
99 {
100  return new viewport(builder, replacements);
101 }
102 
103 void viewport::place(const point& origin, const point& size)
104 {
105  widget::place(origin, size);
106 
108 }
109 
110 void viewport::layout_initialize(const bool full_initialization)
111 {
112  widget::layout_initialize(full_initialization);
113 
115  widget_.layout_initialize(full_initialization);
116  }
117 }
118 
119 void
120 viewport::impl_draw_children(surface& frame_buffer, int x_offset, int y_offset)
121 {
122  x_offset += get_x();
123  y_offset += get_y();
124 
126  widget_.draw_background(frame_buffer, x_offset, y_offset);
127  widget_.draw_children(frame_buffer, x_offset, y_offset);
128  widget_.draw_foreground(frame_buffer, x_offset, y_offset);
129  widget_.set_is_dirty(false);
130  }
131 }
132 
133 void
135  const std::vector<widget*>& call_stack)
136 {
137  std::vector<widget*> child_call_stack = call_stack;
138  widget_.populate_dirty_list(caller, child_call_stack);
139 }
140 
141 void viewport::request_reduce_width(const unsigned /*maximum_width*/)
142 {
143 }
144 
145 widget* viewport::find_at(const point& coordinate, const bool must_be_active)
146 {
147  return viewport_implementation::find_at(this, coordinate, must_be_active);
148 }
149 
151  const bool must_be_active) const
152 {
153  return viewport_implementation::find_at(this, coordinate, must_be_active);
154 }
155 
156 widget* viewport::find(const std::string& id, const bool must_be_active)
157 {
158  return viewport_implementation::find(this, id, must_be_active);
159 }
160 
161 const widget* viewport::find(const std::string& id, const bool must_be_active)
162  const
163 {
164  return viewport_implementation::find(this, id, must_be_active);
165 }
166 
168 {
169  return widget_.get_best_size();
170 }
171 
173 {
174  return false;
175 }
176 
178 {
179  /**
180  * @todo Implement properly.
181  */
182  return nullptr;
183 }
184 
185 // }---------- BUILDER -----------{
186 
187 namespace implementation
188 {
189 
190 builder_viewport::builder_viewport(const config& cfg)
191  : builder_widget(cfg)
192  , widget_(create_widget_builder(cfg.child("widget", "[viewport]")))
193 {
194 }
195 
197 {
198  return build(replacements_map());
199 }
200 
202 {
203  return viewport::build(*this, replacements);
204 }
205 
206 } // namespace implementation
207 
208 // }------------ END --------------
209 
210 } // 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:315
void set_parent(widget *parent)
Definition: widget.cpp:155
virtual void layout_initialize(const bool full_initialization)
How the layout engine works.
Definition: widget.cpp:167
visibility get_visible() const
Definition: widget.cpp:503
Helper to implement private functions without modifying the header.
Definition: viewport.cpp:39
Base class for all widgets.
Definition: widget.hpp:49
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:141
int x
x coordinate.
Definition: point.hpp:45
Generic file dialog.
Definition: field-fwd.hpp:23
void populate_dirty_list(window &caller, std::vector< widget *> &call_stack)
Adds a widget to the dirty list if it is dirty.
Definition: widget.cpp:417
widget & widget_
Definition: viewport.hpp:105
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
std::map< std::string, std::shared_ptr< builder_widget > > replacements_map
The replacements type is used to define replacement types.
point get_best_size() const
Gets the best size for the widget.
Definition: widget.cpp:193
virtual void place(const point &origin, const point &size)
Places the widget.
Definition: widget.cpp:238
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
Definition: viewport.cpp:156
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:466
virtual void place(const point &origin, const point &size) override
See widget::place.
Definition: viewport.cpp:103
static viewport * build(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:97
The walker abstract base class.
Definition: walker.hpp:27
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:145
void draw_background(surface &frame_buffer, int x_offset, int y_offset)
Draws the background of a widget.
Definition: widget.cpp:370
int get_y() const
Definition: widget.cpp:320
typename const_clone< D, S >::pointer const_clone_ptr
Definition: const_clone.hpp:66
virtual void impl_draw_children(surface &frame_buffer, int x_offset, int y_offset) override
See widget::impl_draw_children.
Definition: viewport.cpp:120
A viewport is an special widget used to view only a part of the widget it &#39;holds&#39;.
Definition: viewport.hpp:45
virtual iteration::walker_base * create_walker() override
See widget::create_walker.
Definition: viewport.cpp:177
The user set the widget invisible, that means:
viewport(const implementation::builder_viewport &builder, const builder_widget::replacements_map &replacements)
Definition: viewport.cpp:81
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:71
virtual void layout_initialize(const bool full_initialization) override
See widget::layout_initialize.
Definition: viewport.cpp:110
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: viewport.cpp:172
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:49
virtual void child_populate_dirty_list(window &caller, const std::vector< widget *> &call_stack) override
See widget::child_populate_dirty_list.
Definition: viewport.cpp:134
void draw_foreground(surface &frame_buffer, int x_offset, int y_offset)
Draws the foreground of the widget.
Definition: widget.cpp:402
std::unique_ptr< window > build(const builder_window::window_resolution &definition)
Builds a window.
void draw_children(surface &frame_buffer, int x_offset, int y_offset)
Draws the children of a widget.
Definition: widget.cpp:387
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:61
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:65
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: viewport.cpp:167
virtual widget * build() const override
Definition: viewport.cpp:196
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.