Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/events/android/motion_event_android.h" | 5 #include "ui/events/android/motion_event_android.h" |
| 6 | 6 |
| 7 #include <android/input.h> | 7 #include <android/input.h> |
| 8 | 8 |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 | 10 |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 DCHECK_GE(history_size, 0); | 145 DCHECK_GE(history_size, 0); |
| 146 // While the spec states that only ACTION_MOVE events should contain | 146 // While the spec states that only ACTION_MOVE events should contain |
| 147 // historical entries, it's possible that an embedder could repurpose an | 147 // historical entries, it's possible that an embedder could repurpose an |
| 148 // ACTION_MOVE event into a different kind of event. In that case, the | 148 // ACTION_MOVE event into a different kind of event. In that case, the |
| 149 // historical values are meaningless, and should not be exposed. | 149 // historical values are meaningless, and should not be exposed. |
| 150 if (action != ui::MotionEvent::ACTION_MOVE) | 150 if (action != ui::MotionEvent::ACTION_MOVE) |
| 151 return 0; | 151 return 0; |
| 152 return history_size; | 152 return history_size; |
| 153 } | 153 } |
| 154 | 154 |
| 155 // Convert tilt and orientation to tilt_x and tilt_y. Tilt_x and tilt_y will lie | |
| 156 // in [-90, 90]. | |
| 157 void ConvertTiltRadToTiltXY(float tilt_rad, | |
|
use mustaq_at_chromium.org
2017/05/15 19:46:53
Nit: ConvertTiltOrientationToTiltXY instead?
jkwang
2017/05/16 01:13:26
Done.
| |
| 158 float orientation_rad, | |
| 159 float* tilt_x, | |
| 160 float* tilt_y) { | |
| 161 float r = sin(tilt_rad); | |
| 162 float z = cos(tilt_rad); | |
| 163 *tilt_x = atan2(sin(-orientation_rad) * r, z) * 180.f / M_PI; | |
| 164 *tilt_y = atan2(cos(-orientation_rad) * r, z) * 180.f / M_PI; | |
| 165 } | |
| 166 | |
| 155 } // namespace | 167 } // namespace |
| 156 | 168 |
| 157 MotionEventAndroid::Pointer::Pointer(jint id, | 169 MotionEventAndroid::Pointer::Pointer(jint id, |
| 158 jfloat pos_x_pixels, | 170 jfloat pos_x_pixels, |
| 159 jfloat pos_y_pixels, | 171 jfloat pos_y_pixels, |
| 160 jfloat touch_major_pixels, | 172 jfloat touch_major_pixels, |
| 161 jfloat touch_minor_pixels, | 173 jfloat touch_minor_pixels, |
| 162 jfloat orientation_rad, | 174 jfloat orientation_rad, |
| 163 jfloat tilt_rad, | 175 jfloat tilt_rad, |
| 164 jint tool_type) | 176 jint tool_type) |
| 165 : id(id), | 177 : id(id), |
| 166 pos_x_pixels(pos_x_pixels), | 178 pos_x_pixels(pos_x_pixels), |
| 167 pos_y_pixels(pos_y_pixels), | 179 pos_y_pixels(pos_y_pixels), |
| 168 touch_major_pixels(touch_major_pixels), | 180 touch_major_pixels(touch_major_pixels), |
| 169 touch_minor_pixels(touch_minor_pixels), | 181 touch_minor_pixels(touch_minor_pixels), |
| 170 orientation_rad(orientation_rad), | 182 orientation_rad(orientation_rad), |
| 171 tilt_rad(tilt_rad), | 183 tilt_rad(tilt_rad), |
| 172 tool_type(tool_type) { | 184 tool_type(tool_type) { |
| 173 } | 185 } |
| 174 | 186 |
| 175 MotionEventAndroid::CachedPointer::CachedPointer() | 187 MotionEventAndroid::CachedPointer::CachedPointer() |
| 176 : id(0), | 188 : id(0), |
| 177 touch_major(0), | 189 touch_major(0), |
| 178 touch_minor(0), | 190 touch_minor(0), |
| 179 orientation(0), | 191 orientation(0), |
| 180 tilt(0), | 192 tilt_x(0), |
| 181 tool_type(TOOL_TYPE_UNKNOWN) { | 193 tilt_y(0), |
| 182 } | 194 tool_type(TOOL_TYPE_UNKNOWN) {} |
| 183 | 195 |
| 184 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, | 196 MotionEventAndroid::MotionEventAndroid(JNIEnv* env, |
| 185 jobject event, | 197 jobject event, |
| 186 jfloat pix_to_dip, | 198 jfloat pix_to_dip, |
| 187 jfloat ticks_x, | 199 jfloat ticks_x, |
| 188 jfloat ticks_y, | 200 jfloat ticks_y, |
| 189 jfloat tick_multiplier, | 201 jfloat tick_multiplier, |
| 190 jlong time_ms, | 202 jlong time_ms, |
| 191 jint android_action, | 203 jint android_action, |
| 192 jint pointer_count, | 204 jint pointer_count, |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 // caching the pressure values is not a worthwhile optimization (they're | 368 // caching the pressure values is not a worthwhile optimization (they're |
| 357 // accessed at most once per event instance). | 369 // accessed at most once per event instance). |
| 358 if (!event_.obj()) | 370 if (!event_.obj()) |
| 359 return 0.f; | 371 return 0.f; |
| 360 if (cached_action_ == MotionEvent::ACTION_UP) | 372 if (cached_action_ == MotionEvent::ACTION_UP) |
| 361 return 0.f; | 373 return 0.f; |
| 362 return Java_MotionEvent_getPressureF_I(AttachCurrentThread(), event_, | 374 return Java_MotionEvent_getPressureF_I(AttachCurrentThread(), event_, |
| 363 pointer_index); | 375 pointer_index); |
| 364 } | 376 } |
| 365 | 377 |
| 366 float MotionEventAndroid::GetTilt(size_t pointer_index) const { | 378 float MotionEventAndroid::GetTiltX(size_t pointer_index) const { |
| 367 DCHECK_LT(pointer_index, cached_pointer_count_); | 379 DCHECK_LT(pointer_index, cached_pointer_count_); |
| 368 if (pointer_index < MAX_POINTERS_TO_CACHE) | 380 if (pointer_index < MAX_POINTERS_TO_CACHE) |
| 369 return cached_pointers_[pointer_index].tilt; | 381 return cached_pointers_[pointer_index].tilt_x; |
| 370 if (!event_.obj()) | 382 if (!event_.obj()) |
| 371 return 0.f; | 383 return 0.f; |
| 372 return ToValidFloat(Java_MotionEvent_getAxisValueF_I_I( | 384 float tilt_x, tilt_y; |
| 385 float tilt_rad = ToValidFloat(Java_MotionEvent_getAxisValueF_I_I( | |
| 373 AttachCurrentThread(), event_, AXIS_TILT, pointer_index)); | 386 AttachCurrentThread(), event_, AXIS_TILT, pointer_index)); |
| 387 float orientation_rad = ToValidFloat(Java_MotionEvent_getOrientationF_I( | |
| 388 AttachCurrentThread(), event_, pointer_index)); | |
| 389 ConvertTiltRadToTiltXY(tilt_rad, orientation_rad, &tilt_x, &tilt_y); | |
| 390 return tilt_x; | |
| 391 } | |
| 392 | |
| 393 float MotionEventAndroid::GetTiltY(size_t pointer_index) const { | |
| 394 DCHECK_LT(pointer_index, cached_pointer_count_); | |
| 395 if (pointer_index < MAX_POINTERS_TO_CACHE) | |
| 396 return cached_pointers_[pointer_index].tilt_y; | |
| 397 if (!event_.obj()) | |
| 398 return 0.f; | |
| 399 float tilt_x, tilt_y; | |
| 400 float tilt_rad = ToValidFloat(Java_MotionEvent_getAxisValueF_I_I( | |
| 401 AttachCurrentThread(), event_, AXIS_TILT, pointer_index)); | |
| 402 float orientation_rad = ToValidFloat(Java_MotionEvent_getOrientationF_I( | |
| 403 AttachCurrentThread(), event_, pointer_index)); | |
| 404 ConvertTiltRadToTiltXY(tilt_rad, orientation_rad, &tilt_x, &tilt_y); | |
| 405 return tilt_y; | |
| 374 } | 406 } |
| 375 | 407 |
| 376 base::TimeTicks MotionEventAndroid::GetEventTime() const { | 408 base::TimeTicks MotionEventAndroid::GetEventTime() const { |
| 377 return cached_time_; | 409 return cached_time_; |
| 378 } | 410 } |
| 379 | 411 |
| 380 size_t MotionEventAndroid::GetHistorySize() const { | 412 size_t MotionEventAndroid::GetHistorySize() const { |
| 381 return cached_history_size_; | 413 return cached_history_size_; |
| 382 } | 414 } |
| 383 | 415 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 | 461 |
| 430 MotionEventAndroid::CachedPointer MotionEventAndroid::FromAndroidPointer( | 462 MotionEventAndroid::CachedPointer MotionEventAndroid::FromAndroidPointer( |
| 431 const Pointer& pointer) const { | 463 const Pointer& pointer) const { |
| 432 CachedPointer result; | 464 CachedPointer result; |
| 433 result.id = pointer.id; | 465 result.id = pointer.id; |
| 434 result.position = | 466 result.position = |
| 435 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); | 467 gfx::PointF(ToDips(pointer.pos_x_pixels), ToDips(pointer.pos_y_pixels)); |
| 436 result.touch_major = ToDips(pointer.touch_major_pixels); | 468 result.touch_major = ToDips(pointer.touch_major_pixels); |
| 437 result.touch_minor = ToDips(pointer.touch_minor_pixels); | 469 result.touch_minor = ToDips(pointer.touch_minor_pixels); |
| 438 result.orientation = ToValidFloat(pointer.orientation_rad); | 470 result.orientation = ToValidFloat(pointer.orientation_rad); |
| 439 result.tilt = ToValidFloat(pointer.tilt_rad); | 471 float tilt_rad = ToValidFloat(pointer.tilt_rad); |
| 472 ConvertTiltRadToTiltXY(tilt_rad, result.orientation, &result.tilt_x, | |
| 473 &result.tilt_y); | |
| 440 result.tool_type = FromAndroidToolType(pointer.tool_type); | 474 result.tool_type = FromAndroidToolType(pointer.tool_type); |
| 441 return result; | 475 return result; |
| 442 } | 476 } |
| 443 | 477 |
| 444 MotionEventAndroid::CachedPointer MotionEventAndroid::OffsetCachedPointer( | 478 MotionEventAndroid::CachedPointer MotionEventAndroid::OffsetCachedPointer( |
| 445 const CachedPointer& pointer, | 479 const CachedPointer& pointer, |
| 446 float x, | 480 float x, |
| 447 float y) const { | 481 float y) const { |
| 448 CachedPointer result; | 482 CachedPointer result; |
| 449 result.id = pointer.id; | 483 result.id = pointer.id; |
| 450 result.position = gfx::PointF(pointer.position.x() + ToDips(x), | 484 result.position = gfx::PointF(pointer.position.x() + ToDips(x), |
| 451 pointer.position.y() + ToDips(y)); | 485 pointer.position.y() + ToDips(y)); |
| 452 result.touch_major = pointer.touch_major; | 486 result.touch_major = pointer.touch_major; |
| 453 result.touch_minor = pointer.touch_minor; | 487 result.touch_minor = pointer.touch_minor; |
| 454 result.orientation = pointer.orientation; | 488 result.orientation = pointer.orientation; |
| 455 result.tilt = pointer.tilt; | 489 result.tilt_x = pointer.tilt_x; |
| 490 result.tilt_y = pointer.tilt_y; | |
| 456 result.tool_type = pointer.tool_type; | 491 result.tool_type = pointer.tool_type; |
| 457 return result; | 492 return result; |
| 458 } | 493 } |
| 459 | 494 |
| 460 } // namespace ui | 495 } // namespace ui |
| OLD | NEW |