FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

handle kayboard event

 
Post new topic   Reply to topic     Forum Index -> gtkD
View previous topic :: View next topic  
Author Message
bioinfornatics



Joined: 22 Jun 2010
Posts: 90

PostPosted: Mon Feb 07, 2011 5:36 pm    Post subject: handle kayboard event Reply with quote

hi,
why this code do not handle keyboard event?

keyPressEvent is never called!!

Code:
module gui.gtk.Window;

private import gtk.DrawingArea;
private import gtkc.gdktypes;
private import glgtk.GLCapability;
private import glgdk.GLDrawable;
private import glgdk.GLConfig;
private import glgdk.GLContext;
private import gtkglc.glgdktypes;

//private import gtkglc.gl;
//private import gtkglc.glu;
import derelict.opengl.gl;
import derelict.opengl.glu;

private import gdk.Event;
private import gdk.Keysyms;
private import gtk.MainWindow;
private import gtk.Widget;

private import tango.math.Math;

private import gui.Data;
private import gui.Patterns;

import tango.io.Stdout;

class WindowGTK : MainWindow, IObserver{
    public:
        this(char[] title){
            super(title);
        }
       
        void update(){
           
        }
}

private enum  CameraType{LAND, AIR};

class WindowGL : DrawingArea, IObserver{
   
    private:
        GLfloat     width;
        GLfloat     height;
        IModel[]    models;
   
        /** need to include the mixin to add GL capabilities to this widget */
        mixin GLCapability;
       
       
      CameraType cameraType;
      double[3] position;
      double[3] aLong;
      double[3] up;
        bool buttonIsDown;
       
        double dotProduct(double[3] v1, double[3] v2){
            return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
        }
       
        double degToRad(double deg){
            return cast(double)(PI / 180.0) * deg;
        }
       
