Advertisement
razaron

GUI Library/Core

Jul 1st, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.87 KB | None | 0 0
  1. import org.lwjgl.LWJGLException;
  2. import org.lwjgl.Sys;
  3. import org.lwjgl.input.Keyboard;
  4. import org.lwjgl.input.Mouse;
  5. import org.lwjgl.opengl.Display;
  6. import org.lwjgl.opengl.DisplayMode;
  7. import org.lwjgl.opengl.GL11;
  8.  
  9.  
  10. public class Core {
  11.    
  12.     final int WIDTH=800, HEIGHT=600;
  13.     int lastFrame;
  14.    
  15.     GUI stack;
  16.  
  17.     public void Start(){
  18.        
  19.         try {
  20.             Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
  21.             Display.create();
  22.         } catch (LWJGLException e) {
  23.             e.printStackTrace();
  24.             System.exit(0);
  25.         }
  26.        
  27.         initGL();
  28.         System.out.println("OpenGL version: " + GL11.glGetString(GL11.GL_VERSION));
  29.        
  30.         boolean Close = Display.isCloseRequested();
  31.        
  32.         Layer[] layers = new Layer[2];
  33.        
  34.         Element[] elementsA = new Element[2];
  35.        
  36.         Element[] elementsB = new Element[4];
  37.                
  38.         elementsA[0] = new Element("button one", 1, new Square(50,50,150,100));
  39.         elementsA[1] = new Element("button two", 2, new Square(200,50,300,100));
  40.         elementsB[0] = new Element("button three", 3, new Square(50,50,150,100));
  41.         elementsB[1] = new Element("button four", 4, new Square(200,50,300,100));
  42.         elementsB[2] = new Element("button five", 5, new Square(50,200,150,250));
  43.         elementsB[3] = new Element("button six", 6, new Square(200,200,300,250));
  44.        
  45.         layers[0] = new Layer("layer one", elementsA, new Square(0,0,400,200), new Square(0,0,400,200));
  46.         layers[1] = new Layer("layer two", elementsB, new Square(100,100,500,300), new Square(100,100,500,400));
  47.        
  48.         stack = new GUI(layers);
  49.        
  50.         while(!Close){
  51.             GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  52.            
  53.             if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested())
  54.                 Close = true;
  55.            
  56.             int delta=getDelta();
  57.  
  58.             mouseHandler();
  59.             draw();
  60.            
  61.             Display.update();
  62.             //Display.sync(1);
  63.         }
  64.        
  65.     }
  66.    
  67.     private void draw() {
  68.         for(int i=stack.layers.length-1; i>=0; --i){
  69.             double x1 = stack.layers[i].geomVisible.x1;
  70.             double y1 = stack.layers[i].geomVisible.y1;
  71.             double x2 = stack.layers[i].geomVisible.x2;
  72.             double y2 = stack.layers[i].geomVisible.y2;
  73.             double h1 = x1 + ((x2-x1)*(stack.layers[i].hMin));
  74.             double h2 = x1 + ((x2-x1)*(stack.layers[i].hMax));
  75.             double v1 = y1 + ((y2-y1)*(stack.layers[i].vMin));
  76.             double v2 = y1 + ((y2-y1)*(stack.layers[i].vMax));
  77.            
  78.             //layer
  79.             GL11.glColor3d(0.5,0.5,0.5);
  80.             GL11.glBegin(GL11.GL_QUADS);
  81.                 GL11.glVertex2d(x1,y1);
  82.                 GL11.glVertex2d(x2,y1);
  83.                 GL11.glVertex2d(x2,y2);
  84.                 GL11.glVertex2d(x1,y2);
  85.             GL11.glEnd();
  86.            
  87.             //scroll bars
  88.             GL11.glColor3d(1, 1, 1);
  89.             GL11.glBegin(GL11.GL_QUADS);
  90.                 GL11.glVertex2d(h1,y1);
  91.                 GL11.glVertex2d(h2,y1);
  92.                 GL11.glVertex2d(h2,y1+10);
  93.                 GL11.glVertex2d(h1,y1+10);
  94.             GL11.glEnd();
  95.             GL11.glColor3d(1, 1, 1);
  96.             GL11.glBegin(GL11.GL_QUADS);
  97.                 GL11.glVertex2d(x2,v1);
  98.                 GL11.glVertex2d(x2-10,v1);
  99.                 GL11.glVertex2d(x2-10,v2);
  100.                 GL11.glVertex2d(x2,v2);
  101.             GL11.glEnd();
  102.            
  103.             for(int j=0; j<stack.layers[i].elements.length; j++){
  104.                 double a1 = stack.layers[i].elements[j].geom.x1 +stack.layers[i].geomActual.x1;
  105.                 double b1 = stack.layers[i].elements[j].geom.y1 +stack.layers[i].geomActual.y1;
  106.                 double a2 = stack.layers[i].elements[j].geom.x2 +stack.layers[i].geomActual.x1;
  107.                 double b2 = stack.layers[i].elements[j].geom.y2 +stack.layers[i].geomActual.y1;
  108.                
  109.                 //button
  110.                 GL11.glColor3d(1, 0, 0);
  111.                 GL11.glBegin(GL11.GL_QUADS);
  112.                     GL11.glVertex2d(a1,b1);
  113.                     GL11.glVertex2d(a2,b1);
  114.                     GL11.glVertex2d(a2,b2);
  115.                     GL11.glVertex2d(a1,b2);
  116.                 GL11.glEnd();
  117.             }
  118.         }
  119.     }
  120.  
  121.     public void mouseHandler(){
  122.         /*if(Mouse.isButtonDown(0)){
  123.             stack.update(true, Mouse.getX(), HEIGHT-Mouse.getY(), Mouse.getDX(), -Mouse.getDY(), 0);
  124.         }*/
  125.         while(Mouse.next()){
  126.             if(Mouse.getEventButton()==0){
  127.                 if(Mouse.getEventButtonState()){
  128.                     stack.mouseX = Mouse.getEventX();
  129.                     stack.mouseY = HEIGHT-Mouse.getEventY();
  130.                     //stack.update();
  131.                 }else{
  132.                     stack.deltaX = Mouse.getEventX()-stack.mouseX;
  133.                     stack.deltaY = HEIGHT-Mouse.getEventY()-stack.mouseY;
  134.                     stack.update();
  135.                    
  136.                     stack.mouseX = 0;
  137.                     stack.mouseY = 0;
  138.                     stack.deltaX = 0;
  139.                     stack.deltaY = 0;
  140.                 }
  141.             }
  142.         }
  143.     }
  144.    
  145.     public int getDelta(){
  146.         long time = getTime();
  147.         int delta = (int) (time - lastFrame);
  148.         lastFrame = (int) time;
  149.        
  150.         return delta;
  151.     }
  152.    
  153.     public long getTime(){
  154.         return (Sys.getTime()*1000)/Sys.getTimerResolution();
  155.     }
  156.    
  157.     public void initGL(){
  158.         GL11.glEnable(GL11.GL_TEXTURE_2D);              
  159.        
  160.         //GL11.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);          
  161.        
  162.         GL11.glViewport(0,0,WIDTH,HEIGHT);
  163.            
  164.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  165.         GL11.glMatrixMode(GL11.GL_PROJECTION);
  166.         GL11.glLoadIdentity();
  167.         GL11.glOrtho(0, WIDTH, HEIGHT, 0, 1, -1);
  168.         GL11.glMatrixMode(GL11.GL_MODELVIEW);
  169.     }
  170.    
  171.     public static void main(String args[]){
  172.         Core m = new Core();
  173.         m.Start();
  174.     }
  175.  
  176.    
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement