Loading...
Logo
Processing Forum

Vertical Scrollbar

in General Discussion  •  Other  •  3 years ago  
Has anybody created a vertical scrollbar. I can work with the horizontal scrollbar with the example, but can't figure out what to modify to make a vertical one.

Thanks

Replies(4)

can't you take the X,W values and swap them to Y,H values?  Would imply a second scrollbar class... VScrollbar perhaps.  But with a bit of abstraction, you should be able to combine the two into a single class.
Try to change the X values to Y values and examine your code and try to change Width to Height since you have to determinate the vertical position of the slider in the "update" method , it's almost the same, and of course change the way you're drawing the scrollbar and the slider in the "display" method since the scrollbar is being drawn horizontally. I recomend you to use only one instance of "HScrollbar" for better understanding of code.
Hi there,
if there are people still interested in this topic, see attached code for vertical scrollbars.
I just changed the x,y, width and height variables from the example  scrollbars.

Have fun, best regards.
Copy code
  1. VScrollbar vs1, vs2;

  2. PImage left, right;         // Two image to load
  3. int leftWidth, rightWidth;  // The width of the left and right images

  4. void setup()
  5. {
  6.   size(200, 200);
  7.   noStroke();
  8.   vs1 = new VScrollbar(10, 0, 20, height, 3*5+1);
  9.   vs2 = new VScrollbar(width-10, 0, 20, height, 3*5+1);
  10.   left = loadImage("left.jpg");
  11.   leftWidth = left.width;
  12.   right = loadImage("right.jpg");
  13.   rightWidth = right.width;
  14. }

  15. void draw()
  16. {
  17.   background(255);
  18.   
  19.   // Get the position of the left scrollbar
  20.   // and convert to a value to display the left image 
  21.   float leftPos = vs1.getPos()-height+150;
  22.   fill(255);
  23.   image(left, 0, height/2-leftWidth/2 + leftPos*2);
  24.   
  25.   // Get the position of the right scrollbar
  26.   // and convert to a value to display the right image
  27.   float rightPos = vs2.getPos()-width/2;
  28.   fill(255);
  29.   image(right, leftWidth, height/2-rightWidth/2 + rightPos*2);
  30.  
  31.   vs1.update();
  32.   vs2.update();
  33.   vs1.display();
  34.   vs2.display();
  35. }

  36. class VScrollbar
  37. {
  38.   int swidth, sheight;    // width and height of bar
  39.   int xpos, ypos;         // x and y position of bar
  40.   float spos, newspos;    // x position of slider
  41.   int sposMin, sposMax;   // max and min values of slider
  42.   int loose;              // how loose/heavy
  43.   boolean over;           // is the mouse over the slider?
  44.   boolean locked;
  45.   float ratio;

  46.   VScrollbar (int xp, int yp, int sw, int sh, int l) {
  47.     swidth = sw;
  48.     sheight = sh;
  49.     int heighttowidth = sh - sw;
  50.     ratio = (float)sh / (float)heighttowidth;
  51.     xpos = xp-swidth/2;
  52.     ypos = yp;
  53.     spos = ypos + sheight/2 - swidth/2;
  54.     newspos = spos;
  55.     sposMin = ypos;
  56.     sposMax = ypos + sheight - swidth;
  57.     loose = l;
  58.   }

  59.   void update() {
  60.     if(over()) {
  61.       over = true;
  62.     } else {
  63.       over = false;
  64.     }
  65.     if(mousePressed && over) {
  66.       locked = true;
  67.     }
  68.     if(!mousePressed) {
  69.       locked = false;
  70.     }
  71.     if(locked) {
  72.       newspos = constrain(mouseY-swidth/2, sposMin, sposMax);
  73.     }
  74.     if(abs(newspos - spos) > 1) {
  75.       spos = spos + (newspos-spos)/loose;
  76.     }
  77.   }

  78.   int constrain(int val, int minv, int maxv) {
  79.     return min(max(val, minv), maxv);
  80.   }

  81.   boolean over() {
  82.     if(mouseX > xpos && mouseX < xpos+swidth &&
  83.     mouseY > ypos && mouseY < ypos+sheight) {
  84.       return true;
  85.     } else {
  86.       return false;
  87.     }
  88.   }

  89.   void display() {
  90.     fill(255);
  91.     rect(xpos, ypos, swidth, sheight);
  92.     if(over || locked) {
  93.       fill(255, 255, 0);
  94.     } else {
  95.       fill(255, 0, 0);
  96.     }
  97.     rect(xpos, spos, swidth, swidth);
  98.   }

  99.   float getPos() {
  100.     // Convert spos to be values between
  101.     // 0 and the total width of the scrollbar
  102.     return spos * ratio;
  103.   }
  104. }


