Display Table data from a csv and return the rowID when the user clicks on a row


// shows a table
//    on mouse over: status bar text 
//    on mouse click: Info box

// class with tools 
ToolsForTable toolsForTable = new ToolsForTable(); 

// the table
Table tableResources; 

//----------------------------------------------------------------------------------------------------------------

void setup() {
  size(1600, 660);
  background(toolsForTable.WHITE);

  // setting up table ---
  setupTable();
}

void draw() {
  background(0); 
  toolsForTable.showTable(tableResources, 
    20, 52);

  fill(toolsForTable.WHITE);
  text("The Settlers of Catan - Building Costs", 
    20, 27);
  text("Place the mouse over a word to show full text in the Status Bar. Click to show Info Box. ", 
    20, height/2-110);
}

//----------------------------------------------------------------------------------------------------------------

void setupTable() { 
  // Make entire table 

  // setting up table (column names / headlines) ---
  tableResources = toolsForTable.newTable("Item", "brick", "wood", "sheep", "wheat", "ore", "points");

  // adding data (number of data must match number of headlines)
  println("Adding Road row");   
  tableResources = toolsForTable.tableAddDataAsNewRow ( tableResources, "Road (establishing the longest road: 2 victory points)", "1x", "1x", "", "", "", "0" );
  if (tableResources==null)
    return; 

  println("Adding Settlement row");   
  tableResources = toolsForTable.tableAddDataAsNewRow ( tableResources, "Settlement. Players with a settlement adjacent to a hex containing the number just rolled with the dice receive one card of the corresponding resource.", "1x", "1x", "1x", "1x", "", "1" );  
  if (tableResources==null)
    return; 

  println("Adding City row");   
  tableResources = toolsForTable.tableAddDataAsNewRow ( tableResources, "City (which must replace existing settlement). Cities produce two cards of the corresponding resource.", "", "", "", "2x", "3x", "2");
  if (tableResources==null)
    return; 

  println("Adding 'Development Card' row");   
  String textDevelopmentCard = 
    "Development Card - Three types of development cards include cards worth one victory point; "
    +"knight cards, which allow the player to move the robber as if they had rolled a 7 (but without the remove-half rule); "
    +"and a third set of cards which allow the player one of three abilities when played.";
  tableResources = toolsForTable.tableAddDataAsNewRow ( tableResources, textDevelopmentCard, 
    "", "", "1x", "1x", "1x", "?"  );  
  if (tableResources==null)
    return; 
  //
}

// ------------------------------------------------------------------------------

void mousePressed() {
  // use class: 
  toolsForTable.mousePressedInClass(tableResources);
}//func 

void keyPressed() {
  key=0; // kill ESC 

  // use class: 
  toolsForTable.flagShowInfoBox=false;
}

//===============================================================================
// Tools collection 

class ToolsForTable {

  // class not like a car class Car for an object but a collection of tools. 

  final color RED   = color(255, 0, 0); 
  final color GREEN = color(0, 255, 0); 
  final color BLUE  = color(0, 0, 255); 

  final color WHITE = color(255); 
  final color BLACK = color(0); 
  final color GRAY  = color(255/2); 

  final color YELLOW  = color(255, 255, 13); 

  int factorX=122; // column width 

  boolean cursorSignShowFlag=false; 

  // show Info box?
  boolean flagShowInfoBox=false;
  int flagShowInfoBox_Row=0; 

  // ----------------------------------------------------------------

  void showTable(Table tableBtn, 
    int x, int y) { 

    //  int factorX=122; // column width 

    // rect
    stroke(WHITE);
    noFill();
    rect( x, y, 
      tableBtn.getColumnCount()*factorX-6, (tableBtn.getRowCount()+1) * 22 + 10 );

    // headline 
    showTableHeadline(tableBtn, x+6, y+19, factorX);

    // horizontal line 
    stroke(WHITE);
    line( x+2, y+5+19, 
      6+x+(tableBtn.getColumnCount())*factorX-13, y+5+19);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x) 
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

        fill(WHITE);
        text(row.getString(i2), 
          i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15);

        if (mouseInside(i2*factorX+x+6, 25+ i * 22 +y+8, 
          factorX-8, 15)) {
          statusBar(row.getString(i2));
        }//if

