Nilorea Library

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Macros Groups
lexmenu.h
Go to the documentation of this file.
1
9 #ifndef LEX_MENU_HEADER
10 #define LEX_MENU_HEADER
11
12
13 #include <malloc.h>
14 #include <allegro.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
25 #define LEX_MENU_IMG_COUNT 4
26 #define LEX_MENU_SND_COUNT 5
27 #define LEX_MENU_CBF_COUNT 9
28
29
30 /* Alignment constants. These can be used to
31 specify how an item should be aligned to its
32 coordinates.
33 */
34 #define LEX_MENU_ALIGN_LEFT 0
35 #define LEX_MENU_ALIGN_TOP 0
36 #define LEX_MENU_ALIGN_CENTER 1
37 #define LEX_MENU_ALIGN_RIGHT 2
38 #define LEX_MENU_ALIGN_BOTTOM 2
39
40 /* A menu item can have on of four states:
41 LEX_MENU_NORMAL : The default state for an item
42 LEX_MENU_ACTIVE : The mouse cursor hovers over the item
43 / the item has the focus
44 LEX_MENU_PRESSED : The mouse button was pressed
45 LEX_MENU_DISABLED: The item is disabled
46
47 You can set a bitmap for each of these states,
48 giving you a simple way to simulate "rollover" effects.
49
50 Use lexMenuSetImage() to set the image for a certain state.
51 */
52 #define LEX_MENU_NORMAL 0
53 #define LEX_MENU_ACTIVE 1
54 #define LEX_MENU_PRESSED 2
55 #define LEX_MENU_DISABLED 3
56
57 /* There're a couple of "events" thrown by the items.
58 You register to an event on a per item base.
59
60 Use the lexMenuSetCallback() function to set the function
61 you want to be called if one of the events occurs.
62
63 You can also set a sound which should be played if one of
64 the events is fired. You cannot set sound for the MOVE and
65 DRAG event, though.
66 For all other events, use lexMenuSetSample() to specify a
67 sound effect.
68
69 If ON_DRAW is set, the items will not draw itself, but call
70 the callback function instead. Use this for user-drawn menu
71 items.
72
73 ON_DRAW_DONE is fired as soon as an item was drawn. You can
74 use that callback to decorate an already drawn item
75 */
76 #define LEX_MENU_ON_ENTER 0
77 #define LEX_MENU_ON_LEAVE 1
78 #define LEX_MENU_ON_PRESS 2
79 #define LEX_MENU_ON_RELEASE 3
80 #define LEX_MENU_ON_FIRE 4
81 #define LEX_MENU_ON_MOVE 5
82 #define LEX_MENU_ON_DRAG 6
83 #define LEX_MENU_ON_DRAW 7
84 #define LEX_MENU_ON_DRAW_DONE 8
85
86 /* You can specify how the menu should react on events.
87 Per default it will react on mouse and keyboard events.
88 If you set menu->inputMode to LEX_MENU_MOUSE only mouse
89 input will be accepted.
90 Set it to LEX_MENU_KEYBOARD if you want the menu to react
91 on keyboard only.
92 */
93 #define LEX_MENU_MOUSE 1
94 #define LEX_MENU_KEYBOARD 2
95 #define LEX_MENU_MOUSE_AND_KB 3
96
97 /* A LexMenuItem represents a single entry of the menu.
98 You can set the bitmaps it has using lexMenuSetImage().
99 You can set the sounds that should be played if certain events are
100 triggered by calling lexMenuSetSample().
101
102 Each item has an ID you can set (if you want). That id is returned
103 lexMenuRun()
104
105 If you want to get notified if a certain event occurs, use
106 lexMenuSetCallback() to register your event callback function.
107
108 You can create a LexMenuItem by calling lexMenuCreateItem()
109 and add it to a menu via lexMenuAddItem().
110
111 Once the item is added to the menu, you can forget it.
112 It will be destroyed automatically by lexMenuDestroy if it has
113 been allocated via lexMenuCreateItem, or if the autoDestroy member
114 has been set manually.
115 */
116 typedef struct LexMenuItemStruct LexMenuItem;
117 typedef struct LexMenuItemItorStruct LexMenuItemItor;
118
119 /* The LexMenu represents the actual menu. It contains a list of items,
120 an optional background image (bg), an optional software mouse cursor
121 image (cursor) with hotspot information (hotX, hotY).
122 You can set on what kinds of inputs the menus hould react by setting
123 the inputMode variable. The default is to react on mouse and keyboard.
124
125 You can also specify a function pointer to your update function if you
126 want. If you don't set the showMenu function pointer, the bitmap
127 passed to lexMenuRun() will be blitted to the screen - if the bitmap passed
128 to lexMenuRun() is not the screen already, that is.
129 */
130 typedef struct LexMenuStruct LexMenu;
131
132 /* The event which will be passed to an event callback.
133 type, menu and source are always valid.
134 x,y,dx,dy are only valid for mouse input.
135 dx,dy is only valid for DRAG and MOVE events.
136 */
137 typedef struct LexMenuEventStruct LexMenuEvent;
138
139 /* The event callback functipn */
140 typedef void (*LexMenuCallback)(LexMenuEvent *);
141
142 struct LexMenuItemItorStruct {
143 LexMenuItem* data;
144 LexMenuItemItor* next;
145 LexMenuItemItor* prev;
146 };
147
148 struct LexMenuEventStruct {
149 int type;
150 LexMenu *menu;
151 LexMenuItem *source;
152 int x, y;
153 int dx, dy;
154 };
155
156
157 struct LexMenuItemStruct {
158 BITMAP *img[LEX_MENU_IMG_COUNT];
159 SAMPLE *snd[LEX_MENU_SND_COUNT];
160 LexMenuCallback cbf[LEX_MENU_CBF_COUNT];
161 int id;
162 int state;
163 int x,y;
164 int align, valign;
165 int autoDestroy;
166 int autoDestroyResources;
167 };
168
169
170 struct LexMenuStruct {
171 LexMenuItemItor *begin, *end;
172 BITMAP *bg;
173
174 BITMAP *cursor;
175 int hotX, hotY;
176 int autoDestroyResources;
177
178 int inputMode;
179 void (*showMenu)(BITMAP* img);
180 };
181
182 /* creates a new Menu and sets the BG image */
183 LexMenu* lexMenuCreate(BITMAP *bg);
184 /* Frees the memory occupied by the menu.
185 * If autoDestroyResources is set (say by creating the menu via lexLoadMenuFromFile
186 * or by manually setting it) all BITMAPs and SAMPLEs belonging to the menu
187 * will be destroyed as well.
188 * Passing NULL for menu will do nothing.
189 */
190 LexMenu* lexMenuDestroy(LexMenu* menu);
191
192 /* Creates a menu item.
193 LexMenuItems created by this function, which are added
194 to a menu, will be destroyed automatically by lexMenuDestroy()
195 */
196 LexMenuItem *lexMenuCreateItem();
197
198 /* Adds item to menu */
199 void lexMenuAddItem(LexMenu *menu, LexMenuItem *item);
200 /* Executes the menu. Returns the id of the activated item, or -1 if ESC was pressed */
201 int lexMenuRun(LexMenu* menu, BITMAP* dst);
202 /* Allows you to set a BITMAP for a certain state. The item will never free the image.
203 You need to ensure that the bitmap is destroyed if no longer needed
204 */
205 void lexMenuSetImage(LexMenuItem *item, int state, BITMAP* img);
206 /* Allows you to set a SAMPLE for a certain event. The item will never free the sample .
207 You need to ensure that the sample is destroyed if no longer needed
208 */
209 void lexMenuSetSample(LexMenuItem *item, int event, SAMPLE* sample);
210 /* Allows you to specify a callback to be called if an event occurs.*/
211 void lexMenuSetCallback(LexMenuItem *item, int callbackID, LexMenuCallback func);
212
213 /* Loads a menu from a config file named "filename". "screenname" is the section name for the menu.
214 see "menu.ini" for a detailed example ini file.
215 */
216 LexMenu* lexLoadMenuFromFile(const char* filename, const char* screenname);
217
218 /* Return a pointer to the bitmap of the item or NULL */
219
220 BITMAP *lexMenuGetBitmap( LexMenu* menu , int item , int whichone );
221
224 #ifdef __cplusplus
225 }
226 #endif
227 #endif