Philipp Ronnenberg
Interaction design
-
http://www.phiron.de
I did the same as the poster above, modified the Topic - GUI - Scrollbar example to my own use. I guess that example is very simple by design to get people going and improve it. Still I just thought I'd offer my version, and also a slightly modified Buttons class, with a clicked() method. Not perfect but it works.

The scrollbar class is really a slider, but the name stuck from the example, for now at least. It returns or set a float value from 0 to 1.

I know it's an old thread, but I was planning to post this anyway, and might as well do it in a related place. Also, this was basically made before I knew about the controlP5 library (and which I don't really understand much of). ControlP5 also contains a lot more.


Anyway, the scrollbar and buttons classes:
Copy code
  1. /**  Horizontal scrollbar

      A sightly improved Hscrollbar class
      - Returns or sets a float value from 0 - 1

      Built on Topics/GUI/Scrollbar example in processing 1.2.1 (and 1.5.1).
      Ok, scrollbars are not really scrollbars, but sliders (setting/getting values).
      (But they could theoretically be used as scrollbars...)
    */

    class HScrollbar
    {
      int barWidth, barHeight; // width and height of bar. NOTE: barHeight also used as slider button width.
      int Xpos, Ypos;          // upper-left position of bar
      float Spos, newSpos;     // x (leftmost) position of slider
      int SposMin, SposMax;    // max and min values of slider
      int loose;               // how loose/heavy
      boolean over;            // True if hovering over the scrollbar
      boolean locked;          // True if a mouse button is pressed while on the scrollbar
      color barOutlineCol;
      color barFillCol;
      color barHoverCol;
      color sliderFillCol;
      color sliderPressCol;

      HScrollbar (int X_start, int Y_start, int bar_width, int bar_height, int loosiness,
                  color bar_outline, color bar_background, color slider_bg, color barHover, color slider_press) {
        barWidth = bar_width;
        barHeight = bar_height;
        Xpos = X_start;
        Ypos = Y_start;
        Spos = Xpos + barWidth/2 - barHeight/2; // center it initially
        newSpos = Spos;
        SposMin = Xpos;
        SposMax = Xpos + barWidth - barHeight;
        loose = loosiness;
        if (loose < 1) loose = 1;
        barOutlineCol  = bar_outline;
        barFillCol     = bar_background;
        sliderFillCol  = slider_bg;
        barHoverCol    = barHover;
        sliderPressCol = slider_press;
      }

      void update() {
        over = over();
        if(mousePressed && over) locked = true; else locked = false;

        if(locked) {
          newSpos = constrain(mouseX-barHeight/2, SposMin, SposMax);
        }
       
        if(abs(newSpos - Spos) > 0) {
          Spos = Spos + (newSpos-Spos)/loose;
        }
      }

      int constrain(int val, int minv, int maxv) {
        return min(max(val, minv), maxv);
      }

      boolean over() {
        if(mouseX > Xpos && mouseX < Xpos+barWidth &&
        mouseY > Ypos && mouseY < Ypos+barHeight) {
          return true;
        } else {
          return false;
        }
      }

      void display() {
        stroke(barOutlineCol);
        fill(barFillCol);
        rect(Xpos, Ypos, barWidth, barHeight);
        if(over) {
          fill(barHoverCol);
        }
        if (locked) {
          fill(sliderPressCol);
        }
        if (!over && !locked) {
          fill (sliderFillCol);
        }
        if (abs(Spos-newSpos)>0.1) fill (sliderPressCol);
        rect(Spos, Ypos, barHeight, barHeight);
      }

      float value() {
        // Convert slider position Spos to a value between 0 and 1
        return (Spos-Xpos) / (barWidth-barHeight);
      }
     
      void setValue(float value)
      {
        // convert a value (0 to 1) to slider position
        if (value<0) value=0;
        if (value>1) value=1;
        Spos = Xpos + ((barWidth-barHeight)*value);
        newSpos = Spos;
      }
    }


    /**  Vertical scrollbar

       Modified from Hscrollbar class
       Returns or sets a float value from 0 - 1
    */

    class VScrollbar
    {
      int barWidth, barHeight; // width and height of bar. NOTE: barWidth also used as slider button height.
      int Xpos, Ypos;          // upper-left position of bar
      float Spos, newSpos;     // y (upper) position of slider
      int SposMin, SposMax;    // max and min values of slider
      int loose;               // how loose/heavy
      boolean over;            // True if hovering over the scrollbar
      boolean locked;          // True if a mouse button is pressed while on the scrollbar
      color barOutlineCol;
      color barFillCol;
      color barHoverCol;
      color sliderFillCol;
      color sliderPressCol;

      VScrollbar (int X_start, int Y_start, int bar_width, int bar_height, int loosiness,
                  color bar_outline, color bar_background, color slider_bg, color barHover, color slider_press) {
        barWidth = bar_width;
        barHeight = bar_height;
        Xpos = X_start;
        Ypos = Y_start;
        Spos = Ypos + barHeight/2 - barWidth/2; // center it initially
        newSpos = Spos;
        SposMin = Ypos;
        SposMax = Ypos + barHeight - barWidth;
        loose = loosiness;
        if (loose < 1) loose = 1;
        barOutlineCol  = bar_outline;
        barFillCol     = bar_background;
        sliderFillCol  = slider_bg;
        barHoverCol    = barHover;
        sliderPressCol = slider_press;
      }

      void update() {
        over = over();
        if(mousePressed && over) locked = true; else locked = false;

        if(locked) {
          newSpos = constrain(mouseY-barWidth/2, SposMin, SposMax);
        }
        if(abs(newSpos - Spos) > 0) {
          Spos = Spos + (newSpos-Spos)/loose;
        }
      }

      int constrain(int val, int minv, int maxv) {
        return min(max(val, minv), maxv);
      }

      boolean over() {
        if(mouseX > Xpos && mouseX < Xpos+barWidth &&
        mouseY > Ypos && mouseY < Ypos+barHeight) {
          return true;
        } else {
          return false;
        }
      }
     
      void display() {
        stroke(barOutlineCol);
        fill(barFillCol);
        rect(Xpos, Ypos, barWidth, barHeight);
        if(over) {
          fill(barHoverCol);
        }
        if (locked) {
          fill(sliderPressCol);
        }
        if (!over && !locked) {
          fill (sliderFillCol);
        }
        if (abs(Spos-newSpos)>0.1) fill (sliderPressCol);
        rect(Xpos, Spos, barWidth, barWidth);
      }

      float value() {
        // Convert slider position Spos to a value between 0 and 1
        return (Spos-Ypos) / (barHeight-barWidth);
      }
     
      void setValue(float value) {
        // convert a value (0 to 1) to slider position Spos
        if (value<0) value=0;
        if (value>1) value=1;
        Spos = Ypos + ((barHeight-barWidth)*value);
        newSpos = Spos;
      }
    }




    /**  Button class

    Slightly modified from processing 1.2.1 Buttons class example
    */

    class Button
    {
      int x, y;
      int size;
      color edgeCol, baseColor, highlightColor;
      color hoverColor, currentColor;
      boolean pressed = false;  
      boolean oldPressed = false;
      boolean outsidePressed = false;

      void update()
      {
        if(over()) {
          currentColor = hoverColor;
          if (mousePressed && !oldPressed && !outsidePressed) {
            pressed = true;
            currentColor = highlightColor;
          }
          if (!mousePressed) {
            oldPressed = pressed;
            pressed = false;
            outsidePressed = false;
          }
        } else {
          currentColor = baseColor;
          pressed = false;
          outsidePressed = mousePressed;
        }
      }

      boolean pressed() {
        return pressed;
      }
     
      boolean clicked() {
        return oldPressed;
      }

      boolean over() {
        return false;
      }
    }


    class CircleButton extends Button
    {
      CircleButton(int ix, int iy, int isize, color edColor, color iColor, color iHover, color iHighlight) {
        x = ix;
        y = iy;
        size = isize;
        edgeCol = edColor;   
        baseColor = iColor;
        hoverColor = iHover;
        highlightColor = iHighlight;
        currentColor = baseColor;
      }

      boolean over() {
        float disX = x - mouseX;
        float disY = y - mouseY;
        if(sqrt(sq(disX) + sq(disY)) < size/2 ) {
          return true;
        } else return false;
      }

      void display() {
        stroke(edgeCol);
        fill(currentColor);
        ellipse(x, y, size, size);
      }
    }


    class RectButton extends Button
    {
      RectButton(int ix, int iy, int isize, color edColor, color iColor, color iHover, color iHighlight) {
        x = ix;
        y = iy;
        size = isize;
        edgeCol = edColor;   
        baseColor = iColor;
        hoverColor = iHover;
        highlightColor = iHighlight;
        currentColor = baseColor;
      }

      boolean over() {
        if (mouseX >= x && mouseX <= x+size &&
          mouseY >= y && mouseY <= y+size) {
          return true;
        } else {
          return false;
        }
      }

      void display() {
        stroke(edgeCol);
        fill(currentColor);
        rect(x, y, size, size);
      }
    }

