File: item.c

package info (click to toggle)
lbreakout2 2.5.2-2.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 6,040 kB
  • ctags: 2,374
  • sloc: ansic: 20,911; sh: 2,891; makefile: 374
file content (241 lines) | stat: -rw-r--r-- 8,434 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/***************************************************************************
                          item.c  -  description
                             -------------------
    begin                : Thu Sep 20 2001
    copyright            : (C) 2001 by Michael Speck
    email                : kulkanie@gmx.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "manager.h"

extern SDL_Surface *stk_display;
extern SDL_Surface *mbkgnd;
extern StkFont *mfont, *mhfont;

/*
====================================================================
Locals
====================================================================
*/

float alpha_change = 0.4;

/*
====================================================================
Create a basic item.
====================================================================
*/
Item *item_create_basic( int type, char *name, char *hint )
{
    Item *item = calloc( 1, sizeof( Item ) );
    /* name */
    item->name = strdup( name );
    /* id */
    item->item_id = -1;
    /* alpha */
    item->halpha = 0;
    item->alpha = 255;
    /* type */
    item->type = type;
    /* hint if  astring was passed */
    if ( hint == 0 )
        item->hint = 0;
    else
        item->hint = hint_create( 0, 0, hint ); /* the position will be set when this item is adjusted in menu adjust */
    /* fonts */
    item->font = mfont;
    item->hfont = mhfont;
    /* background */
    item->bkgnd = mbkgnd;
    return item;
}

/*
====================================================================
Publics
====================================================================
*/

/*
====================================================================
Create item.
Return Value: item
====================================================================
*/
Item *item_create_separator( char *name )
{
    Item *item = item_create_basic( ITEM_SEPARATOR, name, 0 );
    return item;
}
Item *item_create_range(     char *name, char *hint, int  *val_int, int min, int max, int step )
{
    Item *item = item_create_basic( ITEM_RANGE, name, hint );
    item->value = value_create_range_int( val_int, min, max, step );
    return item;
}
Item *item_create_switch(    char *name, char *hint, int  *val_int, char *str_off, char *str_on )
{
    char **names = calloc( 2, sizeof( char* ) );
    Item *item = item_create_basic( ITEM_SWITCH, name, hint );
    names[0] = strdup( str_off );
    names[1] = strdup( str_on );
    item->value = value_create_range_str( val_int, names, 2 );
    free( names[0] ); free( names[1] ); free( names );
    return item;
}
Item *item_create_switch_x(  char *name, char *hint, int  *val_int, char **strings, int count )
{
    Item *item = item_create_basic( ITEM_SWITCH_X, name, hint );
    item->value = value_create_range_str( val_int, strings, count );
    return item;
}
Item *item_create_key(       char *name, char *hint, int  *val_int, int *filter )
{
    Item *item = item_create_basic( ITEM_KEY, name, hint );
    item->value = value_create_key( val_int, filter );
    return item;
}
Item *item_create_edit(      char *name, char *hint, char *val_str, int limit )
{
    Item *item = item_create_basic( ITEM_EDIT, name, hint );
    item->value = value_create_edit( val_str, limit );
    return item;
}
Item *item_create_link(      char *name, char *hint, void *menu )
{
    Item *item = item_create_basic( ITEM_LINK, name, hint );
    item->link = menu;
    return item;
}
Item *item_create_action(    char *name, char *hint, int item_id )
{
    Item *item = item_create_basic( ITEM_ACTION, name, hint );
    item->item_id = item_id;
    return item;
}
/*
====================================================================
Delete item (void pointer for compatiblity when using with list)
====================================================================
*/
void item_delete( void *pitem )
{
    Item *item = (Item*)pitem;
    if ( !item ) return;
    if ( item->name ) free( item->name );
    if ( item->value ) value_delete( item->value );
    if ( item->hint ) hint_delete( item->hint );
    free( item );
}
/*
====================================================================
Adjust alignment of name and value strings
====================================================================
*/
void item_adjust( Item *item )
{
    /* alignment - name */
    item->nx = 0; item->ny = item->h / 2; item->nalign = STK_FONT_ALIGN_LEFT | STK_FONT_ALIGN_CENTER_Y;
    if ( item->type == ITEM_LINK || item->type == ITEM_ACTION || item->type == ITEM_SEPARATOR ) {
        item->nx = item->w / 2;
        item->nalign = STK_FONT_ALIGN_CENTER_X | STK_FONT_ALIGN_CENTER_Y;
    }
    item->vx = item->w - 1; item->vy = item->h / 2; item->valign = STK_FONT_ALIGN_RIGHT | STK_FONT_ALIGN_CENTER_Y;
    /* hint position */
    if ( item->hint )
        hint_set_pos( item->hint, item->x + item->w * 0.6, item->y );
}
/*
====================================================================
Hide/Show item
====================================================================
*/
void item_hide( Item *item )
{
    stk_surface_blit( item->bkgnd, 
        item->x, item->y, item->w, item->h,
        stk_display, item->x, item->y );
    stk_display_store_drect();
}
void item_show( Item *item )
{
    /* name */
    item->font->align = item->nalign;
    item->hfont->align = item->nalign;
    stk_font_write( item->font, stk_display, 
        item->x + item->nx, item->y + item->ny, 
        item->alpha, item->name );
    if ( item->halpha > 0 )
        stk_font_write( item->hfont, stk_display, 
            item->x + item->nx, item->y + item->ny, 
            item->halpha, item->name );
    stk_display_store_drect();
    /* value string */
    if ( item->value ) {
        item->font->align = item->valign;
        item->hfont->align = item->valign;
        /* Is this a selected edit item?  */
        if (item->highlighted && (item->type == ITEM_EDIT))
        {
            write_text_with_cursor( item->font, stk_display, 
                item->x + item->vx, item->y + item->vy, 
                item->value->val_str, item->alpha );
            if ( item->halpha > 0 )
                write_text_with_cursor( item->hfont, stk_display, 
                    item->x + item->vx, item->y + item->vy, 
                    item->value->val_str, item->halpha );
        }
        else
        {
            stk_font_write( item->font, stk_display, 
                item->x + item->vx, item->y + item->vy, 
                item->alpha, item->value->val_str );
            if ( item->halpha > 0 )
                stk_font_write( item->hfont, stk_display, 
                    item->x + item->vx, item->y + item->vy, 
                    item->halpha, item->value->val_str );
        }
        stk_display_store_drect();
    }
}
/*
====================================================================
Update alpha of item
====================================================================
*/
void item_update_alpha( Item *item, int ms )
{
    if ( item->highlighted ) {
        item->halpha = 255;
        item->alpha = 0;
    }
    else {
        if ( item->halpha > 0 ) {
            item->halpha -= alpha_change * ms;
            if ( item->halpha < 0 ) item->halpha = 0;
        }
        if ( item->alpha < 255 ) {
            item->alpha += alpha_change * ms;
            if ( item->alpha > 255 ) item->alpha = 255;
        }
    }
}
/*
====================================================================
Check if position's on item.
====================================================================
*/
int item_focus( Item *item, int x, int y )
{
    if ( item->type == ITEM_SEPARATOR ) return 0; /* may never be focused */
    return ( x >= item->x && y >= item->y && x < item->x + item->w && y < item->y + item->h );
}