1 module evael.graphics.gui.animations.TranslationAnimation; 2 3 import evael.graphics.gui.animations.Animation; 4 import evael.graphics.gui.controls.Control; 5 6 import dlib.math.vector; 7 8 /** 9 * TranslationAnimation. 10 */ 11 class TranslationAnimation : Animation 12 { 13 private vec2 m_targetPosition; 14 15 private vec2 m_translation; 16 17 private float m_incX, m_incY; 18 19 @nogc @safe 20 public this()(in auto ref vec2 translation) pure nothrow 21 { 22 super(); 23 24 this.m_translation = translation; 25 this.m_incX = 0.0f; 26 this.m_incY = 0.0f; 27 } 28 29 public override void update(in float deltaTime) 30 { 31 if (this.m_status != Status.Playing) 32 { 33 this.onAnimationStart(); 34 35 this.m_targetPosition = this.m_control.realPosition + this.m_translation; 36 37 // ( pxPerUpdate ) * deltaTime 38 this.m_incX = ( (this.m_targetPosition.x - this.m_control.realPosition.x) / (this.m_duration) ) * deltaTime; 39 this.m_incY = ( (this.m_targetPosition.y - this.m_control.realPosition.y) / (this.m_duration) ) * deltaTime; 40 } 41 42 auto position = this.m_control.realPosition; 43 44 this.m_control.realPosition = vec2(position.x + this.m_incX, position.y + this.m_incY); 45 46 import std.math; 47 48 // We check if we reached the target position 49 if (approxEqual(this.m_control.realPosition.x, this.m_targetPosition.x) 50 && approxEqual(this.m_control.realPosition.y, this.m_targetPosition.y)) 51 { 52 this.m_control.realPosition = this.m_targetPosition; 53 54 this.onAnimationEnd(); 55 return; 56 } 57 } 58 }