Also, here's a little test program to test them:

Copy code
  1. /**   Scrollbar 0.12

    Scrollbars and buttons test.
    Also testing button and slider arrays, with different "loosiness" of sliders.

    Public Domain :p

    2012.05.08 - raron - Improved classes a bit.
    */

    // Nr. of test controls of each type
    int bars = 8;

    // Scrollbars and buttons
    VScrollbar Vslider [];
    HScrollbar Hslider [];
    CircleButton oButton [];
    RectButton xButton [];

    void setup()
    {
      size  (400,500);

      // Scrollbar/slider colors
      color scrEdgdeCol = color(0,0,0);
      color scrBgCol    = color(100,100,100);
      color sliderColor = color(0,150,200);
      color scrHoverCol = color(100,200,200);
      color scrPressCol = color(100,255,255);
     
      // Button colors
      color btnEdgeCol  = color(255,255,0);
      color btnColor    = color(0,0,0);
      color btnHoverCol = color(0,200,0);
      color btnPressCol = color(200,0,0);

      // The scrollbars (or sliders) and buttons array initialization
      Vslider = new VScrollbar[bars];
      Hslider = new HScrollbar[bars];
      oButton = new CircleButton[bars];
      xButton = new RectButton[bars];
     
      // The scrollbars (or sliders) and buttons array instantiation
      for (int i=0; i<bars; i++) {
        Vslider[i] = new VScrollbar(10+i*20, 20,  16, 200, i*2, scrEdgdeCol, scrBgCol, sliderColor, scrHoverCol, scrPressCol);
        Hslider[i] = new HScrollbar(10, 250+i*30, 250, 8+i*3, i*2, scrEdgdeCol, scrBgCol, sliderColor, scrHoverCol, scrPressCol);

        oButton[i] = new CircleButton(bars*20+30, 20+i*30, (i+4)*3, btnEdgeCol, btnColor, btnHoverCol, btnPressCol);
        xButton[i] = new RectButton(bars*20+70-(bars+4-i), 20+i*30, (bars+4-i)*2, btnEdgeCol, btnColor, btnHoverCol, btnPressCol);
      }

      // Set some initial slider values for fun
      for (int i=0; i<bars; i++) {
        Vslider[i].setValue(1-0.66*cos((float)(i+1)/(bars*4)*2*PI));
        Hslider[i].setValue(0.78*sin((float)(bars-i)/(bars*3)*2*PI));
      }
     
    }

    void draw()
    {
      background(192,192,192);
      fill(0,0,0);
     
      text("Sliders and buttons test 0.12",10,10);


      for (int i=0; i<bars; i++) {

        if (oButton[i].clicked() == true) println("Round button " + i + " clicked!");
        if (oButton[i].pressed() == true) print("-");
     
        if (xButton[i].clicked() == true) println("Square Button " + i + " clicked!");
        if (xButton[i].pressed() == true) print("-");
       
        // Write only the horizontal slider values
        text("Hs " + i + ": " + Hslider[i].value(), 265, 265+i*30);

        // Scrollbars/sliders and buttons updates and displaying
        Vslider[i].update();
        Vslider[i].display();

        Hslider[i].update();
        Hslider[i].display();

        oButton[i].update();
        oButton[i].display();

        xButton[i].update();
        xButton[i].display();
      }

    }