The Battle for Wesnoth  1.19.1+dev
drawing.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2024
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"
21 
22 #include "gettext.hpp"
23 #include "wml_exception.hpp"
24 
25 namespace gui2
26 {
27 
28 // ------------ WIDGET -----------{
29 
30 REGISTER_WIDGET(drawing)
31 
32 drawing::drawing(const implementation::builder_drawing& builder)
33  : styled_widget(builder, type())
34  , best_size_(0, 0)
35 {
36 }
37 
39 {
40  return best_size_ != point() ? best_size_
42 }
43 
44 void drawing::request_reduce_width(const unsigned maximum_width)
45 {
46  if(best_size_ != point()) {
47  // This drawing is of fixed size, do nothing.
48  } else {
50  }
51 }
52 
53 void drawing::request_reduce_height(const unsigned maximum_height)
54 {
55  if(best_size_ != point()) {
56  // This drawing is of fixed size, do nothing.
57  } else {
59  }
60 }
61 
62 void drawing::set_active(const bool /*active*/)
63 {
64  /* DO NOTHING */
65 }
66 
67 bool drawing::get_active() const
68 {
69  return true;
70 }
71 
72 unsigned drawing::get_state() const
73 {
74  return 0;
75 }
76 
78 {
79  return false;
80 }
81 
82 // }---------- DEFINITION ---------{
83 
86 {
87  DBG_GUI_P << "Parsing drawing " << id;
88 
89  load_resolutions<resolution>(cfg);
90 }
91 
94 {
95  /*
96  * Note the order should be the same as the enum state_t in drawing.hpp.
97  * Normally the [draw] section is in the config, but for this widget the
98  * original draw section is ignored, so send a dummy.
99  */
100  static const config dummy("draw");
101  state.emplace_back(dummy);
102 }
103 
104 // }---------- BUILDER -----------{
105 
106 namespace implementation
107 {
108 
109 builder_drawing::builder_drawing(const config& cfg)
110  : builder_styled_widget(cfg)
111  , width(cfg["width"])
112  , height(cfg["height"])
113  , draw(VALIDATE_WML_CHILD(cfg, "draw", missing_mandatory_wml_tag("drawing", "draw")))
114 {
115  assert(!draw.empty());
116 }
117 
118 std::unique_ptr<widget> builder_drawing::build() const
119 {
120  auto widget = std::make_unique<drawing>(*this);
121 
123 
124  const unsigned w = width(size);
125  const unsigned h = height(size);
126 
127  if(w || h) {
128  widget->set_best_size(point(w, h));
129  }
130 
131  widget->set_drawing_data(draw);
132 
133  DBG_GUI_G << "Window builder: placed drawing '" << id
134  << "' with definition '" << definition << "'.";
135 
136  return widget;
137 }
138 
139 } // namespace implementation
140 
141 // }------------ END --------------
142 
143 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
bool disable_click_dismiss() const override
See widget::disable_click_dismiss.
Definition: drawing.cpp:77
virtual void request_reduce_width(const unsigned maximum_width) override
See widget::request_reduce_width.
Definition: drawing.cpp:44
point best_size_
When we're used as a fixed size item, this holds the best size.
Definition: drawing.hpp:95
virtual void request_reduce_height(const unsigned maximum_height) override
See widget::request_reduce_height.
Definition: drawing.cpp:53
virtual bool get_active() const override
See styled_widget::get_active.
Definition: drawing.cpp:67
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: drawing.cpp:62
virtual point calculate_best_size() const override
See widget::calculate_best_size.
Definition: drawing.cpp:38
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: drawing.cpp:72
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:53
int w
#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:202
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:130
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:85
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
drawing_definition(const config &cfg)
Definition: drawing.cpp:84
typed_formula< unsigned > height
The height of the widget.
Definition: drawing.hpp:135
typed_formula< unsigned > width
The width of the widget.
Definition: drawing.hpp:132
virtual std::unique_ptr< widget > build() const override
Definition: drawing.cpp:118
std::string definition
Parameters for the styled_widget.
std::vector< state_definition > state
Holds a 2D point.
Definition: point.hpp:25
std::string missing_mandatory_wml_tag(const std::string &section, const std::string &tag)
Returns a standard message for a missing wml child (tag).
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