Elaztek Developer Hub
Blamite Game Engine - blam!  00406.12.10.23.1457.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 <string>
6 #include <Strings/components/utils/math/math.h>
7 #include <Strings/components/classes/events/events.h>
8 
11 
12 #ifndef BLAM
13 #define BLAM
14 #endif
15 
16 namespace Blam::Animation
17 {
21  class ColorTransitionAnimation : public BlamEventListener
22  {
23  private:
24  BlamColor evaluated_color;
25 
26  BlamColor color_1;
27  BlamColor color_2;
28 
29  float fade_duration = 1.0f;
30 
31  int ticks_per_fade = 0;
32  int local_ticks = 0;
33  bool swap_fade_direction = false;
34  float elapsed_time = 0.0f;
35 
36  bool loop = false;
37  bool animation_done = false;
38  bool is_started = false;
39  bool pause = false;
40 
49  byte GetIntermediaryColor(byte subcolor1, byte subcolor2)
50  {
51  byte blend_amount = abs((subcolor1 - subcolor2) * elapsed_time);
52 
53  if (subcolor1 < subcolor2)
54  {
55  return subcolor2 - blend_amount;
56  }
57  else
58  {
59  return subcolor2 + blend_amount;
60  }
61  }
62 
66  void OnTickEvent(TickEvent* event)
67  {
68  if (!pause)
69  {
70  if (loop)
71  {
72  local_ticks++;
73 
74  if (local_ticks == ticks_per_fade)
75  {
76  swap_fade_direction = !swap_fade_direction;
77  local_ticks = 1;
78  }
79 
80  elapsed_time = float(local_ticks) / float(ticks_per_fade);
81 
82  if (swap_fade_direction)
83  {
84  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
85  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
86  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
87  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
88  }
89  else
90  {
91  evaluated_color.r = GetIntermediaryColor(color_2.r, color_1.r);
92  evaluated_color.g = GetIntermediaryColor(color_2.g, color_1.g);
93  evaluated_color.b = GetIntermediaryColor(color_2.b, color_1.b);
94  evaluated_color.a = GetIntermediaryColor(color_2.a, color_1.a);
95  }
96  }
97  else
98  {
99  if (!animation_done)
100  {
101  local_ticks++;
102 
103  elapsed_time = float(local_ticks) / float(ticks_per_fade);
104 
105  evaluated_color.r = GetIntermediaryColor(color_1.r, color_2.r);
106  evaluated_color.g = GetIntermediaryColor(color_1.g, color_2.g);
107  evaluated_color.b = GetIntermediaryColor(color_1.b, color_2.b);
108  evaluated_color.a = GetIntermediaryColor(color_1.a, color_2.a);
109 
110  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)
111  {
112  animation_done = true;
113  }
114  }
115  else
116  {
117  BlamStrings::Events::UnregisterListener(this);
118  }
119  }
120  }
121  }
122 
123  public:
132  void Initialize(BlamColor _c1, BlamColor _c2, float fade_duration, bool _loop)
133  {
134  float full_ticks_per_fade = Blam::Tick::MaxTicksPerSecond() * fade_duration;
135 
136  ticks_per_fade = full_ticks_per_fade;
137 
138  if (!BlamStrings::Utils::Math::FloatIsInteger(full_ticks_per_fade))
139  {
140  Blam::Logger::LogEvent("color transition animation was configured to use fade duration of " + std::to_string(fade_duration) +
141  ", 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 "
142  + "this can cause unforeseen issues!", WSV_WARNING);
143  }
144 
145  color_1 = _c1;
146  color_2 = _c2;
147 
148  loop = _loop;
149 
150  evaluated_color = _c2;
151  }
152 
156  void Start()
157  {
158  if (!is_started)
159  {
160  this->Subscribe(BlamEventType::Tick);
161  BlamStrings::Events::RegisterListener(this, "color_transition_animation");
162 
163  is_started = true;
164  }
165  }
166 
170  BlamColor GetColor()
171  {
172  return evaluated_color;
173  }
174 
178  bool HasStarted()
179  {
180  return is_started;
181  }
182 
183  void Pause()
184  {
185  pause = true;
186  }
187 
188  void Resume()
189  {
190  pause = false;
191  }
192  };
193 }
Blam::Animation::ColorTransitionAnimation::GetColor
BlamColor GetColor()
Returns the evaluated color data.
Definition: ColorTransition.h:170
Blam::Logger::LogEvent
BLAM void LogEvent(std::string message)
Logs a message to the log and/or console.
Definition: aliases.cpp:130
Blam::Animation::ColorTransitionAnimation
Animation used to handle an alternating fade between two colors.
Definition: ColorTransition.h:21
logger.h
Blam::Animation::ColorTransitionAnimation::Initialize
void Initialize(BlamColor _c1, BlamColor _c2, float fade_duration, bool _loop)
Prepares data needed for the animation.
Definition: ColorTransition.h:132
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:208
tick.h
Blam::Animation::ColorTransitionAnimation::Pause
void Pause()
Definition: ColorTransition.h:183
Blam::Animation::ColorTransitionAnimation::Resume
void Resume()
Definition: ColorTransition.h:188
Blam::Animation::ColorTransitionAnimation::Start
void Start()
Instructs the listener to start animating.
Definition: ColorTransition.h:156
Blam::Animation::ColorTransitionAnimation::HasStarted
bool HasStarted()
Determines whether or not the animation has been started.
Definition: ColorTransition.h:178