Back to index

supertuxkart  0.5+dfsg1
widget.hpp
Go to the documentation of this file.
00001 //
00002 //  SuperTuxKart - a fun racing game with go-kart
00003 //  This code originally from Neverball copyright (C) 2003 Robert Kooima
00004 //
00005 //  This program is free software; you can redistribute it and/or
00006 //  modify it under the terms of the GNU General Public License
00007 //  as published by the Free Software Foundation; either version 2
00008 //  of the License, or (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program; if not, write to the Free Software
00017 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019 /* This file should only be used directly by the widget manager. Also, all
00020  * the coordinates in the widget.* and widget_manager.* are based on OpenGL,
00021  * which means that the 0 in the Y-axis is in the bottom, not the top.
00022  */
00023 
00024 #ifndef HEADER_WIDGET_H
00025 #define HEADER_WIDGET_H
00026 
00027 // This include strings causes very many warning in the gui subdir:
00028 // xlocnum(590) : warning C4312: 'type cast' : conversion from 'uintptr_t' to 'void *' of greater size
00029 // These can apparently be removed by removing 64 bit compatibility warnings, or 
00030 // just disable the warning during the include:
00031 #if defined(WIN32) && !defined(__CYGWIN__)
00032 #  pragma warning(disable:4312)
00033 #endif
00034 #include <string>
00035 #if defined(WIN32) && !defined(__CYGWIN__)
00036 #  pragma warning(default:4312)
00037 #endif
00038 
00039 #include "gui/font.hpp"
00040 
00041 #ifdef __APPLE__
00042 #  include <OpenGL/gl.h>
00043 #else
00044 #  ifdef WIN32
00045 #    define WIN32_LEAN_AND_MEAN
00046 #    include <windows.h>
00047 #  endif
00048 #  include <GL/gl.h>
00049 #endif
00050 
00051 
00052 enum WidgetFontSize { WGT_FNT_SML = 18, WGT_FNT_MED = 24, WGT_FNT_LRG = 30};
00053 
00054 enum WidgetArea //One of the uses of this, is for rounded corners
00055 {
00056     WGT_AREA_NONE = 0,
00057     WGT_AREA_NW = 1, WGT_AREA_SW = 2, WGT_AREA_NE = 4, WGT_AREA_SE = 8,
00058     WGT_AREA_LFT = (WGT_AREA_NW  | WGT_AREA_SW),
00059     WGT_AREA_RGT = (WGT_AREA_NE  | WGT_AREA_SE),
00060     WGT_AREA_TOP = (WGT_AREA_NW  | WGT_AREA_NE),
00061     WGT_AREA_BOT = (WGT_AREA_SW  | WGT_AREA_SE),
00062     WGT_AREA_ALL = (WGT_AREA_TOP | WGT_AREA_BOT)
00063 };
00064 
00065 enum WidgetFont
00066 {
00067     WGT_FONT_GUI,
00068     WGT_FONT_RACE
00069 };
00070 
00071 //The lowest scroll values here must be bigger than
00072 //Widget::MAX_SCROLL or lower than -Widget::MAX_SCROLL
00073 enum WidgetScrollPos
00074 {
00075     //For the X axis
00076     WGT_SCROLL_START_LEFT = 2000001,
00077     WGT_SCROLL_START_RIGHT = 2000002,
00078     WGT_SCROLL_END_LEFT = -2000001,
00079     WGT_SCROLL_END_RIGHT = -2000002,
00080     //For the Y axis
00081     WGT_SCROLL_START_TOP = 1000001,
00082     WGT_SCROLL_START_BOTTOM = 1000002,
00083     WGT_SCROLL_END_TOP = -1000001,
00084     WGT_SCROLL_END_BOTTOM = -1000002,
00085     //Works for both axis
00086     WGT_SCROLL_CENTER = 3000000
00087 };
00088 
00089 //I suggest that you do not use the white or light colors for the rects in
00090 //most cases, because they don't have lighter versions that can be used to
00091 //highlight those rects and then revert them, for example, when you select a
00092 //widget. For textures, you should use WGT_WHITE usually, thought you can get
00093 //nice effects by using other colors.
00094 extern const GLfloat WGT_WHITE  [4];
00095 extern const GLfloat WGT_GRAY  [4];
00096 extern const GLfloat WGT_BLACK  [4];
00097 extern const GLfloat WGT_YELLOW [4];
00098 extern const GLfloat WGT_RED    [4];
00099 extern const GLfloat WGT_GREEN  [4];
00100 extern const GLfloat WGT_BLUE   [4];
00101 extern const GLfloat WGT_TRANS_WHITE  [4];
00102 extern const GLfloat WGT_TRANS_GRAY   [4];
00103 extern const GLfloat WGT_TRANS_BLACK  [4];
00104 extern const GLfloat WGT_TRANS_YELLOW [4];
00105 extern const GLfloat WGT_TRANS_RED    [4];
00106 extern const GLfloat WGT_TRANS_GREEN  [4];
00107 extern const GLfloat WGT_TRANS_BLUE   [4];
00108 
00109 extern const GLfloat WGT_LIGHT_GRAY   [4];
00110 extern const GLfloat WGT_LIGHT_BLACK  [4];
00111 extern const GLfloat WGT_LIGHT_YELLOW [4];
00112 extern const GLfloat WGT_LIGHT_RED    [4];
00113 extern const GLfloat WGT_LIGHT_GREEN  [4];
00114 extern const GLfloat WGT_LIGHT_BLUE   [4];
00115 extern const GLfloat WGT_LIGHT_TRANS_GRAY   [4];
00116 extern const GLfloat WGT_LIGHT_TRANS_BLACK  [4];
00117 extern const GLfloat WGT_LIGHT_TRANS_YELLOW [4];
00118 extern const GLfloat WGT_LIGHT_TRANS_RED    [4];
00119 extern const GLfloat WGT_LIGHT_TRANS_GREEN  [4];
00120 extern const GLfloat WGT_LIGHT_TRANS_BLUE   [4];
00121 
00122 extern const GLfloat WGT_TRANSPARENT [4];
00123 
00124 
00125 class Widget
00126 {
00127     //The only class that can access the Widget class is WidgetManager;
00128     //they are meant to always be used together, and the widgets should only
00129     //be used through the WidgetManager class.
00130     friend class WidgetManager;
00131 
00132     /* Basic widget properties that will always be used. */
00133     int m_x, m_y;
00134     int m_width, m_height;
00135     int m_radius;
00136 
00137     /* Low level features. They are off by default. */
00138     bool m_enable_rect;
00139     GLuint m_rect_list; //A display list number that draws the rectangle with
00140                          //possibly rounded corners.
00141     const GLfloat *m_rect_color; //This const cannot change the value it points to, but it
00142                                  //can change where it points to.
00143     WidgetArea m_round_corners;
00144 
00145     bool m_enable_border;
00146     GLuint m_border_list; //Display list for the border
00147     float m_border_percentage;
00148     const GLfloat *m_border_color;
00149 
00150     bool m_enable_texture;
00151     GLuint m_texture;
00152 
00153     bool m_enable_text;
00154     std::string m_text;
00155     WidgetFontSize m_text_size;
00156     Font *m_font;
00157     const GLfloat *m_text_color;
00158 
00159     //TODO: This variable exists only to go around a bug; should be removed
00160     //after better restructuration.
00161     WidgetFont m_curr_widget_font;
00162 
00163     static const int MAX_SCROLL;
00164     bool m_enable_scroll;
00165     float m_scroll_pos_x;
00166     float m_scroll_pos_y;
00167     int m_scroll_speed_x;
00168     int m_scroll_speed_y;
00169 
00170     bool m_enable_rotation;
00171     float m_rotation_angle;
00172     int m_rotation_speed;
00173 
00174 
00175     //The widget calls the Track::drawScaled2D() function to draw a given
00176     //track, and m_track_num tells which track to draw.
00177     int m_enable_track;
00178     int m_track_num;
00179 
00180     /* High level, pattern following features; they deactivate themselves
00181      * after they follow their pattern, and might use low level features.*/
00182     static const float MAX_TEXT_SCALE;
00183     static const float MIN_TEXT_SCALE;
00184     float m_text_scale; //Used for the pulse effect
00185 
00186     Widget
00187     (
00188         const int X_,
00189         const int Y_,
00190         const int WIDTH_,
00191         const int HEIGHT_
00192     );
00193     ~Widget();
00194 
00195     void update(const float DELTA);
00196 
00197     /* Time limited features' functions. */
00198     void pulse() {m_text_scale = MAX_TEXT_SCALE;}
00199 
00200     /* Convenience functions. */
00201     void resizeToText(); //This checks if the widget is smaller than the
00202                            //text, and if so, changes the width and height.
00203 
00204     void lightenColor();
00205     void darkenColor();
00206 
00207     void setFont( const WidgetFont FONT );
00208     void setTexture( const char* FILENAME, const bool is_full_path=true );
00209 
00210     /* Functions created simply to organize the code */
00211     bool createRect();
00212     void updateVariables( const float DELTA );
00213     void draw();
00214     void applyTransformations();
00215 
00216 };
00217 
00218 #endif
00219 
00220 /* EOF */