The Battle for Wesnoth  1.19.7+dev
frame.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
3  by Jeremy Rosen <jeremy.rosen@enst-bretagne.fr>
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 /**
17  * @file
18  * Frame for unit's animation sequence.
19  */
20 
21 #pragma once
22 
23 #include "units/frame_private.hpp"
24 
25 #include "color.hpp"
26 #include "drawing_layer.hpp"
27 #include "halo.hpp"
28 #include "picture.hpp"
29 #include "utils/optional_fwd.hpp"
30 
31 #include <boost/logic/tribool.hpp>
32 
33 #include <chrono>
34 
35 class config;
36 
37 constexpr int get_abs_frame_layer(drawing_layer layer)
38 {
39  return int(layer) - int(drawing_layer::unit_first);
40 }
41 
42 /** All parameters from a frame at a given instant */
44 {
45  std::chrono::milliseconds duration{0};
46 
49 
50  std::string image_mod;
51  std::string halo;
52 
53  int halo_x = 0;
54  int halo_y = 0;
55 
56  std::string halo_mod;
57  std::string sound;
58  std::string text;
59 
60  utils::optional<color_t> text_color;
61  utils::optional<color_t> blend_with;
62 
63  double blend_ratio = 0.0;
64  double highlight_ratio = 1.0;
65  double offset = 0.0;
66  double submerge = 0.0;
67 
68  int x = 0;
69  int y = 0;
70  int directional_x = 0;
71  int directional_y = 0;
72 
73  boost::tribool auto_vflip = boost::logic::indeterminate;
74  boost::tribool auto_hflip = boost::logic::indeterminate;
75  boost::tribool primary_frame = boost::logic::indeterminate;
76 
78 };
79 
80 /**
81  * Easily build frame parameters with the serialized constructors
82  */
84 {
85 public:
86  frame_builder();
87  frame_builder(const config& cfg, const std::string& frame_string = "");
88 
89  /** Allow easy chained modifications. Will raised assert if used after initialization */
90  frame_builder& duration(const std::chrono::milliseconds& duration);
91  frame_builder& image(const std::string& image, const std::string& image_mod = "");
92  frame_builder& image_diagonal(const std::string& image_diagonal, const std::string& image_mod = "");
93  frame_builder& sound(const std::string& sound);
94  frame_builder& text(const std::string& text, const color_t text_color);
95  frame_builder& halo(const std::string& halo, const std::string& halo_x, const std::string& halo_y, const std::string& halo_mod);
96  frame_builder& blend(const std::string& blend_ratio, const color_t blend_color);
97  frame_builder& highlight(const std::string& highlight);
98  frame_builder& offset(const std::string& offset);
99  frame_builder& submerge(const std::string& submerge);
100  frame_builder& x(const std::string& x);
101  frame_builder& y(const std::string& y);
102  frame_builder& directional_x(const std::string& directional_x);
103  frame_builder& directional_y(const std::string& directional_y);
104  frame_builder& auto_vflip(const bool auto_vflip);
105  frame_builder& auto_hflip(const bool auto_hflip);
107  frame_builder& drawing_layer(const std::string& drawing_layer);
108 
109 private:
111 
112  std::chrono::milliseconds duration_;
113 
114  std::string image_;
115  std::string image_diagonal_;
116  std::string image_mod_;
117  std::string halo_;
118  std::string halo_x_;
119  std::string halo_y_;
120  std::string halo_mod_;
121  std::string sound_;
122  std::string text_;
123 
124  utils::optional<color_t> text_color_;
125  utils::optional<color_t> blend_with_;
126 
127  std::string blend_ratio_;
128  std::string highlight_ratio_;
129  std::string offset_;
130  std::string submerge_;
131  std::string x_;
132  std::string y_;
133  std::string directional_x_;
134  std::string directional_y_;
135 
136  boost::tribool auto_vflip_;
137  boost::tribool auto_hflip_;
138  boost::tribool primary_frame_;
139 
140  std::string drawing_layer_;
141 };
142 
143 /**
144  * Keep most parameters in a separate class to simplify the handling of the large
145  * number of parameters between the frame level and animation level.
146  */
148 {
149 public:
151  const std::chrono::milliseconds& override_duration = std::chrono::milliseconds{0});
152 
153  void override(const std::chrono::milliseconds& duration,
154  const std::string& highlight = "",
155  const std::string& blend_ratio = "",
156  color_t blend_color = {0,0,0},
157  const std::string& offset = "",
158  const std::string& layer = "",
159  const std::string& modifiers = "");
160 
161  /** Getters for the different parameters */
162  frame_parameters parameters(const std::chrono::milliseconds& current_time) const;
163 
164  const std::chrono::milliseconds& duration() const { return duration_; }
165  bool does_not_change() const;
166  bool need_update() const;
167 
168  /** Contents of frame in strings */
169  std::vector<std::string> debug_strings() const;
170 
171 private:
172  std::chrono::milliseconds duration_;
173 
176 
177  std::string image_mod_;
178 
182 
183  std::string halo_mod_;
184  std::string sound_;
185  std::string text_;
186 
187  utils::optional<color_t> text_color_;
188  utils::optional<color_t> blend_with_;
189 
198 
199  boost::tribool auto_vflip_;
200  boost::tribool auto_hflip_;
201  boost::tribool primary_frame_;
202 
204 };
205 
206 /** Describes a unit's animation sequence. */
208 {
209 public:
210  // Constructors
211  unit_frame(const frame_builder& builder = frame_builder()) : builder_(builder) {}
212 
213  void redraw(const std::chrono::milliseconds& frame_time, bool on_start_time, bool in_scope_of_frame, const map_location& src, const map_location& dst,
214  halo::handle& halo_id, halo::manager& halo_man, const frame_parameters& animation_val, const frame_parameters& engine_val) const;
215 
216  frame_parameters merge_parameters(const std::chrono::milliseconds& current_time,
217  const frame_parameters& animation_val,
218  const frame_parameters& engine_val = frame_parameters()) const;
219 
220  frame_parameters parameters(const std::chrono::milliseconds& current_time) const
221  {
222  return builder_.parameters(current_time);
223  }
224 
226  {
227  return builder_.parameters(duration());
228  }
229 
230  const std::chrono::milliseconds& duration() const
231  {
232  return builder_.duration();
233  }
234 
235  bool does_not_change() const
236  {
237  return builder_.does_not_change();
238  }
239 
240  bool need_update() const
241  {
242  return builder_.need_update();
243  }
244 
245  std::vector<std::string> debug_strings() const
246  {
247  // Contents of frame in strings
248  return builder_.debug_strings();
249  }
250 
251  std::set<map_location> get_overlaped_hex(const std::chrono::milliseconds& frame_time, const map_location& src, const map_location& dst,
252  const frame_parameters& animation_val, const frame_parameters& engine_val) const;
253 
254 private:
256 };
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:172
Easily build frame parameters with the serialized constructors.
Definition: frame.hpp:84
std::string offset_
Definition: frame.hpp:129
std::string halo_y_
Definition: frame.hpp:119
std::string image_mod_
Definition: frame.hpp:116
utils::optional< color_t > blend_with_
Definition: frame.hpp:125
std::string halo_mod_
Definition: frame.hpp:120
std::string image_diagonal_
Definition: frame.hpp:115
frame_builder()
Definition: frame.cpp:28
boost::tribool auto_vflip_
Definition: frame.hpp:136
boost::tribool primary_frame_
Definition: frame.hpp:138
frame_builder & auto_hflip(const bool auto_hflip)
Definition: frame.cpp:208
std::string halo_
Definition: frame.hpp:117
frame_builder & highlight(const std::string &highlight)
Definition: frame.cpp:160
std::string sound_
Definition: frame.hpp:121
frame_builder & image_diagonal(const std::string &image_diagonal, const std::string &image_mod="")
Definition: frame.cpp:118
frame_builder & directional_x(const std::string &directional_x)
Definition: frame.cpp:190
frame_builder & submerge(const std::string &submerge)
Definition: frame.cpp:172
std::string image_
Definition: frame.hpp:114
std::string submerge_
Definition: frame.hpp:130
frame_builder & y(const std::string &y)
Definition: frame.cpp:184
frame_builder & primary_frame(const bool primary_frame)
Definition: frame.cpp:214
frame_builder & auto_vflip(const bool auto_vflip)
Definition: frame.cpp:202
std::string y_
Definition: frame.hpp:132
std::string highlight_ratio_
Definition: frame.hpp:128
boost::tribool auto_hflip_
Definition: frame.hpp:137
std::string blend_ratio_
Definition: frame.hpp:127
std::string text_
Definition: frame.hpp:122
std::string drawing_layer_
Definition: frame.hpp:140
frame_builder & drawing_layer(const std::string &drawing_layer)
Definition: frame.cpp:220
std::string directional_x_
Definition: frame.hpp:133
std::string directional_y_
Definition: frame.hpp:134
std::string halo_x_
Definition: frame.hpp:118
frame_builder & text(const std::string &text, const color_t text_color)
Definition: frame.cpp:131
frame_builder & blend(const std::string &blend_ratio, const color_t blend_color)
Definition: frame.cpp:153
frame_builder & x(const std::string &x)
Definition: frame.cpp:178
utils::optional< color_t > text_color_
Definition: frame.hpp:124
frame_builder & offset(const std::string &offset)
Definition: frame.cpp:166
frame_builder & directional_y(const std::string &directional_y)
Definition: frame.cpp:196
frame_builder & image(const std::string &image, const std::string &image_mod="")
Definition: frame.cpp:111
frame_builder & sound(const std::string &sound)
Definition: frame.cpp:125
std::string x_
Definition: frame.hpp:131
frame_builder & duration(const std::chrono::milliseconds &duration)
Allow easy chained modifications.
Definition: frame.cpp:147
frame_builder & halo(const std::string &halo, const std::string &halo_x, const std::string &halo_y, const std::string &halo_mod)
Definition: frame.cpp:138
std::chrono::milliseconds duration_
Definition: frame.hpp:112
Keep most parameters in a separate class to simplify the handling of the large number of parameters b...
Definition: frame.hpp:148
std::string text_
Definition: frame.hpp:185
progressive_int directional_x_
Definition: frame.hpp:196
progressive_int halo_y_
Definition: frame.hpp:181
boost::tribool auto_hflip_
Definition: frame.hpp:200
progressive_image image_
Definition: frame.hpp:174
boost::tribool primary_frame_
Definition: frame.hpp:201
bool need_update() const
Definition: frame.cpp:272
progressive_double blend_ratio_
Definition: frame.hpp:190
progressive_double highlight_ratio_
Definition: frame.hpp:191
progressive_double offset_
Definition: frame.hpp:192
boost::tribool auto_vflip_
Definition: frame.hpp:199
progressive_string halo_
Definition: frame.hpp:179
frame_parameters parameters(const std::chrono::milliseconds &current_time) const
Getters for the different parameters.
Definition: frame.cpp:277
utils::optional< color_t > blend_with_
Definition: frame.hpp:188
progressive_int x_
Definition: frame.hpp:194
std::string sound_
Definition: frame.hpp:184
std::chrono::milliseconds duration_
Definition: frame.hpp:172
progressive_double submerge_
Definition: frame.hpp:193
std::string halo_mod_
Definition: frame.hpp:183
std::string image_mod_
Definition: frame.hpp:177
progressive_int halo_x_
Definition: frame.hpp:180
progressive_int y_
Definition: frame.hpp:195
progressive_int drawing_layer_
Definition: frame.hpp:203
utils::optional< color_t > text_color_
Definition: frame.hpp:187
const std::chrono::milliseconds & duration() const
Definition: frame.hpp:164
std::vector< std::string > debug_strings() const
Contents of frame in strings.
Definition: frame.cpp:388
progressive_int directional_y_
Definition: frame.hpp:197
progressive_image image_diagonal_
Definition: frame.hpp:175
frame_parsed_parameters(const frame_builder &builder=frame_builder(), const std::chrono::milliseconds &override_duration=std::chrono::milliseconds{0})
Definition: frame.cpp:226
bool does_not_change() const
Definition: frame.cpp:253
Generic locator abstracting the location of an image.
Definition: picture.hpp:59
Describes a unit's animation sequence.
Definition: frame.hpp:208
bool need_update() const
Definition: frame.hpp:240
void redraw(const std::chrono::milliseconds &frame_time, bool on_start_time, bool in_scope_of_frame, const map_location &src, const map_location &dst, halo::handle &halo_id, halo::manager &halo_man, const frame_parameters &animation_val, const frame_parameters &engine_val) const
Definition: frame.cpp:630
const std::chrono::milliseconds & duration() const
Definition: frame.hpp:230
bool does_not_change() const
Definition: frame.hpp:235
std::vector< std::string > debug_strings() const
Definition: frame.hpp:245
frame_parameters parameters(const std::chrono::milliseconds &current_time) const
Definition: frame.hpp:220
frame_parameters end_parameters() const
Definition: frame.hpp:225
std::set< map_location > get_overlaped_hex(const std::chrono::milliseconds &frame_time, const map_location &src, const map_location &dst, const frame_parameters &animation_val, const frame_parameters &engine_val) const
Definition: frame.cpp:796
frame_parameters merge_parameters(const std::chrono::milliseconds &current_time, const frame_parameters &animation_val, const frame_parameters &engine_val=frame_parameters()) const
This function merges the value provided by:
Definition: frame.cpp:917
unit_frame(const frame_builder &builder=frame_builder())
Definition: frame.hpp:211
frame_parsed_parameters builder_
Definition: frame.hpp:255
drawing_layer
@ unit_default
Default layer for drawing units.
@ unit_first
Reserve layers to be selected for wml.
constexpr int get_abs_frame_layer(drawing_layer layer)
Definition: frame.hpp:37
Definition: halo.cpp:39
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
Functions to load and save images from/to disk.
Audio output for sound and music.
Definition: sound.cpp:42
rect dst
Location on the final composed sheet.
rect src
Non-transparent portion of the surface to compose.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
All parameters from a frame at a given instant.
Definition: frame.hpp:44
std::string halo
Definition: frame.hpp:51
utils::optional< color_t > blend_with
Definition: frame.hpp:61
double highlight_ratio
Definition: frame.hpp:64
std::string text
Definition: frame.hpp:58
boost::tribool auto_hflip
Definition: frame.hpp:74
int directional_x
Definition: frame.hpp:70
double offset
Definition: frame.hpp:65
std::string sound
Definition: frame.hpp:57
std::string image_mod
Definition: frame.hpp:50
double submerge
Definition: frame.hpp:66
image::locator image_diagonal
Definition: frame.hpp:48
boost::tribool primary_frame
Definition: frame.hpp:75
int directional_y
Definition: frame.hpp:71
image::locator image
Definition: frame.hpp:47
std::chrono::milliseconds duration
Definition: frame.hpp:45
utils::optional< color_t > text_color
Definition: frame.hpp:60
double blend_ratio
Definition: frame.hpp:63
std::string halo_mod
Definition: frame.hpp:56
boost::tribool auto_vflip
Definition: frame.hpp:73
Encapsulates the map of the game.
Definition: location.hpp:45