-
Notifications
You must be signed in to change notification settings - Fork 3
/
button.c
128 lines (114 loc) · 2.84 KB
/
button.c
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
/********************************************************
* @author Airead Fan <[email protected]> *
* @date 2011 9月 19 21:26:11 CST *
********************************************************
* after studying C 63 days *
* after studying APUE 28 days *
********************************************************/
#include <SDL/SDL.h>
#include <stdbool.h>
#include "button.h"
/*
* Init a button, set it's (x,y), width and height;
* set it's mouse state image
*
*/
Button *button_init(Button *bt, int x, int y, int w, int h)
{
int i;
/* Set the button's attributes */
bt->box.x = x;
bt->box.y = y;
bt->box.w = w;
bt->box.h = h;
/* Set the button's downing or not */
bt->downing = false;
/* Set the button's current stat */
bt->cur_stat = BUTTON_MOUSEOUT;
/* Set the default img_stat */
for(i = 0; i < BUTTON_MOUSESTAT; i++){
bt->img_mouse_stat[i] = NULL;
}
return bt;
}
/*
* set a image to a button mouse state
*/
Button *button_set_stat_img(Button *bt, int mouse_stat, SDL_Surface *img)
{
bt->img_mouse_stat[mouse_stat] = img;
return bt;
}
/*
* get button current stat
*/
int button_get_stat(Button *bt)
{
return bt->cur_stat;
}
/*
* Button handle event
* The buuton have four state image
* 1.BUTTON_MOUSEOVER
* 2.BUTTON_MOUSEOUT
* 3.BUTTON_MOUSEDOWN
* 4.BUTTON_MOUSEUP
* set button current stat according to event
*/
void button_handle_event(Button *bt, SDL_Event *event)
{
/* The mouse offsets */
int x = 0, y = 0;
bt->cur_stat = 0;
/* If the mouse moved */
if(event->type == SDL_MOUSEMOTION){
/* Get the mouse offsets */
x = event->motion.x;
y = event->motion.y;
/* If the mouse is over the button */
if( (x > bt->box.x) && (x < bt->box.x + bt->box.w) && (y > bt->box.y) && ( y < bt->box.y + bt->box.h) ){
/* Set the button sprite */
bt->cur_stat |= MOUSEOVER;
}else{
bt->cur_stat |= MOUSEOUT;
}
}
if(event->type == SDL_MOUSEBUTTONDOWN){
if(event->button.button == SDL_BUTTON_LEFT){
x = event->motion.x;
y = event->motion.y;
if( (x > bt->box.x) && (x < bt->box.x + bt->box.w) && (y > bt->box.y) && ( y < bt->box.y + bt->box.h) ){
/* Set the button sprite */
bt->cur_stat |= MOUSEDOWN;
bt->downing = true;
}
}
}
if(event->type == SDL_MOUSEBUTTONUP){
if(event->button.button == SDL_BUTTON_LEFT){
x = event->motion.x;
y = event->motion.y;
if( (x > bt->box.x) && (x < bt->box.x + bt->box.w) && (y > bt->box.y) && ( y < bt->box.y + bt->box.h) ){
/* Set the button sprite */
bt->cur_stat |= MOUSEUP;
if(bt->downing = true){
bt->downing = false;
bt->cur_stat |= MOUSECLICK;
}
}
}
}
}
/*
* simple put button to screen
*/
void buttonShow(Button *bt, SDL_Surface *screen)
{
int i;
/* Mouse over */
if(bt->cur_stat & (1 << 1)){
i = 1;
}
/* Mouse out */
SDL_BlitSurface(bt->img_mouse_stat[i], NULL, screen, &bt->box);
}