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 Animation object constructor. 46 47 Params: 48 speed = Animation speed. How many ticks to skip for the next frame. 49 isRepeat = Whether to repeat the animation. 50 +/ 51 this(float speed = 0.0f, bool isRepeat = false) 52 { 53 this.speed = speed; 54 this.isRepeat = isRepeat; 55 } 56 57 /++ 58 The current frame of the animation. 59 +/ 60 @property IDrawableEx currentFrame() nothrow pure 61 { 62 return frames[cast(size_t) _current > $ - 1 ? $ - 1 : cast(size_t) _current]; 63 } 64 65 /++ 66 The current position of the animation. 67 +/ 68 @property float numFrame() nothrow pure 69 { 70 return _current; 71 } 72 73 /++ 74 The current position of the animation. 75 +/ 76 @property float numFrame(float currFrame) nothrow pure 77 { 78 return _current = currFrame; 79 } 80 81 /++ 82 Starts the animation from the beginning. 83 +/ 84 void reset() nothrow pure 85 { 86 _current = 0.0f; 87 } 88 89 /++ 90 Carries out animation of objects by frame-by-frame change. 91 92 Returns: 93 The current frame of animation. 94 +/ 95 IDrawableEx step() nothrow pure 96 { 97 if (cast(int) _current >= frames.length) 98 { 99 if (isRepeat) 100 { 101 _current = -speed; 102 } 103 } else 104 { 105 _current += speed; 106 } 107 108 return currentFrame(); 109 } 110 111 override: 112 void draw(IRenderer renderer, Vecf position) 113 { 114 IDrawableEx draws = step(); 115 116 draws.drawEx(renderer, position, 0.0, vecfNaN, vecfNaN, 255, rgb(255, 255, 255)); 117 } 118 119 void drawEx(IRenderer renderer, 120 Vecf position, 121 float angle, 122 Vecf center, 123 Vecf size, 124 ubyte alpha, 125 Color!ubyte color = rgb(255, 255, 255)) 126 { 127 IDrawableEx draws = step(); 128 129 draws.drawEx(renderer, position, angle, center, size, alpha, color); 130 } 131 }