        // vertical line 
        stroke(WHITE);
        line( i2*factorX+x, +y, 
          i2*factorX+x, tableBtn.getRowCount() * 22 + y + 31);
      }//for
    }//for

    showInfoBox(tableBtn);
  }// method

  void showInfoBox(Table tableBtn) {
    // abort? 
    if (!flagShowInfoBox)
      return; 

    // current data row
    TableRow row = tableBtn.getRow(flagShowInfoBox_Row);

    String builtText=""; 

    // loop over columns in that row (i2 is for x) 
    for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
      builtText+=row.getString(i2) + "\n";
    }//for

    // Yellow box
    fill(YELLOW);
    rect(width-212, 25 +33+8-6, 
      190, 400);
    // box frame
    noFill(); 
    stroke(BLACK);
    final int innerDist=4; 
    rect(width-212+innerDist, 25 +33+8-6+innerDist, 
      190-innerDist*2, 400-innerDist*2);

    // Text
    fill(BLACK);
    text(builtText, 
      width-200, 25 +33+8, 
      180, 915);
  }//method

  void mousePressedInClass(Table tableBtn) {
    //
    int x=22;
    int y=52;

    flagShowInfoBox=false;

    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      //// loop over columns in that row (i2 is for x) 
      //for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {

      if (mouseInsideRow(0, 25+ i * 22 +y+8, 
        factorX-8, 15)) {
        // statusBar(row.getString(i2));
        flagShowInfoBox=true; 
        flagShowInfoBox_Row = i; 
        return;
      }//if
      //   }
    }
  }

  // ---

  boolean mouseInsideRow( float x_, float y_, 
    float w_, float h_) {
    return 
      mouseY>y_ &&
      mouseY<y_+h_;
  }//func

  boolean mouseInside( float x_, float y_, 
    float w_, float h_) {
    return mouseX>x_ &&
      mouseX<x_+w_ &&
      mouseY>y_ &&
      mouseY<y_+h_;
  }//func

  // ---

  void statusBar(String textStatus) {
    if (textStatus.equals(""))
      return;

    fill(GRAY); 
    noStroke(); 
    rect(0, height-32, 
      width, 33);
    fill(WHITE); 
    text (textStatus, 
      3, height-12);
  }//func 

  void showTableHeadline(Table tableBtn, 
    int x, int y, 
    int factorX) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      fill(GREEN);
      text(row0.getColumnTitle(i), 
        i*factorX+x, y-2);
    }
  }//method 

  // ---
  // make table 

  Table newTable (String... listColumnNames) { 

    Table newT = new Table();

    // make columns
    for (String s1 : listColumnNames) {
      newT.addColumn(s1);
    }

    return newT;
  }//method

  Table tableAddDataAsNewRow(Table table1, String... data1) {
    //
    //println(data1.length);
    //println(table1.getColumnCount());

    if (data1.length != table1.getColumnCount()) {
      // 
      println("Big Error: "+"tableAddData says: the number of values doesn't match the number of columns (values were: "
        +stringFromArray(data1)+").");
      exit();
      return null;
    }

    // add rows with data 
    TableRow newRow = table1.addRow();

    // add rows with data 
    int i=0; 
    for (String s1 : data1) {
      newRow.setString(newRow.getColumnTitle(i), s1);
      i++;
    }

    // return result 
    return table1;
  }//method

  // --- 

  String stringFromArray(String... data1) {
    String result=" | "; 

    for (String s1 : data1) {
      result += s1+" | ";
    }//for

    return result;
  }//method

  void printlnTable(Table tableBtn) { 

    // rect
    stroke(WHITE);
    noFill();

    println("------------------------------------");

    // headline 
    printlnTableHeadline(tableBtn);

    // grid 
    // loop over rows (y)
    for (int i=0; i<tableBtn.getRowCount(); i++) {

      // current data row
      TableRow row = tableBtn.getRow(i);

      // loop over columns in that row (i2 is for x)
      String s1="";
      for (int i2=0; i2<tableBtn.getColumnCount(); i2++) {
        s1+="   "+row.getString(i2);
        //
      }//for
      println(s1);
    }//for
    println("===========================================");
    //
  } // method 

  void printlnTableHeadline(Table tableBtn ) { 
    // headline for table 
    TableRow row0 = tableBtn.getRow(0);
    for (int i=0; i<tableBtn.getColumnCount(); i++) {
      // headline 
      print("   "+row0.getColumnTitle(i));
    }
    println("");
  }//method 

  // ------------------------------------------------------------------

  String cursorSign() {
    // blinking cursor sign | for Input Box 
    if (frameCount % 13 == 0)
      cursorSignShowFlag= ! cursorSignShowFlag;
    if (cursorSignShowFlag)
      return"|";
    else return"";
  }//method
  //
}//class
//

2 Likes