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

Lesson 4

Lesson4: Using widgets.
In this lesson you will learn how to use widget to interact with the application. We will modify code from the Lesson 3. We will add a checkbox, which will control the animation. When it is checked animation is on, when unchecked triangle stops spinning.
See also:
sjgui, sjgui::CWnd::RegisterChild(), sjgui::CCheckBox
// For this lesson, you will learn how to use controls.
// we will use checkbox widget to control animation.
class CSpcWnd : public sjgui::CWnd
{
    // This variable will hold the current rotation angle
    float   m_fRotation;
    // This is our new widget
    sjgui::CCheckBox m_chbOn;
public:
    // We reset m_fRotation and also set our check box parameters.
    CSpcWnd():CWnd()
    {
        m_fRotation=0.0f; // Reset rotation
        // We need to register check box as a child
        // so it will be drawn and moved along with the parent window
        RegisterChild(&m_chbOn);
        // Now we can set text of the checkbox.
        m_chbOn.SetLabel("Animation On/Off");
        // And let's put it in the upper-left corner,
        // preserving its original height.
        m_chbOn.PosWnd(0,0,320,m_chbOn.GetHeight());
    }
    // New handler for animation
    virtual void OnAnimate()
    {
        // change rotation by 1 degree
        // if checkbox is checked
        if(m_chbOn.IsChecked())
            m_fRotation+=1;
        // we do not want to have rotation angle more then 360 degrees
        if(m_fRotation>360)m_fRotation-=360;
    }
    // All child widgets are drawn after this even is called
    // that is why we need to set proper projection mode,
    // for widgets to be drawn correctly. The reason why each widget
    // does not make sure it is in a proper mode, is because it would
    // take up some time and we want it to work fast. 
    virtual void OnDraw()
    {
        Animate(); // Animate everything
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        glTranslatef(0.0f,0.0f,-10.0f); // Move triangle down
        // Now we rotate our triangle around Y axis
        glRotatef(m_fRotation,0.0f,1.0f,0.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();
        // Widget are drawn after the scene is drawn, so they always are
        // on top of them. There is another macro that you should use to
        // set OpenGL for drawing widget.
        SET_GL_FOR_GUI_DRAW;
        // All children will be drawn using this mode, unless,
        // they change it to something else.
    }
}; // end of CSpcWnd class
Back to Lesson 3. Forward to Lesson 5.
The following is full source code for this lesson.
// This header includes all resources of the sjgui library
#include <sjgui/sjgui.h>

// For this lesson, you will learn how to use controls.
// we will use checkbox widget to control animation.
class CSpcWnd : public sjgui::CWnd
{
    // This variable will hold the current rotation angle
    float   m_fRotation;
    // This is our new widget
    sjgui::CCheckBox m_chbOn;
public:
    // We reset m_fRotation and also set our check box parameters.
    CSpcWnd():CWnd()
    {
        m_fRotation=0.0f; // Reset rotation
        // We need to register check box as a child
        // so it will be drawn and moved along with the parent window
        RegisterChild(&m_chbOn);
        // Now we can set text of the checkbox.
        m_chbOn.SetLabel("Animation On/Off");
        // And let's put it in the upper-left corner,
        // preserving its original height.
        m_chbOn.PosWnd(0,0,320,m_chbOn.GetHeight());
    }
    // New handler for animation
    virtual void OnAnimate()
    {
        // change rotation by 1 degree
        // if checkbox is checked
        if(m_chbOn.IsChecked())
            m_fRotation+=1;
        // we do not want to have rotation angle more then 360 degrees
        if(m_fRotation>360)m_fRotation-=360;
    }
    // All child widgets are drawn after this even is called
    // that is why we need to set proper projection mode,
    // for widgets to be drawn correctly. The reason why each widget
    // does not make sure it is in a proper mode, is because it would
    // take up some time and we want it to work fast. 
    virtual void OnDraw()
    {
        Animate(); // Animate everything
        SET_GL_SIMPLE_PROSPECTIVE; // The same as it was in lesson2
        glTranslatef(0.0f,0.0f,-10.0f); // Move triangle down
        // Now we rotate our triangle around Y axis
        glRotatef(m_fRotation,0.0f,1.0f,0.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();
        // Widget are drawn after the scene is drawn, so they always are
        // on top of them. There is another macro that you should use to
        // set OpenGL for drawing widget.
        SET_GL_FOR_GUI_DRAW;
        // All children will be drawn using this mode, unless,
        // they change it to something else.
    }
}; // 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("Using widgets",&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