forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snis_button.c
98 lines (88 loc) · 2.46 KB
/
snis_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
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include "snis_font.h"
#include "snis_typeface.h"
#include "snis_graph.h"
#define DEFINE_BUTTON_GLOBALS
#include "snis_button.h"
#undef DEFINE_BUTTON_GLOBALS
struct button {
int x, y, width, height;
char label[20];
int color;
int font;
int *checkbox_value;
button_function bf;
void *cookie;
unsigned char button_press_feedback_counter;
};
void snis_button_set_label(struct button *b, char *label)
{
strncpy(b->label, label, sizeof(b->label) - 1);
}
struct button *snis_button_init(int x, int y, int width, int height,
char *label, int color, int font, button_function bf,
void *cookie)
{
struct button *b;
b = malloc(sizeof(*b));
b->x = x;
b->y = y;
b->width = width;
b->height = height;
snis_button_set_label(b, label);
b->label[sizeof(b->label) - 1] = '\0';
b->color = color;
b->font = font;
b->bf = bf;
b->cookie = cookie;
b->checkbox_value = NULL;
b->button_press_feedback_counter = 0;
return b;
}
void snis_button_draw(struct button *b)
{
sng_set_foreground(b->color);
sng_current_draw_rectangle(0, b->x, b->y, b->width, b->height);
if (b->button_press_feedback_counter)
sng_current_draw_rectangle(0, b->x + 1, b->y + 1,
b->width - 2, b->height - 2);
if (!b->checkbox_value) {
sng_abs_xy_draw_string(b->label, b->font, b->x + 10, b->y + b->height / 1.7);
if (b->button_press_feedback_counter)
sng_abs_xy_draw_string(b->label, b->font, b->x + 11, b->y + b->height / 1.7 + 1);
} else {
sng_current_draw_rectangle(0, b->x + 5, b->y + 2, 15, 15);
if (*b->checkbox_value) {
sng_current_draw_line(b->x + 5, b->y + 2, b->x + 5 + 15, b->y + 2 + 15);
sng_current_draw_line(b->x + 5, b->y + 2 + 15, b->x + 5 + 15, b->y + 2);
}
sng_abs_xy_draw_string(b->label, b->font, b->x + 30, b->y + b->height / 1.7);
if (b->button_press_feedback_counter)
sng_abs_xy_draw_string(b->label, b->font,
b->x + 31, b->y + 1 + b->height / 1.7);
}
if (b->button_press_feedback_counter)
b->button_press_feedback_counter--;
}
int snis_button_button_press(struct button *b, int x, int y)
{
if (x < b->x || x > b->x + b->width ||
y < b->y || y > b->y + b->height)
return 0;
if (b->bf)
b->bf(b->cookie);
if (b->checkbox_value)
*b->checkbox_value = !*b->checkbox_value;
b->button_press_feedback_counter = 5;
return 1;
}
void snis_button_set_color(struct button *b, int color)
{
b->color = color;
}
void snis_button_checkbox(struct button *b, int *value)
{
b->checkbox_value = value;
}