/**************************************************
 *   File:     Transparency.cpp
 *   Author:   Dr. Dalton R. Hunkins     
 *   Date:     February 2009
 *
 *
 *  Modes of User Interaction
 *   GLUI Constrols:
 *     Spinner -  set alpha value
 *     Button  -  close program
**************************************************/
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GL/glui.h>
#include <iostream>
using namespace std;

#include "CGWorld.h"
#include "Selection.h"

class ProgramExit { };

GLUI_Panel       *mainPanel;
GLUI_Spinner     *spinner;

CGWorld     cgWorld;
Selection   selection;

int mainWindowHandle;
float mainClearColor[] = {0.75, 0.75, 1.00, 1.00};

 // The GLUI Spinner and Button Handlers
void spinnerHandler(int id) {
   cgWorld.setAlpha(spinner->get_float_val());
}

void closeButtonHandler(int id) {
   throw ProgramExit();
}

void displayMain() {
    glClearColor(mainClearColor[0], mainClearColor[1], mainClearColor[2], mainClearColor[3]);  
    glClear(GL_COLOR_BUFFER_BIT);
    glutSwapBuffers();
}

void reshapeMain(int width, int height) {
   glutSetWindow(cgWorld.getWindowHandle());
   glutPositionWindow((cgWorld.getCornerX()*width)/100,
                      (cgWorld.getCornerY()*height)/100);
   glutReshapeWindow((cgWorld.getWidth()*width)/100,
                     (cgWorld.getHeight()*height)/100);  
                     
   glutSetWindow(selection.getWindowHandle());
   glutPositionWindow((selection.getCornerX()*width)/100,
                      (selection.getCornerY()*height)/100);
   glutReshapeWindow((selection.getWidth()*width)/100,
                     (selection.getHeight()*height)/100);                                                                              
}	

void displayCGWorld() {
   cgWorld.display();
}

void displaySelection() {
   selection.display();
}

void createGLUIInterface() {   
    // Create Main Panel
    GLUI *gluiSubWindow = GLUI_Master.create_glui_subwindow(selectionWindow.getWindowHandle(),
                                         GLUI_SUBWINDOW_TOP );
    mainPanel = new GLUI_Panel(gluiSubWindow, "", GLUI_PANEL_NONE);
    
    //  Create Spinner
    spinner = gluiSubWindow->add_spinner_to_panel(mainPanel, "Alpha",
                                                  GLUI_SPINNER_FLOAT,
                                                  NULL, -1, spinnerHandler);
    spinner->set_float_limits(0.00, 1.00, GLUI_LIMIT_CLAMP);
    spinner->set_float_val(1.00);

    // Add Separator
    gluiSubWindow->add_separator_to_panel(mainPanel);
    
    // Create Button
    gluiSubWindow->add_button_to_panel(mainPanel, "Close Program", -1, closeButtonHandler);
}

 int main (int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    int mainWindowWidth  = 800;
    int mainWindowHeight = 500;

    // Creating the Main Window
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(mainWindowWidth, mainWindowHeight);
    mainWindowHandle = glutCreateWindow("Transparency");     
    glutDisplayFunc(displayMain);
    glutReshapeFunc(reshapeMain);
    
       // Creating the SubWindow for the Graphical Content 
    cgWorld.createSubWindow(mainWindowHandle,
                            mainWindowWidth, mainWindowHeight, 2, 2, 60, 96); 
    glutDisplayFunc(displayCGWorld);
    cgWorld.init(black);    
              
       //   Creating the SubWindow for the GLUI Controls         
    selection.createSubWindow(mainWindowHandle,
                              mainWindowWidth, mainWindowHeight, 64, 45, 32, 20); 
    glutDisplayFunc(displaySelection);
    selection.init(mainClearColor);
    
    createGLUIInterface();
        
    try {
      glutMainLoop();
    }
    catch (ProgramExit) { 
    	cout << "Closing program" << endl;}
    return 0;
}