Author Topic: Multiline? Expanded tokens? Oh my!  (Read 9070 times)

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Multiline? Expanded tokens? Oh my!
« on: August 15, 2015, 07:52:14 PM »
Hi all,

I'm poking around under the hood, probably in places I shouldn't, but here are my latest questions:

I have a text item that I want to span 3 lines. Once in a while, it'll work if I feed it a string like g_name+"\n"+g_cat+"\"+g_players ; other times, AM instantly crashes. Am I doing something wrong? Here's what I really want to happen:

When switching to the next game in the list, I want its ROM name, its category, and how many players it supports listed (hence the above variables). Here's the other thing: I'd also like to get the actual text for the category and the actual integer value for how many players, but I don't know how to do that. I tried using fe.game_info(Info.Category, index_offset) and fe.game_info(Info.Players, index_offset), but that only ever returns information related to the very first ROM displayed in the layout; moving up or down changes nothing. For the number of players, at least, I'd like to add a statement that "prettifies" the count, something like this:

Code: [Select]
local num_players, g_players;
g_players=(num_players==1 ? "1 player" : num_players.tostring()+" players");

So what I need to do is find some way to get the  currently displayed game's category and number of players (and name, and everything, but hey) in an object format, or find some means to expand the tokens and save the actual values into new variables.

Am I misunderstanding how fe.game_info() should work, or is there something else I need to implement to "kick it" to get updated ROM data when I switch to the previous/next game?

Oh, some notes: I have word_wrap enabled in the particular text field I'm using, and, as I said, sometimes it works, but -- and here's the head scratcher -- if the last game displayed didn't have anything in its category slot, AM would instantly die; if I disabled the newlines and category for a moment, moved up or down to a game with category information, then reenabled it, it would work! Weird!

As always, I'd appreciate any help or insight anyone can provide! Thanks!

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: Multiline? Expanded tokens? Oh my!
« Reply #1 on: August 16, 2015, 10:13:58 AM »
You should be retrieving game_info at ToNewList and ToNewSelection transitions - not sure if you are doing that.

Here's an example:
Code: [Select]
local text = fe.add_text("Three\nLine\nText", 0, 0, 640, 400);
    text.charsize = 18;
    text.word_wrap = true;
    text.align = Align.Left;

function transition_callback( ttype, var, ttime )
{
    switch ( ttype )
    {
        case Transition.ToNewList:
        case Transition.ToNewSelection:
            local title, category, players, final_text;
            title = fe.game_info( Info.Title, var );
            category = fe.game_info( Info.Category, var );
            players = fe.game_info( Info.Players, var );
            final_text = "";
            if ( title != null ) final_text += title;
            if ( category != null ) final_text += "\n" + category;
            if ( players != null ) final_text += "\n" + players;
            text.msg = final_text;
            break;
    }
}

fe.add_transition_callback( this, "transition_callback" );

Just quick tested this but that should be pretty close to what you want.

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Re: Multiline? Expanded tokens? Oh my!
« Reply #2 on: August 16, 2015, 11:57:48 AM »
Thanks, liquid8d! I was not doing that, but I'll definitely investigate! From the looks of it, that's exactly what I needed. I'll give it a go and see what happens. Again, thanks!

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Re: Multiline? Expanded tokens? Oh my!
« Reply #3 on: August 16, 2015, 09:47:18 PM »
All right, thanks to liquid8d's example, I managed to get most of what I'm gunning for working! Now (of course!) I've run into another snag. :(

With the following:

Code: [Select]
local tfCountDeets=fe.add_text("", 0, 0, 100, 100);
local countDeets;

countDeets=(fe.list.index.tointeger()+1)+" ("+fe.list.size+")";
tfCountDeets.msg=countDeets;

on first run the index appears correct: as soon as I start AM, I see whatever the last list was and whatever the last game was, just like I should, and they're properly indexed. However, if I press up or down (using a keyboard right now), the list changes appropriately, but the index starts doing crazy things! For example, if I was at, say, game 10 of 40, when I start AM, the following would spit out "9":

Code: [Select]
print (fe.list.index); // outputs 9 (as expected!)

But the next up/down would give me the same result (9 in this case)! And, supposing I pressed down in the prior example, the next press up would display 10 (game 11); the next press up would display 9 again... What it looks like is happening is AM is one game behind (or ahead) of where it needs to be. I'd think it was just a screen redraw issue except the print() statement returns the same (wrong) values.

Any thoughts? Thanks!

EDIT: All right, I just noticed that if I'm on, say, filter A and game 20, if I switch filters, then switch back, suddenly I'm on game 21, but an up or down leaves me at the same "new" index, which sorts itself with the next up/down, but it's odd nonetheless...So here's what I've got going on, just in case anyone more knowledgeable than me can tell me what I'm doing wrong:

