Tscope5
mouse.c
Go to the documentation of this file.
1 
2 
3 
4 
5 
6 
7 #include "../include/tscope5/mouse.h"
8 #include "../include/tscope5/timer.h"
9 #include "../include/tscope5/graphics.h"
10 #include "../include/tscope5/display.h"
11 #include "../include/tscope5/primitives.h"
12 #include "../include/tscope5/mouse_internal.h"
13 #include "../include/tscope5/system_internal.h"
14 #include "../include/tscope5/graphics_internal.h"
15 
16 
19 
20 
21 
22 
27 {
28  ts5_check_mouse("ts5_show_mouse");
29  ts5_log(TS5_LOGLEVEL_5, "ts5_show_mouse()\n");
30 
31  al_show_mouse_cursor(_ts5_status.display[_ts5_status.active_display]);
32 }
33 
34 
39 {
40  ts5_check_mouse("ts5_hide_mouse");
41  ts5_log(TS5_LOGLEVEL_5, "ts5_hide_mouse()\n");
42 
43  al_hide_mouse_cursor(_ts5_status.display[_ts5_status.active_display]);
44 }
45 
46 
53 void ts5_set_mouse_position(double x, double y)
54 {
55  ts5_check_mouse("ts5_set_mouse_position");
56  ts5_log(TS5_LOGLEVEL_5, "ts5_set_mouse_position(%f,%f)\n", x, y);
57 
58  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
61  }
62 
63  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
66  }
67 
68  al_set_mouse_xy(_ts5_status.display[_ts5_status.active_display], (int)x, (int)y);
69 }
70 
79 void ts5_get_mouse_position(double *x, double *y)
80 {
81  ts5_check_mouse("ts5_get_mouse_position");
82  ts5_log(TS5_LOGLEVEL_6, "ts5_get_mouse_position(%p,%p)\n", x, y);
83 
84  ALLEGRO_MOUSE_STATE status;
85  al_get_mouse_state(&status);
86 
87  double xx, yy;
88 
89  xx = al_get_mouse_state_axis(&status, 0);
90  yy = al_get_mouse_state_axis(&status, 1);
91 
92  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
95  }
96 
97  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
100  }
101 
102  if (x) {
103  *x = xx;
104  }
105 
106  if (y) {
107  *y = yy;
108  }
109 }
110 
111 
118 {
119  ts5_check_mouse("ts5_get_mouse_x");
120  ts5_log(TS5_LOGLEVEL_6, "ts5_get_mouse_x()\n");
121 
122  double x;
123  ts5_get_mouse_position(&x, NULL);
124 
125  return x;
126 }
127 
128 
135 {
136  ts5_check_mouse("ts5_get_mouse_y");
137  ts5_log(TS5_LOGLEVEL_6, "ts5_get_mouse_y()\n");
138 
139  double y;
140  ts5_get_mouse_position(NULL, &y);
141 
142  return y;
143 }
144 
145 
155 void ts5_draw_mouse_button(double x, double y)
156 {
157  ts5_check_mouse("ts5_draw_mouse_button");
158  ts5_log(TS5_LOGLEVEL_5, "ts5_draw_mouse_button(%f,%f)\n", x, y);
159 
160  // always write to the display
161  ts5_set_active_display(_ts5_status.active_display+1);
162  TS5_BITMAP *oldmap = ts5_set_drawing_target(_ts5_status.display_buffer[_ts5_status.active_display]);
163 
164  TS5_COLOR oldcolor = ts5_set_foreground_color(ts5_make_named_color("chartreuse", 1.0));
165  double oldthick = ts5_set_drawing_thickness(4.0);
166  int oldfill = ts5_set_fill_mode(TS5_FILL_OFF);
167 
168  double w_abs_disp = 40, h_abs_disp = 40;
169  double x1_abs_disp, x2_abs_disp, y1_abs_disp, y2_abs_disp;
170 
171  // covert to absolute scale
172  if (_ts5_status.graphics_status.coordinate_scale == TS5_RELATIVE_COORDINATES) {
173  x1_abs_disp = ts5_relative_to_absolute_coordinate_x(x);
174  y1_abs_disp = ts5_relative_to_absolute_coordinate_y(y);
175  }
176  else {
177  x1_abs_disp = x;
178  y1_abs_disp = y;
179  }
180 
181  // convert to display coordinates.
182  // for cartesian display coordinates x and y are the center
183  // for display coordinates x and y are
184  if (_ts5_status.graphics_status.coordinate_system == TS5_CARTESIAN_COORDINATES) {
185  x1_abs_disp = ts5_cartesian_to_display_coordinate_x(x1_abs_disp);
186  y1_abs_disp = ts5_cartesian_to_display_coordinate_y(y1_abs_disp);
187 
188  x1_abs_disp -= w_abs_disp/2;
189  y1_abs_disp -= h_abs_disp/2;
190  }
191 
192  x2_abs_disp = x1_abs_disp + w_abs_disp;
193  y2_abs_disp = y1_abs_disp + h_abs_disp;
194 
195  int oldsystem = ts5_set_coordinate_system(TS5_DISPLAY_COORDINATES);
196  int oldscale = ts5_set_coordinate_scale(TS5_ABSOLUTE_COORDINATES);
197 
198  // check if the button will be on the screen
199  double dispw = _ts5_status.display_status[_ts5_status.active_display].w;
200  double disph = _ts5_status.display_status[_ts5_status.active_display].h;
201 
202  if (x1_abs_disp < 0.0 || x2_abs_disp > dispw || y1_abs_disp < 0.0 || y2_abs_disp > disph) {
203  ts5_fatal("ts5_draw_mouse_button: button will be off screen (%f,%f)\n", x1_abs_disp, y1_abs_disp);
204  }
205 
206  ts5_draw_rounded_rectangle(x1_abs_disp, y1_abs_disp, x2_abs_disp, y2_abs_disp, 3.0, 3.0);
207  ts5_set_fill_mode(TS5_FILL_ON);
208  ts5_draw_triangle(x2_abs_disp - 5.0, (y1_abs_disp+y2_abs_disp)/2.0,
209  x1_abs_disp+5.0, y1_abs_disp+5.0, x1_abs_disp+5.0, y2_abs_disp-5.0);
211  ts5_show_mouse();
212 
213  ALLEGRO_MOUSE_STATE status;
214  int down;
215  int ok = 0;
216  do {
217  al_rest(0.01);
218  double xp, yp;
219  ts5_get_mouse_position(&xp, &yp);
220  al_get_mouse_state(&status);
221  down = al_mouse_button_down(&status, 1);
222  if (down && xp > x1_abs_disp && xp < x2_abs_disp && yp > y1_abs_disp && yp < y2_abs_disp) {
223  ok = 1;
224  }
225  } while (!ok);
226 
228  ts5_draw_triangle(x2_abs_disp - 5, (y1_abs_disp+y2_abs_disp)/2,
229  x1_abs_disp+5, y1_abs_disp+5, x1_abs_disp+5, y2_abs_disp-5);
231 
232  ok = 0;
233  do {
234  al_rest(0.01);
235  al_get_mouse_state(&status);
236  down = al_mouse_button_down(&status, 1);
237  if (!down) {
238  ok = 1;
239  }
240  } while (!ok);
241 
243  ts5_draw_triangle(x2_abs_disp - 5, (y1_abs_disp+y2_abs_disp)/2,
244  x1_abs_disp+5, y1_abs_disp+5, x1_abs_disp+5, y2_abs_disp-5);
246 
247  ts5_set_drawing_target(oldmap);
248  ts5_set_foreground_color(oldcolor);
249  ts5_set_drawing_thickness(oldthick);
250  ts5_set_fill_mode(oldfill);
251  ts5_set_coordinate_system(oldsystem);
252  ts5_set_coordinate_scale(oldscale);
253 }
254 
255 
257 
258 
259 
260 
265 
266 
267 
268 
280 {
281  ts5_check_mouse("ts5_define_mouse_key");
282  ts5_log(TS5_LOGLEVEL_3, "ts5_define_mouse_key(%d)\n", key);
283 
284  if (key==0) {
285  ts5_fatal("ts5_define_mouse_key: key argument is 0, response keys are numbered from 1\n");
286  }
287 
288  if (abs(key)>_ts5_status.timer_status.mouse.num_buttons) {
289  ts5_fatal("ts5_define_mouse_key: key argument is %d, number of mouse buttons is %d\n",
290  key, _ts5_status.timer_status.mouse.num_buttons);
291  }
292 
293  if (key>0 && _ts5_status.timer_status.mouse.button_press_defined[key-1]!=0) {
294  ts5_fatal("ts5_define_mouse_key: key press %d is already defined\n", key);
295  }
296 
297  if (key<0 && _ts5_status.timer_status.mouse.button_release_defined[-key-1]!=0) {
298  ts5_fatal("ts5_define_mouse_key: key release %d is already defined\n", key);
299  }
300 
301  _ts5_status.timer_status.mouse_is_response_device = 1;
302  _ts5_status.timer_status.num_defined_buttons++;
303  _ts5_status.timer_status.num_active_buttons++;
304  _ts5_status.timer_status.mouse.num_defined_buttons++;
305  _ts5_status.timer_status.mouse.num_active_buttons++;
306 
307  if (key>0) {
308  _ts5_status.timer_status.mouse.button_press_defined[key-1] =
309  _ts5_status.timer_status.num_defined_buttons;
310 
311  _ts5_status.timer_status.mouse.button_press_active[key-1] =
312  _ts5_status.timer_status.num_defined_buttons;
313  }
314  else {
315  _ts5_status.timer_status.mouse.button_release_defined[-key-1] =
316  _ts5_status.timer_status.num_defined_buttons;
317 
318  _ts5_status.timer_status.mouse.button_release_active[-key-1] =
319  _ts5_status.timer_status.num_defined_buttons;
320  }
321 
322  return _ts5_status.timer_status.num_defined_buttons;
323 }
324 
325 
327 
328