Fixed Score = -1 on worldmap display when no level has been played in a fresh slot.
[supertux.git] / lib / gui / button.h
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 #ifndef SUPERTUX_BUTTON_H
22 #define SUPERTUX_BUTTON_H
23
24 #include <vector>
25
26 #include "../video/surface.h"
27 #include "../video/font.h"
28 #include "../special/timer.h"
29
30 namespace SuperTux
31   {
32
33   /// Possible states of a button.
34   enum ButtonState {
35     BUTTON_NONE = -1,
36     BUTTON_CLICKED,
37     BUTTON_PRESSED,
38     BUTTON_HOVER,
39     BUTTON_WHEELUP,
40     BUTTON_WHEELDOWN,
41     BUTTON_DEACTIVE
42   };
43
44   class ButtonPanel;
45
46   /// Button
47   /** Buttons can be placed on the screen and used like any other
48       simple button known from desktop applications. */
49   class Button
50     {
51       friend class ButtonPanel;
52
53     public:
54       /// Constructor
55       Button(Surface* button_image, const std::string& ninfo,
56              SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1);
57       Button(const std::string& imagefilename, const std::string& ninfo,
58              SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1);
59
60       ~Button();
61
62       /// Apply SDL_Event on button.
63       void event(SDL_Event& event);
64       /// Draw button.
65       void draw(DrawingContext& context);
66       /// Get button state.
67       int get_state();
68       /// Activate/Deactivate button.
69       void set_active(bool active)
70       {        active ? state = BUTTON_NONE : state = BUTTON_DEACTIVE;      };
71       /// Add an icon
72       /** The last added icon is the last one, which gets drawn. */
73       void add_icon(const std::string& imagefile, int mw, int mh);
74       /// Get position of the button on screen.
75       /** Returns a SDL_Rect. */
76       SDL_Rect get_pos()
77       {        return rect;      }
78       /// Get tag of the button
79       /** Useable for button identification etc. */
80       int get_tag()
81       {        return tag;      }
82       //  void set_drawable(Drawable* newdrawable)
83       //  { drawable = newdrawable; }
84
85       static Font* info_font;
86       
87     private:
88       static Timer popup_timer;
89       //  Drawable* drawable;
90       std::vector<Surface*> icon;
91       std::string info;
92       SDLKey shortcut;
93       SDL_Rect rect;
94       bool show_info;
95       ButtonState state;
96       int tag;
97     };
98
99   /// Panel of buttons
100   /** A ButtonPanel manages buttons inside
101       its scope. It also dispatches events
102       and draws the buttons it contains. */
103   class ButtonPanel
104     {
105     public:
106       /// Constructor.
107       /** Expects X,Y coordinates and the width and height values
108           of the ButtonPanel. */
109       ButtonPanel(int x, int y, int w, int h);
110       /// Constructor.
111       /** SDL_Rect version of above. */
112       ButtonPanel(const SDL_Rect& rect);
113
114       ~ButtonPanel();
115       /// Draw the panel and its buttons.
116       void draw(DrawingContext& context);
117       /// Dispatch button events.
118       Button* event(SDL_Event &event);
119       /// Add a button to the panel.
120       /** @param tag: Can be used to identify a button. */
121       void additem(Button* pbutton, int tag);
122       /// Set the default size of contained buttons.
123       void set_button_size(int w, int h);
124       /// Manipulate a button.
125       Button* manipulate_button(int i);
126       /// Set if the last clicked/used item, should be drawn highlighted.
127       void highlight_last(bool b);
128       /// Set the last clicked/used button.
129       /** Set which button is internally the last clicked/used and
130           therefore drawn highlighted in case button highlighting
131           is enabled for the ButtonPanel. */
132       void set_last_clicked(unsigned int last);
133
134     private:
135       int bw, bh;
136       bool hlast;
137       bool hidden;
138       SDL_Rect rect;
139       std::vector<Button*> item;
140       std::vector<Button*>::iterator last_clicked;
141     };
142
143 } // namespace SuperTux
144
145 #endif /*SUPERTUX_BUTTON_H*/