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 im�genes.
24  * @file    JImageMenu.h
25  * @author  Juan Carlos Seijo P�rez
26  * @date    28/04/2004
27  * @version 0.0.1 - 28/04/2004 - Primera versi�n.
28  * @version 0.0.2 - 25/09/2004 - Modificaci�n del m�todo 2D de Pos() para aceptar floats (quita libertad sino).
29  */
30 
31 #ifndef _JIMAGEMENU_INCLUDED
32 #define _JIMAGEMENU_INCLUDED
33 
34 #include <JLib/Graphics/JDrawable.h>
35 #include <JLib/Graphics/JImage.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 /** Distribuci�n del men�.
42  */
43 typedef enum
44 {
45 	JIMAGEMENU_LEFT = 1,                   /**< Alineado a la izquierda. */
46 	JIMAGEMENU_RIGHT,                      /**< Alineado a la derecha. */
47 	JIMAGEMENU_CENTER,                     /**< Centrado. */
48 	JIMAGEMENU_SAMELINE,                   /**< Todas las opciones en la misma l�nea. */
49 	JIMAGEMENU_FREE,                       /**< Opciones en cualquier posici�n */
50 } JImageMenuLayout;
51 
52 /** Estructura de configuraci�n del men�.
53  */
54 struct JImageMenuConfig
55 {
56 	JImageMenuLayout layout;              /**< Distribuci�n del men�. */
57 	bool trackMouse;                      /**< Indica si puede manejarse con rat�n. */
58 	bool trackKeyboard;                   /**< Indica si puede manejarse con teclado. */
59 	bool trackJoystick;                   /**< Indica si puede manejarse con joystick/pad. */
60 	bool autoEnter;                       /**< Indica si desciende si no hay acci�n asociada. */
61 };
62 
63 /** Opci�n de men� b�sica.
64  */
65 class JImageMenuEntry
66 {
67  protected:
68 	JImage *image;                        /**< Imagen asociada a la opci�n. */
69 	JImage *hiImage;                      /**< Imagen asociada a la opci�n resaltada. */
70 
71 	void (*Action)(void *data);           /**< Acci�n asociada a la opci�n de men�. */
72 	void *data;                           /**< Datos asociados a la opci�n de men�. */
73 
74  public:
75   /** Creates a menu entry.
76    * @param  img Image for the item unhighlighted.
77    * @param  hiImg Image for the item highlighted.
78    * @param  pAct Pointer to function to call when the item is selected.
79    * @param  actionData Pointer to additional data to be passed to the action function. In case you want to pass
80    * a value (not a pointer to the value) to store it into the pointer itself, you can use the macros
81    * JCAST_TSS_TO_VOIDPTR in JLib/Util/JTypes.h, where T in TSS is the signedness of the type (S or U)
82    * and SS is the size in bits (8, 16 or 32). Read carefully the comments in that file before using them.
83    */
84 	JImageMenuEntry(JImage *img, JImage *hiImg, void (*pAct)(void *) = 0, void *actionData = 0)
image(img)85 	: image(img), hiImage(hiImg), Action(pAct), data(actionData)
86 	{}
87 
88 	/** Selecciona la opci�n de men�, llamando a la callback asociada, si existe.
89 	 * @return <b>true</b> en caso de haber acci�n asociada, <b>false</b> si no.
90 	 */
91 	bool Select();
92 
93 	/** Devuelve la imagen asociada.
94 	 * @return La imagen asociada.
95 	 */
Image()96 	JImage * Image() {return image;}
97 
98 	/** Devuelve la imagen asociada al estado resaltado.
99 	 * @return La imagen asociada al estado resaltado.
100 	 */
HiImage()101 	JImage * HiImage() {return hiImage;}
102 
103 	/** Destruye el objeto.
104 	 */
~JImageMenuEntry()105 	~JImageMenuEntry()
106 	{}
107 };
108 
109 /** Encapsula un men� de texto en pantalla.
110  */
111 class JImageMenu : public JDrawable
112 {
113 	JTree<JImageMenuEntry *> options;       /**< �rbol de opciones de men�. */
114 	JTree<JImageMenuEntry *>::Iterator *curOption; /**< Opci�n actual. */
115 	JImageMenuConfig config;              /**< Par�metros de configuraci�n del men�. */
116 
117 	/** Aplica la distribuci�n de men�.
118 	 */
119 	void ApplyLayout(JTree<JImageMenuEntry *>::Iterator *it);
120 
121  public:
122 	/** Crea un men� vac�o.
123 	 */
124 	JImageMenu();
125 
126 	/** Inicializa el men� con la configuraci�n dada. Esta funci�n debe
127 	 * ser llamada despu�s de haber rellenado el �rbol del men�.
128 	 * @param  cfg Configuraci�n del men�.
129 	 * @return <b>true</b> Si todo fue bien, <b>false</b> si no se encontr� la
130 	 * fuente dada.
131 	 */
132 	bool Init(JImageMenuConfig &cfg);
133 
134 	/** Devuelve el iterador de opci�n actual.
135 	 * @return Iterador de opci�n actual.
136 	 */
Menu()137 	JTree<JImageMenuEntry *>::Iterator * Menu() {return curOption;}
138 
139 	/** Establece la posici�n del men�. La alineaci�n se hace respecto de
140 	 * esta posici�n.
141 	 * @param  x Posici�n x.
142 	 * @param  y Posici�n y.
143 	 */
144 	virtual void Pos(float x, float y);
145 
146 	/** Funci�n de dibujo del men�.
147 	 */
148 	virtual void Draw();
149 
150 	/** Procesa tecla arriba
151 	 */
152 	virtual void TrackKeyboard(SDL_keysym key);
153 
154 	/** Procesa movimiento de mouse
155 	 */
156 	virtual void TrackMouse(s32 bt, s32 x, s32 y);
157 
158 	/** Destruye el objeto y libera la memoria asociada.
159 	 */
~JImageMenu()160 	virtual ~JImageMenu()
161 	{
162 		for (options.Begin(); options.End(); options.Next())
163 			JDELETE(options.Cur());
164 
165 		options.Clear();
166 	}
167 };
168 
169 #endif // _JIMAGEMENU_INCLUDED
170