Author Topic: If statements using magic tokens  (Read 6889 times)

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
If statements using magic tokens
« on: May 08, 2017, 11:31:19 PM »
I can't figure this out.

I want to make a function that runs if a particular emulator is showing. Is this possible?

Code: [Select]
emu = [Emulator]
if ( emu == "nes|Nintendo Entertainment System|Nintendo") {
    blah blah
}

kent79

  • Hero Member
  • *****
  • Posts: 842
    • View Profile
Re: If statements using magic tokens
« Reply #1 on: May 09, 2017, 12:17:14 AM »
Code: [Select]
function transition_callback(ttype, var, ttime)
  {
    switch ( ttype )
    {
        case Transition.ToNewList:
            switch ( fe.list.name )
            {             
case "MAME":
                OBJECTS.logo.file_name = "";
lt.set_bg_rgb( 155, 0, 40 );
lb.set_bg_rgb( 155, 0, 40 );
OBJECTS.gameListBox.set_selbg_rgb( 155, 0, 40 );
OBJECTS.gameListList2.set_rgb( 155, 0, 40 );
                break;

case "NES":         
        ...........
                break;
}
break;
    }
  }


fe.add_transition_callback("transition_callback" );


You may referred by built-in  Game Station Theme. Thanks.
« Last Edit: May 09, 2017, 01:51:10 AM by kent79 »

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #2 on: May 09, 2017, 08:50:34 PM »
So this works,

