1 /***************************************************************************
2 item.c - description
3 -------------------
4 begin : Thu Sep 20 2001
5 copyright : (C) 2001 by Michael Speck
6 email : kulkanie@gmx.net
7 ***************************************************************************/
8
9 /***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18 #include "manager.h"
19
20 extern Sdl sdl;
21 extern SDL_Surface *mbkgnd;
22 extern Font *mfont, *mhfont;
23
24 /*
25 ====================================================================
26 Locals
27 ====================================================================
28 */
29
30 float alpha_change = 0.4;
31
32 /*
33 ====================================================================
34 Create a basic item.
35 ====================================================================
36 */
item_create_basic(int type,char * name,char * hint)37 Item *item_create_basic( int type, char *name, char *hint )
38 {
39 Item *item = calloc( 1, sizeof( Item ) );
40 /* name */
41 item->name = strdup( name );
42 /* id */
43 item->item_id = -1;
44 /* alpha */
45 item->halpha = 255;
46 item->alpha = 0;
47 /* type */
48 item->type = type;
49 /* hint if astring was passed */
50 if ( hint == 0 )
51 item->hint = 0;
52 else
53 item->hint = hint_create( 0, 0, hint ); /* the position will be set when this item is adjusted in menu adjust */
54 return item;
55 }
56
57 /*
58 ====================================================================
59 Publics
60 ====================================================================
61 */
62
63 /*
64 ====================================================================
65 Create item.
66 Return Value: item
67 ====================================================================
68 */
item_create_separator(char * name)69 Item *item_create_separator( char *name )
70 {
71 Item *item = item_create_basic( ITEM_SEPARATOR, name, 0 );
72 return item;
73 }
item_create_range(char * name,char * hint,int * val_int,int min,int max,int step)74 Item *item_create_range( char *name, char *hint, int *val_int, int min, int max, int step )
75 {
76 Item *item = item_create_basic( ITEM_RANGE, name, hint );
77 item->value = value_create_range_int( val_int, min, max, step );
78 return item;
79 }
item_create_switch(char * name,char * hint,int * val_int,char * str_off,char * str_on)80 Item *item_create_switch( char *name, char *hint, int *val_int, char *str_off, char *str_on )
81 {
82 char **names = calloc( 2, sizeof( char* ) );
83 Item *item = item_create_basic( ITEM_SWITCH, name, hint );
84 names[0] = strdup( str_off );
85 names[1] = strdup( str_on );
86 item->value = value_create_range_str( val_int, names, 2 );
87 free( names[0] ); free( names[1] ); free( names );
88 return item;
89 }
item_create_switch_x(char * name,char * hint,int * val_int,char ** strings,int count)90 Item *item_create_switch_x( char *name, char *hint, int *val_int, char **strings, int count )
91 {
92 Item *item = item_create_basic( ITEM_SWITCH_X, name, hint );
93 item->value = value_create_range_str( val_int, strings, count );
94 return item;
95 }
item_create_key(char * name,char * hint,int * val_int,int * filter)96 Item *item_create_key( char *name, char *hint, int *val_int, int *filter )
97 {
98 Item *item = item_create_basic( ITEM_KEY, name, hint );
99 item->value = value_create_key( val_int, filter );
100 return item;
101 }
item_create_edit(char * name,char * hint,char * val_str,int limit)102 Item *item_create_edit( char *name, char *hint, char *val_str, int limit )
103 {
104 Item *item = item_create_basic( ITEM_EDIT, name, hint );
105 item->value = value_create_edit( val_str, limit );
106 return item;
107 }
item_create_link(char * name,char * hint,void * menu)108 Item *item_create_link( char *name, char *hint, void *menu )
109 {
110 Item *item = item_create_basic( ITEM_LINK, name, hint );
111 item->link = menu;
112 return item;
113 }
item_create_action(char * name,char * hint,int item_id)114 Item *item_create_action( char *name, char *hint, int item_id )
115 {
116 Item *item = item_create_basic( ITEM_ACTION, name, hint );
117 item->item_id = item_id;
118 return item;
119 }
120 /*
121 ====================================================================
122 Delete item (void pointer for compatiblity when using with list)
123 ====================================================================
124 */
item_delete(void * pitem)125 void item_delete( void *pitem )
126 {
127 Item *item = (Item*)pitem;
128 if ( !item ) return;
129 if ( item->name ) free( item->name );
130 if ( item->value ) value_delete( item->value );
131 if ( item->hint ) hint_delete( item->hint );
132 free( item );
133 }
134 /*
135 ====================================================================
136 Adjust alignment of name and value strings
137 ====================================================================
138 */
item_adjust(Item * item)139 void item_adjust( Item *item )
140 {
141 /* alignment - name */
142 item->nx = 0; item->ny = item->h / 2; item->nalign = ALIGN_X_LEFT | ALIGN_Y_CENTER;
143 if ( item->type == ITEM_LINK || item->type == ITEM_ACTION || item->type == ITEM_SEPARATOR ) {
144 item->nx = item->w / 2;
145 item->nalign = ALIGN_X_CENTER | ALIGN_Y_CENTER;
146 }
147 item->vx = item->w - 1; item->vy = item->h / 2; item->valign = ALIGN_X_RIGHT | ALIGN_Y_CENTER;
148 /* hint position */
149 if ( item->hint )
150 hint_set_pos( item->hint, item->x + item->w * 0.6, item->y );
151 }
152 /*
153 ====================================================================
154 Hide/Show item
155 ====================================================================
156 */
item_hide(Item * item)157 void item_hide( Item *item )
158 {
159 DEST( sdl.screen, item->x, item->y, item->w, item->h );
160 SOURCE( mbkgnd, item->x, item->y );
161 blit_surf();
162 add_refresh_rect( item->x, item->y, item->w, item->h );
163 }
item_show(Item * item)164 void item_show( Item *item )
165 {
166 Font *font = mfont;
167 /* name */
168 if ( item->font ) font = item->font; /* manual override if special font was set */
169 font->align = item->nalign;
170 mhfont->align = item->nalign;
171 write_text( font, sdl.screen, item->x + item->nx, item->y + item->ny, item->name, item->alpha );
172 if ( item->halpha < 255 )
173 write_text( mhfont, sdl.screen, item->x + item->nx, item->y + item->ny, item->name, item->halpha );
174 /* value string */
175 if ( item->value ) {
176 font->align = item->valign;
177 mhfont->align = item->valign;
178 write_text( font, sdl.screen, item->x + item->vx, item->y + item->vy, item->value->val_str, item->alpha );
179 if ( item->halpha < 255 )
180 write_text( mhfont, sdl.screen, item->x + item->vx, item->y + item->vy, item->value->val_str, item->halpha );
181 }
182 add_refresh_rect( item->x, item->y, item->w, item->h );
183 }
184 /*
185 ====================================================================
186 Update alpha of item
187 ====================================================================
188 */
item_update_alpha(Item * item,int ms)189 void item_update_alpha( Item *item, int ms )
190 {
191 if ( item->highlighted ) {
192 item->halpha = 0;
193 item->alpha = 255;
194 }
195 else {
196 if ( item->halpha < 255 ) {
197 item->halpha += alpha_change * ms;
198 if ( item->halpha > 255 ) item->halpha = 255;
199 }
200 if ( item->alpha > 0 ) {
201 item->alpha -= alpha_change * ms;
202 if ( item->alpha < 0 ) item->alpha = 0;
203 }
204 }
205 }
206 /*
207 ====================================================================
208 Check if position's on item.
209 ====================================================================
210 */
item_focus(Item * item,int x,int y)211 int item_focus( Item *item, int x, int y )
212 {
213 if ( item->type == ITEM_SEPARATOR ) return 0; /* may never be focused */
214 return ( x >= item->x && y >= item->y && x < item->x + item->w && y < item->y + item->h );
215 }
216