We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › slider value to define elements in a loop
Page Index Toggle Pages: 1
slider value to define elements in a loop (Read 889 times)
slider value to define elements in a loop
Oct 14th, 2009, 6:17am
 
hi, i have got a very long text (and graphics)  that I would like to draw to the screen. I would like to draw it bit by bit, so that only ever 7 words of the text are seen on the screen. I am using a slider to move the elements on and off the screen. So when I am calling my classes in draw() I would like the slider value to determine whether i am loading elements 0-6, 1-7, 2-8, 3-9... and so on.
My lowest slider value is 0 and the highest 600.  When the slidervalue has gone up +3.0 to the previous value or gone down -3.0 to the previous value I want to change which words are shown. Below is an outline of my sketch. I am struggling to figure out, how i can make this work for all values between 0 and 600 and how I would figure out if the value has increased or decreased.

int sliderController = 0;
ControlP5 controlP5;
Slider s;
float yPos = 0 ;
Rect2Gram[] rects2Gram;


void setup()
{
size(screen.width,screen.height-100);
 frameRate(25);
 background(backGroundCoLour);
 smooth();
controlP5 = new ControlP5(this);
Slider s = controlP5.addSlider("mySlider", 0, 600, 20 , width - 15, marginY , 5  , height - (marginY)-80);
s.setId(21);
}


void draw()
 {  
  //how do I amend this to work for all values between 0 and 600
 // to add 1 to sliderController if yPos increases by 3
// or to minus 1 off sliderController if yPos decreases by 3
   if ( (yPos >=3.0) && (yPos <=6.0))
  {
     println( "yPos: " + yPos);
     sliderController = 1;    
     println( "sliderController: " + sliderController);
     
  }
     rects2Gram = new Rect2Gram[7];
     for (int i = sliderController; i < sliderController + 6; i++)
     {  
        rects2Gram[i] = new Rect2Gram (words.length, words, i, words[i], yPos,);
        rects2Gram[i].createTwoCombinations();
         
     }
 }

void controlEvent(ControlEvent theEvent)
{
 if( theEvent.controller().id() == 21)
  {
       yPos = theEvent.controller().value();
  }
Re: slider value to define elements in a loop
Reply #1 - Oct 14th, 2009, 8:24am
 
OK, first thought is to suggest to use a slider range adapted to your needs, eg. equal to words.length, more or less.
Now, perhaps you have a reason for such range, eg. if you use the slider for something else (obviously we don't have the complete code here).
In this case, you can probably use map() to adapt the two ranges.

Note I fail to see the relationship between sliderController and yPos. Since I don't know what Rect2Gram does, it is hard to guess.
Re: slider value to define elements in a loop
Reply #2 - Oct 14th, 2009, 9:36am
 
I am loading my Rect2Gram class for words in stories of different length. For each word the class is loaded and some graphics are drawn. map() sounds great for generating the slidervalues in accordance to how many words each story has. But it doesn't tell me yet whether the slidervalue has increased or decreased from the value it previously had. how would i do that?
yPos moves the y position of the graphic for each word. When the user moves the slider some words are moved off the screen and others come onto it. Because my stories are very long i thought the slidervalue could also control which words in my word[] i am drawing. word[0] to word[6], word[1] to word[7]. Is that possible?
Re: slider value to define elements in a loop
Reply #3 - Oct 14th, 2009, 9:40am
 
The simplest way would be to store the previous value as a global variable (defined outside of all functions), and recognize when it changes, and in what direction.

As for determining range of array access with a single slider value, sure -- just use the slider value as one end of your range.  For slider value n, display elements n - (n+7).  constrain() will help keep you within the bounds of your array.length...
Re: slider value to define elements in a loop
Reply #4 - Oct 14th, 2009, 9:48am
 
sorry, i am being really slow, just can't get into my head how the variable would be able to know in which direction it has changed. if i set something like this

int sliderController = 0;

up before setup() what to i need to do in draw() to recognize the change?
Re: slider value to define elements in a loop
Reply #5 - Oct 14th, 2009, 1:34pm
 
adnil wrote on Oct 14th, 2009, 9:36am:
But it doesn't tell me yet whether the slidervalue has increased or decreased from the value it previously had.

No, because I feel you don't need the information. If I am not mistaken, you are using the slider as a scrollbar, so the absolute position should be enough to know which words to display.
Page Index Toggle Pages: 1