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 serializeSelf(ref InochiSerializerCompact serializer) { 52 serializer.putKey("speed"); 53 serializer.putValue(speed); 54 serializer.putKey("sine_type"); 55 serializer.putValue(cast(int)sineType); 56 } 57 58 override 59 void deserializeSelf(Fghj data) { 60 data["speed"].deserializeValue(speed); 61 data["sine_type"].deserializeValue(sineType); 62 } 63 public: 64 65 /** 66 Speed of the wave 67 */ 68 float speed = 1f; 69 70 /** 71 The phase of the wave 72 */ 73 float phase = 0f; 74 75 /** 76 The type of wave 77 */ 78 SineType sineType = SineType.Sin; 79 80 this(Puppet parent) { 81 this.typeId = "sine"; 82 super(parent); 83 } 84 } 85 86 mixin InAutomation!SineAutomation;