-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.c
More file actions
167 lines (144 loc) · 4.08 KB
/
Copy pathinput.c
File metadata and controls
167 lines (144 loc) · 4.08 KB
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
/* Base graphics library for coursework
*
* Input
*/
#include <stdio.h> /* fprintf stderr */
#include <stdlib.h> /* malloc */
#include <stddef.h> /* NULL */
#include <GLUT/glut.h>
/* Though the OpenGL APIs used in this program is abandoned and outdated, I have
* to use it still because I don't have plenty of time to improve my coursework and
* use new version APIs.
*/
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include "point.h"
static int
last_menu_value=0;
struct menu_item {
int value;
void (*callback)();
struct menu_item * next;
};
static struct menu_item * menu_item_list_head=NULL;
/* add menu item to menu item list for searching when click menu */
static void
menu_item_list_push(
int value,
void (*callback)()) {
struct menu_item * ret=(struct menu_item *)malloc(sizeof(struct menu_item));
ret->value = value;
ret->callback = callback;
ret->next = menu_item_list_head;
menu_item_list_head = ret;
return;
}
/* Call the callback by value */
static void
menu_item_list_callback_by_value(int value) {
struct menu_item * p=menu_item_list_head;
while (NULL != p) {
fprintf(stderr, "p.callback=%p next=%p\n", p->callback, p->next);
if (p->value != value) {
p = p->next;
}
else {
(p->callback)();
break;
}
}
return;
}
/* mouse events bindings */
void (*press_callback)(struct bg_point *),
(*release_callback)(struct bg_point *),
(*move_callback)(struct bg_point *),
(*drag_callback)(struct bg_point *);
/* keyboard events bindings */
void (*kb_callback[256])()={NULL};
/* public functions */
void bg_menu_callback(int value) {
menu_item_list_callback_by_value(value);
}
void bg_menu_bind(
char * title,
void (*callback)()) {
glutAddMenuEntry(title, last_menu_value);
menu_item_list_push(last_menu_value, callback);
last_menu_value += 1;
return;
}
void bg_mouse_plot_callback(
GLint button,
GLint action,
GLint xMouse,
GLint yMouse) {
if (button == GLUT_LEFT_BUTTON) {
if (action == GLUT_DOWN) {
if (NULL != press_callback) {
struct bg_point * tmp_point=
bg_point_make((unsigned int)xMouse, 400-(unsigned int)yMouse);
press_callback(tmp_point);
free(tmp_point); tmp_point = NULL;
}
}
else if (action == GLUT_UP) {
if (NULL != release_callback) {
struct bg_point * tmp_point=
bg_point_make((unsigned int)xMouse, 400-(unsigned int)yMouse);
release_callback(tmp_point);
free(tmp_point); tmp_point = NULL;
}
}
}
return;
}
void bg_mouse_move_callback(
GLint xMouse,
GLint yMouse) {
if (NULL != move_callback) {
struct bg_point * tmp_point=
bg_point_make((unsigned int)xMouse, 400-(unsigned int)yMouse);
move_callback(tmp_point);
free(tmp_point); tmp_point = NULL;
}
return;
}
void bg_mouse_drag_callback(
GLint xMouse,
GLint yMouse) {
if (NULL != drag_callback) {
struct bg_point * tmp_point=
bg_point_make((unsigned int)xMouse, 400-(unsigned int)yMouse);
drag_callback(tmp_point);
free(tmp_point); tmp_point = NULL;
}
return;
}
void bg_mouse_press_bind(void (*callback)(struct bg_point *)) {
press_callback = callback;
return;
}
void bg_mouse_release_bind(void (*callback)(struct bg_point *)) {
release_callback = callback;
return;
}
void bg_mouse_move_bind(void (*callback)(struct bg_point *)) {
move_callback = callback;
return;
}
void bg_mouse_drag_bind(void (*callback)(struct bg_point *)) {
drag_callback = callback;
return;
}
void bg_keyboard_callback(unsigned char key, int x, int y) {
if (NULL != kb_callback[key]) {
kb_callback[key]();
}
return;
}
void bg_keyboard_bind(
unsigned char key,
void (*callback)()) {
kb_callback[key] = callback;
return;
}