1 /**
2  * @file gui.h
3  * @author Frederic MARTIN [martin-frederic@users.sourceforge.net]
4  * @brief A very basic GUI
5  */
6 
7 #ifndef __GUI_H__
8 #define __GUI_H__
9 
10 #include <SDL/SDL_opengl.h>
11 #include <list>
12 #include <string>
13 #include <cstring>
14 
15 
16 using namespace std;
17 
18 
19 class CGui;
20 class CGuiWidget;
21 class CGuiButton;
22 class CGuiLabel;
23 class CTextDrawer;
24 
25 
26 struct CGuiInfos
27 {
28 	CTextDrawer* aTextDrawer;	//!< A text drawer instance which allows us to draw some text on the screen.
29 	int x,y;					//!< Mouse coordinates.
30 	bool left_button_down;		//!< If the left mouse button is down, left_button_down is true.
31 	bool on_left_button_up;		//!< If the left button is no more down (ie: just up) this variable is true.
32 	bool on_left_button_down;	//!< If the left button is no more up (ie: just down) this variable is true.
33 	CGuiWidget* down;			//!< It stores the widget where the mouse button has been down
34 };
35 
36 
37 class CGui
38 {
39 	public:
40 		CGui();
41 		~CGui();
42 		void Go2d();
43 		void Exit2d();
44 		void Init(float window_width, float window_height, GLuint aFontTextureId);
45 		void Destroy();
46 		void AddWidget(CGuiWidget* aWidget);
47 		void RemoveWidget(CGuiWidget* aWidget);
48 		void RemoveAllWidgets();
49 		void Update(int mouse_x=0, int mouse_y=0, bool left_mouse_button_is_down=false);
50 
51 	private:
52 		list<CGuiWidget*> widgets;
53 		CGuiInfos infos;
54 		float width,height;
55 };
56 
57 
58 class CGuiWidget
59 {
60 	public:
61 		CGuiWidget();
62 		virtual ~CGuiWidget();
63 		/**
64 		*	Update the widget.
65 		*	In this function the widget is drawn and you can also check some input states (mouse or keyboard)
66 		*	to do something in particular (like call a "call-back" function or something)
67 		*	@param infos is a CGuiInfos structure. See the CGuiInfos structure for more informations.
68 		*/
69 		virtual void Update(CGuiInfos* infos)=0;
70 		/**
71 		*	All the widgets have a position.
72 		*	This method allows the user of the class to change the widget's position.
73 		*	@param x is the x position of the widget on the screen
74 		*	@param y is the y position of the widget on the screen
75 		*/
76 		virtual void SetPos(int x, int y);
getX()77 		int getX() {return x1;}
getY()78 		int getY() {return y1;}
hide()79 		void hide(){hidden=true;}
show()80 		void show(){hidden=false;}
is_visible()81 		bool is_visible(){return (!hidden);}
82 
83 	private:
84 		bool hidden;
85 	protected:
86 		int x1,y1;
87 
88 };
89 
90 
91 class CGuiButton: public CGuiWidget
92 {
93 	public:
94 		CGuiButton(const string& atext="");
95 		virtual ~CGuiButton();
96 		virtual void Update(CGuiInfos* infos);
97 		void SetText(const string& atext);
98 		void SetSize(int width, int height);
99 		void SetPos(int x, int y);
100 		void OnClick(void (*afunction)());
101 
102 	protected:
103 		string text;
104 		int x2,y2;
105 		int textPos;
106 		void SetTextPos();// Put the text at the middle of the button
107 		void (*call_back_on_click)(); // CALL BACK function
108 
109 };
110 
111 
112 class CGuiButtonImage: public CGuiButton
113 {
114 	public:
115 		CGuiButtonImage(const string& atext="");
116 		virtual ~CGuiButtonImage();
117 		virtual void Update(CGuiInfos* infos);
118 		void SetImage(GLuint id_texture);
119 
120 	private:
121 		GLuint id;
122 
123 };
124 
125 class CGuiLabel: public CGuiWidget
126 {
127 	public:
128 		CGuiLabel(const string& atext="");
129 		virtual ~CGuiLabel();
130 		virtual void Update(CGuiInfos* infos);
131 		void SetText(const string& atext);
132 		void SetPos(int x, int y);
133 		void SetColor(float aR, float aG, float aB, float aA=1.0f);
134 
SetAlpha(float aA)135 		void SetAlpha(float aA){mA=aA;}
SetSpeciaux(bool _speciaux)136 		void SetSpeciaux(bool _speciaux){speciaux=_speciaux;};
137 		void SetSize(int width, int height);
138 
139 	protected:
140 		string text;
141 		int x2,y2;
142 		int textPos;
143 		void SetTextPos();// Put the text at the middle of the label
144 		float mR, mG, mB, mA;	// Color.
145 		bool speciaux;
146 
147 };
148 
149 
150 class CTextDrawer
151 {
152 	public:
153 		CTextDrawer();
154 		~CTextDrawer();
155 		void Initialise();
156 		void Destroy();
157 		void PrintText(float aX, float aY, bool first, const char* text);
158 		void SetFontTextureId(GLuint aFontTextureId);
159 		void SetColor(float aR, float aG, float aB, float aA);
160 
161 	protected:
162 		GLuint mBaseListId;		// Start list id
163 		GLuint mFontTexId;		// GL font texture id.
164 		float mR, mG, mB, mA;	// Color.
165 };
166 
167 
168 
169 
170 #endif
171