/*
 This template code provides a method for combining ControlP5 
 with the piGeon geometry library and interface.  The method
 gives the user dynamic control over geometry variables using 
 interactive controls such as sliders, toggles, knobs, and 
 other cool GUI stuff.
 
 Required Libraries 
 piGeon:  http://igeo.jp
 ControlP5:  http://www.sojamo.de/libraries/controlP5/
*/

//2011 Nathan Miller, The Proving Ground

import igeo.*;
import processing.opengl.*;
import controlP5.*;

//DECLARE CONTROLS
ControlP5 controlP5;
ControlWindow myControlWindow;  //initialize control window
Controller slider;  //initialize slider

//SETUP
/*
 Use void setup() to initialize piGeon interface, and ControlP5. 
 void setup() can also be used to establish static geometry 
 (imports, references)
*/
void setup() {
  //INITIALIZE MAIN INTERFACE
  size(480, 360, IG.GL);

  //INITIALIZE CONTROLP5 AND CONTROL WINDOW
  controlP5 = new ControlP5(this);
  myControlWindow = controlP5.addControlWindow("piGeon Controls", 100, 100, 480, 360);
  myControlWindow.hideCoordinates(); //hides window dimensions

  //INITIALIZE CONTROLS
  slider = controlP5.addSlider("X", 0, 20, 10, 80, 70, 300, 14);
  slider.setWindow(myControlWindow); //assign slider to myControlWindow
}

//DRAW
/*
 Use void draw() for creating variable geometry that will 
 update with slider changes.
*/
void draw() {
  //REFRESH PIGEON GEOMETRY
  refpiGeon(); //function to refresh variable piGeon geometry

  //SLIDER PARAMETERS
  //Assign slider parameters to script variables.
  float myParam = slider.value();

  //MAIN CODE TO RUN GOES BELOW....
  new IBox(0,0,0,  myParam, myParam, myParam).clr(255, 150, 0);
}

//REFRESH FUNCTION.
//Clears piGeon geometry in the scene.
void refpiGeon() {
  //Refreshes (deletes) iGeo geometry.
  
  IPoint[] ptarr = IG.points();
  ICurve[] crvarr = IG.curves();
  ISurface[] srfarr = IG.surfaces();
  IBrep[] breparr = IG.breps();
  IMesh[] mesharr = IG.meshes();

  //clear points
  for (IPoint pt: ptarr) {
    pt.del();
  }
  //clear curves
  for (ICurve crv: crvarr) {
    crv.del();
  }
  //clear surfaces
  for (ISurface srf:  srfarr) {
    srf.del();
  }
  //clear BReps
  for (IBrep brep:  breparr) {
    brep.del();
  }
  //clear meshes
  for (IMesh mesh: mesharr) {
    mesh.del();
  }
}

