X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=lib%2Fgui%2Fbutton.h;h=5a78d4592dee969703b05d4092cc19067d2a9d4b;hb=8e0bad9f82ccbc811a18edd7ce6c6f69c5bca082;hp=3ad96750d7ec41ead13e8856683fa4eb77000a9d;hpb=b2d8d1eac9fd6328d3a8c61cf09d4b2882145042;p=supertux.git diff --git a/lib/gui/button.h b/lib/gui/button.h index 3ad96750d..5a78d4592 100644 --- a/lib/gui/button.h +++ b/lib/gui/button.h @@ -1,145 +1,92 @@ -// $Id$ -// -// SuperTux -// Copyright (C) 2004 Tobias Glaesser -// -// This program is free software; you can redistribute it and/or -// modify it under the terms of the GNU General Public License -// as published by the Free Software Foundation; either version 2 -// of the License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program; if not, write to the Free Software -// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -// 02111-1307, USA. +/*************************************************************************** + button.h - graphical buttons + ------------------- + begin : June, 23 2004 + copyright : (C) 2004 by Ricardo Cruz + email : rick2@aeiou.pt + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ #ifndef SUPERTUX_BUTTON_H #define SUPERTUX_BUTTON_H #include +#include -#include "../video/surface.h" -#include "../video/font.h" -#include "../special/timer.h" +#include "math/vector.h" +#include "video/drawing_context.h" 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 Surface; +class ButtonGroup; + +enum { + BT_NONE, + BT_HOVER, + BT_SELECTED, + BT_SHOW_INFO }; - 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; } - - static Font* info_font; - - 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*/ +class Button +{ +public: + Button(Surface* image_, std::string info_, SDLKey binding_); + ~Button(); + + void draw(DrawingContext& context, bool selected); + int event(SDL_Event& event, int x_offset = 0, int y_offset = 0); + + static Font* info_font; + +private: + friend class ButtonGroup; + + Vector pos, size; + + Surface* image; + SDLKey binding; + + int id; + int state; + std::string info; +}; + +class ButtonGroup +{ +public: + ButtonGroup(Vector pos_, Vector size_, Vector button_box_); + ~ButtonGroup(); + + void draw(DrawingContext& context); + bool event(SDL_Event& event); + + void add_button(Button button, int id, bool select = false); + void add_pair_of_buttons(Button button1, int id1, Button button2, int id2); + + int selected_id(); + void set_unselected(); + bool is_hover(); + +private: + Vector pos, buttons_size, buttons_box; + typedef std::vector