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

rand0m

  • Sr. Member
  • ****
  • Posts: 160
    • 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: 815
    • 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: 160
    • 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: 815
    • 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

  • Sr. Member
  • ****
  • Posts: 318
    • 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: 160
    • 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