1 /*
2  *  JLib - Jacob's Library.
3  *  Copyright (C) 2003, 2004  Juan Carlos Seijo P�rez
4  *
5  *  This library is free software; you can redistribute it and/or
6  *  modify it under the terms of the GNU Library General Public
7  *  License as published by the Free Software Foundation; either
8  *  version 2 of the License, or (at your option) any later version.
9  *
10  *  This library is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  *  Library General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Library General Public
16  *  License along with this library; if not, write to the Free
17  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  *  Juan Carlos Seijo P�rez
20  *  jacob@mainreactor.net
21  */
22 
23 /** Menu en pantalla compuesto de cadenas de texto.
24  * @file    JTextMenu.h
25  * @author  Juan Carlos Seijo P�rez
26  * @date    28/03/2004
27  * @version 0.0.1 - 28/03/2004 - Primera versi�n.
28  */
29 
30 #ifndef _JTEXTMENU_INCLUDED
31 #define _JTEXTMENU_INCLUDED
32 
33 #include <JLib/Graphics/JDrawable.h>
34 #include <JLib/Graphics/JImage.h>
35 #include <JLib/Graphics/JFont.h>
36 #include <JLib/Util/JApp.h>
37 #include <JLib/Util/JString.h>
38 #include <JLib/Util/JTree.h>
39 #include <JLib/Util/JTimer.h>
40 
41 /** Modo de renderizado de la fuente del men�.
42  */
43 typedef enum
44 {
45 	JTEXTMENU_SOLID = 1,                  /**< S�lido con colorkey. */
46 	JTEXTMENU_SHADED,                     /**< Antialiasing con fondo s�lido. */
47 	JTEXTMENU_BLENDED,                    /**< Antialiasing con fondo transparente. */
48 } JTextMenuRenderStyle;
49 
50 /** Distribuci�n del men�.
51  */
52 typedef enum
53 {
54 	JTEXTMENU_LEFT = 1,                   /**< Alineado a la izquierda. */
55 	JTEXTMENU_RIGHT,                      /**< Alineado a la derecha. */
56 	JTEXTMENU_CENTER,                     /**< Centrado. */
57 	JTEXTMENU_SAMELINE,                   /**< Todas las opciones en la misma l�nea. */
58 	JTEXTMENU_UP,                         /**< Alineado arriba. */
59 	JTEXTMENU_DOWN,                       /**< Alineado abajo. */
60 } JTextMenuLayout;
61 
62 /** Estructura de configuraci�n del men�.
63  */
64 struct JTextMenuConfig
65 {
66 	JFont * font;                         /**< Fuente a usar. */
67 	SDL_Color color;                      /**< Color de fuente. */
68 	SDL_Color backColor;                  /**< Color de fondo (JTEXTMENU_SHADED). */
69 	SDL_Color hiColor;                    /**< Color resaltado. */
70 	SDL_Color hiBackColor;                /**< Color de fondo resaltado (JTEXTMENU_SHADED). */
71 	s32 lineDistance;                     /**< Separaci�n entre l�neas (-1 para la predeterminada de la fuente). */
72 	JTextMenuLayout layout;               /**< Distribuci�n del men� en horizontal. */
73 	JTextMenuLayout layoutV;              /**< Distribuci�n del men� en vertical. */
74 	JTextMenuRenderStyle renderMode;      /**< Modo de renderizado. */
75 	bool trackMouse;                      /**< Indica si puede manejarse con rat�n. */
76 	bool trackKeyboard;                   /**< Indica si puede manejarse con teclado. */
77 	bool trackJoystick;                   /**< Indica si puede manejarse con joystick/pad. */
78 	bool autoEnter;                       /**< Indica si desciende si no hay acci�n asociada. */
79 };
80 
81 /** Opci�n de men� b�sica.
82  */
83 class JTextMenuEntry
84 {
85  protected:
86  public:
87 	JImage *image;                        /**< Imagen asociada a la opci�n. */
88 	JImage *hiImage;                      /**< Imagen asociada a la opci�n resaltada. */
89 
90 	JString text;                         /**< Texto de la opci�n de men�. */
91 	void (*Action)(void *data);           /**< Acci�n asociada a la opci�n de men�. */
92 	void *data;                           /**< Datos asociados a la opci�n de men�. */
93 
94  public:
95 	JTextMenuEntry(const JString &str, void (*pAct)(void *) = 0, void *actionData = 0)
96 	: image(0), hiImage(0), text(str), Action(pAct), data(actionData)
97 	{}
98 
99 	/** Selecciona la opci�n de men�, llamando a la callback asociada, si existe.
100 	 * @return <b>true</b> en caso de haber acci�n asociada, <b>false</b> si no.
101 	 */
102 	bool Select();
103 
104 	/** Devuelve la imagen asociada.
105 	 * @return La imagen asociada.
106 	 */
Image()107 	JImage * Image() {return image;}
108 
109 	/** Devuelve la imagen asociada al estado resaltado.
110 	 * @return La imagen asociada al estado resaltado.
111 	 */
HiImage()112 	JImage * HiImage() {return hiImage;}
113 
114 	/** Renderiza la opci�n de men� con la configuraci�n dada.
115 	 * @param  cfg Configuraci�n de men�.
116 	 * @return <b>true</b> Si todo fue bien, <b>false</b> si no se pudo renderizar.
117 	 */
118 	bool Init(JTextMenuConfig &cfg);
119 
120 	/** Devuelve el texto de esta opci�n.
121 	 */
Text()122 	const JString& Text() {return text;}
123 
124 	/** Destruye el objeto y libera las imagenes asociadas.
125 	 */
Destroy()126 	void Destroy()
127 	{
128 		JDELETE(image);
129 		JDELETE(hiImage);
130 	}
131 
132 	/** Destruye el objeto y libera las imagenes asociadas.
133 	 */
~JTextMenuEntry()134 	virtual ~JTextMenuEntry()
135 	{
136 		Destroy();
137 	}
138 };
139 
140 /** Encapsula un men� de texto en pantalla.
141  */
142 class JTextMenu : public JDrawable
143 {
144 	JTree<JTextMenuEntry *> options;      /**< �rbol de opciones de men�. */
145 	JTree<JTextMenuEntry *>::Iterator *curOption; /**< Opci�n actual. */
146 	JTextMenuConfig config;               /**< Par�metros de configuraci�n del men�. */
147 	s32 maxW;                             /**< Anchura m�xima. */
148 	s32 maxH;                             /**< Altura m�xima. */
149 
150 	/** M�todo recursivo de renderizaci�n de las opciones del men�.
151 	 * @param it Iterador de la opci�n actual.
152 	 * @return <b>true</b> Si todo fue bien, <b>false</b> si no hay memoria.
153 	 */
154 	bool RenderBranch(JTree<JTextMenuEntry *>::Iterator *it);
155 
156 	/** Aplica la distribuci�n de men�.
157 	 */
158 	void ApplyLayout(JTree<JTextMenuEntry *>::Iterator *it);
159 
160  public:
161 	/** Crea un men� vac�o. Init debe ser llamada antes de utilizar el objeto.
162 	 */
163 	JTextMenu();
164 
165 	/** Inicializa el men� con la configuraci�n dada. Esta funci�n debe
166 	 * ser llamada despu�s de haber rellenado el �rbol del men�.
167 	 * @param  cfg Configuraci�n del men�.
168 	 * @return <b>true</b> Si todo fue bien, <b>false</b> si no se encontr� la
169 	 * fuente dada.
170 	 */
171 	bool Init(JTextMenuConfig &cfg);
172 
173 	/** Devuelve el iterador de opci�n actual.
174 	 * @return Iterador de opci�n actual.
175 	 */
Menu()176 	JTree<JTextMenuEntry *>::Iterator * Menu() {return curOption;}
177 
178 	/** Funci�n de dibujo del men�.
179 	 */
180 	virtual void Draw();
181 
182 	/** Procesa tecla arriba
183 	 */
184 	virtual void TrackKeyboard(SDL_keysym key);
185 
186 	/** Procesa movimiento de mouse
187 	 */
188 	virtual void TrackMouse(s32 bt, s32 x, s32 y);
189 
190 	/** Devuelve la anchura m�xima del men�.
191 	 * @return Anchura m�xima del men�.
192 	 */
MaxW()193 	s32 MaxW() {return maxW;}
194 
195 	/** Devuelve la altura m�xima del men�.
196 	 * @return Altura m�xima del men�.
197 	 */
MaxH()198 	s32 MaxH() {return maxH;}
199 
200 	/** Destruye el objeto y libera la memoria asociada.
201 	 */
~JTextMenu()202 	virtual ~JTextMenu()
203 	{
204 		for (options.Begin(); !options.End(); options.Next())
205 		{
206 			JDELETE(options.Cur());
207 		}
208 
209 		options.Clear();
210 		JDELETE(curOption);
211 	}
212 };
213 
214 #endif // _JTEXTMENU_INCLUDED
215