package testplugin.views;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.glu.GLU;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.newt.swt.NewtCanvasSWT;

public class JOGLTestNewt extends ViewPart {
	
	public static final String ID = "testplugin.views.JOGLTestNewt";

    private Composite composite;
    private NewtCanvasSWT glcanvas;
    private GLContext glcontext;
    SashForm sash;
    private int rot = 0;
    Label label,abel;
    Canvas c;
	public JOGLTestNewt() {

	}
	@Override
	public void createPartControl(Composite parent) {
		label = new Label(parent, 0);
        label.setText("Hello World");
		    composite = new Composite( parent, SWT.NONE );
		    composite.setLayout( new FillLayout() );
	    final Composite parentFinal = parent;
		c = new Canvas (composite,SWT.NONE);
		c.addPaintListener(new PaintListener() { 
	        public void paintControl(PaintEvent e) { 
	            Rectangle clientArea = c.getClientArea(); 
	            int width = clientArea.width; 
	            int height = clientArea.height; 
	            e.gc.setBackground(c.getDisplay().getSystemColor(SWT.COLOR_CYAN)); 
	         e.gc.fillPolygon(new int[] {0,0,width,0,width/2,height});
	         System.out.println(composite.getLocation());
	         System.out.println("todisplay " + composite.toDisplay(0,0));
	         System.out.println(parentFinal.getClientArea());
	         // this is the proper (on shell) location
	         System.out.println(parentFinal.getParent().getParent().getParent().getLocation());
	        } 
	    });
	    GLWindow glWindow = GLWindow.create(
	    		new GLCapabilities( GLProfile.get(GLProfile.GL2) ));
		glWindow.addGLEventListener(new BaseClass());
		
	    glcanvas = new NewtCanvasSWT( composite, SWT.NONE, glWindow );
	    abel = new Label(parent, 0);
	    abel.setText("Hello World");

	}

	@Override
	public void dispose() {
	    glcanvas.dispose();
	    super.dispose();
	}

	class BaseClass implements GLEventListener {
		
		int x, y;
		int w, h;

		protected void setup(GL2 gl2) {
			gl2.glMatrixMode(GL2.GL_PROJECTION);
			gl2.glLoadIdentity();

			// coordinate system origin at lower left with width and height same
			// as the window
			GLU glu = new GLU();
			glu.gluOrtho2D(0.0f, w, 0.0f, h);

			gl2.glMatrixMode(GL2.GL_MODELVIEW);
			gl2.glLoadIdentity();

			gl2.glViewport(0, 0, w, h);
		}

		protected  void render(GL2 gl2) {
			gl2.glClear(GL.GL_COLOR_BUFFER_BIT);

			// draw a triangle filling the window
			gl2.glLoadIdentity();
			gl2.glBegin(GL.GL_TRIANGLES);
			gl2.glColor3f(1, 0, 0);
			gl2.glVertex2f(0, 0);
			gl2.glColor3f(0, 1, 0);
			gl2.glVertex2f(w, 0);
			gl2.glColor3f(0, 0, 1);
			gl2.glVertex2f(w / 2, h);
			gl2.glEnd();
		}

		@Override
		public void display(GLAutoDrawable arg0) {
			render((GL2) arg0.getGL());
		}

		@Override
		public void dispose(GLAutoDrawable arg0) {
		}

		@Override
		public void init(GLAutoDrawable arg0) {
			setup((GL2) arg0.getGL());
		}

		@Override
		public void reshape(GLAutoDrawable arg0, int x, int y, int w, int h) {
			this.x = x; this.y = y; this.w = w; this.h = h;
			setup((GL2) arg0.getGL());
		}
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		
	}

}