Code: [Select]
function transitionCallback (ttype, var, ttime)
{
switch (ttype) {
case Transition.ToNewList:
local filter;
local filterDeets;

filter=fe.filters[fe.list.filter_index].name;
filterDeets=filter;
// print ("\nCurrent filter ("+fe.list.filter_index+"): "+filterDeets+"\n");
tfFilter.msg=filterDeets;

case Transition.ToNewSelection:
local title, rom, cat, players, year, mfr;
local titleDeets, romDeets, copyDeets, countDeets;

title=fe.game_info(Info.Title, var);
titleDeets=(title!=null ? getPrettyTitle(title) : "???");
tfTitle.msg=titleDeets;
tfTitleShadow.msg=titleDeets;

rom=fe.game_info(Info.Name, var);
cat=fe.game_info(Info.Category, var);
players=fe.game_info(Info.Players, var);

romDeets=(rom!=null ? rom : "???")+"\n";
romDeets+=(cat!=null ? getPrettyCat(cat) : "n/a")+"\n";
romDeets+=(players!=null ? getPrettyPlayers(players) : "? player(s)");
tfRomDeets.msg=romDeets;
tfRomShadow.msg=romDeets;

year=fe.game_info(Info.Year, var);
mfr=fe.game_info(Info.Manufacturer, var);

copyDeets=(mfr!=null ? (year!=null ? "© "+year+" "+mfr : "") : "© n/a");
tfCopyDeets.msg=copyDeets;

// countDeets=(fe.list.index.tointeger()+1)+" ("+fe.list.size+")"; // Always one index "behind" it seems...
tfCountDeets.msg="[ListEntry] ([ListSize])";
// tfCountDeets.msg=countDeets;
break;
}
}

fe.add_transition_callback (this, "transitionCallback");
« Last Edit: August 16, 2015, 09:57:43 PM by Bgoulette »

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: Multiline? Expanded tokens? Oh my!
« Reply #4 on: August 16, 2015, 10:45:08 PM »
If that's your exact code, you are missing a break after your ToNewList so you might get unexpected results. Part of that is probably my fault since I did the same in my code for ToNewList and ToNewSelection - but according to docs, var for ToNewList would be the filter index offset (-1, 0, or 1, etc) of the list you are transitioning to - while var for ToNewSelection would be the list index (selected game).

You might have to experiment a bit with the different transitions and var variable to get what you are looking for...

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Re: Multiline? Expanded tokens? Oh my!
« Reply #5 on: August 17, 2015, 05:24:11 AM »
That was my exact code, and I thought the missing break was intentional! :) I added it in, but that gives me an initially blank layout (textwise). It got me thinking, though, to I tossed in some cases for other transition types. Except now I'm not sure exactly which transition is firing when! I'm printing out ttype when the callback executes, but I'm only seeing the constant value (e.g., 0, 6, etc.) and I can't find a list of what each named constant actually represents (and in some cases, simply moving to the next/prev game triggers 3 callback executions). For example, is a ttype of 0 Transition.StartLayout or maybe Transition.ToNewList? If there's a list of transition types and their actual values somewhere, I'd love to know about it! Thanks, as always!

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: Multiline? Expanded tokens? Oh my!
« Reply #6 on: August 17, 2015, 05:39:53 AM »
Yes you will need to do something in addition to ToNewSelection... hence why it isn't updating initially - because ToNewSelection doesn't fire when it first launches. I'm just not sure what specifically for your particular case. You might even need to do the same thing under multiple cases - or slightly modified for each....

Code: [Select]
//just for debugging purposes, so we know which 'when' we are talking about
    function when(w) {
        switch (w) {
            case 0:
                return "StartLayout";
            case 1:
                return "EndLayout";
            case 2:
                return "ToNewSelection";
            case 3:
                return "FromOldSelection";
            case 4:
                return "ToGame";
            case 5:
                return "FromGame";
            case 6:
                return "ToNewList";
            case 7:
                return "EndNavigation";
        }
    }

Bgoulette

  • Sr. Member
  • ****
  • Posts: 116
  • I wrote a book.
    • View Profile
    • BlakeGoulette.com
Re: Multiline? Expanded tokens? Oh my!
« Reply #7 on: August 17, 2015, 06:33:23 AM »
Thanks -- after posting that last response, I realized I could do just what you've done above, so thanks for that!

I stripped everything out and replaced it with the following:

Code: [Select]
switch (ttype) {
case Transition.ToNewSelection:
print ("\nTransition: "+ttype+"; ");
print ("var: "+var+"\n");
print ("Current selection index: "+fe.list.index+"\n");
break;

case Transition.ToNewList:
print ("\nTransition: "+ttype+"; ");
print ("var: "+var+"\n");
print ("Current filter index: "+fe.list.filter_index+"\n");
break;

case Transition.StartLayout:
print ("\nTransition: "+ttype+"; ");
print ("var: "+var+"\n");
break;
}

While watching the output window, moving from list to list works as it should: var is applied correctly; however, when moving from selection to selection, var is always one iteration "behind;" that is, if I'm incrementing, var will add 1, but if I then decrement, var will add 1, and if I decrement again, var will subtract 1. If I then increment again, var will subtract 1 and, on the next increment, add 1. Programmatically, I can't see that I'm doing anything wrong, but I must be, because ++/-- shouldn't behave like it is... :-\

Any other thoughts? I do appreciate the help, and I'm trying not to come across like a complete moron (too late?!), so again, thank you very much!

(Full disclosure: years ago, I spent a lot of time coding Flash ActionScript 3, so there might be some language baggage I'm carrying! :-) )

liquid8d

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 442
    • View Profile
Re: Multiline? Expanded tokens? Oh my!
« Reply #8 on: August 21, 2015, 09:24:05 AM »
perhaps your confusing 'var' with index? var on ToNewSelection is the index offset, not the index... -1 meaning the previous item/index, 0 being the current and 1 being the next item/index. Rather than dealing with which index you are on, you are dealing with which offset you are working with. See my reply to your other post that talks about objects .index_offset attribute.

It's also important to remember that var is different depending on which transition you are working with:
https://github.com/mickelson/attract/blob/v1.5.2/Layouts.md#add_transition_callback