level 10
#include <iostream>
#include <SDL2/SDL.h>
#include "sdlgui/sdlgui.h"
using namespace std;
class userplane : public GUI<userplane,sdl_widget>
{
public:
userplane();
userplane(const char*,int,int,int,int,Uint32);
int init();
int init(const char*,int,int,int,int,Uint32);
int sysevent(SDL_Event*);
protected:
static int _title_event(sdl_widget*,SDL_Event*);
protected:
sdl_widget_ptr _title;
sdl_button_ptr _min,_close;
sdl_view_ptr _view;
sdl_h_scroll_ptr _scroll;
//
int _is_down;
SDL_Rect _mouse_rt;
};
userplane::userplane()
:
GUI<userplane,sdl_widget>()
{
init();
}
userplane::userplane(const char* ptitle,int px,int py,int pw,int ph,Uint32 pflag)
:
GUI<userplane,sdl_widget>()
{
if(!init())init(ptitle,px,py,pw,ph,pflag);
}
int userplane::init()
{
if(sdl_widget::init())return -1;
_title = NULL;
_min = NULL;
_view = NULL;
_scroll = NULL;
//
_is_down = 0;
return 0;
}
int userplane::init(const char* ptitle,int px,int py,int pw,int ph,Uint32 pflag)
{
if(sdl_widget::init(ptitle,px,py,pw,ph,pflag))return -1;
_title = add<sdl_widget>("璇锋袅锷ㄦ垜",0,0,pw,30,1);
_title->fill_rect(NULL,0xfffff0);
_title->event(userplane::_title_event);
_min = _title->add<sdl_button>("-",pw-60,0,30,30,1);
_close = _title->add<sdl_button>("x",pw-30,0,30,30,1);
_view = add<sdl_view>("",0,30,pw-30,ph-30,1);
_view->view.init("",0,0,600,600,1);
_view->view.img_load(ptitle);
_view->scroll(3);
_scroll = add<sdl_h_scroll>("",pw-30,30,30,ph-30,1);
return 0;
}
int userplane::sysevent(SDL_Event* e)
{
switch(e->type)
{
case SDL_USEREVENT:
switch(e->user.code)
{
case sdlgui_button_click:
if((sdl_button*)(e->user.data1) == _min)
{
(height()==30)?height(300):height(30);
}
else
{
hide();
}
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
if(!_is_down)
{
_mouse_rt.x = e->button.x;
_mouse_rt.y = e->button.y;
_mouse_rt.w = global_pos_x()-_mouse_rt.x;
_mouse_rt.h = global_pos_y()-_mouse_rt.y;
_is_down = 1;
}
break;
case SDL_MOUSEBUTTONUP:
_is_down = 0;
break;
case SDL_MOUSEMOTION:
if(_is_down)
{
pos(e->motion.x+_mouse_rt.w,e->motion.y+_mouse_rt.h);
}
break;
}
return sdl_widget::sysevent(e);
}
int userplane::_title_event(sdl_widget* obj,SDL_Event* e)
{
switch(e->type)
{
case SDL_USEREVENT:
switch(e->user.code)
{
case sdlgui_button_click:
obj->parent()->event(e);
break;
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
case SDL_MOUSEMOTION:
obj->parent()->event(e);
break;
}
}
int main(int argc,char** argv)
{
userplane* user;
sdl_frame f("sdlgui user plane",100,100,600,600,SDL_WINDOW_SHOWN);
user = f.add<userplane>("bg.jpg",0,0,300,300,1);
user = f.add<userplane>("bg1.jpg",0,300,300,300,1);
return f.run();
}
2014年05月31日 09点05分




