Commits

Anonymous committed 5664af2

- Changed main menu selection sections from 33/33/33 to 25/50/25.
- Some work on an initial gesture detection system. It is too unreliable
to be useful.

Comments (0)

Files changed (1)

 #include <SDL.h>
 #include <SDL_gesture.h>
 
-#ifndef NDEBUG
+//#ifndef NDEBUG
 #include <android/log.h>
-#endif
+//#endif
 #include <math.h>
 #include <time.h>
 
+
+#define LENGTH2I(X0,Y0, X1,Y1) sqrt(pow(X1-X0,2)+pow(Y1-Y0,2))
+#define LENGTH2F(X0,Y0, X1,Y1) sqrtf(powf(X1-X0,2.0f)+powf(Y1-Y0,2.0f))
+
+#define DOT_PRODUCT_2F(x0,y0, x1,y1) (x0*x1+y0*y1)
+#define ANGLE_BETWEEN_2F(x0,y0, x1,y1) acosf(DOT_PRODUCT_2F(x0,y0,x1,y1)/(sqrtf(x0*x0+y0*y0)*sqrtf(x1*x1+y1*y1)))
+
+
 #define APPFLAG_HAVE_TOUCHID (1 << 0)
 #define APPFLAG_QUITTING     (1 << 1)
 
+#define MAX_TOUCH_COORDS 400
 #define MAX_GESTURE_COORDS 200
 #define MAX_GESTURES 20
 #define MAX_FINGERS 3
 #endif
 
 #define SIZEOF_GESTURE(coord_count) (sizeof(struct _gesture) + sizeof(struct _gesture_coord) * (coord_count - 1))
+#define SIZEOF_TOUCH_COORDS(coord_count) (sizeof(struct _touch_coords) + sizeof(struct _touch_coord) * (coord_count - 1))
 
 #define GESTURE_ERROR_RECORDING_OVERFLOW (1 << 0)
 #define GESTURE_ERROR_RECORDING_DENIED (1 << 1)
 
