Setting highlight in a ListBox

Started by Cassiebsg, Sun 15/10/2017 21:34:29

Previous topic - Next topic

Cassiebsg

Hey.

So, I decided to let the player change the game language by selecting it in my settings, works almost perfectly (have a problem with the credits module, and this one).

What I want it to do:
I populate the list with the Language I want (using AddItem).
The check which translation is loaded (if any) and highlight the correct Language.

Result: When opening the GUI I see the current Language that is selected.

I was trying to use ListBox.SelectedIndex but this was resulting in some wierd behaviour... have a feeling I was changing the selecting but not the highlight or something.

Anyway, any help?
I currently have done a work around, since there's really only 2 languages to selected from, by just using an if/else and loading the list with the current language on top... but seems like a "stupid" way to do this, considering it would be much simpler to just set the highlight on the correct index. (roll)
There are those who believe that life here began out there...

Khris

Afaik, setting the .SelectedIndex is the proper way to highlight an item. Can you describe what you mean by "weird behaviour"? Also keep in mind that the first item has index 0.

Cassiebsg

Okay

This is more or less what I had:
Code: ags

function show_translation_list() // called when gSettings is selected to be displayed
{
    lbxTranslation.AddItem("English");
    lbxTranslation.AddItem("Turkish");
    if (IsTranslationAvailable() && Game.TranslationFilename=="Turkish")
    {
        lbxTranslation.SelectedIndex = 1;
    }
    else lbxTranslation.SelectedIndex = 0;
}


Now the weird behaviour is: If I load the game with Turkish, Language list will display empty first time. 2nd time it will display English and Turkish, but neither is highlighted.

If I load the game with the default English, then list shows fine 1st time with English selected (or maybe because it's the top option...)
Or is this one of those cases that I need to check the condition after the populate function is done? ???
There are those who believe that life here began out there...

Crimson Wizard

#3
To be honest I do not know if that's related to your problem, but
Code: ags

Game.TranslationFilename=="Turkish"


This is not a good way to compare strings in this case, IMO, because filename could be lowercase, and == operator compares them precisely.
I propose using Game.TranslationFilename.CompareTo("Turkish", false) == 0, where "false" means "non case-sensitive".


EDIT: on a side note, if you don't want to compare items individually:
Code: ags

if (IsTranslationAvailable()) {
    int i;
    for (i = 1; i < lbxTranslation.ItemCount; i++)
    {
        if (Game.TranslationFilename.CompareTo(lbxTranslation.Items[i], false) == 0)
        {
            lbxTranslation.SelectedIndex = i;
            break;
        }
    }
}

Snarky

Quote from: Cassiebsg on Mon 16/10/2017 14:54:49
Now the weird behaviour is: If I load the game with Turkish, Language list will display empty first time. 2nd time it will display English and Turkish, but neither is highlighted.

I haven't really used translations, but could it be that loading the game in Turkish translation actually interferes with the strings somehow? I mean that it tries to look up a translation for "English" and "Turkish", doesn't find it, and so the list remains blank? (Not sure why it would work on the second attempt, though.)

Crimson Wizard

Quote from: Snarky on Mon 16/10/2017 15:22:46
I haven't really used translations, but could it be that loading the game in Turkish translation actually interferes with the strings somehow? I mean that it tries to look up a translation for "English" and "Turkish", doesn't find it, and so the list remains blank? (Not sure why it would work on the second attempt, though.)

I just remembered something, back in old days AGS ListBoxes were not translated automatically, then I added this feature by user request in AGS 3.3.0. There is now "Translated" property in ListBoxes, and it is "true" by default. In theory one would rather turn it off for listboxes containing savegame names... IDK if anyone noticed that it even exists :-\.

But AFAIK if there is no translation, then AGS uses original string.

Cassiebsg

#6
CW, game doesn't compile that code (and I'm too rookie to even understand it, much less debug it :-\ ).

It says about line 5: must have an instance of the struct to access a non-static member.
Says the same about Game.TranslationFilename.CompareTo("Turkish", false) == 0 ...