        double[] normalize(double[3] vector){
            double a = sqrt(vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2]);
            vector[0]=vector[0]/a;
            vector[1]=vector[1]/a;
            vector[2]=vector[2]/a;
            return vector;
        }
       
        double[] crossProduct(double[3] v1, double[3] v2){
            return [ v1[1]*v2[2] - v2[1]*v1[2] , v1[2]*v2[0] - v2[2]*v1[0] , v1[0]*v2[1] - v2[0]*v1[1] ];
        }
      
        void init(){
            // Initialize GL Derelict modules
            DerelictGL.load();
            // Initialize GLU Derelict modules
            DerelictGLU.load();
            // set the GL capabilities for this widget
            setGLCapability();
            addEvents(GdkEventMask.KEY_PRESS_MASK);
            addOnKeyPress(&keyPressEvent);
            addOnMotionNotify(&motionNotifyCallback);
            addOnButtonPress(&onButtonPress);
            addOnButtonRelease(&onButtonRelease);           
        }
        /**
         * put any gl initializations here
         * returns true to consume the event
         */
        bool initGL(){
            resetCamera();
            resizeGL(null);
            resetCamera();
            return true;
        }
       
        /**
         * This method is called every time the window must be paint or repaint
         * This is where you put the OpenGL call to draw something.
         * This method call be called directly by the application without an event object
         * to force redrawing of the scene.
         * returns true to consume the event
         */
        bool drawGL(GdkEventExpose* event = null){
            glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity ();
            double[][] coords = models[0].getCoordinates3D();
            //gluLookAt(0, 0, 10, 0, 0, 0, 0, 1,0); //Set the camera position
            updateCamera();
            foreach(index, point;coords)
                Stdout.formatln("point num: {} position: {}", index, point);
            //Just Draw a tri-colored triangle
            glBegin(GL_TRIANGLES);
                glColor3f(1.0f,0.0f,0.0f);
                glVertex3f( coords[0][0], coords[0][1], coords[0][2] );
                glColor3f(0.0f,1.0f,0.0f);
                glVertex3f( coords[0][3], coords[0][4], coords[0][5] );
                glColor3f(0.0f,0.0f,1.0f);
                glVertex3f( coords[0][6], coords[0][7], coords[0][8] );
                glColor3f(1.0f,0.0f,0.0f);
                glVertex3f( coords[1][0], coords[1][1], coords[1][2] );
                glColor3f(0.0f,1.0f,0.0f);
                glVertex3f( coords[1][3], coords[1][4], coords[1][5] );
                glColor3f(0.0f,0.0f,1.0f);
                glVertex3f( coords[1][6], coords[1][7], coords[1][8] );
                glColor3f(1.0f,0.0f,0.0f);
                glVertex3f( coords[2][0], coords[2][1], coords[2][2] );
                glColor3f(0.0f,1.0f,0.0f);
                glVertex3f( coords[2][3], coords[2][4], coords[2][5] );
                glColor3f(0.0f,0.0f,1.0f);
                glVertex3f( coords[2][6], coords[2][7], coords[2][8] );
            glEnd();
               
            return true;
        }

        /**
         * This method is called when the window is resized
         * returns true to consume the event
         */
        bool resizeGL(GdkEventConfigure* event = null){
            GLfloat w;
            GLfloat h;
           
            if ( event == null ){
                w = getWidth();
                h = getHeight();
            }
            else{
                w = event.width;
                h = event.height;
            }
           
            width   = w;
            height  = h;
           
            //Adjust the viewport according to new window dimensions
            glViewport (0, 0, cast(int)w, cast(int)h);

            /*
             * Update the projection Matrix accoding to the new dimension
             * and reset the OpenGL state to MODELVIEW
             */
            glMatrixMode (GL_PROJECTION);
            glLoadIdentity ();
            gluPerspective(20, w/h, 0.1, 10);
            glMatrixMode (GL_MODELVIEW);

            return true;
        }

        bool keyPressEvent(GdkEventKey* event, Widget widget){
            Stdout.formatln("event:{} ",event.keyval).nl;
            switch(event.keyval){
                case(GdkKeysyms.GDK_Up):    // up arrow
                    Stdout("up").nl;
                    break;
                case(GdkKeysyms.GDK_Down): // down arrow
                    Stdout("down").nl;
                    break;
                case(GdkKeysyms.GDK_Right): // right arrow
                    Stdout("right").nl;
                    break;
                case(GdkKeysyms.GDK_Left):  // left arrow
                    Stdout("left").nl;
                    break;
                default:
                    Stdout("default").nl;
                    break;
            }
            return false;
        }
       
        bool motionNotifyCallback(GdkEventMotion* event, Widget widget){
            Stdout.formatln("motion: {} {}", event.x, event.y);
            return true;
        }
       
        bool onButtonPress(GdkEventButton* event, Widget widget){
            Stdout.formatln("press mouse: {} {}", event.x, event.y);
            if ( event.button == 1 ){
                buttonIsDown    = true;
                //event.x, event.y;
            }
             return false;
        }
       
        bool onButtonRelease(GdkEventButton* event, Widget widget){
            Stdout.formatln("release mouse: {} {}", event.x, event.y);
            if ( event.button == 1 ){
                buttonIsDown    = false;
                //event.x, event.y;
            }
             return false;
        }

    public:
        /**
         * Construct a simple DrawingArea and sets the GLCapabilities
         */
        this(){
            super(800, 800);
            this.models ~= new Pyramid();
         this.cameraType = CameraType.LAND;
            init();
        }

       
        this(CameraType ct){
            super(800, 800);
            this.models ~= new Pyramid();
         this.cameraType = ct;
            init();
      }
       
       
        void update(){
            drawGL();
        }
      
        void setCameraType(CameraType ct){
            cameraType = ct;
            resetCamera();
        }
       
      double[] getPosition(){
            return position.dup;
        }
       
      void resetCamera(){
            position[0..$]  = [0.0f, 0.0f,10.0f];
            aLong[0..$]     = [0.0f, 0.0f,0.0f];
            up[0..$]        = [0.0f, 1.0f, 0.0f];
            updateCamera();           
        }
       
      void updateCamera(){
            GLdouble eyeX = position[0];
            GLdouble eyeY = position[1];
            GLdouble eyeZ = position[2];

          GLdouble centerX = aLong[0];
          GLdouble centerY = aLong[1];
          GLdouble centerZ = aLong[2];

          GLdouble upX = up[0];
          GLdouble upY = up[1];
          GLdouble upZ = up[2];
         
          gluLookAt(eyeX , eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
        }
       
       
        // Move
       
      void walk(GLdouble delta){
            position[2] -= delta;
            updateCamera();
      }
      
      void strafe(GLdouble delta){
            position[1] -= delta;
            updateCamera();
      }
      
      void fly(GLdouble delta){
         if(cameraType == CameraType.AIR){
                position[1] += delta;
                updateCamera();
            }
      }
      
      // Rotation
      
      void rotate(GLdouble phi){
          GLdouble theta      = 30;
          GLdouble radius     = 2.0f;
          GLdouble radiusTmp  = radius * cast(double)cos(degToRad(phi));
         position[0]         = radius * radiusTmp * cos(degToRad(theta));
         position[1]         = radius * radiusTmp * sin(degToRad(theta));
         position[2]         = radius *  sin(degToRad(theta));
            updateCamera();
      }
}
Back to top
View user's profile Send private message
Mike Wey



Joined: 07 May 2007
Posts: 428

PostPosted: Sun Mar 06, 2011 9:16 am    Post subject: Reply with quote

The code looks correct, is the event executed when you call onKeyPressEvent ?
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> gtkD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group