+/* SDL touch screen coords. */
+
+struct _touch_coord {
+	Sint16 x, y;
+};
+
+struct _touch_coords {
+	unsigned short coord_count;
+	struct _touch_coord coords[1];
+};
+
+/* TCOD console coords. */
+
 struct _gesture_coord {
 	unsigned char finger_index;
 	unsigned short cx, cy;
 				/* Only one coord for the finger, just plot it. */
 				if (used_finger_coord_count == 1) {
 					int cx = gesture->coords[coord_index].cx, cy = gesture->coords[coord_index].cy;
-					TCOD_console_set_char_background(NULL, cx, cy, color, TCOD_BKGND_SET);
-					TCOD_console_set_char(NULL, cx, cy, '0'+finger_index);
+					TCOD_console_put_char_ex(NULL, cx, cy, '0'+finger_index, TCOD_white, color);
 					break;
 				}
 				/* Otherwise from the second coord on, draw lines to connect to the preceding one. */
 					int cx = gesture->coords[last_finger_coord_index].cx, cy = gesture->coords[last_finger_coord_index].cy;
 					TCOD_line_init(cx, cy, gesture->coords[coord_index].cx, gesture->coords[coord_index].cy);
 					do {
-						TCOD_console_set_char_background(NULL, cx, cy, color, TCOD_BKGND_SET);
-						TCOD_console_set_char(NULL, cx, cy, '0'+finger_index);
+						TCOD_console_put_char_ex(NULL, cx, cy, '0'+finger_index, TCOD_white, color);
 					} while (!TCOD_line_step(&cx, &cy));
 				}
 
 void show_coregestures_test_menu() {
 	SDL_Event event;
 	struct _gesture *temp_gesture = alloca(SIZEOF_GESTURE(MAX_GESTURE_COORDS));
+	struct _touch_coords *temp_touch_coords[MAX_FINGERS];
 	int frame_cx = 0,
 		frame_cy = 0,
 		frame_cw = console_width,
 		frame_ch = console_height - Y_LOWER_GAP;
 	int fingers_in_use, long_press;
-	double direct_distance, path_distance;
-	float dx_sum, dy_sum;
 	int clocks_start, clocks_end;
-	unsigned short cx0, cy0, cxN, cyN;
-	Sint16 x0, y0, xN, yN;
-	float dx_average, dy_average;
+	float x0, y0, xN, yN, dx1, dy1, dxN, dyN, angleM, angleM_b_length, xM, yM, xM_xN_last, yM_yN_last, angleM_last;
+	float angleM_from_distance;
+	int d_count, i;
 
+	for (i = 0; i < MAX_FINGERS; i++)
+		temp_touch_coords[i] = alloca(SIZEOF_TOUCH_COORDS(MAX_TOUCH_COORDS));
 	INIT_GESTURE(temp_gesture);
 
 	fingers_in_use = long_press = 0;
-	dx_sum = dy_sum = 0.0f;
 	clocks_start = clocks_end = 0;
 	do {
-		int cx = 0, cy = 0;
 		int num_events_polled = 0;
 
 		while (num_events_polled++ < MAX_EVENTS_POLLED_PER_FRAME && SDL_PollEvent(&event)) {
 			switch (event.type) {
+			case SDL_FINGERMOTION:
+			case SDL_FINGERUP:
 			case SDL_FINGERDOWN:
 				if (event.tfinger.touchId == touch_id) {
-					if (fingers_in_use == 0) {
-						SDL_Touch *touch = SDL_GetTouch(event.tfinger.touchId);
-						cx0 = cxN = (event.tfinger.x * console_width) / touch->xres;
-						cy0 = cxN = (event.tfinger.y * console_height) / touch->yres;
+					SDL_Touch *touch = SDL_GetTouch(event.tfinger.touchId);
+					if (event.type == SDL_FINGERDOWN) {
+						if (fingers_in_use == 0) {
+							x0 = xN = xM = (float)event.tfinger.x / touch->xres;
+							y0 = yN = yM = (float)event.tfinger.y / touch->yres;
+							angleM = angleM_last = angleM_b_length = xM_xN_last = yM_yN_last = angleM_from_distance = 0.0f;
+							long_press = 0;
+							clocks_start = clock();
+							INIT_GESTURE(temp_gesture);
 
-						long_press = 0;
-						direct_distance = path_distance = 0.0;
-						dx_sum = dy_sum = 0.0f;
-						clocks_start = clock();
-						INIT_GESTURE(temp_gesture);
+							/*dx_sum = dy_sum = 0.0f;*/
+							d_count = 0;
+						}
+						fingers_in_use += 1;
+					}
+					if (fingers_in_use > 0) {
+						float xN_last = xN, yN_last = yN, magnitude;
+						xN = (float)event.tfinger.x / touch->xres;
+						yN = (float)event.tfinger.y / touch->yres;
+						magnitude = LENGTH2F(xN_last,yN_last, xN,yN);
+						angleM_from_distance += magnitude;
+						if (magnitude > 1e-3f) {
+							d_count += 1;
+							dxN = xN - xN_last;
+							dyN = yN - yN_last;
+							if (d_count == 2) {
+								/* Note the initial direction. */
+								dx1 = dxN;
+								dy1 = dyN;
+							} else if (d_count > 2) {
+								/* Note the widest variance from the initial direction, and where.
+								 * There are cases where large variances between two*/
+								float angle = ANGLE_BETWEEN_2F(dx1,dy1, dxN,dyN);
+								float step_distance = LENGTH2F(xN,yN, xM,yM);
+								if (angle - angleM > 0.01f && angleM_from_distance > 0.05f) {
+									if (angleM < angle*0.2 && step_distance < 0.010) {
+										;
+									} else {
+										angleM_last = angleM;
+										angleM = angle;
+										magnitude = LENGTH2F(x0,y0, xN,yN);
+										angleM_b_length = cosf(0.5f*M_PI - angleM) * magnitude;
+										xM = xN;
+										yM = yN;
+										xM_xN_last = xN_last;
+										yM_yN_last = yN_last;
+										angleM_from_distance = 0.0f;
+									}
+								}
+							}
+						}
+						/* Record the event. */
 						process_fingermotion_event(&event, temp_gesture);
 					}
-					fingers_in_use += 1;
-				}
-				break;
-			case SDL_FINGERUP:
-				if (event.tfinger.touchId == touch_id) {
-					fingers_in_use -= 1;
-				}
-				if (fingers_in_use == 0) {
-					int cxN_last = cxN, cyN_last = cyN;
-					SDL_Touch *touch = SDL_GetTouch(event.tfinger.touchId);
-					cxN = (event.tfinger.x * console_width) / touch->xres;
-					cyN = (event.tfinger.y * console_height) / touch->yres;
-					clocks_end = clock();
+					if (event.type == SDL_FINGERUP) {
+						fingers_in_use -= 1;
 
-					long_press = (clocks_end - clocks_start) > NORMAL_TOUCH_CLOCKS;
-					direct_distance = sqrt(pow(cxN-cx0,2)+pow(cyN-cy0,2));
-					path_distance += sqrt(pow(cxN-cxN_last,2)+pow(cyN-cyN_last,2));
-				}
-				break;
-			case SDL_FINGERMOTION:
-				if (event.tfinger.touchId == touch_id && fingers_in_use > 0) {
-					int cxN_last = cxN, cyN_last = cyN;
-					SDL_Touch *touch = SDL_GetTouch(event.tfinger.touchId);
-					cxN = (event.tfinger.x * console_width) / touch->xres;
-					cyN = (event.tfinger.y * console_height) / touch->yres;
-					direct_distance = sqrt(pow(cxN-cx0,2)+pow(cyN-cy0,2));
-					path_distance += sqrt(pow(cxN-cxN_last,2)+pow(cyN-cyN_last,2));
-
-					process_fingermotion_event(&event, temp_gesture);
-					dx_sum += (float)event.tfinger.dx/touch->xres;
-					dy_sum += (float)event.tfinger.dy/touch->yres;
+						if (fingers_in_use == 0) {
+							clocks_end = clock();
+							long_press = (clocks_end - clocks_start) > NORMAL_TOUCH_CLOCKS;
+						}
+					}
 				}
 				break;
 			case SDL_KEYDOWN:
 
 		TCOD_console_set_default_foreground(NULL, TCOD_white);
 		TCOD_console_print_frame(NULL, frame_cx, frame_cy, frame_cw, frame_ch, 1, TCOD_BKGND_NONE, "TESTING \"CORE\" GESTURES", gesture_index+1);
-		if (temp_gesture->coord_count) {
-			struct _gesture_coord *temp_coord = &(temp_gesture->coords[temp_gesture->coord_count-1]);
-			cx = temp_coord->cx;
-			cy = temp_coord->cy;
+		{
+			float angle_last;
+			float dxN, dyN;
+			dxN = xN-x0;
+			dyN = yN-y0;
+			angle_last = ANGLE_BETWEEN_2F(dx1,dy1, dxN,dyN);
+			TCOD_console_print_ex(NULL, frame_cx+2, frame_cy+2, TCOD_BKGND_NONE, TCOD_LEFT, "TIME: %0.1fs\nANGLE-STARTEND: %0.3f\nANGLE-FS-MAX-NOW: %0.3f\nANGLE-FS-MAX-LAST: %0.3f\nANGLE-FS-DIST: %0.3f\nANGLE-FS-POS-NOW: %0.3f,%0.3f\nANGLE-FS-POS-LAST: %0.3f,%0.3f", (float)(clocks_end-clocks_start)/CLOCKS_PER_SEC, angle_last, angleM, angleM_last, angleM_b_length, xM, yM, xM_xN_last, yM_yN_last);
 		}
-		TCOD_console_print_ex(NULL, frame_cx+2, frame_cy+2, TCOD_BKGND_NONE, TCOD_LEFT, "MOVE: %0.2f,%0.2f\nTIME: %0.1fs\nPOS: %d,%d\nDISTANCE: %d\nPATH LEN: %d", dx_sum, dy_sum, (float)(clocks_end-clocks_start)/CLOCKS_PER_SEC, cx, cy, (int)direct_distance, (int)path_distance);
 		TCOD_console_print_ex(NULL, console_width-1, frame_cy+frame_ch, TCOD_BKGND_NONE, TCOD_RIGHT, "% 3d recorded events", temp_gesture->coord_count);
 
 		TCOD_console_flush();
 				SDL_Touch *touch = SDL_GetTouch(event.tfinger.touchId);
 				cx = (event.tfinger.x * console_width) / touch->xres;
 
-				tx = console_width/3;
-				if (cx > tx*2) { /* RHS - next */
+				tx = console_width/4;
+				if (cx > tx*3) { /* RHS - next */
 					if (gesture_index < gesture_count-1)
 						gesture_index += 1;
 					else
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.