The Battle for Wesnoth  1.17.23+dev
drawing.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2023
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/drawing.hpp"
19 
22 
24 #include "gui/widgets/settings.hpp"
25 
26 #include "gettext.hpp"
27 #include "wml_exception.hpp"
28 
29 #include <functional>
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
36 REGISTER_WIDGET(drawing)
37 
38 drawing::drawing(const implementation::builder_drawing& builder)
39  : styled_widget(builder, type())
40  , best_size_(0, 0)
41 {
42 }
43 
45 {
46  return best_size_ != point() ? best_size_
48 }
49 
50 void drawing::request_reduce_width(const unsigned maximum_width)
51 {
52  if(best_size_ != point()) {
53  // This drawing is of fixed size, do nothing.
54  } else {
56  }
57 }
58 
59 void drawing::request_reduce_height(const unsigned maximum_height)
60 {
61  if(best_size_ != point()) {
62  // This drawing is of fixed size, do nothing.
63  } else {
65  }
66 }
67 
68 void drawing::set_active(const bool /*active*/)
69 {
70  /* DO NOTHING */
71 }
72 
73 bool drawing::get_active() const
74 {
75  return true;
76 }
77 
78 unsigned drawing::get_state() const
79 {
80  return 0;
81 }
82 
84 {
85  return false;
86 }
87 
88 // }---------- DEFINITION ---------{
89 
92 {
93  DBG_GUI_P << "Parsing drawing " << id;
94 
95  load_resolutions<resolution>(cfg);
96 }
97 
100 {
101  /*
102  * Note the order should be the same as the enum state_t in drawing.hpp.
103  * Normally the [draw] section is in the config, but for this widget the
104  * original draw section is ignored, so send a dummy.
105  */
106  static const config dummy("draw");
107  state.emplace_back(dummy);
108 }
109 
110 // }---------- BUILDER -----------{
111 
112 namespace implementation
113 {
114 
115 builder_drawing::builder_drawing(const config& cfg)
116  : builder_styled_widget(cfg)
117  , width(cfg["width"])
118  , height(cfg["height"])
119  , draw(VALIDATE_WML_CHILD(cfg, "draw", _("Missing [draw] in drawing")))
120 {
121  assert(!draw.empty());
122 }
123 
124 std::unique_ptr<widget> builder_drawing::build() const
125 {
126  auto widget = std::make_unique<drawing>(*this);
127 
129 
130  const unsigned w = width(size);
131  const unsigned h = height(size);
132 
133  if(w || h) {
134  widget->set_best_size(point(w, h));
135  }
136 
137  widget->set_drawing_data(draw);
138 
139  DBG_GUI_G << "Window builder: placed drawing '" << id
140  << "' with definition '" << definition << "'.";
141 
142  return widget;
143 }
144 
145 } // namespace implementation
146 
147 // }------------ END --------------
148 
149 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
A drawing is widget with a fixed size and gives access to the canvas of the widget in the window inst...
Definition: drawing.hpp:53
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: drawing.cpp:83
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: drawing.cpp:50
point best_size_
When we're used as a fixed size item, this holds the best size.
Definition: drawing.hpp:118
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Definition: drawing.cpp:59
virtual bool get_active() const override
See styled_widget::get_active.
Definition: drawing.cpp:73
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: drawing.cpp:68
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: drawing.cpp:44
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: drawing.cpp:78
Base class for all visible items.
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Base class for all widgets.
Definition: widget.hpp:54
int w
static std::string _(const char *str)
Definition: gettext.hpp:93
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
Definition: draw.hpp:43
void point(int x, int y)
Draw a single point.
Definition: draw.cpp:203
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:96
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
This file contains the settings handling of the widget library.
drawing_definition(const config &cfg)
Definition: drawing.cpp:90
typed_formula< unsigned > height
The height of the widget.
Definition: drawing.hpp:158
typed_formula< unsigned > width
The width of the widget.
Definition: drawing.hpp:155
virtual std::unique_ptr< widget > build() const override
Definition: drawing.cpp:124
std::string definition
Parameters for the styled_widget.
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)
#define h