Elaztek Developer Hub
Blamite Game Engine - blam!  00398.09.22.23.2015.blamite
The core library for the Blamite Game Engine.
ColorTransition.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../animation.h"
4 
5 #include <d2d1.h>
6 #include <string>
7 
9 #include <Strings/components/utils/math/math.h>
11 #include <Strings/components/classes/events/events.h>
12 
13 #ifndef BLAM
14 #define BLAM
15 #endif
16 
17 namespace Blam::Animation
18 {
24  class ColorTransitionAnimation : public BlamEventListener
25  {
26  private:
27  D2D1_COLOR_F evaluated_color;
28 
29  D2D1_COLOR_F color_1;
30  D2D1_COLOR_F color_2;
31 
32  float fade_duration = 1.0f;
33 
34  int ticks_per_fade = 0;
35  int local_ticks = 0;
36  bool swap_fade_direction = false;
37  float elapsed_time = 0.0f;
38 
39  bool loop = false;
40  bool animation_done = false;
41  bool is_started = false;
42  bool pause = false;
43 
52  float GetIntermediaryColor(float subcolor1, float subcolor2)
53  {
54  float blend_amount = fabsf((subcolor1 - subcolor2) * elapsed_time);
55 
56  if (subcolor1 < subcolor2)
57  {
58  return subcolor2 - blend_amount;
59  }
60  else
61  {
62  return subcolor2 + blend_amount;
63  }
64  }
65 
69  void OnTickEvent(TickEvent* event)
70  {
71  if (!pause)
72  {
73  if (loop)
74  {
75  local_ticks++;
76 
77  if (local_ticks == ticks_per_fade)
78  {
79  swap_fade_direction = !swap_fade_direction;
80  local_ticks = 1;
81  }
82 
83  elapsed_time = float(local_ticks) / float(ticks_per_fade);
84 
85  if (swap_fade_direction)
86  {
87  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
88  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
89  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
90  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
91  }
92  else
93  {
94  evaluated_color.r = GetIntermediaryColor(color_2.r, color_1.r);
95  evaluated_color.g = GetIntermediaryColor(color_2.g, color_1.g);
96  evaluated_color.b = GetIntermediaryColor(color_2.b, color_1.b);
97  evaluated_color.a = GetIntermediaryColor(color_2.a, color_1.a);
98  }
99  }
100  else
101  {
102  if (!animation_done)
103  {
104  local_ticks++;
105 
106  elapsed_time = float(local_ticks) / float(ticks_per_fade);
107 
108  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
109  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
110  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
111  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
112 
113  if (evaluated_color.r == color_1.r && evaluated_color.g == color_1.g && evaluated_color.b == color_1.b && evaluated_color.a == color_1.a)
114  {
115  animation_done = true;
116  }
117  }
118  else
119  {
120  BlamStrings::Events::UnregisterListener(this);
121  }
122  }
123  }
124  }
125 
126  public:
135  void Initialize(D2D1_COLOR_F _c1, D2D1_COLOR_F _c2, float fade_duration, bool _loop)
136  {
137  float full_ticks_per_fade = Blam::Tick::MaxTicksPerSecond() * fade_duration;
138 
139  ticks_per_fade = full_ticks_per_fade;
140 
141  if (!BlamStrings::Utils::Math::FloatIsInteger(full_ticks_per_fade))
142  {
143  Blam::Logger::LogEvent("color transition animation was configured to use fade duration of " + std::to_string(fade_duration) +
144  ", which results in a tick per fade of " + std::to_string(full_ticks_per_fade) + " - the animation will not be properly fixed to a per-second timestep and "
145  + "this can cause unforeseen issues!", WSV_WARNING);
146  }
147 
148  color_1 = _c1;
149  color_2 = _c2;
150 
151  loop = _loop;
152 
153  evaluated_color = _c2;
154  }
155 
159  void Start()
160  {
161  if (!is_started)
162  {
163  this->Subscribe(BlamEventType::Tick);
164  BlamStrings::Events::RegisterListener(this, "color_transition_animation");
165 
166  is_started = true;
167  }
168  }
169 
173  D2D1_COLOR_F GetColor()
174  {
175  return evaluated_color;
176  }
177 
181  bool HasStarted()
182  {
183  return is_started;
184  }
185 
186  void Pause()
187  {
188  pause = true;
189  }
190 
191  void Resume()
192  {
193  pause = false;
194  }
195  };
196 }
Blam::Logger::LogEvent
BLAM void LogEvent(std::string message)
Logs a message to the log and/or console.
Definition: aliases.cpp:142
Blam::Animation::ColorTransitionAnimation
Animation used to handle an alternating fade between two colors.
Definition: ColorTransition.h:24
logger.h
Blam::Animation
Namespace for animation data.
Definition: animation.h:12
Blam::Tick::MaxTicksPerSecond
BLAM int MaxTicksPerSecond()
Retrieves the maximum number of ticks per second.
Definition: tick.cpp:152
Blam::Animation::ColorTransitionAnimation::GetColor
D2D1_COLOR_F GetColor()
Returns the evaluated color data.
Definition: ColorTransition.h:173
Blam::Animation::ColorTransitionAnimation::Initialize
void Initialize(D2D1_COLOR_F _c1, D2D1_COLOR_F _c2, float fade_duration, bool _loop)
Prepares data needed for the animation.
Definition: ColorTransition.h:135
tick.h
Blam::Animation::ColorTransitionAnimation::Pause
void Pause()
Definition: ColorTransition.h:186
Blam::Animation::ColorTransitionAnimation::Resume
void Resume()
Definition: ColorTransition.h:191
Blam::Animation::ColorTransitionAnimation::Start
void Start()
Instructs the listener to start animating.
Definition: ColorTransition.h:159
Blam::Animation::ColorTransitionAnimation::HasStarted
bool HasStarted()
Determines whether or not the animation has been started.
Definition: ColorTransition.h:181