Code: [Select]
function transition_callback(ttype, var, ttime)
  {
    switch ( ttype )
    {
        case Transition.ToNewList:
            switch ( fe.list.name )
            {             
        case "Nintendo Entertainment System":
            local textnestest = fe.add_text( "nes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
            break;
        case "Super Nintendo":
            local textnestest = fe.add_text( "snes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
            break; 
            }
        break;
    }
}
fe.add_transition_callback("transition_callback" );

It won't clear (redraw) the text once the layout transitions to a new list. It will add "nes test" on top of "snes test"

Looking at the documentation it seems I need to add
Code: [Select]
return true; to clear it? When I did that it crashed the layout. Super confused.

in http://attractmode.org/docs/Layouts.html#add_transition_callback

This is the example they use to illustrate how to redraw, but it doesn't make sense to me.

Code: [Select]
function transition( ttype, var, transition_time )
{
   local redraw_needed = false;

   // do stuff...

   if ( redraw_needed )
      return true;

   return false;
}
« Last Edit: May 09, 2017, 09:04:42 PM by Joelf »

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #3 on: May 09, 2017, 08:56:28 PM »
It's also only adding the text when you transition away from the list, not when you transition into the list.

I also tried
Transition.ToNewSelection
Transition.FromOldSelection

Neither of them seemed to work at all. I'm not sure if I have the language right, but I am transitioning from one emulator to another (i.e. one romlist to another.)
« Last Edit: May 09, 2017, 09:00:54 PM by Joelf »

mahuti

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
    • View Profile
    • Github Repositories
Re: If statements using magic tokens
« Reply #4 on: May 10, 2017, 10:18:45 PM »
At present:

1.  If statements using magic tokens do not work. Magic tokens are parsed too late.

2. When transitioning back to a theme (or to another romlist's use of the same theme) the local initialized variables are already set. Setting them anew does not make them change to the new values... they're stuck in the old values until the theme is reloaded.

The best option I found to work around this was to check the title of the romlist and use that in an If statement... which is basically where you're at. The extra step is to reload based on the changed romlist name on transition.

Code: [Select]

local console = fe.list.name;
 
fe.add_transition_callback(this, "on_transition");
function on_transition(ttype, var, ttime) {

local last_console = console;
console = fe.list.name;

switch( ttype)
{
case Transition.ToNewList:
//console = fe.list.name;
if (last_console != console)
{
fe.signal("reload");
}
break;
default:
  }
   return false;
}

//Background
local bg = fe.add_image(console + "/" + "bg.jpg", 0,0, width(1440), height(1080) ); # references my own functions. just shown here for how I'm using the console variable. I use it with if statements, and to load images from console-named folders with generic image names in them (bg.jpg, foreground.jpg, etc)



In my script there, I check the list name against the prior list name. if they're different I reload. Using this method, my Ol Room 4x3 layout works for multiple emulators (romlists, really)

https://github.com/mahuti/Ol-Room-4x3

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #5 on: May 11, 2017, 07:00:17 AM »
Huh. I'm basically reverse engineering your theme without realizing it existed!

I'm using robospin 4 and trying to make it universal. It needs to make a cabinet image, snap surface and image logo for each theme type. I have all the artwork and theme types done, but I want this universal thing working before I release it.

I'll have a look at your old room and see if this helps thanks so much!

mahuti

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
    • View Profile
    • Github Repositories
Re: If statements using magic tokens
« Reply #6 on: May 11, 2017, 08:09:00 AM »
The other thing to remember is that the layout options for one theme are universal. That took me
Awhile to realize. The theme folder needs to be named different to get a new set of layout preferences for a different rom list. The reload and "shared" preferences are the two things that really threw me when I was building a more-or-less universal theme.

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #7 on: May 11, 2017, 05:46:56 PM »
The other thing to remember is that the layout options for one theme are universal.

Yeah, I'm hoping that there aren't any options that will be needed with the theme once I'm done - it should be all automatic.

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #8 on: May 11, 2017, 07:49:53 PM »
Gave this a shot: but it performed a redraw regardless of what was going on.

Code: [Select]
    local console = fe.list.name;
    fe.add_transition_callback(this, "on_transition");
    function transition_callback(ttype, var, ttime)
    {
    local last_console = console;
      console = fe.list.name;
      switch ( ttype )
      {
          case Transition.ToNewList:
              switch ( fe.list.name )
              {             
          case "Nintendo Entertainment System":
                local textnestest = fe.add_text( "nes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
                if (last_console != console)
                {
                    fe.signal("reload");
                }
                break;
          case "Super Nintendo":
                local textnestest = fe.add_text( "snes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
                if (last_console != console)
                {
                    fe.signal("reload");
                }
                break; 
              }
          break;
        }
    }
    fe.add_transition_callback("transition_callback" );

mahuti

  • Administrator
  • Sr. Member
  • *****
  • Posts: 252
    • View Profile
    • Github Repositories
Re: If statements using magic tokens
« Reply #9 on: May 12, 2017, 07:23:08 AM »
Try this:

Code: [Select]
local console = fe.list.name;
fe.add_transition_callback("transition_callback" );

function transition_callback(ttype, var, ttime)
{
local last_console = console;
  console = fe.list.name;

switch ( ttype )
{
case Transition.ToNewList:
if (last_console != console)
{
fe.signal("reload");
break;
}

switch ( fe.list.name )
{             
case "Nintendo Entertainment System":
local textnestest = fe.add_text( "nes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
break;

case "Super Nintendo":
local textnestest = fe.add_text( "snes test", flx*0.30, fly*0.88, flw*0.68, flh*0.06  );
break; 

default:
}
break;
default:
}
}

Joelf

  • Jr. Member
  • **
  • Posts: 23
    • View Profile
Re: If statements using magic tokens
« Reply #10 on: May 19, 2017, 01:58:15 PM »
Thanks! I'll give this a shot. Just from a quick look it does what it's supposed to but the formatting is weird - it adds the text, but doesn't place it. THe refresehes on changing a filter, but not a romlist. I'll play with it. Thanks for the help!

omegaman

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 880
    • View Profile
Re: If statements using magic tokens
« Reply #11 on: May 19, 2017, 11:49:53 PM »
Joeif-

Mahuti is spot on but there is another way to use one theme for all your displays. That is, if you don't mind your theme using the same snap and text positions for each layout transition etc. You can use magic tokens for emulator or display matching on arts to update backgrounds, cab skins, and whatever for each system like I do in robospin. This allows each display to be different but yet have the same consistency. It might not be as sexy as maybe changing the snap or text to a different position on each layout transition but it looks good just the same. 

For example, if you want different background arts for each display you just do something like this with magic tokens; Each background art in the bg folder will be named to match the defined emulator names. You can do the same for DisplayName tokens to match different cabinet skins for each system as well.

local bg = fe.add_image("BG/[Emulator]", flx*0.25, fly*0.53, flw*0.38, flh*0.38 );
bg.trigger = Transition.EndNavigation;
   
I believe the Transition.Endnavigation will allow the art to be refreshed or redrawn on the next display transition etc. I could be wrong though because I'm a little rusty. It helps regardless, without having to use a transition_callback for each case. 

I've already done this on an updated robospin that I'm working on. It's a simpler way relying more on efficient design. And, its great just needing to use one layout for all the displays, including the displays menu. It's late so this might all just be gibberish at this point.