Proessing Java: Scrolling scrollbar does not alter text

For Processing, I am creating a scrollbar linked to a custom textarea. Although controlling the text by clicking the slider works perfectly fine, controlling the slider through scrolling does not work. Sorry if some things aren't proportioned properly, since I am taking the code from the text editor I am building in Processing.

I am splitting the idea of changing the text into 2 components (When the user clicks the slider to navigate, or when the user scrolls the slider, each with different lines of code to run). I know it's a bit long, so feel free to ask qusetions.

Here is the main code:

String content = "";
Scrollbar bar;
float scrollbarRatio;
float wheelCount;
int line = 1;
String absolutePath;
String fileName;

void setup()
{
  // Background and size
  background(0);
  size(displayWidth, displayWidth);

  selectInput("Select file input: ", "openFile");

  // Resizable Window
  if (frame != null) {
    frame.setResizable(true);
  }

  // Scrollbar
  bar = new Scrollbar(width-12, 55, 24, height - 79, 1);
}

void draw()
{
  // Flat design
  noStroke();
  smooth();

  textAlign(CENTER);

  // Get maximum line difference
  int maxLines = floor((height - 80) / 22);
  String[] adjustedLines = content.split("\n");

  int maxLineDifference = bar.getMaxLineDifference((float) maxLines / ((float) adjustedLines.length));
  int subtract = maxLineDifference + maxLines - adjustedLines.length;

  if (adjustedLines.length > maxLines)
  {
    scrollbarRatio = ((float) maxLines) / ((float) adjustedLines.length - subtract);
  } else
  {
    scrollbarRatio = 1;
  }

  // Text Box
  fill(80);
  rect(width/5, 55, width*4/5, height-55);

  textAlign(LEFT);
  textLeading(22);
  fill(225);

  String[] contentLines = content.split("\n");
  String display = "";
  int lineDifference = 0;

  if (wheelCount != 0)
  {
    if (adjustedLines.length > maxLines)
    {
      lineDifference = bar.scroll(int(wheelCount), scrollbarRatio, content.split("\n"), floor((height - 80) / 22), line);
      for (int i = lineDifference; i < maxLines + lineDifference; i++)
      {
        if (i < adjustedLines.length)
        {
          display += adjustedLines[i];
          display += "\n";
        }
      }
    } else
    {
      display = content;
    }

    wheelCount = 0;
    text(display, width/5+55, 75);
  } else
  {
    if (adjustedLines.length > maxLines)
    {
      lineDifference = bar.getLineDifference();

      for (int i = lineDifference; i < maxLines + lineDifference; i++)
      {
        if (i < adjustedLines.length)
        {
          display += adjustedLines[i];
          display += "\n";
        }
      }
    } else
    {
      display = content;
    }
  }

  text(display, width/5+55, 75);

  bar.resizeScrollbar(width-12, 55, 24, height - 79);

  if (adjustedLines.length > maxLines)
  {
    bar.update(scrollbarRatio);
    bar.display(scrollbarRatio);
  } else
  {
    bar.fullScrollbar(width - 24, 55);
  }
}

void mouseWheel(MouseEvent event)
{
  int e = event.getCount();
  int start = bar.scroll(e, scrollbarRatio, content.split("\n"), floor((height - 80) / 22), line);
  wheelCount = event.getCount();
}

// File - Open
void openFile(File selection)
{
  if (selection != null)
  {
    try
    {
      // Get file path
      absolutePath = selection.getAbsolutePath();
      String[] locations = split(absolutePath, "\\");


      // Read content of file
      String lines[] = loadStrings(absolutePath);

      // Reset content and line numbers
      content = "";
      line = 0;

      for (int i = 0; i < lines.length; i++) {
        String lineContent = lines[i] + "\n";
        content += lineContent;
        line++;
      }
    }
    catch (Exception exc)
    {
      println(exc.getMessage());
    }
  }
}

And here is the Scrollbar class:

class Scrollbar
{
  float swidth, sheight;    // width and height of bar
  float xpos, ypos;         // x and y position of bar
  float spos, newspos;      // x position of slider
  float sposMin, sposMax;   // max and min values of slider
  int loose;                // how loose/heavy
  boolean over;             // is the mouse over the slider?
  boolean locked;           // is the mouse clicked over the slider
  int lineDifference;       

  Scrollbar (int xp, int yp, int sw, int sh, int l) {
    swidth = sw;
    sheight = sh;
    xpos = xp-swidth/2;
    ypos = yp;
    spos = ypos;
    newspos = spos;
    sposMin = ypos;
    sposMax = ypos + sheight;
    loose = l;
  }

  void resizeScrollbar(int xp, int yp, int sw, int sh)
  {
    xpos = xp - swidth/2;
    ypos = yp;
    swidth = sw;
    sheight = sh;
  }

  void update(float ratio) {
    if (over()) {
      over = true;
    } else {
      over = false;
    }
    if (mousePressed && over) {
      locked = true;
    }
    if (!mousePressed) {
      locked = false;
    }
    if (locked) {
      sposMax = ypos + sheight;
      newspos = constrain(mouseY-swidth/2, sposMin, sposMax - sheight * ratio);
      lineDifference = ceil((newspos - 55)/22 * (1/ratio));

      if (abs(newspos - spos) > 1) {
        spos = spos + (newspos-spos)/loose;
      }
    }
  }

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

  boolean over() {
    if (mouseX > xpos && mouseX < xpos+swidth &&
      mouseY > ypos && mouseY < ypos+sheight) {
      return true;
    } else {
      return false;
    }
  }

  void fullScrollbar(int x, int y)
  {
    if (mouseX > x && mouseX < x + 24 && mouseY > 55 && mouseY < height - 20)
    {
      fill(80);
    } else
    {
      fill(190);
    }

    rect(x, y, 24, height - 79);
  }

  int scroll(int delta, float ratio, String[] adjustedLines, int maxlines, int line)
  {
    spos += 15 * delta;
    spos = constrain(spos, 55, height - 24 - sheight*ratio);

    int start = ceil((spos) * adjustedLines.length / (sheight) - 24 + 1);
    return start;
  }

  void display(float ratio) {
    fill(220);
    rect(xpos, ypos, swidth, sheight);

    if (over || locked) {
      fill(80);
    } else {
      fill(190);
    }

    // Removes whitespace between slider and beginning of scrollbar
    if (spos != 55 && floor(spos) == 55)
    {
      spos = 55;
    }

    // Removes whitespace between slider and end of scrollbar
    if ((spos + sheight * ratio) != (sheight + ypos) && ceil(spos + sheight * ratio) == (sheight + ypos))
    {
      spos = sheight + ypos - (sheight * ratio);
    }

    //println(spos + ", " + str(height - 24 - sheight*ratio));
    rect(xpos, spos, swidth, sheight * ratio);
  }

  int getLineDifference()
  {
    return lineDifference;
  }

  int getMaxLineDifference(float ratios)
  {
    return ceil(((height - 24 - scrollbarRatio * sheight) - 55)/22 * (1/ratios));
  }
}
Tagged:
Sign In or Register to comment.