File: mouse.c

package info (click to toggle)
mlv 3.1.0-8
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 10,596 kB
  • sloc: ansic: 18,982; sh: 4,760; makefile: 381; objc: 246; xml: 92
file content (150 lines) | stat: -rw-r--r-- 3,508 bytes parent folder | download | duplicates (3)
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
/*
 *   This file is part of the MLV Library.
 *
 *   Copyright (C) 2010,2011,2012 Adrien Boussicault, Marc Zipstein
 *
 *
 *    This Library is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This Library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this Library.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MLV_mouse.h"
#include "MLV_event.h"
#include "MLV_time.h"

#ifndef MEMORY_DEBUG
#include <SDL/SDL.h>
#else
#include "memory_debug.h"
#endif

#include "warning_error.h"

#include "memory_management.h"

void MLV_wait_mouse(int *x, int *y){
	MLV_Button_state state;
	MLV_Mouse_button mouse_button;

	// We remove all existing event from the queue
	MLV_flush_event_queue();

	//We wait for a new mouse event
	while(
		(
			MLV_wait_event(
					NULL, NULL, NULL,
					NULL, NULL,
					x, y, &mouse_button,
					&state
			) != MLV_MOUSE_BUTTON
		) ||
		( mouse_button != MLV_BUTTON_LEFT ) ||
		( state != MLV_PRESSED )
	);
};


MLV_Event MLV_wait_mouse_or_milliseconds(int *x, int *y, int milliseconds){
	MLV_Event resultat = MLV_NONE;
	MLV_Button_state state;
	MLV_Mouse_button mouse_button;

	// We remove all existing event from the queue
	MLV_flush_event_queue();

	int time = MLV_get_time();

	//We wait for a new mouse event
	int tmp_x, tmp_y;
	while(
		(	
			(
				(
					resultat = MLV_wait_event_or_milliseconds(
						NULL, NULL, NULL,
						NULL, NULL,
						&tmp_x, &tmp_y, &mouse_button,
						&state,
						milliseconds - ( MLV_get_time() - time )
					)
				) != MLV_MOUSE_BUTTON
			) ||
			( mouse_button != MLV_BUTTON_LEFT ) ||
			( state != MLV_PRESSED )
		) && (
			(MLV_get_time() - time) < milliseconds
		)
	);
	if( 
		resultat == MLV_MOUSE_BUTTON &&
		mouse_button == MLV_BUTTON_LEFT &&
		state == MLV_PRESSED
	){
		if( x ) *x = tmp_x;
		if( y ) *y = tmp_y;
	}else{
		resultat = MLV_NONE;
	}
	return resultat;
}


MLV_Event MLV_wait_mouse_or_seconds(int *x, int *y, int seconds){
	return MLV_wait_mouse_or_milliseconds(
		x, y, seconds*1000
	);
}

const char* MLV_convert_mouse_button_to_string( MLV_Mouse_button button_code ){
	switch( button_code ){
		case MLV_BUTTON_LEFT:
			return "MLV_BUTTON_LEFT";
		case MLV_BUTTON_MIDDLE:
			return "MLV_BUTTON_MIDDLE";
		case MLV_BUTTON_RIGHT:
			return "MLV_BUTTON_RIGHT";
		default:
			ERROR("Button code unexpected");
	}
	return NULL;
}

MLV_Mouse_button MLV_convert_string_to_mouse_button( const char* button_string ){
	if( strcmp( button_string, "MLV_BUTTON_LEFT" )==0 ){
		return MLV_BUTTON_LEFT;
	}
	if( strcmp( button_string, "MLV_BUTTON_MIDDLE" )==0 ){
		return MLV_BUTTON_MIDDLE;
	}
	if( strcmp( button_string, "MLV_BUTTON_RIGHT" )==0 ){
		return MLV_BUTTON_RIGHT;
	}
	ERROR("Button name unexpected");
	return -1;
}

void MLV_get_mouse_position( int* x, int* y ){
	SDL_PumpEvents();
	SDL_GetMouseState(x,y);
}

MLV_Button_state MLV_get_mouse_button_state( MLV_Mouse_button mouse_button ){
	SDL_PumpEvents();
	if( SDL_GetMouseState(NULL,NULL) & SDL_BUTTON( mouse_button ) ){
		return MLV_PRESSED;
	}
	return MLV_RELEASED;
}