Quote
I haven't really used translations, but could it be that loading the game in Turkish translation actually interferes with the strings somehow? I mean that it tries to look up a translation for "English" and "Turkish", doesn't find it, and so the list remains blank? (Not sure why it would work on the second attempt, though.)

Oh, that might be it. These lines aren't on the translation file yet, as I just added them and haven't updated the translation file.

Quote
There is now "Translated" property in ListBoxes, and it is "true" by default.

Uhm... I thought it was off by default. 8-0
How do I turn it off? I tried lbxTranslations.Translated=false; but it just says that Translated is not a public member of ListBoxes... ???
Okay, found the option in the editor... One for the big list of stuff you can do in editor but not in script? (roll)

EDIT: Okay, added the lines to the translation file and translated them with Google 8-0. It's fun to see the translation on the fly when I change it from one to the other... but still doing the same as before: 1st time empty, second time no hightlight.
There are those who believe that life here began out there...

Crimson Wizard

Quote from: Cassiebsg on Mon 16/10/2017 17:32:19
CW, game doesn't compile that code (and I'm too rookie to even understand it, much less debug it :-\ ).

It says about line 5: must have an instance of the struct to access a non-static member.
Says the same about Game.TranslationFilename.CompareTo("Turkish", false) == 0 ...

Oh, that might be the same issue as posted here: http://www.adventuregamestudio.co.uk/forums/index.php?topic=55308.0

Workaround is:
Code: ags

String trans = Game.TranslationFilename;
if (trans.CompareTo("Turkish", false) == 0)

Cassiebsg

#8
Okay, that works. (nod)

But I still have the weird behavior happening when starting the game with Turkish. (EDIT: And turning off translation for the ListBox didn't solve this.)

EDIT: Also tried the alternate script that CW suggested, now with the string. Works now. (nod) But odd enough Turkish is jumping to 1st place above English after selecting it the first time, and then closeing the gui and reopening it. :-\
There are those who believe that life here began out there...

Crimson Wizard

Quote from: Cassiebsg on Mon 16/10/2017 18:02:33
EDIT: Also tried the alternate script that CW suggested, now with the string. Works now. (nod) But odd enough Turkish is jumping to 1st place above English after selecting it the first time, and then closeing the gui and reopening it. :-\

I noticed that there is no command to clear list box. Do you clear it elsewhere between closing and reopening GUI? Because if you aren't, every call to show_translation_list will add more and more items there.
Could that be that your ListBox is very small in height just to fit 2 visible items at a time? Just a random thought... nevermind if it's not.
Also, do you do anything with this listbox outside of this function?

Cassiebsg

#10
Ah, yes, forgot to add the clear. (roll)
I had it on my "work around" but not in the original solution, so I've added it and now it's almost perfect. :-D
It's no longer switching Turkish to the top, and it's no longer not showing the highlighted on the 2nd time I open the GUI (with the game started with Turkish). It still showing empty on the first time I open it. Thoughts?
And the listbox is suppose to be 3 high, but haven't actually tested it to make sure it can display 3, so it might be only able to display 2 (but shouldn't the scroll bar then show up??). :-[ Think I'll go test and see if what happens if I add more items just for testing.

EDIT: Okay... yes, it could only show 2... and didn't show the arrows cause I didn't want the border visible... >:( So, guess I'm forced to have the border there.

EDIT2:
Quote
Also, do you do anything with this listbox outside of this function?

Well, yes, I'm changing the language.

Code: ags

function lbxTranslations_OnSelectionCha(GUIControl *control)
{
    String selectedTranslation = lbxTranslations.Items[lbxTranslations.SelectedIndex];
    if (selectedTranslation=="English") Game.ChangeTranslation(""); // yes, I'll change it to your purposed solution ASAP. :)
    else if (selectedTranslation=="Turkish") Game.ChangeTranslation("Turkish");
    else Display("Unable to change the translation");
    selectedTranslation="";
}


Am I doing something stupid in here that might be messing up the list display?



And then I just have to figure out how to solve the credits module... (roll)
There are those who believe that life here began out there...

SMF spam blocked by CleanTalk