Using slider to scroll a Listbox ( SOLVED THANKS RICKJ!)

Started by Dualnames, Fri 09/04/2010 18:35:50

Previous topic - Next topic

Dualnames

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
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

NsMn

Try something like this:

Code: ags
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.

Dualnames

Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

RickJ

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.

Code: ags

// 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;
   }
}

Dualnames

#4
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.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Shane 'ProgZmax' Stevens

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.


Dualnames

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.
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

Dualnames

#7
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.

Code: ags

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;
}
}
Worked on Strangeland, Primordia, Hob's Barrow, The Cat Lady, Mage's Initiation, Until I Have You, Downfall, Hunie Pop, and every game in the Wadjet Eye Games catalogue (porting)

KiraHaraReturns

is there any possibility to make a top-down slider?

Khris

Just resize the slider so it's taller than wide, and it'll switch to a vertical one.

KiraHaraReturns

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 :)

SMF spam blocked by CleanTalk