From cd0b6636bf1c72c7f8a94103f7c6b37c83c0ba8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Gl=C3=A4=C3=9Fer?= Date: Wed, 21 Jul 2004 11:56:48 +0000 Subject: [PATCH] Introduction of SuperTux namespace. Added comments to Button and ButtonPanel classes. SVN-Revision: 1610 --- lib/gui/button.cpp | 250 ++++++++++++++++++++++++++++------------------------- lib/gui/button.h | 180 +++++++++++++++++++++++--------------- src/leveleditor.h | 2 + 3 files changed, 248 insertions(+), 184 deletions(-) diff --git a/lib/gui/button.cpp b/lib/gui/button.cpp index 66b096320..680ee4e11 100644 --- a/lib/gui/button.cpp +++ b/lib/gui/button.cpp @@ -27,10 +27,12 @@ #include "app/globals.h" #include "gui/button.h" +using namespace SuperTux; + Timer Button::popup_timer; Button::Button(Surface* button_image, const std::string& ninfo, - SDLKey nshortcut, int x, int y, int mw, int mh) + SDLKey nshortcut, int x, int y, int mw, int mh) { popup_timer.init(false); @@ -51,12 +53,12 @@ Button::Button(Surface* button_image, const std::string& ninfo, } Button::Button(const std::string& imagefilename, const std::string& ninfo, - SDLKey nshortcut, int x, int y, int mw, int mh) + SDLKey nshortcut, int x, int y, int mw, int mh) { popup_timer.init(false); add_icon(imagefilename, mw, mh); - + info = ninfo; shortcut = nshortcut; @@ -75,21 +77,21 @@ void Button::add_icon(const std::string& icon_file, int mw, int mh) char filename[1024]; if(!icon_file.empty()) - { - snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str()); - if(!faccessible(filename)) - snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str()); - } + { + snprintf(filename, 1024, "%s/%s", datadir.c_str(), icon_file.c_str()); + if(!faccessible(filename)) + snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str()); + } else - { - snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str()); - } + { + snprintf(filename, 1024, "%s/images/icons/default-icon.png", datadir.c_str()); + } if(mw != -1 || mh != -1) - { - icon.push_back(new Surface(filename,true)); - icon.back()->resize(mw,mh); - } + { + icon.push_back(new Surface(filename,true)); + icon.back()->resize(mw,mh); + } else icon.push_back(new Surface(filename,true)); @@ -107,26 +109,26 @@ void Button::draw(DrawingContext& context) for(std::vector::iterator it = icon.begin(); it != icon.end(); ++it) context.draw_surface(*it, Vector(rect.x,rect.y), LAYER_GUI); -/* if(drawable) - { - Camera viewport; - viewport.set_translation(Vector(rect.x, rect.y)); - drawable->draw(viewport, 0); - }*/ + /* if(drawable) + { + Camera viewport; + viewport.set_translation(Vector(rect.x, rect.y)); + drawable->draw(viewport, 0); + }*/ if(show_info) - { - char str[80]; - int i = -32; - - if(0 > rect.x - white_small_text->get_text_width(info)) - i = rect.w + (int)white_small_text->get_text_width(info); - - if(!info.empty()) - context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI); - sprintf(str,"(%s)", SDL_GetKeyName(shortcut)); - context.draw_text(white_small_text, str, Vector(i + rect.x - white_small_text->get_text_width(str), rect.y + white_small_text->get_height()+2), LAYER_GUI); - } + { + char str[80]; + int i = -32; + + if(0 > rect.x - white_small_text->get_text_width(info)) + i = rect.w + (int)white_small_text->get_text_width(info); + + if(!info.empty()) + context.draw_text(white_small_text, info, Vector(i + rect.x - white_small_text->get_text_width(info), rect.y), LAYER_GUI); + sprintf(str,"(%s)", SDL_GetKeyName(shortcut)); + context.draw_text(white_small_text, str, Vector(i + rect.x - white_small_text->get_text_width(str), rect.y + white_small_text->get_height()+2), LAYER_GUI); + } if(state == BUTTON_PRESSED || state == BUTTON_DEACTIVE) fillrect(rect.x,rect.y,rect.w,rect.h,75,75,75,200); else if(state == BUTTON_HOVER) @@ -151,77 +153,82 @@ void Button::event(SDL_Event &event) SDLKey key = event.key.keysym.sym; if(event.type == SDL_MOUSEBUTTONDOWN || event.type == SDL_MOUSEBUTTONUP) - { - if(event.button.x < rect.x || event.button.x >= rect.x + rect.w || - event.button.y < rect.y || event.button.y >= rect.y + rect.h) - return; - - if(event.button.button == SDL_BUTTON_RIGHT) - { - show_info = true; - return; - } - else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */ { - state = BUTTON_WHEELUP; - return; + if(event.button.x < rect.x || event.button.x >= rect.x + rect.w || + event.button.y < rect.y || event.button.y >= rect.y + rect.h) + return; + + if(event.button.button == SDL_BUTTON_RIGHT) + { + show_info = true; + return; + } + else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 4) /* Mouse wheel up. */ + { + state = BUTTON_WHEELUP; + return; + } + else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */ + { + state = BUTTON_WHEELDOWN; + return; + } + + if(event.button.button == SDL_BUTTON_LEFT) + if(event.type == SDL_MOUSEBUTTONDOWN) + state = BUTTON_PRESSED; + else + state = BUTTON_CLICKED; } - else if(event.type == SDL_MOUSEBUTTONUP && event.button.button == 5) /* Mouse wheel down. */ - { - state = BUTTON_WHEELDOWN; - return; - } - - if(event.button.button == SDL_BUTTON_LEFT) - if(event.type == SDL_MOUSEBUTTONDOWN) - state = BUTTON_PRESSED; - else - state = BUTTON_CLICKED; - } else if(event.type == SDL_MOUSEMOTION) - { - if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w || - event.motion.y < rect.y || event.motion.y >= rect.y + rect.h) { - state = BUTTON_NONE; + if(event.motion.x < rect.x || event.motion.x >= rect.x + rect.w || + event.motion.y < rect.y || event.motion.y >= rect.y + rect.h) + { + state = BUTTON_NONE; + } + else + { + state = BUTTON_HOVER; + popup_timer.start(1500); + } + + if(show_info) + { + show_info = false; + } } - else + else if(event.type == SDL_KEYDOWN) { - state = BUTTON_HOVER; - popup_timer.start(1500); + if(key == shortcut) + state = BUTTON_PRESSED; } - - if(show_info) + else if(event.type == SDL_KEYUP) { - show_info = false; + if(state == BUTTON_PRESSED && key == shortcut) + state = BUTTON_CLICKED; } - } - else if(event.type == SDL_KEYDOWN) - { - if(key == shortcut) - state = BUTTON_PRESSED; - } - else if(event.type == SDL_KEYUP) - { - if(state == BUTTON_PRESSED && key == shortcut) - state = BUTTON_CLICKED; - } } int Button::get_state() { int rstate; switch(state) - { - case BUTTON_CLICKED: - case BUTTON_WHEELUP: - case BUTTON_WHEELDOWN: - rstate = state; - state = BUTTON_NONE; - return rstate; - default: - return state; - } + { + case BUTTON_CLICKED: + case BUTTON_WHEELUP: + case BUTTON_WHEELDOWN: + rstate = state; + state = BUTTON_NONE; + return rstate; + default: + return state; + } +} + +ButtonPanel::ButtonPanel(const SDL_Rect& rect) +{ + ButtonPanel(rect.x, rect.y, rect.w, rect.h); } ButtonPanel::ButtonPanel(int x, int y, int w, int h) @@ -239,32 +246,32 @@ ButtonPanel::ButtonPanel(int x, int y, int w, int h) Button* ButtonPanel::event(SDL_Event& event) { if(!hidden) - { - Button* ret = NULL; - for(std::vector::iterator it = item.begin(); it != item.end(); ++it) { - (*it)->event(event); - if((*it)->state != BUTTON_NONE) - { - if(hlast && (*it)->state == BUTTON_CLICKED) - last_clicked = it; - ret = (*it); - } + Button* ret = NULL; + for(std::vector::iterator it = item.begin(); it != item.end(); ++it) + { + (*it)->event(event); + if((*it)->state != BUTTON_NONE) + { + if(hlast && (*it)->state == BUTTON_CLICKED) + last_clicked = it; + ret = (*it); + } + } + return ret; } - return ret; - } else - { - return NULL; - } + { + return NULL; + } } ButtonPanel::~ButtonPanel() { for(std::vector::iterator it = item.begin(); it != item.end(); ++it) - { - delete (*it); - } + { + delete (*it); + } item.clear(); } @@ -272,17 +279,17 @@ void ButtonPanel::draw(DrawingContext& context) { if(hidden == false) - { - fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200); - for(std::vector::iterator it = item.begin(); it != item.end(); ++it) { - (*it)->draw(context); - if(hlast && it == last_clicked) - { - fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128); - } + fillrect(rect.x,rect.y,rect.w,rect.h,100,100,100,200); + for(std::vector::iterator it = item.begin(); it != item.end(); ++it) + { + (*it)->draw(context); + if(hlast && it == last_clicked) + { + fillrect((*it)->get_pos().x,(*it)->get_pos().y,(*it)->get_pos().w,(*it)->get_pos().h,100,100,100,128); + } + } } - } } void ButtonPanel::additem(Button* pbutton, int tag) @@ -322,3 +329,14 @@ void ButtonPanel::highlight_last(bool b) { hlast = b; } + +void ButtonPanel::set_last_clicked(unsigned int last) +{ + if(hlast) + { + if(item.size() >= last) + { + last_clicked = item.begin() + last; + } + } +} diff --git a/lib/gui/button.h b/lib/gui/button.h index a24cc9d39..65d0fe350 100644 --- a/lib/gui/button.h +++ b/lib/gui/button.h @@ -26,73 +26,117 @@ #include "video/surface.h" #include "special/timer.h" -enum ButtonState { - BUTTON_NONE = -1, - BUTTON_CLICKED, - BUTTON_PRESSED, - BUTTON_HOVER, - BUTTON_WHEELUP, - BUTTON_WHEELDOWN, - BUTTON_DEACTIVE -}; - -class ButtonPanel; - -class Button -{ - friend class ButtonPanel; - -public: - Button(Surface* icon_file, const std::string& info, SDLKey shortcut, - int x, int y, int mw = -1, int h = -1); - Button(const std::string& icon_name, const std::string& info, SDLKey shortcut, - int x, int y, int mw = -1, int h = -1); - - ~Button(); - void event(SDL_Event& event); - void draw(DrawingContext& context); - int get_state(); - void set_active(bool active) { active ? state = BUTTON_NONE : state = BUTTON_DEACTIVE; }; - void add_icon(const std::string& imagefile, int mw, int mh); - SDL_Rect get_pos() { return rect; } - int get_tag(){return tag; } -// void set_drawable(Drawable* newdrawable) -// { drawable = newdrawable; } - -private: - static Timer popup_timer; -// Drawable* drawable; - std::vector icon; - std::string info; - SDLKey shortcut; - SDL_Rect rect; - bool show_info; - ButtonState state; - int tag; -}; - -class ButtonPanel -{ -public: - ButtonPanel(int x, int y, int w, int h); - ~ButtonPanel(); - void draw(DrawingContext& context); - Button* event(SDL_Event &event); - void additem(Button* pbutton, int tag); - Button* button_panel_event(SDL_Event& event); - void set_button_size(int w, int h); - Button* manipulate_button(int i); - void highlight_last(bool b); - void set_last_clicked(unsigned int last) - { if(hlast) { if(item.size() >= last) { last_clicked = item.begin() + last; } } }; - -private: - int bw, bh; - bool hlast; - bool hidden; - SDL_Rect rect; - std::vector item; - std::vector::iterator last_clicked; -}; +namespace SuperTux + { + + /// Possible states of a button. + enum ButtonState { + BUTTON_NONE = -1, + BUTTON_CLICKED, + BUTTON_PRESSED, + BUTTON_HOVER, + BUTTON_WHEELUP, + BUTTON_WHEELDOWN, + BUTTON_DEACTIVE + }; + + class ButtonPanel; + + /// Button + /** Buttons can be placed on the screen and used like any other + simple button known from desktop applications. */ + class Button + { + friend class ButtonPanel; + + public: + /// Constructor + Button(Surface* button_image, const std::string& ninfo, + SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1); + Button(const std::string& imagefilename, const std::string& ninfo, + SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1); + + ~Button(); + + /// Apply SDL_Event on button. + void event(SDL_Event& event); + /// Draw button. + void draw(DrawingContext& context); + /// Get button state. + int get_state(); + /// Activate/Deactivate button. + void set_active(bool active) + { active ? state = BUTTON_NONE : state = BUTTON_DEACTIVE; }; + /// Add an icon + /** The last added icon is the last one, which gets drawn. */ + void add_icon(const std::string& imagefile, int mw, int mh); + /// Get position of the button on screen. + /** Returns a SDL_Rect. */ + SDL_Rect get_pos() + { return rect; } + /// Get tag of the button + /** Useable for button identification etc. */ + int get_tag() + { return tag; } + // void set_drawable(Drawable* newdrawable) + // { drawable = newdrawable; } + + private: + static Timer popup_timer; + // Drawable* drawable; + std::vector icon; + std::string info; + SDLKey shortcut; + SDL_Rect rect; + bool show_info; + ButtonState state; + int tag; + }; + + /// Panel of buttons + /** A ButtonPanel manages buttons inside + its scope. It also dispatches events + and draws the buttons it contains. */ + class ButtonPanel + { + public: + /// Constructor. + /** Expects X,Y coordinates and the width and height values + of the ButtonPanel. */ + ButtonPanel(int x, int y, int w, int h); + /// Constructor. + /** SDL_Rect version of above. */ + ButtonPanel(const SDL_Rect& rect); + + ~ButtonPanel(); + /// Draw the panel and its buttons. + void draw(DrawingContext& context); + /// Dispatch button events. + Button* event(SDL_Event &event); + /// Add a button to the panel. + /** @Param tag: Can be used to identify a button. */ + void additem(Button* pbutton, int tag); + /// Set the default size of contained buttons. + void set_button_size(int w, int h); + /// Manipulate a button. + Button* manipulate_button(int i); + /// Set if the last clicked/used item, should be drawn highlighted. + void highlight_last(bool b); + /// Set the last clicked/used button. + /** Set which button is internally the last clicked/used and + therefore drawn highlighted in case button highlighting + is enabled for the ButtonPanel. */ + void set_last_clicked(unsigned int last); + + private: + int bw, bh; + bool hlast; + bool hidden; + SDL_Rect rect; + std::vector item; + std::vector::iterator last_clicked; + }; + +} // namespace SuperTux #endif /*SUPERTUX_BUTTON_H*/ diff --git a/src/leveleditor.h b/src/leveleditor.h index d4bf7f5c8..c51411bdf 100644 --- a/src/leveleditor.h +++ b/src/leveleditor.h @@ -32,6 +32,8 @@ #include "gui/button.h" #include "gui/menu.h" +using namespace SuperTux; + enum LevelEditorMainMenuIDs { MNID_RETURNLEVELEDITOR, MNID_SUBSETSETTINGS, -- 2.11.0