Simple timeline using scrollbar
in
Programming Questions
•
11 months ago
I'm trying to create a timeline without using a library, where the timeline is a scrollbar. The position of the scrollbar gives which image to show. Here is what I've got so far:
- Scrollbar bar1;
- PImage img;
- int img_index=0;
- //folder with images (4 images)
- String path = "C:/Users/Vibeke/Documents/Processing/mine_tester/bildegalleri4/Data";
- String[] img_list = images(path);
- void setup() {
- size(400, 400);
- noStroke();
- // Inputs: x, y, width, height, minVal, maxVal
- bar1 = new Scrollbar(0, height-20, width, 10, img_index, img_list.length);
- img = loadImage(img_list[img_index]);
- }
- void draw() {
- background(175);
- fill(0);
- int pos1 = int(bar1.getPos());
- //Will this work? What to put in the if statement?
- //if(){
- // img_index++;
- // if(img_index >= img_list.length) img_index=0;
- // img = loadImage(img_list[img_index]);
- //}
- text(nf(pos1, 2), 50, 30); //don't need this
- bar1.update(mouseX, mouseY);
- bar1.display();
- }
- void mousePressed() {
- bar1.press(mouseX, mouseY);
- }
- void mouseReleased() {
- bar1.release();
- }
- class Scrollbar {
- int x, y; // The x- and y-coordinates
- float sw, sh; // Width and height of scrollbar
- float pos; // Position of thumb
- float posMin, posMax; // Max and min values of thumb
- boolean rollover; // True when the mouse is over
- boolean locked; // True when its the active scrollbar
- float minVal, maxVal; // Min and max values for the thumb
- Scrollbar (int xp, int yp, int w, int h, float miv, float mav) {
- x = xp;
- y = yp;
- sw = w;
- sh = h;
- minVal = miv;
- maxVal = mav;
- pos = x + sw/2 - sh/2;
- posMin = x;
- posMax = x + sw - sh;
- }
- // Updates the over boolean and the position of the thumb
- void update(int mx, int my) {
- if (over(mx, my) == true) {
- rollover = true;
- }
- else {
- rollover = false;
- }
- if (locked == true) {
- pos = constrain(mx-sh/2, posMin, posMax);
- }
- }
- // Locks the thumb so the mouse can move off and still update
- void press(int mx, int my) {
- if (rollover == true) {
- locked = true;
- }
- else {
- locked = false;
- }
- }
- // Resets the scrollbar to neutral
- void release() {
- locked = false;
- }
- // Returns true if the cursor is over the scrollbar
- boolean over(int mx, int my) {
- if ((mx > x) && (mx < x+sw) && (my > y) && (my < y+sh)) {
- return true;
- }
- else {
- return false;
- }
- }
- // Draws the scrollbar to the screen
- void display() {
- fill(255);
- rect(x, y, sw, sh);
- if ((rollover==true) || (locked==true)) {
- fill(0);
- }
- else {
- fill(102);
- }
- rect(pos, y, sh, sh);
- }
- // Returns the current value of the thumb
- float getPos() {
- float scalar = sw/(sw-sh);
- float ratio = (pos - x) * scalar;
- float offset = minVal + (ratio/sw * (maxVal-minVal));
- return offset;
- }
- }
- String[] images(String dir) {
- File file = new File(dir);
- if (file.isDirectory()) {
- String names[] = file.list();
- return names;
- } else {
- // If it's not a directory
- return null;
- }
- }
I have no idea what test should be in the if statement, or if this is the right way to do it at all. Anybody got an idea?
1