Main Page Namespace List Class Hierarchy Compound List File List Namespace Members Compound Members Related Pages
Lesson7: Creating custom widget - In this lesson you will learn how to enhance widget so it suits your needs, and also how to create a new widget. We will start by creating an enhanced widget, then we will use it in existing application, so you could see how easy it is to update your code, once you decide to change something. And the most interesting part would be how to make our widget and make it work with already existing classes (you can not even imagine the simplicity of this process). We will modify code from Lesson 6.
- Let us enhance a widget:
class CDigEdit: public CEdit
{
public:
virtual void OnKeyDown(int &iKey)
{
if(iKey>31 && iKey<128)
if(iKey<48 || iKey>57)iKey=SJ_KEY_IGNORE;
}
};
Now how can we use it? Easy! Remember your first dialog from Lesson 6? There is only one change to make it use your enhanced widget:
class CCustomTextDlg: public CDlg
{
CDigEdit m_edt;
public:
std::string m_str;
CCustomTextDlg():CDlg()
{
SetAutoHide();
SetCaption("Enter text:");
SetSize(250,90);
RegisterChild(&m_edt);
m_edt.SetText("sjgui");
}
virtual void OnReshape()
{
m_edt.PosWnd(5,m_Caption.GetBottom()+1,GetWidth()-10,m_edt.GetHeight());
}
virtual void OnComplete(){if(m_btnOk.IsPushed())m_str=m_edt.GetText();}
};
Now let us add some fancy graphics. This is another widget:
class CWig : public CLabel
{
float m_fRotation;
public:
CWig(){m_fRotation=0.0f;}
virtual void OnAnimate()
{
m_fRotation+=1;
if(m_fRotation>360)m_fRotation-=360;
}
virtual void OnDraw()
{
SET_GL_SIMPLE_PROSPECTIVE;
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(m_fRotation,0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
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();
SET_GL_FOR_GUI_DRAW;
}
};
Once it is ready to use, look how you can make use of it:
class CSpcWnd : public CWnd
{
CButtonTmpl<CWig> m_btn;
CCustomTextDlg m_dlg;
public:
CSpcWnd():CWnd()
{
RegisterChild(&m_btn);
RegisterChild(&m_dlg);
m_btn.SetLabel("default text");
}
void virtual OnReshape()
{
m_btn.SetSize((m_btn.GetTextLen()+1)*m_btn.GetFontSize(),80);
m_btn.ToCenter();
m_dlg.ToCenter();
}
void virtual OnKeyUp(int &iKey)
{
if(m_dlg.IsOk())
{
m_btn.SetLabel(m_dlg.m_str.data());
Reshape();
}
if(m_dlg.IsOk() || m_dlg.IsCancel())
m_dlg.Reset();
if(m_btn.IsPushed())
{
m_btn.Reset();
m_dlg.Show(!m_dlg.IsVisible());
}
}
virtual void OnDraw(){Animate();SET_GL_FOR_GUI_DRAW;}
};
- Some information about widgets.
- There is a notation of the style for widgets, the default is sjgui::modern, if you want to use any other style you should define SJGUI_USE_STYLE with the name of the style. This is the list of all available Widget Styles.
- If you want to change colors of the widgets, you may do that for each control individually. But when widget is created it uses default settings (it correspond not only to color but to the sizes also). So if you which change that value you just need to do so before you create element. These parameters are unique for each style. These are available default parameters and their values for sjgui::modern:
namespace modern
{
CWnd::CSize def_wnd_min_size(0,0);
GLfloat def_wnd_ctrl_border_light_color[4]={1.0f,1.0f,1.0f,1.0f};
GLfloat def_wnd_ctrl_border_dark_color[4]={0.3f,0.3f,0.3f,0.3f};
GLfloat def_wnd_ctrl_shadow_color[4]={0.3f,0.3f,0.3f,0.5f};
GLfloat def_wnd_ctrl_bg_color[4]={0.3f,0.5f,0.7f,1.0f};
GLfloat def_wnd_ctrl_bg_sel_color[4]={0.6f,0.5f,0.7f,1.0f};
CWnd::CSize def_button_size(100,30);
GLfloat def_dlg_caption_color[4] ={0.1f,0.9f,1.0f,1.0f};
GLfloat def_dlg_button_text_color[4]={0.1f,0.9f,1.0f,1.0f};
CWnd::CSize def_dlg_min_size(240,50);
int def_dlg_btn_dist =5;
int def_dlg_btn_offset =5;
int def_dlg_btn_panel_height =40;
GLfloat def_dlg_wnd_border_color[4] ={0.5f,0.5f,0.5f,1.0f};
GLfloat def_dlg_wnd_bg_color[4] ={0.1f,0.5f,0.5f,1.0f};
GLfloat def_dlg_wnd_bg_sel_color[4] ={0.11f,0.51f,0.51f,1.0f};
int def_edit_font_size =10;
int def_edit_blink_period =750;
CWnd::CSize def_edit_size(100,12);
GLfloat def_label_color[4] ={1.0f,1.0f,1.0f,1.0f};
CWnd::eAligns def_label_hor_aling =CWnd::ALIGN_CENTER;
CWnd::eAligns def_label_ver_aling =CWnd::ALIGN_CENTER;
CWnd::CSize def_slider_size(16,16);
float def_slider_length =0.2f;
CWnd::eOrient def_slider_orientation=CWnd::HORIZONTAL;
GLfloat def_text_color[4] ={1.0f,1.0f,1.0f,1.0f};
GLfloat def_text_box_text_color[4] ={0.8f,1.0f,0.8f,1.0f};
int def_text_box_fonst_size =12;
int def_text_box_sl_dst =0;
GLfloat def_clear_color[4]={0.0f,0.0f,0.0f,1.0f};
}
Back to Lesson 6.
- Now you are ready to go on your own, explore and create! I will be more then happy to hear about your experience with this library! Waiting for your comments. And for people who are not happy with glut and I can understand why :) go on to Lesson 8. Good-luck.
- This is full source code:
using namespace sjgui;
class CDigEdit: public CEdit
{
public:
virtual void OnKeyDown(int &iKey)
{
if(iKey>31 && iKey<128)
if(iKey<48 || iKey>57)iKey=SJ_KEY_IGNORE;
}
};
class CWig : public CLabel
{
float m_fRotation;
public:
CWig(){m_fRotation=0.0f;}
virtual void OnAnimate()
{
m_fRotation+=1;
if(m_fRotation>360)m_fRotation-=360;
}
virtual void OnDraw()
{
SET_GL_SIMPLE_PROSPECTIVE;
glTranslatef(0.0f,0.0f,-10.0f);
glRotatef(m_fRotation,0.0f,1.0f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
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();
SET_GL_FOR_GUI_DRAW;
}
};
class CCustomTextDlg: public CDlg
{
CDigEdit m_edt;
public:
std::string m_str;
CCustomTextDlg():CDlg()
{
SetAutoHide();
SetCaption("Enter text:");
SetSize(250,90);
RegisterChild(&m_edt);
m_edt.SetText("sjgui");
}
virtual void OnReshape()
{
m_edt.PosWnd(5,m_Caption.GetBottom()+1,GetWidth()-10,m_edt.GetHeight());
}
virtual void OnComplete(){if(m_btnOk.IsPushed())m_str=m_edt.GetText();}
};
class CSpcWnd : public CWnd
{
CButtonTmpl<CWig> m_btn;
CCustomTextDlg m_dlg;
public:
CSpcWnd():CWnd()
{
RegisterChild(&m_btn);
RegisterChild(&m_dlg);
m_btn.SetLabel("default text");
}
void virtual OnReshape()
{
m_btn.SetSize((m_btn.GetTextLen()+1)*m_btn.GetFontSize(),80);
m_btn.ToCenter();
m_dlg.ToCenter();
}
void virtual OnKeyUp(int &iKey)
{
if(m_dlg.IsOk())
{
m_btn.SetLabel(m_dlg.m_str.data());
Reshape();
}
if(m_dlg.IsOk() || m_dlg.IsCancel())
m_dlg.Reset();
if(m_btn.IsPushed())
{
m_btn.Reset();
m_dlg.Show(!m_dlg.IsVisible());
}
}
virtual void OnDraw(){Animate();SET_GL_FOR_GUI_DRAW;}
};
int main(int argc, char* argv[])
{
CSpcWnd SpcWnd;
SpcWnd.PosWnd(20,50,320,240);
if(Create(argv[0],&SpcWnd))
{
GlobalMainLoop();
}
else
printf("Could not create opengl window.\n");
return 0;
}
|
|