1 /*
2     Copyright © 2022, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module inochi2d.core.automation.sine;
8 import inochi2d.core.automation;
9 import inochi2d;
10 import std.math;
11 
12 enum SineType {
13     Sin,
14     Cos,
15     Tan
16 }
17 
18 @TypeId("sine")
19 class SineAutomation : Automation {
20 protected:
21     override
22     void onUpdate() {
23         foreach(ref binding; bindings) {
24             float wave;
25             switch(sineType) {
26                 case SineType.Sin:
27                     wave = this.remapRange((sin((currentTime()*speed)+phase)+1.0)/2f, binding.range);
28                     break;
29                 case SineType.Cos:
30                     wave = this.remapRange((cos((currentTime()*speed)+phase)+1.0)/2f, binding.range);
31                     break;
32                 case SineType.Tan:
33                     wave = this.remapRange((tan((currentTime()*speed)+phase)+1.0)/2f, binding.range);
34                     break;
35                 default: assert(0);
36             }
37 
38             binding.addAxisOffset(wave);
39         }
40     }
41 
42     override
43     void serializeSelf(ref InochiSerializer serializer) {
44         serializer.putKey("speed");
45         serializer.putValue(speed);
46         serializer.putKey("sine_type");
47         serializer.putValue(cast(int)sineType);
48     }
49 
50     override
51     void deserializeSelf(Fghj data) {
52         data["speed"].deserializeValue(speed);
53         data["sine_type"].deserializeValue(sineType);
54     }
55 public:
56 
57     /**
58         Speed of the wave
59     */
60     float speed = 1f;
61 
62     /**
63         The phase of the wave
64     */
65     float phase = 0f;
66 
67     /**
68         The type of wave
69     */
70     SineType sineType = SineType.Sin;
71 
72     this(Puppet parent) {
73         this.typeId = "sine";
74         super(parent);
75     }
76 }
77 
78 mixin InAutomation!SineAutomation;