level 10
#include <SDL2/SDL.h>
#include "sdlgui/sdlgui.h"
usingnamespace std;
typedef class sdl_check_button : public GUI<sdl_check_button,sdl_button>
{
public:
sdl_check_button();
sdl_check_button(const char*,int,int,int,int,Uint32);
int init();
int init(const char*,int,int,int,int,Uint32);
int sysevent(SDL_Event*);
int state();
int state(int);
int draw();
protected:
int _state;
}*sdl_check_button_ptr;
sdl_check_button::sdl_check_button()
:
GUI<sdl_check_button,sdl_button>()
{
init();
}
sdl_check_button::sdl_check_button(const char* ptitle,int px,int py,int pw,int ph,Uint32 pflag)
:
GUI<sdl_check_button,sdl_button>()
{
init(ptitle,px,py,pw,ph,pflag);
}
int sdl_check_button::init()
{
if(sdl_button::init())return -1;
_state = 0;
_text_rect.x =20;
return 0;
}
int sdl_check_button::init(const char* ptitle,int px,int py,int pw,int ph,Uint32 pflag)
{
if(sdl_button::init(ptitle,px,py,pw,ph,pflag))return -1;
color_key(1,0xffffff);
return 0;
}
int sdl_check_button::sysevent(SDL_Event*e)
{
switch(e->type)
{
case SDL_MOUSEBUTTONDOWN:
state((_state)?0:1);
break;
default:
break;
}
return sdl_button::sysevent(e);
}
int sdl_check_button::state()
{
return _state;
}
int sdl_check_button::state(int s)
{
sdlsurface* tsur=_button_clip(0,0);
_state = s&1;
tsur->fill_rect(NULL,0xffffff);
tsur->rectangle(1,1,_rect.h-1,_rect.h-1,0,1);
tsur->rectangle(4,4,_rect.h-4,_rect.h-4,0xffffff,1);
if(_state)
{
tsur->rectangle(8,8,_rect.h-8,_rect.h-8,0x00,1);
}
tsur->blit_surface(NULL,_button_clip(1,0),NULL);
tsur->blit_surface(NULL,_button_clip(2,0),NULL);
return 0;
}
int sdl_check_button::draw()
{
state(0);
}
int frame_event(sdl_frame* obj,SDL_Event*e)
{
switch(e->type)
{
case SDL_USEREVENT:
switch(e->user.code)
{
case sdlgui_button_click:
if(((sdl_check_button_ptr)e->user.data1)->state())
{
obj->fill_rect(NULL,0x00ff00);
}
else
{
obj->fill_rect(NULL,0xff0000);
}
break;
}
break;
}
}
int main(int argc, char * argv[])
{
sdl_check_button_ptr c;
sdl_frame f("check_button",100,100,600,600,SDL_WINDOW_SHOWN);
f.event(frame_event);
f.fill_rect(NULL,0xff0000);
c = f.add<sdl_check_button>("选择我吧",0,0,120,30,1);
return f.run();
}
2014年06月02日 16点06分

