Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Dualnames on Fri 09/04/2010 18:35:50

Title: Using slider to scroll a Listbox ( SOLVED THANKS RICKJ!)
Post by: Dualnames on Fri 09/04/2010 18:35:50
Perhaps its been addressed, but I  couldn't find it. I want to use a silder to scroll a listbox. The listbox items number will not always be the same. I've tried some stuff but totally no go. If this is a newbie question, I apologize.

CODE can be found below. :D
Title: Re: Using slider to scroll a Listbox
Post by: NsMn on Fri 09/04/2010 18:42:33
Try something like this:

int SliderValue;

function game_start(){
 SliderValue=ScrollSlider.Value;
}

//rest of global script

function ScrollSlider_OnChange(GUIControl *control)
{
 if(ScrollSlider.Value > SliderValue)ListBoxScroll.ScrollDown();
 else if(ScrollSlider.Value < SliderValue)ListBoxScroll.ScrollUp();
}


Could work. I don't know if the value of the slider is immidiatelly changed once it's activated though.
Title: Re: Using slider to scroll a Listbox
Post by: Dualnames on Fri 09/04/2010 19:19:28
Couldn't work.
Title: Re: Using slider to scroll a Listbox
Post by: RickJ on Fri 09/04/2010 20:17:18
You will need to manipulate ListBox.Top item when the slider changes it's value.   In the example below the slider represents a a percentage value.   If the number of items in the list change then the slider value and listbox top item need to be re-synchronized.  Resynch is also required if the listbox is scrolled using the the ScrollUp/Dn functions as well.  The technique is illustrated in the code sample below which is by no means complete or error free.


// Assumes a GUI with a slider control gSlider
// and a listbox control gListBox.

// Scale the the slider to 0-100% and set it's initial value
// according to the list box value.

function game_start() {

   // Scale slider to percent
   gSlider.Min = 0;
   gSlider.Max = 100;

   // Calculate the listbox's top item's position in percent
   if (gListBox.ItemCount<gListBox.RowCount)  {
      gSlider.Value = 0;
   }
   else {
      gSlider.Value = (gListBox.TopItem *100) /  (gListBox.ItemCount-gListBox.RowCount);
   }
}


function ScrollSlider_OnChange(GUIControl *control) {

   // Convert slider % value to list box top item position
   if (gListBox.ItemCount<gListBox.RowCount)  {
      gListBox.TopItem  = 0;
   }
   else {
      gListBox.TopItem  = (gSlider.Value*(gListBox.ItemCount-gListBox.RowCount))/100;
   }
}
Title: Re: Using slider to scroll a Listbox
Post by: Dualnames on Fri 09/04/2010 20:21:27
Thanks a lot RickJ42(Yeah, I know about your sinful past.) :D. Going to give it a shot, and let you know.

EDIT: It works but kind of reversed.
I have 6 items. 1,2,3,4,5,6 are their names. Normally in the order they appear if I used Scroll function the way I wanted would be: 1,2,3,4,5,6 So scroll down goes 1,2,3,4,5,6 and scroll up 6,5,4,3,2,1.

This code however, does it reversed. Scroll up goes 1,2,3,4,5,6. And Scroll down 6,5,4,3,2,1.
Title: Re: Using slider to scroll a Listbox
Post by: Shane 'ProgZmax' Stevens on Sat 10/04/2010 04:47:59
Up/Down buttons might save you a lot of pain in the end with this.  I'm guessing the listbox will be used in the Guide for displaying topics?  If that's the case, I don't think most people would be bothered at all by parsing up or down the list x entries at a time.  In the very least it should avoid the need to fiddle with converting slider percentages and all that.

Title: Re: Using slider to scroll a Listbox
Post by: Dualnames on Sat 10/04/2010 10:37:47
Quote from: ProgZmax on Sat 10/04/2010 04:47:59
Up/Down buttons might save you a lot of pain in the end with this.  I'm guessing the listbox will be used in the Guide for displaying topics?  If that's the case, I don't think most people would be bothered at all by parsing up or down the list x entries at a time.  In the very least it should avoid the need to fiddle with converting slider percentages and all that.

It's not for the guide actually. It's for an inventory sort of gadget. I know I'll probably resolve to good old buttons for scrolling, but I'm still having my hopes on this.
Title: Re: Using slider to scroll a Listbox
Post by: Dualnames on Sat 10/04/2010 16:19:01
Quote from: RickJ on Fri 09/04/2010 20:17:18
You will need to manipulate ListBox.Top item when the slider changes it's value.   In the example below the slider represents a a percentage value.   If the number of items in the list change then the slider value and listbox top item need to be re-synchronized.  Resynch is also required if the listbox is scrolled using the the ScrollUp/Dn functions as well.  The technique is illustrated in the code sample below which is by no means complete or error free.

Best of codes Rick. :D
Not to hard to understand as well, I've reversed and works like a charm!
So here is what you change if you want reverse order.


function game_start() {

  // Scale slider to percent
  gSlider.Min = 0;
  gSlider.Max = 100;

if (invlistbox.ItemCount<invlistbox.RowCount)  {
ScrollSlider.Value = 100;
}
else {
ScrollSlider.Value = 100-((invlistbox.TopItem *100) /  (invlistbox.ItemCount-invlistbox.RowCount));
}
}

function ScrollSlider_OnChange(GUIControl *control) {
// Convert slider % value to list box top item position
  if (invlistbox.ItemCount<invlistbox.RowCount)  {
     invlistbox.TopItem  = 0;
   }
  else {
    int scrollingvaluereversed;
     scrollingvaluereversed=(100-ScrollSlider.Value);
    invlistbox.TopItem  = (scrollingvaluereversed*(invlistbox.ItemCount-invlistbox.RowCount))/100;
}
}
Title: Re: Using slider to scroll a Listbox ( SOLVED THANKS RICKJ!)
Post by: KiraHaraReturns on Fri 05/05/2017 14:44:23
is there any possibility to make a top-down slider?
Title: Re: Using slider to scroll a Listbox ( SOLVED THANKS RICKJ!)
Post by: Khris on Fri 05/05/2017 14:54:16
Just resize the slider so it's taller than wide, and it'll switch to a vertical one.
Title: Re: Using slider to scroll a Listbox ( SOLVED THANKS RICKJ!)
Post by: KiraHaraReturns on Fri 05/05/2017 15:09:39
Quote from: Khris on Fri 05/05/2017 14:54:16
Just resize the slider so it's taller than wide, and it'll switch to a vertical one.
thanks, I didn't expect this to be that easy :)