Author Topic: About show text issue  (Read 6164 times)

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
About show text issue
« on: November 08, 2015, 07:59:28 PM »
I would like to show a text with a while (example 3 seconds)  and then disappear with fade out effect. How to do that? Thanks.  :)

omegaman

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 880
    • View Profile
Re: About show text issue
« Reply #1 on: November 08, 2015, 10:50:22 PM »
Have you taking a look at liquid8s animate or scrolling text module yet? He has a tutorial and some sample layouts here: https://github.com/liquid8d/attract-extra/tree/master/layouts

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: About show text issue
« Reply #2 on: November 10, 2015, 04:40:05 AM »
Have you taking a look at liquid8s animate or scrolling text module yet? He has a tutorial and some sample layouts here: https://github.com/liquid8d/attract-extra/tree/master/layouts

Many thanks and add it in newGrid theme  :)

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: About show text issue
« Reply #3 on: November 10, 2015, 04:22:36 PM »
Yes you should be able to use animate to do such a thing. It will probably be much easier when I (eventually) make the new version of animate available, since that allows for timeline animations. I also went ahead and put this module together as well.. could be used with text or other objects and has an optional fade in and fade out duration. Not on my github just yet, but you can just copy the text into /modules/showfade.nut and follow the instructions:

/modules/showfade.nut:
Code: [Select]
//////////////////////////////////////////////////
//
// Description:
//  ShowFade will display an object for a specified length of time, with an optional fade in and fade out.
//  Object is displayed at Transition.StartLayout, Transition.ToNewSelection and Transition.ToNewList.
//
// Usage:
//  fe.load_module("ShowFade");
//  local text = fe.add_text( "[Title]", 0, 0, fe.layout.width, 30 );
//
//  local fade_text = ShowFade(text);
//  fade_text.fade_in = 0;
//  fade_text.show_duration = 3000;
//  fade_text.fade_out = 1500;
//
///////////////////////////////////////

class ShowFade
{
    fade_in = 750;
    show_duration = 5000;
    fade_out = 750;
    is_fading = false;
    last_time = 0;
    ref = null;
    constructor( obj )
    {
        ref = obj;
        fe.add_transition_callback( this, "on_transition" );
        fe.add_ticks_callback( this, "on_tick" );
    }
   
    function on_transition( ttype, var, ttime )
    {
        switch( ttype )
        {
            case Transition.StartLayout:
            case Transition.ToNewSelection:
            case Transition.ToNewList:
                if ( ref == null ) ::print("ShowFade: warning: object is null!\n");
                is_fading = true;
                last_time = get_time();
                break;
        }
        return false;
    }
   
    function clamp ( value, min, max ) {
        if (value < min) value = min; if (value > max) value = max; return value;
    }
   
    function get_time()
    {
        return ::clock() * 1000;
    }
   
    function on_tick( ttime )
    {
        if ( is_fading )
        {
            local elapsed = get_time() - last_time;
            local duration = fade_in + show_duration + fade_out;
            local progress = elapsed / duration.tofloat();
            local in_progress = fade_in / duration.tofloat();
            local out_progress = ( fade_in + show_duration ) / duration.tofloat();
            local alpha_progress = 0;
            if ( progress > out_progress )
            {
                //fade_out
                alpha_progress = clamp( 1.0 - ( progress - out_progress ) / ( 1.0 - out_progress ), 0.0, 1.0 );
            } else if ( progress > in_progress )
            {
                //waiting
                alpha_progress = 1.0;
            } else
            {
                //fade_in
                alpha_progress = clamp( 1.0 - ( in_progress - progress ) / in_progress, 0.0, 1.0 );
            }
            if ( ref != null ) ref.alpha = alpha_progress * 255;
            if ( progress >= 1.0 )
            {
                is_fading = false;
                if ( ref != null ) ref.alpha = 0;
            }
        }
    }
}