Mutator.h
1 /*
2 
3 Copyright (c) 2018, Johnathan Corkery. (jcorkery@umich.edu)
4 All rights reserved.
5 
6 This file is part of the Dynacoe project (https://github.com/jcorks/Dynacoe)
7 Dynacoe was released under the MIT License, as detailed below.
8 
9 
10 
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is furnished
16 to do so, subject to the following conditions:
17 
18 The above copyright notice and this permission notice shall
19 be included in all copies or substantial portions of the Software.
20 
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 
29 
30 
31 */
32 
33 #ifndef H_DC_COMPONENT_MUTATOR_INCLUDED
34 #define H_DC_COMPONENT_MUTATOR_INCLUDED
35 
36 
37 
38 
39 #include <Dynacoe/Component.h>
40 #include <Dynacoe/Components/Clock.h>
41 #include <set>
42 
43 
44 struct MutationState;
45 class StateContainer;
46 namespace Dynacoe {
47 
48 
51 
62 class Mutator : public Component {
63  public:
64 
68  enum class Function {
69  Constant,
70  Linear,
71  Quadratic,
72  SquareRoot,
73  Cubic,
74  Sinusoidal,
75  Logarithmic,
77  };
78 
79  Mutator();
86  Mutator(float beginValue,float endValue, Function f, float duration);
87  ~Mutator();
88 
89 
92  void Clear(float beginValue);
93 
98  void NewMutation(float when, float value, Function travelFunction);
99 
102  void NewRandomMutation(float when, float minValue, float MaxValue, Function travelFunction);
103 
104 
107  void SetMutationDestination(int mutationIndex, float newValue);
108 
109 
110 
115  void Start();
116 
120  void Stop();
121 
125  void Loop(bool);
126 
129  bool Expired();
130 
133  float GetCurrentTime();
134 
137  float GetEnd();
138 
141  float GetAt(float time);
142 
145  float Value();
146 
147 
150  enum class BindFunction {
151  Set,
152  Add,
153  Subtract,
154  Multiply,
155  Divide
156  };
157 
166  void Bind(float &, BindFunction f = BindFunction::Set);
167  void Bind(int &, BindFunction f = BindFunction::Set);
168  void Bind(uint8_t &, BindFunction f = BindFunction::Set);
169  void Bind(uint32_t &, BindFunction f = BindFunction::Set);
170  void Bind(uint64_t &, BindFunction f = BindFunction::Set);
171 
172  void Unbind(float &);
173  void Unbind(int &);
174  void Unbind(uint8_t &);
175  void Unbind(uint32_t &);
176  void Unbind(uint64_t &);
178 
179 
180 
181 
182 
186  static float StepTowards(float current,
187  float destination,
188  float step = .5f,
190  static Vector StepTowards(Vector current,
191  Vector destination,
192  float step = .5f,
194 
195 
196 
197  void OnStep();
198 
199 
200 
201 
202  std::string GetInfo();
203 
204 
205 
206 
207 
208 
209 
210 
211  private:
212  struct MutationState {
213  MutationState(float t, Mutator::Function f, float val, float min, float max, bool ran) :
214  time(t),
215  func(f),
216  value(val),
217  minValue(min),
218  maxValue(max),
219  isRandom(ran) {}
220  float time;
221  Mutator::Function func;
222  float value;
223  float minValue;
224  float maxValue;
225  bool isRandom;
226 
227  bool operator<(const MutationState & m) const {
228  return time < m.time;
229  }
230  };
231  class CompareMutationState {
232  public:
233  bool operator()(const MutationState * a, const MutationState *b) const {
234  return (*a < *b);
235  }
236  };
237 
238  std::set<MutationState*, CompareMutationState> states;
239  float begin;
240  Dynacoe::Clock timer;
241  bool isLooping;
242  bool hasBinds;
243  bool accountForLastStep;
244  MutationState * registr;
245 
246  typedef void (*BindFunctionBase)(void *, int type, float val);
247 
248  std::vector<std::pair<void*, BindFunctionBase>> * bindVectors;
249 
250  void Bind(void *, int, BindFunction);
251  void Unbind(void*, int);
252  static float compute(float first, float end, Function f, float ratio);
253 
254 };
255 
257 
258 
259 
260 
261 }
262 
263 
264 #endif
The state instantaneously changes to the next value.
static float StepTowards(float current, float destination, float step=.5f, Function f=Function::Linear)
Iteratively eases a value to another and returns the stepped value. This is known in other toolkits a...
void Clear(float beginValue)
Resets all mutations.
void NewMutation(float when, float value, Function travelFunction)
Sets a new point of mutation.
3D and 2D positional vector.
Definition: Vector.h:55
The state changes to random values between the state at the start and the end state.
void OnStep()
Function that is called upon each Run iteration. Component Run()s are run before the entity's run fun...
bool Expired()
Returns whether or not the terminal value has been reached.
The state changes in a logarithmic manner to the next value.
Definition: AssetID.h:37
The state alters in a cubic manner to the next value.
Timing class with millisecond resolution.
Definition: Clock.h:59
void Loop(bool)
Sets whether or not to automatically restart after the final value has been reached.
The state transforms in a reverse-quadratic manner to the next value.
void Bind(float &, BindFunction f=BindFunction::Set)
Binds a reference to the Mutator.
float GetAt(float time)
Returns the Mutator's mutation value at a specified time.
Updates the value with the current Value(), replacing the old one.
Function
For constructing the Mutator's mutations, you must specify a mutation function. These are the various...
Definition: Mutator.h:68
float GetCurrentTime()
Returns the mutator's progress in seconds.
The state linearly changes to the next value.
void Stop()
Stops the mutation.
void SetMutationDestination(int mutationIndex, float newValue)
Sets a new destination value for the mutation.
The state sinusoidally changes to the next value.
float Value()
Returns the current mutation value result.
The state quadratically transforms to the next value.
A piecewise function manager that changes inputs over time.
Definition: Mutator.h:62
float GetEnd()
Returns the duration of the mutation.
Class that extends the functionality of an Entity, but as a removable and addable object...
Definition: Component.h:66
void NewRandomMutation(float when, float minValue, float MaxValue, Function travelFunction)
Like NewMutation(), except the destination value is random chosen upon start(). min / max bound the r...
std::string GetInfo()
Returns a string containing human-readable information on the state of the component.
BindFunction
Tells how values should change when bound.
Definition: Mutator.h:150
void Start()
Begins the mutation lifetime.