package org.slask.jogl;

import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
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.graphics.Rectangle;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;

import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.jogamp.opengl.swt.GLCanvas;
import com.jogamp.opengl.util.Animator;

public class JOGAMPGLCanvasSWTSashTest {
	
	final GLCanvas glcanvas;

	public JOGAMPGLCanvasSWTSashTest() {
		Display d = new Display();
		Shell shell = new Shell(d);
		shell.setLayout(new FillLayout());
		shell.setSize(400, 200);
		SashForm sash = new SashForm (shell, SWT.BORDER | SWT.HORIZONTAL);

		Text left = new Text(sash, SWT.CENTER);
	    left.setText(" --- Left text ----");

	    final BaseClass demo = new BaseClass();

	    
	    final GLProfile glprofile = GLProfile.get( GLProfile.GL2 ) ;
        final GLCapabilities caps = new GLCapabilities( glprofile ) ;
        glcanvas = new GLCanvas(sash, 0, caps, null, null);
        

		shell.open();
		glcanvas.addGLEventListener(demo);

		
		Animator animator = new Animator(glcanvas);
	    animator.start();
	    
	    
		while (!shell.isDisposed()) {
			if (!d.readAndDispatch()) {
				d.sleep();
			}
		}	
		
		d.dispose();
		System.exit(0);
	}
	
	
	class BaseClass implements GLEventListener {
		
		volatile int x, y;
		volatile 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(x, y, 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;
			System.out.println("x: " + x + " y: " + y + " w: " + w + " h: " + h);
			setup((GL2) arg0.getGL());
		}
	}
	
	public static void main(String args[]){
		new JOGAMPGLCanvasSWTSashTest();
	}

}
