Commits

Anonymous committed 3576a6d

- Different stroke detection, based on start and end of the stroke. It is
more reliable, but not good enough as often strokes can curve, which
can have the same results as swerving.

  • Participants
  • Parent commits 5664af2

Comments (0)

Files changed (1)

File jni/src/main.c

 /* SDL touch screen coords. */
 
 struct _touch_coord {
-	Sint16 x, y;
+	float x, y;
 };
 
 struct _touch_coords {
  * Average direction = normalize(average direction + (current point - last point))
  */
 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_ch = console_height - Y_LOWER_GAP;
 	int fingers_in_use, long_press;
 	int clocks_start, clocks_end;
-	float x0, y0, xN, yN, dx1, dy1, dxN, dyN, angleM, angleM_b_length, xM, yM, xM_xN_last, yM_yN_last, angleM_last;
+	/*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;
+	int d_count, */
+	int i;
 
 	for (i = 0; i < MAX_FINGERS; i++)
 		temp_touch_coords[i] = alloca(SIZEOF_TOUCH_COORDS(MAX_TOUCH_COORDS));
 	fingers_in_use = long_press = 0;
 	clocks_start = clocks_end = 0;
 	do {
+		SDL_Event event;
 		int num_events_polled = 0;
 
 		while (num_events_polled++ < MAX_EVENTS_POLLED_PER_FRAME && SDL_PollEvent(&event)) {
 					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;
+							for (i = 0; i < MAX_FINGERS; i++)
+								temp_touch_coords[i]->coord_count = 0;
+							INIT_GESTURE(temp_gesture);
 							long_press = 0;
 							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) {
+						int finger_index;
+						/* Record the event. */
+						process_fingermotion_event(&event, temp_gesture);
+						finger_index = get_gesture_finger_index(temp_gesture, event.tfinger.fingerId);
+						if (finger_index == -1) {
+							show_error_message_screen("Core gesture finger index problem.", 3);
+							return;
+						} else {
+							int coord_index = temp_touch_coords[finger_index]->coord_count;
+							struct _touch_coord *temp_touch_coord = &temp_touch_coords[finger_index]->coords[coord_index];
+							temp_touch_coord->x = (float)event.tfinger.x / touch->xres;
+							temp_touch_coord->y = (float)event.tfinger.y / touch->yres;
+							temp_touch_coords[finger_index]->coord_count = coord_index + 1;
+						}
+						/* Process the event.
+						 * - Vector 1: From
+						 **/
+						/*
 						float xN_last = xN, yN_last = yN, magnitude;
 						xN = (float)event.tfinger.x / touch->xres;
 						yN = (float)event.tfinger.y / touch->yres;
 							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) {
 									}
 								}
 							}
-						}
-						/* Record the event. */
-						process_fingermotion_event(&event, temp_gesture);
+						} */
 					}
 					if (event.type == SDL_FINGERUP) {
 						fingers_in_use -= 1;
 		}
 
 		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);
-		{
-			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_frame(NULL, frame_cx, frame_cy, frame_cw, frame_ch, 0, TCOD_BKGND_NONE, "TESTING \"CORE\" GESTURES", gesture_index+1);
+		TCOD_console_print_ex(NULL, console_width-1, frame_cy+frame_ch, TCOD_BKGND_NONE, TCOD_RIGHT, "% 3d recorded events", temp_gesture->coord_count);
+
+		/* fingers_in_use == 0 &&  */
+		if (temp_gesture->finger_count == 1 && temp_touch_coords[0]->coord_count > 1) {
+			float angle_max = 0.0f, angle;
+			float angle_dist_max = 0.0f, angle_dist;
+			float line_dist;
+
+			if (temp_gesture->finger_count == 1) {
+				float dx_line, dy_line, x0, y0;
+				int last_coord_index = temp_touch_coords[0]->coord_count-1;
+
+				x0 = temp_touch_coords[0]->coords[0].x;
+				y0 = temp_touch_coords[0]->coords[0].y;
+				dx_line = temp_touch_coords[0]->coords[last_coord_index].x - x0;
+				dy_line = temp_touch_coords[0]->coords[last_coord_index].y - y0;
+				line_dist = sqrtf(pow(dx_line,2) + pow(dy_line,2));
+
+				for (i = 1; i < last_coord_index; i++) {
+					struct _touch_coord *tcoord = &temp_touch_coords[0]->coords[i];
+					float dx_other = tcoord->x - x0;
+					float dy_other = tcoord->y - y0;
+					float angle_max_h_length = LENGTH2F(x0,y0, tcoord->x,tcoord->y);
+
+					angle = ANGLE_BETWEEN_2F(dx_line,dy_line, dx_other,dy_other);
+					angle_dist = cosf(0.5f*M_PI - angle) * angle_max_h_length;
+
+					if (angle_dist > angle_dist_max) {
+						angle_max = angle;
+						angle_dist_max = angle_dist;
+					}
+ 				}
+				/*
+				 * Length > 0.1
+				 * Stray distance < 0.4
+				 * Distance ratio < 0.4
+				 *
+				 * Still have a possible problem:
+				 * - Often strokes will be relatively linear curves, with values outside this.
+				 * TODO: Using two fingers won't detect lifting two fingers.
+				 */
+
+				TCOD_console_print_ex(NULL, frame_cx+2, frame_cy+2, TCOD_BKGND_NONE, TCOD_LEFT,
+						"TIME: %0.1fs\nFINGERS: %d\nLENGTH: %0.3f\nANGLE-MAX: %0.3f\nANGLE-MAX-DIST: %0.3f\nRATIO: %0.3f",
+						(float)(clocks_end-clocks_start)/CLOCKS_PER_SEC, temp_gesture->finger_count, line_dist,
+						angle_max, angle_dist_max, angle_dist_max/line_dist);
+			}
+		} else {
+			TCOD_console_print_ex(NULL, frame_cx+2, frame_cy+2, TCOD_BKGND_NONE, TCOD_LEFT, "TIME: %0.1fs\nFINGERS: %d",
+					(float)(clocks_end-clocks_start)/CLOCKS_PER_SEC, temp_gesture->finger_count);
 		}
-		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();
 	} while (!TCOD_console_is_window_closed());