Author Topic: Transition with Input State not workign as expected  (Read 4025 times)

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Transition with Input State not workign as expected
« on: January 24, 2019, 09:40:25 AM »
Hi, I am using the following code:

Code: [Select]
local image_up = fe.add_image ("images/up.png", 430, 44, 0, 0);
local image_down = fe.add_image ("images/down.png", 430, 1014, 0, 0);
image_up.visible = false;
image_down.visible = false;

function blink_transition( ttype, var, transition_time )
{ switch ( ttype )
{
case Transition.ToNewSelection:
{
   if (fe.get_input_state("Up")==true)
{
image_up.visible = true;
}
   else if (fe.get_input_state("Down")==true)
{
image_down.visible = true;
}
      return true;

   return false;
}
}
}

What I am trying to do is to show images of up-arrow and down-arrow when I press up and down. I used the code in layout directly because using a plugin applies the effect on all layouts in use (not isolated). Can anyone please tell me why it isn't working? Does transition require further instructions?

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #1 on: January 24, 2019, 11:13:48 AM »
When ttype is Transition.ToNewSelection, var indicates the index offset of the selection being transitioned to (i.e. -1 when moving back one position in the list, 1 when moving forward one position, 2 when moving forward two positions, etc.)

Check if a specific keyboard key, mouse button, joystick button or joystick direction is currently pressed, or check if any input mapped to a particular frontend action is pressed.

Your mixing things. You can use a transition, and when ttype is to a new selection, var will indicate which direction the user went. Excellent as it doesn’t matter what controls the user has set up. This function would only occur when there is a new selection, so it would only be good for imagines that would remain on screen, or to toggle an animation, or animated gif that fades and doesn’t loop.

You can use input state to detect a button press. Excellent choice if you want the image to only show up when the user presses a certain key. You would have to add this inside a tick function to constantly poll for the input state.

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #2 on: January 26, 2019, 12:25:51 AM »
Your mixing things. You can use a transition, and when ttype is to a new selection, var will indicate which direction the user went. Excellent as it doesn’t matter what controls the user has set up. This function would only occur when there is a new selection, so it would only be good for imagines that would remain on screen, or to toggle an animation, or animated gif that fades and doesn’t loop.

You can use input state to detect a button press. Excellent choice if you want the image to only show up when the user presses a certain key. You would have to add this inside a tick function to constantly poll for the input state.

Thanks for the tips, I got it working via following code:
Code: [Select]
local image_up = fe.add_image ("images/up.png", 428, 46, 42, 24);
local image_down = fe.add_image ("images/down.png", 428, 1014, 42, 24);
image_up.visible = false;
image_down.visible = false;
fe.add_ticks_callback( "tick_up" );
fe.add_ticks_callback( "tick_down" );
function tick_up( ttime )
{
if (fe.get_input_state("Up")==true)
{
image_up.visible = true;
}
   else if (fe.get_input_state("Up")== false)
{
image_up.visible = false;
}
return true;
   return false;
}

function tick_down ( ttime )
{
if (fe.get_input_state("Down")==true)
{
image_down.visible = true;
}
   else if (fe.get_input_state("Up")== false)
{
image_down.visible = false;
}
return true;
   return false;

}

Some small Q's:
1) Is there a negative impact on speed (scrolling etc) when using too many functions? It seems Tick_call back consistently operates in the background.
2) what does "return true;" accomplish? in most codes taking it out has no effect on function output.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #3 on: January 26, 2019, 12:55:02 AM »
Maybe it effects performance? I’ve been trying to combine ticks transitions and signal handlers within same class in hopes it would eventually help prevent lag. I haven’t really tested.

Transitions need a boolean return. Signal handlers do not.

zpaolo11x

  • Hero Member
  • *****
  • Posts: 1233
    • View Profile
    • My deviantart page
Re: Transition with Input State not workign as expected
« Reply #4 on: January 28, 2019, 12:36:49 AM »
I would do it this way: add a signal_callback where you detect if "up" or "down" are pressed, if up is pressed "engage" a counter (like counterup = 255) if down is pressed engage counterdown = 255, then add just one tick callback where you check the counters and if they are !=0 apply the value of the counter to the alpha of the arrows images, then decrease the counter. In this way when the counter reaches zero tick callback doesn't hog your system changing alpha channel continuously.

Code: [Select]
local counterup = 0
local counterdown = 0
local fadespeed = 0.85

local snap = fe.add_artwork("snap",0,0,200,200)
snap.video_flags = Vid.ImagesOnly

local uparrow = fe.add_text("^",200,0,100,100)
uparrow.alpha = 0

local downarrow = fe.add_text("v",200,100,100,100)
downarrow.alpha = 0

fe.add_signal_handler( this, "on_signal" )
fe.add_ticks_callback( this, "tick" )


function on_signal( sig ){
   if (sig == "up") {
      counterup = 255
      return false
   }
   if (sig == "down") {
      counterdown = 255
      return false
   }
   
}

 function tick( tick_time ) {

    if (counterdown != 0){
       counterdown *= fadespeed
       if (counterdown < 1) counterdown = 0
       downarrow.alpha = counterdown
    }
    if (counterup != 0){
       counterup *= fadespeed
       if (counterup < 1) counterup = 0
       uparrow.alpha = counterup
    }

 }
