ZhenjaMax

Untitled

Sep 20th, 2021
605
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --
  2. -- Simple String Animation - semi-hard-coded variant - you can alter the chars table - update the count and run it...
  3. --
  4.  
  5. -- The basic animation runtime controller. This is where you assign the active animation ( you could create a simple function to replace the animation table and nil count, index and expiry to extend this system to allow multiple animations -
  6. --  and since you can replace after a previous has been output, it would appear as though you were running different animations at the same time - that wouldn't be async compatible though )...
  7. -- So you can either activate the animation you want permanently, or create a simple function to update the animation table and reset the control variables... ie: ( function string.SetBasicAnimation( _tab_of_chars ) string.__basic_anim_runtime.anim = _tab_of_chars; string.__basic_anim_runtime.index = nil; string.__basic_anim_runtime.count = nil; string.__basic_anim_runtime.expiry = nil; end )
  8. string.__basic_anim_runtime = {
  9.     -- The animation - can not ping pong... requires full sequence. Resets to first after full sequence. Dots animation.. Requires center segment because of ping / pong
  10.     anim = { '.', '..', '...', '..' };
  11.  
  12.     -- Pipes animation - This performs a complete rotation, no need to add extra segments.
  13.     -- anim = { '|', '/', '─', '\\' };
  14.  
  15.     -- Stars - This is reversible so requires the center segments..
  16.     -- anim = { '⁎', '⁑', '⁂', '⁑' };
  17.  
  18.     -- Clock - This still needs to be ordered...
  19.     -- anim = { '🕛', '🕧', '🕐', '🕜', '🕑', '🕝', '🕒', '🕞', '🕓', '🕟', '🕔', '🕠', '🕕', '🕖', '🕗', '🕘', '🕙', '🕚', '🕡', '🕢', '🕣', '🕤', '🕥', '🕦' };
  20.  
  21.     -- Arrows - This does a complete circle and doesn't need to reverse
  22.     -- anim = { '⬍', '⬈', '➞', '⬊', '⬍', '⬋', '⬅', '⬉' };
  23.  
  24.     -- Bird Flying - this is reversible so it requires all.. 1 2 3 4 5 6 5 4 3 2
  25.     -- anim = { '︷', '︵', '︹', '︺', '︶', '︸', '︶', '︺', '︹', '︵' };
  26.  
  27.     -- Plants - Set as reversible, requires all..
  28.     -- anim = { '☘', '❀', '❁', '❀' };
  29.  
  30.     -- Eclipse - From Raku Throbber post author
  31.     -- anim = { '🌑', '🌒', '🌓', '🌔', '🌕', '🌖', '🌗', '🌘' };
  32. };
  33.  
  34.  
  35. --
  36. -- The basic animator function - accepts a numerical delay and a boolean backwards switch.. It only accepts a single animation from the helper local table above..
  37. --
  38. -- Argument - _delay - <Number> - Accepts a time, in seconds with fraction support, that designates how long a frame should last. Optional. If no number given, it uses the default value of 1 / 8 seconds.
  39. -- Argument - _play_backwards - <Boolean> - Toggles whether or not the animation plays backwards or forwards. Default is forwards. Optional.
  40. --
  41. -- RETURN: <String> - Character( s ) of the current animation frame - if the frame is invalid, it returns an empty string.
  42. -- RETURN: <Boolean> - Has Advanced Frame Controller - set to true if this call resulted in a new frame / index being assigned with new character( s )
  43. --
  44. function string.BasicAnimation( _delay, _play_backwards )
  45.     -- Setup delay - make sure it is a number and Reference our runtime var
  46.     local _delay = ( ( type( _delay ) == 'number' ) and _delay or 1 / 8 ), string.__basic_anim_runtime, os.clock( );
  47.  
  48.     -- cache our count so we count once per refresh.
  49.     _data.count = ( type( _data.count ) == 'number' ) and _data.count or #_data.anim;
  50.  
  51.     -- Setup our helpers...
  52.     local _expiry, _index, _chars, _count, _has_advanced_frame = _data.expiry, ( _data.index or ( _play_backwards and _data.count or 1 ) ), _data.anim, _data.count, false;
  53.  
  54.     -- If expiry has occurred, advance... Expiry can be nil the first call, this is ok because it will just use the first character - or the last if playing backwards.
  55.     if ( _expiry and _expiry < _time ) then
  56.         -- Advance..
  57.         _index, _has_advanced_frame = ( ( _index + ( 1 * ( _play_backwards and -1 or 1 ) ) ) % ( _count + 1 ) ), true;
  58.  
  59.         -- If 0, add 1 otherwise keep the same.
  60.         _index = _index < 1 and ( _play_backwards and _count or 1 ) or _index;
  61.  
  62.         -- Update the index
  63.         _data.index = _index;
  64.     end
  65.  
  66.     -- Update the trackers and output the char. -- Note: This is best done in the loop, but since we are checking the expiry above I decided to integrate it here.
  67.     _data.expiry = ( not _data.expiry or _has_advanced_frame ) and _time + _delay or _data.expiry;
  68.  
  69.     -- Return the character at the index or nothing.
  70.     return _chars[ _index ] or '', _has_advanced_frame;
  71. end
  72.  
  73.  
  74. --
  75. -- Helper / OPTIONAL FUNCTION - Updates the animation and resets the controlling variables
  76. --
  77. -- Argument: _tab - <Table> - This is the table containing the animation characters... ie: { '.', '..', '...', '..' } would be a valid entry. Requires at least 2 entries.
  78. --
  79. function string.SetBasicAnimation( _tab )
  80.     -- Prevent non tables, empty tables, or tables with only 1 entry.
  81.     if not ( type( _tab ) == 'table' and #_tab > 1 ) then return error( 'Can not update basic animation without argument #1 as a table, with at least 2 entries...' ); end
  82.  
  83.     -- Helper
  84.     local _data = string.__basic_anim_runtime;
  85.  
  86.     -- Update the animation table and Clear the controllers...
  87.     _data.anim, _data.count, _data.index, _data.expiry = _tab, nil, nil, nil;
  88. end
  89.  
RAW Paste Data