The Battle for Wesnoth  1.15.1+dev
animation_component.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 // This class encapsulates the animation functionality of unit.
16 
17 #pragma once
18 
19 #include "halo.hpp"
20 #include "units/animation.hpp" //Note: only needed for enum
21 
22 class config;
23 class unit;
24 class unit_drawer;
25 class unit_type;
26 
28 {
29 public:
30  /** States for animation. */
31  enum STATE {
32  STATE_STANDING, /** anim must fit in a hex */
33  STATE_FORGET, /** animation will be automatically replaced by a standing anim when finished */
34  STATE_ANIM}; /** normal anims */
35 
36  /** Default construct a unit animation component corresponding to a unit. */
38  u_(my_unit),
39  anim_(nullptr),
40  animations_(),
42  next_idling_(0),
44  draw_bars_(false),
45  refreshing_(false),
46  unit_halo_() {}
47 
48  /** Copy construct a unit animation component, for use when copy constructing a unit. */
50  u_(my_unit),
51  anim_(nullptr),
53  state_(o.state_),
54  next_idling_(0),
58  unit_halo_() {}
59 
60  /** Chooses an appropriate animation from the list of known animations. */
61  const unit_animation* choose_animation(const display& disp,
62  const map_location& loc, const std::string& event,
63  const map_location& second_loc = map_location::null_location(),
64  const int damage=0,
65  const unit_animation::hit_type hit_type = unit_animation::hit_type::INVALID,
66  const_attack_ptr attack=nullptr,const_attack_ptr second_attack = nullptr,
67  int swing_num =0);
68 
69  /** Sets the animation state to standing. */
70  void set_standing(bool with_bars = true);
71 
72  /** Sets the animation state to ghosted. (For use with whiteboard / planning mode.) */
73  void set_ghosted(bool with_bars = true);
74 
75  /** Whiteboard related somehow. TODO: Figure out exactly what this does. */
76  void set_disabled_ghosted(bool with_bars = true);
77 
78  /** Sets the animation state to idling. */
79  void set_idling();
80 
81  /** Sets the animation state to that when the unit is selected */
82  void set_selecting();
83 
84  /** Begin an animation. */
85  void start_animation (int start_time, const unit_animation *animation,
86  bool with_bars, const std::string &text = "",
87  color_t text_color = {}, STATE state = STATE_ANIM);
88 
89  /** Invalidates an animation with respect to a display object, preparing it for redraw. */
90  bool invalidate(const display & disp);
91 
92  /** Intermittently activates the idling animations in place of the standing animations. Used by display object. */
93  void refresh();
94 
95  /** Clear the haloes associated to the unit */
96  void clear_haloes();
97 
98  /** Resets the animations list after the unit is advanced. */
99  void reset_after_advance(const unit_type * newtype = nullptr);
100 
101  /** Adds an animation described by a config. Uses an internal cache to avoid redoing work. */
102  void apply_new_animation_effect(const config & effect);
103 
104  /** Get a pointer to the current animation. */
105  unit_animation* get_animation() const { return anim_.get(); }
106 
107  /** Get the flags of all registered animations. */
108  std::vector<std::string> get_flags();
109 
110  friend class unit;
111  friend class unit_drawer;
112 private:
113  const unit & u_; /**< A reference to the unit that owns this object. It does so with a scoped pointer, so this reference should not dangle. */
114 
115  std::unique_ptr<unit_animation> anim_; /**< The current animation. */
116  std::vector<unit_animation> animations_; /**< List of registered animations for this unit. */
117 
118  STATE state_; //!< animation state
119 
120  int next_idling_; //!< time for next idle animation
121  int frame_begin_time_; //!< time for the frame to begin
122 
123  bool draw_bars_; //!< bool indicating whether to draw bars with the unit
124  bool refreshing_; //!< avoid infinite recursion. flag used for drawing / animation
125 
126  halo::handle unit_halo_; //!< handle to the halo of this unit
127 };
void start_animation(int start_time, const unit_animation *animation, bool with_bars, const std::string &text="", color_t text_color={}, STATE state=STATE_ANIM)
Begin an animation.
bool refreshing_
avoid infinite recursion. flag used for drawing / animation
const unit & u_
A reference to the unit that owns this object.
This class represents a single unit of a specific type.
Definition: unit.hpp:99
void set_disabled_ghosted(bool with_bars=true)
Whiteboard related somehow.
std::unique_ptr< unit_animation > anim_
The current animation.
void set_selecting()
Sets the animation state to that when the unit is selected.
animation will be automatically replaced by a standing anim when finished
bool draw_bars_
bool indicating whether to draw bars with the unit
void refresh()
Intermittently activates the idling animations in place of the standing animations.
unit_animation * get_animation() const
Get a pointer to the current animation.
int frame_begin_time_
time for the frame to begin
A single unit type that the player may recruit.
Definition: types.hpp:42
unit_animation_component(unit &my_unit, const unit_animation_component &o)
Copy construct a unit animation component, for use when copy constructing a unit. ...
void clear_haloes()
Clear the haloes associated to the unit.
const unit_animation * choose_animation(const display &disp, const map_location &loc, const std::string &event, const map_location &second_loc=map_location::null_location(), const int damage=0, const unit_animation::hit_type hit_type=unit_animation::hit_type::INVALID, const_attack_ptr attack=nullptr, const_attack_ptr second_attack=nullptr, int swing_num=0)
Chooses an appropriate animation from the list of known animations.
STATE state_
animation state
bool invalidate(const display &disp)
Invalidates an animation with respect to a display object, preparing it for redraw.
STATE
States for animation.
Encapsulates the map of the game.
Definition: location.hpp:42
void set_idling()
Sets the animation state to idling.
std::vector< std::string > get_flags()
Get the flags of all registered animations.
void apply_new_animation_effect(const config &effect)
Adds an animation described by a config.
std::vector< unit_animation > animations_
List of registered animations for this unit.
unit_animation_component(unit &my_unit)
normal anims
static const map_location & null_location()
Definition: location.hpp:85
halo::handle unit_halo_
handle to the halo of this unit
void set_standing(bool with_bars=true)
Sets the animation state to standing.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
std::shared_ptr< const attack_type > const_attack_ptr
Definition: ptr.hpp:37
std::shared_ptr< halo_record > handle
Definition: halo.hpp:31
void set_ghosted(bool with_bars=true)
Sets the animation state to ghosted.
void reset_after_advance(const unit_type *newtype=nullptr)
Resets the animations list after the unit is advanced.
int next_idling_
time for next idle animation