« Last Edit: January 28, 2019, 12:46:20 AM by zpaolo11x »

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #5 on: January 28, 2019, 09:10:50 AM »
I would do it this way: add a signal_callback where you detect if "up" or "down" are pressed, if up is pressed "engage" a counter (like counterup = 255) if down is pressed engage counterdown = 255, then add just one tick callback where you check the counters and if they are !=0 apply the value of the counter to the alpha of the arrows images, then decrease the counter. In this way when the counter reaches zero tick callback doesn't hog your system changing alpha channel continuously.

Code: [Select]
local counterup = 0
local counterdown = 0
local fadespeed = 0.85

local snap = fe.add_artwork("snap",0,0,200,200)
snap.video_flags = Vid.ImagesOnly

local uparrow = fe.add_text("^",200,0,100,100)
uparrow.alpha = 0

local downarrow = fe.add_text("v",200,100,100,100)
downarrow.alpha = 0

fe.add_signal_handler( this, "on_signal" )
fe.add_ticks_callback( this, "tick" )


function on_signal( sig ){
   if (sig == "up") {
      counterup = 255
      return false
   }
   if (sig == "down") {
      counterdown = 255
      return false
   }
   
}

 function tick( tick_time ) {

    if (counterdown != 0){
       counterdown *= fadespeed
       if (counterdown < 1) counterdown = 0
       downarrow.alpha = counterdown
    }
    if (counterup != 0){
       counterup *= fadespeed
       if (counterup < 1) counterup = 0
       uparrow.alpha = counterup
    }

 }

Thanks a lot Zpaolo11x, lots of new things in your code. I'm gonna work on it :D

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #6 on: February 05, 2020, 04:56:24 AM »
I would do it this way: add a signal_callback where you detect if "up" or "down" are pressed, if up is pressed "engage" a counter (like counterup = 255) if down is pressed engage counterdown = 255, then add just one tick callback where you check the counters and if they are !=0 apply the value of the counter to the alpha of the arrows images, then decrease the counter. In this way when the counter reaches zero tick callback doesn't hog your system changing alpha channel continuously.

Code: [Select]
local counterup = 0
local counterdown = 0
local fadespeed = 0.85

local snap = fe.add_artwork("snap",0,0,200,200)
snap.video_flags = Vid.ImagesOnly

local uparrow = fe.add_text("^",200,0,100,100)
uparrow.alpha = 0

local downarrow = fe.add_text("v",200,100,100,100)
downarrow.alpha = 0

fe.add_signal_handler( this, "on_signal" )
fe.add_ticks_callback( this, "tick" )


function on_signal( sig ){
   if (sig == "up") {
      counterup = 255
      return false
   }
   if (sig == "down") {
      counterdown = 255
      return false
   }
   
}

 function tick( tick_time ) {

    if (counterdown != 0){
       counterdown *= fadespeed
       if (counterdown < 1) counterdown = 0
       downarrow.alpha = counterdown
    }
    if (counterup != 0){
       counterup *= fadespeed
       if (counterup < 1) counterup = 0
       uparrow.alpha = counterup
    }

 }

Hi zpaolo11x, I'm trying to use this code like this:

Code: [Select]
local arrows_counter_up = 0
local arrows_counter_down = 0
local arrows_fadespeed = 0.90

local image_up = fe.add_image ("images/arrow_up.png", 17.8, 396, 0, 0);
image_up.alpha = 0

local image_down = fe.add_image ("images/arrow_down.png", 17.8, 620, 0, 0);
image_down.alpha = 0

function signal_arrows( sig )
{
if (sig == "up")
{
arrows_counter_up = 255
return false
}

if (sig == "down")
{
arrows_counter_down = 255
return false
}
   
}

function tick_arrows( tick_time )
{
if (arrows_counter_down != 0)
{
arrows_counter_down *= arrows_fadespeed
if (arrows_counter_down < 1) arrows_counter_down = 0
image_down.alpha = arrows_counter_down
}

if (arrows_counter_up != 0)
{
arrows_counter_up *= arrows_fadespeed
if (arrows_counter_up < 1) arrows_counter_up = 0
image_up.alpha = arrows_counter_up
}

}

fe.add_signal_handler( "signal_arrows" )
fe.add_ticks_callback( "tick_arrows" )

The code is working (thanks) but the upward and downward arrow snap appears faded from start (doesn't look solid even for a few ms) i tried tweaking the numbers but couldn't get it to appear un-faded/ solid at start of animation. Can you please guide me in the correct direction regarding this.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #7 on: February 05, 2020, 05:38:29 AM »
@rand0m with your code as it, it will never appear solid. Your first tick satisfying the if statement will multiply the alpha value of 255 by 0.9.

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #8 on: February 05, 2020, 06:41:05 AM »
@rand0m with your code as it, it will never appear solid. Your first tick satisfying the if statement will multiply the alpha value of 255 by 0.9.

Hiya Keil, long time no see! changing the value to .96-.97 is near solid at .999 the arrow snaps are permanent. One small problem is that while keeping up or down pressed the arrows disappear after a few ms. I don't think we can constantly check the status of up/ down unless we utilize tick callback which I was trying to avoid.

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Transition with Input State not workign as expected
« Reply #9 on: February 05, 2020, 06:52:07 AM »
I would create a tick callback that would check for input state (not a signal handler). Or you could use a signal handler with ticks, but utilize different logic. Shoot me a message on discord in 6hrs if you need help.