00001 #ifndef H_SDLPP_UI
00002 #define H_SDLPP_UI
00003
00004 namespace SDLPP {
00005
00006 struct Highlight
00007 {
00008 Highlight(Uint32 fg=MapRGB(255,255,255),
00009 Uint32 bg=MapRGB(0,0,0),
00010 Uint32 hl_fg=MapRGB(0,255,255),
00011 Uint32 hl_bg=MapRGB(0,0,128)) : m_FG(fg), m_BG(bg), m_HLFG(hl_fg), m_HLBG(hl_bg) {}
00012 Uint32 m_FG,m_BG,m_HLFG,m_HLBG;
00013 };
00014
00015 class ScreenMenu
00016 {
00017 Font& m_Font;
00018 Highlight m_HL;
00019 str_vec m_Items;
00020 iVec2 m_Size;
00021 public:
00022 ScreenMenu(Font& font=get_font("Arial",36), const Highlight& hl=Highlight());
00023 void add_item(const char* text);
00024 int run(Bitmap* background=0, SoundClip* hover=0, SoundClip* select=0);
00025 };
00026
00027 class Component
00028 {
00029 protected:
00030 Bitmap m_Image;
00031 iRect2 m_Rect;
00032 Component* m_Parent;
00033
00034 typedef std::vector<Component> comp_seq;
00035 comp_seq m_Children;
00036
00037 iVec2 get_global_position() const
00038 {
00039 iVec2 offset=m_Rect.tl;
00040 if (m_Parent) offset+=m_Parent->get_global_position();
00041 return offset;
00042 }
00043
00044 iVec2 get_local_position(const iVec2& p) const
00045 {
00046 iVec2 global=get_global_position() - m_Rect.tl;
00047 return p-global;
00048 }
00049
00050 void set_bitmap(Bitmap bmp) { m_Image=bmp; }
00051 void set_position(const iVec2& p) { m_Rect=iRect2(p,p+m_Image.get_size()); }
00052 void set_parent(Component* parent) { m_Parent=parent; }
00053 virtual void set_capture(Component* child) { if (m_Parent) m_Parent->set_capture(child); }
00054 public:
00055 Component(Component* parent=0) : m_Parent(parent), m_Rect(0,0,0,0) {}
00056 virtual ~Component();
00057
00058 virtual void draw(Bitmap target)
00059 {
00060 m_Image.draw(target,get_global_position());
00061 }
00062
00063 Component& add_child(const Component& c) { m_Children.push_back(c); return m_Children.back(); }
00064
00065 virtual void draw() { draw(get_screen()); }
00066
00067 const iRect2& get_rect() const { return m_Rect; }
00068 const Component* get_parent() const { return m_Parent; }
00069
00070
00071 bool inside(const iVec2& p) const { return m_Rect.inside(get_local_position(p)); }
00072 virtual void button_down(int button, const iVec2& position);
00073 virtual void button_up(int button, const iVec2& position);
00074 virtual void mouse_move(int button, const iVec2& position);
00075 };
00076
00077 class RootComponent : public Component, public EventListener
00078 {
00079 Component* m_Capture;
00080 public:
00081 RootComponent()
00082 : m_Capture(0)
00083 {
00084 Bitmap scr=get_screen();
00085 m_Rect=iRect2(0,0,scr.get_width(),scr.get_height());
00086 EventManager::instance()->register_listener(SDL_MOUSEMOTION,this);
00087 EventManager::instance()->register_listener(SDL_MOUSEBUTTONDOWN,this);
00088 EventManager::instance()->register_listener(SDL_MOUSEBUTTONUP,this);
00089 }
00090
00091 virtual bool handle_event(const SDL_Event& e)
00092 {
00093 switch (e.type)
00094 {
00095 case SDL_MOUSEMOTION:
00096 {
00097 int button=0;
00098 for(int i=1;i<3;++i)
00099 if (e.motion.state & SDL_BUTTON(i)) button=i;
00100 mouse_move(button,iVec2(e.motion.x,e.motion.y));
00101 return true;
00102 }
00103 case SDL_MOUSEBUTTONDOWN: button_down(e.button.button,iVec2(e.button.x,e.button.y)); return true;
00104 case SDL_MOUSEBUTTONUP: button_up(e.button.button,iVec2(e.button.x,e.button.y)); return true;
00105 }
00106 return false;
00107 }
00108
00109 virtual void button_down(int button, const iVec2& position)
00110 {
00111 if (m_Capture) m_Capture->button_down(button,position);
00112 else Component::button_down(button,position);
00113 }
00114
00115 virtual void button_up(int button, const iVec2& position)
00116 {
00117 if (m_Capture) m_Capture->button_up(button,position);
00118 else Component::button_up(button,position);
00119 }
00120
00121 virtual void mouse_move(int button, const iVec2& position)
00122 {
00123 if (m_Capture) m_Capture->mouse_move(button,position);
00124 else Component::mouse_move(button,position);
00125 }
00126
00127
00128 protected:
00129 virtual void set_capture(Component* child) { m_Capture=child; }
00130 };
00131
00132 class OButton : public Component
00133 {
00134 Bitmap m_Regular,m_Pressed;
00135 public:
00136 OButton(const iVec2& size, const xstring& text);
00137 OButton(const iVec2& size, Bitmap bmp);
00138
00139 virtual void button_down(int button, const iVec2& position);
00140 virtual void button_up(int button, const iVec2& position);
00141 virtual void mouse_move(int button, const iVec2& position);
00142 };
00143
00144
00145 }
00146
00147 #endif // H_SDLPP_UI
00148