File: menubuttons.c

package info (click to toggle)
bomberclone 0.11.9-7.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 11,228 kB
  • sloc: ansic: 14,256; sh: 3,800; makefile: 336
file content (104 lines) | stat: -rw-r--r-- 4,103 bytes parent folder | download | duplicates (2)
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
/* $Id: menubuttons.c,v 1.2 2004-05-20 16:55:30 stpohle Exp $
 * Menuhandling: buttons */

#include "basic.h"
#include "bomberclone.h"
#include "menu.h"
#include "menugui.h"


/* create a button only in the menudatas.. darf all this only when menu_loop
 * is called */
_menuitem *menu_create_button (_menu *menu, char *name, int x, int y, int w, int id) {
	_menuitem *m = menuitem_findfree (menu);
	if (m == NULL) return NULL;

	m->type = MENU_button;
	m->pos.w = (1 + (int)((w - menubuttonimages[0][0]->w - menubuttonimages[0][2]->w) / menubuttonimages[0][1]->w)) * menubuttonimages[0][1]->w + menubuttonimages[0][0]->w + menubuttonimages[0][2]->w;	
	if (x != -1)
		m->pos.x = x;
	else 
		m->pos.x = (menu->oldscreenpos.w - 2 * menuimages[0]->w - m->pos.w) / 2;
	m->pos.y = y;
	m->state = 0;
	m->id = id;
	strncpy (m->label, name, MENU_TITLELEN);

	return m;
};


/* draw the menuitem button or bool
 * menuitem->pos.[x|y|w] - Position and X-Size inside the menu
 *           label       - Text of the Button/Bool
 */
void menu_draw_button (_menuitem *mi) {
	int px, py, i;
	SDL_Rect dest;

	if (mi->type != MENU_button && mi->type != MENU_bool)
		return;

	dest.x = mi->pos.x;
	dest.y = mi->pos.y;
	dest.w = mi->pos.w;
	dest.h = menubuttonimages[0][0]->h;
	menu_draw_background ((_menu *)mi->menu, &dest);

	/* check the focus of the button */
	if (((_menu *)mi->menu)->focusvis && mi == ((_menu *)mi->menu)->focus)
		mi->state = 1;
	else if (mi->type == MENU_bool && (*((int*) mi->ptrdata)) > 0)
		mi->state = 2; // bool
	else 
		mi->state = 0; // button or bool == FALSE

	// draw the left side of the button
	dest.x = MENUOFFSET_X(((_menu *)mi->menu)) + mi->pos.x;
	dest.y = MENUOFFSET_Y(((_menu *)mi->menu)) + mi->pos.y;
	dest.w = menubuttonimages[mi->state][0]->w;
	dest.h = menubuttonimages[mi->state][0]->h;
	gfx_blit (menubuttonimages[mi->state][0], NULL, gfx.screen, &dest, 10000);
	// draw the center of the button
	for (i = 0; i < ((mi->pos.w - (menubuttonimages[mi->state][0]->w + menubuttonimages[mi->state][2]->w)) / menubuttonimages[mi->state][1]->w); i++) {
		dest.x = MENUOFFSET_X(((_menu *)mi->menu)) + mi->pos.x + menubuttonimages[mi->state][0]->w + (i * menubuttonimages[mi->state][1]->w);
		dest.y = MENUOFFSET_Y(((_menu *)mi->menu)) + mi->pos.y;
		dest.w = menubuttonimages[mi->state][1]->w;
		dest.h = menubuttonimages[mi->state][1]->h;
		gfx_blit (menubuttonimages[mi->state][1], NULL, gfx.screen, &dest, 10000);
	}
	// draw the right side of the button
	dest.x = MENUOFFSET_X(((_menu *)mi->menu)) + mi->pos.x + mi->pos.w - menubuttonimages[mi->state][2]->w;
	dest.y = MENUOFFSET_Y(((_menu *)mi->menu)) + mi->pos.y;
	dest.w = menubuttonimages[mi->state][2]->w;
	dest.h = menubuttonimages[mi->state][2]->h;
	gfx_blit (menubuttonimages[mi->state][2], NULL, gfx.screen, &dest, 10000);

	// calculate the center of the button
	px = (mi->pos.w - (strlen (mi->label) * font[MENU_BUTTON_FONTSIZE].size.x)) / 2 + mi->pos.x;
	py = (menubuttonimages[mi->state][0]->h - font[MENU_BUTTON_FONTSIZE].size.y) / 2 + mi->pos.y;

	if (mi->type == MENU_bool && mi->state == 2) // disabled bool == FALSE
		font_gfxdraw (MENUOFFSET_X(((_menu *)mi->menu)) + px, MENUOFFSET_Y(((_menu *)mi->menu)) + py, mi->label, MENU_BUTTON_FONTSIZE, COLOR_black, 10000);
	else
		font_gfxdraw (MENUOFFSET_X(((_menu *)mi->menu)) + px, MENUOFFSET_Y(((_menu *)mi->menu)) + py, mi->label, MENU_BUTTON_FONTSIZE, COLOR_yellow, 10000);

};


/* handle the event on the button
 * Return: 1 - if the button was pressed (With Enter) */
int menu_event_button (_menuitem *mi, SDL_Event *event) {
	switch (event->type) {
		case (SDL_KEYDOWN): /* key was pressed */
			if (event->key.keysym.sym == SDLK_LEFT || event->key.keysym.sym == SDLK_UP) 
				menu_focus_prev ((_menu *)mi->menu);
			else if (event->key.keysym.sym == SDLK_RIGHT || event->key.keysym.sym == SDLK_DOWN) 
				menu_focus_next ((_menu *)mi->menu);
			else if (event->key.keysym.sym == SDLK_RETURN || event->key.keysym.sym == SDLK_KP_ENTER || event->key.keysym.sym == SDLK_LCTRL || event->key.keysym.sym == SDLK_RCTRL)
				return 1;
			break;
	}
	
	return 0;
};