Had to change the #includes of dependend headers from "dir/header.h" to "../dir...
[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 "../special/timer.h"
28
29 namespace SuperTux
30   {
31
32   /// Possible states of a button.
33   enum ButtonState {
34     BUTTON_NONE = -1,
35     BUTTON_CLICKED,
36     BUTTON_PRESSED,
37     BUTTON_HOVER,
38     BUTTON_WHEELUP,
39     BUTTON_WHEELDOWN,
40     BUTTON_DEACTIVE
41   };
42
43   class ButtonPanel;
44
45   /// Button
46   /** Buttons can be placed on the screen and used like any other
47       simple button known from desktop applications. */
48   class Button
49     {
50       friend class ButtonPanel;
51
52     public:
53       /// Constructor
54       Button(Surface* button_image, const std::string& ninfo,
55              SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1);
56       Button(const std::string& imagefilename, const std::string& ninfo,
57              SDLKey nshortcut, int x, int y, int mw = -1, int mh = -1);
58
59       ~Button();
60
61       /// Apply SDL_Event on button.
62       void event(SDL_Event& event);
63       /// Draw button.
64       void draw(DrawingContext& context);
65       /// Get button state.
66       int get_state();
67       /// Activate/Deactivate button.
68       void set_active(bool active)
69       {        active ? state = BUTTON_NONE : state = BUTTON_DEACTIVE;      };
70       /// Add an icon
71       /** The last added icon is the last one, which gets drawn. */
72       void add_icon(const std::string& imagefile, int mw, int mh);
73       /// Get position of the button on screen.
74       /** Returns a SDL_Rect. */
75       SDL_Rect get_pos()
76       {        return rect;      }
77       /// Get tag of the button
78       /** Useable for button identification etc. */
79       int get_tag()
80       {        return tag;      }
81       //  void set_drawable(Drawable* newdrawable)
82       //  { drawable = newdrawable; }
83
84     private:
85       static Timer popup_timer;
86       //  Drawable* drawable;
87       std::vector<Surface*> icon;
88       std::string info;
89       SDLKey shortcut;
90       SDL_Rect rect;
91       bool show_info;
92       ButtonState state;
93       int tag;
94     };
95
96   /// Panel of buttons
97   /** A ButtonPanel manages buttons inside
98       its scope. It also dispatches events
99       and draws the buttons it contains. */
100   class ButtonPanel
101     {
102     public:
103       /// Constructor.
104       /** Expects X,Y coordinates and the width and height values
105           of the ButtonPanel. */
106       ButtonPanel(int x, int y, int w, int h);
107       /// Constructor.
108       /** SDL_Rect version of above. */
109       ButtonPanel(const SDL_Rect& rect);
110
111       ~ButtonPanel();
112       /// Draw the panel and its buttons.
113       void draw(DrawingContext& context);
114       /// Dispatch button events.
115       Button* event(SDL_Event &event);
116       /// Add a button to the panel.
117       /** @param tag: Can be used to identify a button. */
118       void additem(Button* pbutton, int tag);
119       /// Set the default size of contained buttons.
120       void set_button_size(int w, int h);
121       /// Manipulate a button.
122       Button* manipulate_button(int i);
123       /// Set if the last clicked/used item, should be drawn highlighted.
124       void highlight_last(bool b);
125       /// Set the last clicked/used button.
126       /** Set which button is internally the last clicked/used and
127           therefore drawn highlighted in case button highlighting
128           is enabled for the ButtonPanel. */
129       void set_last_clicked(unsigned int last);
130
131     private:
132       int bw, bh;
133       bool hlast;
134       bool hidden;
135       SDL_Rect rect;
136       std::vector<Button*> item;
137       std::vector<Button*>::iterator last_clicked;
138     };
139
140 } // namespace SuperTux
141
142 #endif /*SUPERTUX_BUTTON_H*/