Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Lesson 5

Lesson5: Handling user input (mouse, keyboard).
In this lesson you will learn how mouse and keyboard can be used, to interact with the program. We will modify code from Lesson 1. We will use mouse to move our triangle around and keyboard key SPACE to control movement. Mouse buttons and keyboard keys are handled the same way, so if we would like to use left or right mouse button, we would just change, the name of the key. I suggest you to have a look at sjgui::CKeys class, which is not used inside of the gui widgets but could be very helpful.
See also:
sjgui::CWnd::OnKeyUp(), sjgui::CWnd::OnKeyDown(), sjgui::CWnd::OnMouseMove(),sjgui::CKeys
// For this lesson, we need to override events handlers OnMouseMove(),
// OnKeyDown() and OnKeyUp. The task is, to move the triangle with a mouse, but
// when key SPACE is pressed, triangle should stay in its original location.
class CSpcWnd : public sjgui::CWnd
{
    // We need to know when space is pressed
    bool        m_ySpacePressed;
    // We also need to keep track of a new x and y location for the triangle.
    float       m_fTriX;
    float       m_fTriY;
public:
    // constructor to set member varables to a known state.
    CSpcWnd():CWnd(){m_ySpacePressed=false;m_fTriX=0.0f;m_fTriY=0.0f;}
    // Let us create handler for mouse movements.
    // Two parameters are global mouse position in screen coordinates, which is
    // upper-left corner is point 0,0; positive X in to the right, and positive Y
    // is to the bottom. There is also an issue of scalling down the movement, which
    // should account for the scale and location of the trianlge, we will avoid
    // these calculations,making everything simple.
    virtual void OnMouseMove(int iX, int iY)
    {
        // we do not want to do anything when SPACE key is not pressed
        if(m_ySpacePressed) return;
        // System of coordinates for the screen and for our 3D space
        // is different, so we need to make a convertion.
        // Remember we set it this way in OnDraw() function.
        // Differences are that in 3D point (0.0) is at the center of the screen
        // and positive Y is to the top. Also scale of the movement should be
        // altered, so object would move in the direction of mouse movement.
        // Shift 0,0 point to the center, and scale it down.
        m_fTriX=(iX-GetWidth()/2)*0.03f;
        m_fTriY=-(iY-GetHeight()/2)*0.03f; // not to forget to reverse axis
    }
    // Now let's take care of SPACE key.
    // We check what button was presed and if it is space we set the flag
    // and return trianlge to its original location.
    virtual void OnKeyDown(int& iKey)
    {
        if(iKey==SJ_KEY_SPACE)
        {
            m_ySpacePressed=true;
            // Reset location
            m_fTriX=0.0f;m_fTriY=0.0f;
        }
    }
    // When key is released we to reset the flag.
    virtual void OnKeyUp(int& iKey){if(iKey==SJ_KEY_SPACE)m_ySpacePressed=false;}
    // All we want to do it is drawing of a triangle,
    // so the only event we need to take care of is drawing.
    virtual void OnDraw()
    {
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        // Here we move our triangle according to its position stored
        // in m_fTriX and m_fTriY member variables.
        glTranslatef(m_fTriX,m_fTriY,-10.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class
By the way if you do not want parent windows to receive key value, you can change the value of iKey to SJ_KEY_IGNORE.
Back to Lesson 4. Forward to Lesson 6.
This is full source code:
// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

// For this lesson, we need to override events handlers OnMouseMove(),
// OnKeyDown() and OnKeyUp. The task is, to move the triangle with a mouse, but
// when key SPACE is pressed, triangle should stay in its original location.
class CSpcWnd : public sjgui::CWnd
{
    // We need to know when space is pressed
    bool        m_ySpacePressed;
    // We also need to keep track of a new x and y location for the triangle.
    float       m_fTriX;
    float       m_fTriY;
public:
    // constructor to set member varables to a known state.
    CSpcWnd():CWnd(){m_ySpacePressed=false;m_fTriX=0.0f;m_fTriY=0.0f;}
    // Let us create handler for mouse movements.
    // Two parameters are global mouse position in screen coordinates, which is
    // upper-left corner is point 0,0; positive X in to the right, and positive Y
    // is to the bottom. There is also an issue of scalling down the movement, which
    // should account for the scale and location of the trianlge, we will avoid
    // these calculations,making everything simple.
    virtual void OnMouseMove(int iX, int iY)
    {
        // we do not want to do anything when SPACE key is not pressed
        if(m_ySpacePressed) return;
        // System of coordinates for the screen and for our 3D space
        // is different, so we need to make a convertion.
        // Remember we set it this way in OnDraw() function.
        // Differences are that in 3D point (0.0) is at the center of the screen
        // and positive Y is to the top. Also scale of the movement should be
        // altered, so object would move in the direction of mouse movement.
        // Shift 0,0 point to the center, and scale it down.
        m_fTriX=(iX-GetWidth()/2)*0.03f;
        m_fTriY=-(iY-GetHeight()/2)*0.03f; // not to forget to reverse axis
    }
    // Now let's take care of SPACE key.
    // We check what button was presed and if it is space we set the flag
    // and return trianlge to its original location.
    virtual void OnKeyDown(int& iKey)
    {
        if(iKey==SJ_KEY_SPACE)
        {
            m_ySpacePressed=true;
            // Reset location
            m_fTriX=0.0f;m_fTriY=0.0f;
        }
    }
    // When key is released we to reset the flag.
    virtual void OnKeyUp(int& iKey){if(iKey==SJ_KEY_SPACE)m_ySpacePressed=false;}
    // All we want to do it is drawing of a triangle,
    // so the only event we need to take care of is drawing.
    virtual void OnDraw()
    {
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        // Here we move our triangle according to its position stored
        // in m_fTriX and m_fTriY member variables.
        glTranslatef(m_fTriX,m_fTriY,-10.0f);
        // Let us set color to a blue
        glColor3f(0.0f,0.0f,1.0f);
        // this is our triangle in the center of the screen.
        glBegin(GL_TRIANGLES);
            glVertex3f( 0.0f, 1.0f, 0.0f);
            glVertex3f(-1.0f,-1.0f, 0.0f);
            glVertex3f( 1.0f,-1.0f, 0.0f);
        glEnd();
    }
}; // end of CSpcWnd class

// Now we need to setup our window, we do it in the function main.
int main(int argc, char* argv[])
{
    // Events receiver
    CSpcWnd SpcWnd;
    // Position the window
    SpcWnd.PosWnd(20,50,320,240);
    // Create window
    if(sjgui::Create("Interactions(SPACE-stops movement)",&SpcWnd))
    {
        // Run it. Now all events are transferred to our class.
        sjgui::GlobalMainLoop();
    }
    else
        // Print error message if something was wrong.
        printf("Could not create opengl window.\n");
    return 0;
} // the end of main
sjgui logo
Quick Links:

 News
 Description
 Screen Shots
 Projects
 Downloads
 Source Code
 Help/FAQ
 Want to help?
 Credits
 Disclaimer


Documentation:

 Documentation
 Reference
 Lessons


Useful links:

sjcomp logo
sjcomp

opengl logo

nehe logo

SourceForge.net Logo

Last modified:


Started by Alexander Shyrokov. Generated at Wed Apr 28 12:31:05 2004 for the sjgui by doxygen 1.3.1. SourceForge.net Logo