1 /++ 2 An module for describing animation 3 (frame-by-frame, by changing rendering objects among themselves). 4 5 Macros: 6 LREF = <a href="#$1">$1</a> 7 HREF = <a href="$1">$2</a> 8 9 Authors: $(HREF https://github.com/TodNaz,TodNaz) 10 Copyright: Copyright (c) 2020 - 2021, TodNaz. 11 License: $(HREF https://github.com/TodNaz/Tida/blob/master/LICENSE,MIT) 12 +/ 13 module tida.animation; 14 15 import tida.drawable; 16 17 /++ 18 An object for describing animation 19 (frame-by-frame, by changing rendering objects among themselves). 20 21 The work of such animation takes place not only with images, but with 22 drawn objects, implemented through IDrawableEx. 23 +/ 24 class Animation : IDrawable, IDrawableEx 25 { 26 import tida.vector; 27 import tida.color; 28 import tida.render; 29 30 private: 31 float _current = 0.0f; 32 33 public: 34 /// Animation speed. 35 float speed = 0.0f; 36 37 /// Animation frames. 38 IDrawableEx[] frames = []; 39 40 /// When the animation ends, whether the animation needs to be re-run. 41 bool isRepeat = false; 42 43 @safe: 44 /++ 45 46 +/ 47 this(float speed = 0.0f, bool isRepeat = false) 48 { 49 this.speed = speed; 50 this.isRepeat = isRepeat; 51 } 52 53 /++ 54 The current frame of the animation. 55 +/ 56 @property IDrawableEx currentFrame() nothrow pure 57 { 58 return frames[cast(size_t) _current > $ - 1 ? $ - 1 : cast(size_t) _current]; 59 } 60 61 /++ 62 The current position of the animation. 63 +/ 64 @property float numFrame() nothrow pure 65 { 66 return _current; 67 } 68 69 /++ 70 The current position of the animation. 71 +/ 72 @property void numFrame(float currFrame) nothrow pure 73 { 74 _current = currFrame; 75 } 76 77 /++ 78 Starts the animation from the beginning. 79 +/ 80 void reset() nothrow pure 81 { 82 _current = 0.0f; 83 } 84 85 /++ 86 Carries out animation of objects by frame-by-frame change. 87 88 Returns: 89 The current frame of animation. 90 +/ 91 IDrawableEx step() nothrow pure 92 { 93 if (cast(int) _current >= frames.length) 94 { 95 if (isRepeat) 96 { 97 _current = -speed; 98 } 99 } else 100 { 101 _current += speed; 102 } 103 104 return currentFrame(); 105 } 106 107 override: 108 void draw(IRenderer renderer, Vecf position) 109 { 110 IDrawableEx draws = step(); 111 112 draws.drawEx(renderer, position, 0.0, vecfNaN, vecfNaN, 255, rgb(255, 255, 255)); 113 } 114 115 void drawEx(IRenderer renderer, 116 Vecf position, 117 float angle, 118 Vecf center, 119 Vecf size, 120 ubyte alpha, 121 Color!ubyte color = rgb(255, 255, 255)) 122 { 123 IDrawableEx draws = step(); 124 125 draws.drawEx(renderer, position, angle, center, size, alpha, color); 126 } 127 }