widget/gonk/libui/PointerController.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 * Copyright (C) 2010 The Android Open Source Project
michael@0 3 *
michael@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 5 * you may not use this file except in compliance with the License.
michael@0 6 * You may obtain a copy of the License at
michael@0 7 *
michael@0 8 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 9 *
michael@0 10 * Unless required by applicable law or agreed to in writing, software
michael@0 11 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 13 * See the License for the specific language governing permissions and
michael@0 14 * limitations under the License.
michael@0 15 */
michael@0 16
michael@0 17 #define LOG_TAG "PointerController"
michael@0 18
michael@0 19 //#define LOG_NDEBUG 0
michael@0 20
michael@0 21 // Log debug messages about pointer updates
michael@0 22 #define DEBUG_POINTER_UPDATES 0
michael@0 23
michael@0 24 #include "PointerController.h"
michael@0 25
michael@0 26 #include "cutils_log.h"
michael@0 27
michael@0 28 #include <SkBitmap.h>
michael@0 29 #include <SkCanvas.h>
michael@0 30 #include <SkColor.h>
michael@0 31 #include <SkPaint.h>
michael@0 32 #include <SkXfermode.h>
michael@0 33
michael@0 34 namespace android {
michael@0 35
michael@0 36 // --- PointerController ---
michael@0 37
michael@0 38 // Time to wait before starting the fade when the pointer is inactive.
michael@0 39 static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL = 15 * 1000 * 1000000LL; // 15 seconds
michael@0 40 static const nsecs_t INACTIVITY_TIMEOUT_DELAY_TIME_SHORT = 3 * 1000 * 1000000LL; // 3 seconds
michael@0 41
michael@0 42 // Time to wait between animation frames.
michael@0 43 static const nsecs_t ANIMATION_FRAME_INTERVAL = 1000000000LL / 60;
michael@0 44
michael@0 45 // Time to spend fading out the spot completely.
michael@0 46 static const nsecs_t SPOT_FADE_DURATION = 200 * 1000000LL; // 200 ms
michael@0 47
michael@0 48 // Time to spend fading out the pointer completely.
michael@0 49 static const nsecs_t POINTER_FADE_DURATION = 500 * 1000000LL; // 500 ms
michael@0 50
michael@0 51
michael@0 52 // --- PointerController ---
michael@0 53
michael@0 54 PointerController::PointerController(const sp<PointerControllerPolicyInterface>& policy,
michael@0 55 const sp<Looper>& looper, const sp<SpriteController>& spriteController) :
michael@0 56 mPolicy(policy), mLooper(looper), mSpriteController(spriteController) {
michael@0 57 mHandler = new WeakMessageHandler(this);
michael@0 58
michael@0 59 AutoMutex _l(mLock);
michael@0 60
michael@0 61 mLocked.animationPending = false;
michael@0 62
michael@0 63 mLocked.displayWidth = -1;
michael@0 64 mLocked.displayHeight = -1;
michael@0 65 mLocked.displayOrientation = DISPLAY_ORIENTATION_0;
michael@0 66
michael@0 67 mLocked.presentation = PRESENTATION_POINTER;
michael@0 68 mLocked.presentationChanged = false;
michael@0 69
michael@0 70 mLocked.inactivityTimeout = INACTIVITY_TIMEOUT_NORMAL;
michael@0 71
michael@0 72 mLocked.pointerFadeDirection = 0;
michael@0 73 mLocked.pointerX = 0;
michael@0 74 mLocked.pointerY = 0;
michael@0 75 mLocked.pointerAlpha = 0.0f; // pointer is initially faded
michael@0 76 mLocked.pointerSprite = mSpriteController->createSprite();
michael@0 77 mLocked.pointerIconChanged = false;
michael@0 78
michael@0 79 mLocked.buttonState = 0;
michael@0 80
michael@0 81 loadResources();
michael@0 82 }
michael@0 83
michael@0 84 PointerController::~PointerController() {
michael@0 85 mLooper->removeMessages(mHandler);
michael@0 86
michael@0 87 AutoMutex _l(mLock);
michael@0 88
michael@0 89 mLocked.pointerSprite.clear();
michael@0 90
michael@0 91 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 92 delete mLocked.spots.itemAt(i);
michael@0 93 }
michael@0 94 mLocked.spots.clear();
michael@0 95 mLocked.recycledSprites.clear();
michael@0 96 }
michael@0 97
michael@0 98 bool PointerController::getBounds(float* outMinX, float* outMinY,
michael@0 99 float* outMaxX, float* outMaxY) const {
michael@0 100 AutoMutex _l(mLock);
michael@0 101
michael@0 102 return getBoundsLocked(outMinX, outMinY, outMaxX, outMaxY);
michael@0 103 }
michael@0 104
michael@0 105 bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
michael@0 106 float* outMaxX, float* outMaxY) const {
michael@0 107 if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {
michael@0 108 return false;
michael@0 109 }
michael@0 110
michael@0 111 *outMinX = 0;
michael@0 112 *outMinY = 0;
michael@0 113 switch (mLocked.displayOrientation) {
michael@0 114 case DISPLAY_ORIENTATION_90:
michael@0 115 case DISPLAY_ORIENTATION_270:
michael@0 116 *outMaxX = mLocked.displayHeight - 1;
michael@0 117 *outMaxY = mLocked.displayWidth - 1;
michael@0 118 break;
michael@0 119 default:
michael@0 120 *outMaxX = mLocked.displayWidth - 1;
michael@0 121 *outMaxY = mLocked.displayHeight - 1;
michael@0 122 break;
michael@0 123 }
michael@0 124 return true;
michael@0 125 }
michael@0 126
michael@0 127 void PointerController::move(float deltaX, float deltaY) {
michael@0 128 #if DEBUG_POINTER_UPDATES
michael@0 129 ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
michael@0 130 #endif
michael@0 131 if (deltaX == 0.0f && deltaY == 0.0f) {
michael@0 132 return;
michael@0 133 }
michael@0 134
michael@0 135 AutoMutex _l(mLock);
michael@0 136
michael@0 137 setPositionLocked(mLocked.pointerX + deltaX, mLocked.pointerY + deltaY);
michael@0 138 }
michael@0 139
michael@0 140 void PointerController::setButtonState(int32_t buttonState) {
michael@0 141 #if DEBUG_POINTER_UPDATES
michael@0 142 ALOGD("Set button state 0x%08x", buttonState);
michael@0 143 #endif
michael@0 144 AutoMutex _l(mLock);
michael@0 145
michael@0 146 if (mLocked.buttonState != buttonState) {
michael@0 147 mLocked.buttonState = buttonState;
michael@0 148 }
michael@0 149 }
michael@0 150
michael@0 151 int32_t PointerController::getButtonState() const {
michael@0 152 AutoMutex _l(mLock);
michael@0 153
michael@0 154 return mLocked.buttonState;
michael@0 155 }
michael@0 156
michael@0 157 void PointerController::setPosition(float x, float y) {
michael@0 158 #if DEBUG_POINTER_UPDATES
michael@0 159 ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
michael@0 160 #endif
michael@0 161 AutoMutex _l(mLock);
michael@0 162
michael@0 163 setPositionLocked(x, y);
michael@0 164 }
michael@0 165
michael@0 166 void PointerController::setPositionLocked(float x, float y) {
michael@0 167 float minX, minY, maxX, maxY;
michael@0 168 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
michael@0 169 if (x <= minX) {
michael@0 170 mLocked.pointerX = minX;
michael@0 171 } else if (x >= maxX) {
michael@0 172 mLocked.pointerX = maxX;
michael@0 173 } else {
michael@0 174 mLocked.pointerX = x;
michael@0 175 }
michael@0 176 if (y <= minY) {
michael@0 177 mLocked.pointerY = minY;
michael@0 178 } else if (y >= maxY) {
michael@0 179 mLocked.pointerY = maxY;
michael@0 180 } else {
michael@0 181 mLocked.pointerY = y;
michael@0 182 }
michael@0 183 updatePointerLocked();
michael@0 184 }
michael@0 185 }
michael@0 186
michael@0 187 void PointerController::getPosition(float* outX, float* outY) const {
michael@0 188 AutoMutex _l(mLock);
michael@0 189
michael@0 190 *outX = mLocked.pointerX;
michael@0 191 *outY = mLocked.pointerY;
michael@0 192 }
michael@0 193
michael@0 194 void PointerController::fade(Transition transition) {
michael@0 195 AutoMutex _l(mLock);
michael@0 196
michael@0 197 // Remove the inactivity timeout, since we are fading now.
michael@0 198 removeInactivityTimeoutLocked();
michael@0 199
michael@0 200 // Start fading.
michael@0 201 if (transition == TRANSITION_IMMEDIATE) {
michael@0 202 mLocked.pointerFadeDirection = 0;
michael@0 203 mLocked.pointerAlpha = 0.0f;
michael@0 204 updatePointerLocked();
michael@0 205 } else {
michael@0 206 mLocked.pointerFadeDirection = -1;
michael@0 207 startAnimationLocked();
michael@0 208 }
michael@0 209 }
michael@0 210
michael@0 211 void PointerController::unfade(Transition transition) {
michael@0 212 AutoMutex _l(mLock);
michael@0 213
michael@0 214 // Always reset the inactivity timer.
michael@0 215 resetInactivityTimeoutLocked();
michael@0 216
michael@0 217 // Start unfading.
michael@0 218 if (transition == TRANSITION_IMMEDIATE) {
michael@0 219 mLocked.pointerFadeDirection = 0;
michael@0 220 mLocked.pointerAlpha = 1.0f;
michael@0 221 updatePointerLocked();
michael@0 222 } else {
michael@0 223 mLocked.pointerFadeDirection = 1;
michael@0 224 startAnimationLocked();
michael@0 225 }
michael@0 226 }
michael@0 227
michael@0 228 void PointerController::setPresentation(Presentation presentation) {
michael@0 229 AutoMutex _l(mLock);
michael@0 230
michael@0 231 if (mLocked.presentation != presentation) {
michael@0 232 mLocked.presentation = presentation;
michael@0 233 mLocked.presentationChanged = true;
michael@0 234
michael@0 235 if (presentation != PRESENTATION_SPOT) {
michael@0 236 fadeOutAndReleaseAllSpotsLocked();
michael@0 237 }
michael@0 238
michael@0 239 updatePointerLocked();
michael@0 240 }
michael@0 241 }
michael@0 242
michael@0 243 void PointerController::setSpots(const PointerCoords* spotCoords,
michael@0 244 const uint32_t* spotIdToIndex, BitSet32 spotIdBits) {
michael@0 245 #if DEBUG_POINTER_UPDATES
michael@0 246 ALOGD("setSpots: idBits=%08x", spotIdBits.value);
michael@0 247 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
michael@0 248 uint32_t id = idBits.firstMarkedBit();
michael@0 249 idBits.clearBit(id);
michael@0 250 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
michael@0 251 ALOGD(" spot %d: position=(%0.3f, %0.3f), pressure=%0.3f", id,
michael@0 252 c.getAxisValue(AMOTION_EVENT_AXIS_X),
michael@0 253 c.getAxisValue(AMOTION_EVENT_AXIS_Y),
michael@0 254 c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE));
michael@0 255 }
michael@0 256 #endif
michael@0 257
michael@0 258 AutoMutex _l(mLock);
michael@0 259
michael@0 260 mSpriteController->openTransaction();
michael@0 261
michael@0 262 // Add or move spots for fingers that are down.
michael@0 263 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
michael@0 264 uint32_t id = idBits.clearFirstMarkedBit();
michael@0 265 const PointerCoords& c = spotCoords[spotIdToIndex[id]];
michael@0 266 const SpriteIcon& icon = c.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE) > 0
michael@0 267 ? mResources.spotTouch : mResources.spotHover;
michael@0 268 float x = c.getAxisValue(AMOTION_EVENT_AXIS_X);
michael@0 269 float y = c.getAxisValue(AMOTION_EVENT_AXIS_Y);
michael@0 270
michael@0 271 Spot* spot = getSpotLocked(id);
michael@0 272 if (!spot) {
michael@0 273 spot = createAndAddSpotLocked(id);
michael@0 274 }
michael@0 275
michael@0 276 spot->updateSprite(&icon, x, y);
michael@0 277 }
michael@0 278
michael@0 279 // Remove spots for fingers that went up.
michael@0 280 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 281 Spot* spot = mLocked.spots.itemAt(i);
michael@0 282 if (spot->id != Spot::INVALID_ID
michael@0 283 && !spotIdBits.hasBit(spot->id)) {
michael@0 284 fadeOutAndReleaseSpotLocked(spot);
michael@0 285 }
michael@0 286 }
michael@0 287
michael@0 288 mSpriteController->closeTransaction();
michael@0 289 }
michael@0 290
michael@0 291 void PointerController::clearSpots() {
michael@0 292 #if DEBUG_POINTER_UPDATES
michael@0 293 ALOGD("clearSpots");
michael@0 294 #endif
michael@0 295
michael@0 296 AutoMutex _l(mLock);
michael@0 297
michael@0 298 fadeOutAndReleaseAllSpotsLocked();
michael@0 299 }
michael@0 300
michael@0 301 void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout) {
michael@0 302 AutoMutex _l(mLock);
michael@0 303
michael@0 304 if (mLocked.inactivityTimeout != inactivityTimeout) {
michael@0 305 mLocked.inactivityTimeout = inactivityTimeout;
michael@0 306 resetInactivityTimeoutLocked();
michael@0 307 }
michael@0 308 }
michael@0 309
michael@0 310 void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
michael@0 311 AutoMutex _l(mLock);
michael@0 312
michael@0 313 // Adjust to use the display's unrotated coordinate frame.
michael@0 314 if (orientation == DISPLAY_ORIENTATION_90
michael@0 315 || orientation == DISPLAY_ORIENTATION_270) {
michael@0 316 int32_t temp = height;
michael@0 317 height = width;
michael@0 318 width = temp;
michael@0 319 }
michael@0 320
michael@0 321 if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
michael@0 322 mLocked.displayWidth = width;
michael@0 323 mLocked.displayHeight = height;
michael@0 324
michael@0 325 float minX, minY, maxX, maxY;
michael@0 326 if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
michael@0 327 mLocked.pointerX = (minX + maxX) * 0.5f;
michael@0 328 mLocked.pointerY = (minY + maxY) * 0.5f;
michael@0 329 } else {
michael@0 330 mLocked.pointerX = 0;
michael@0 331 mLocked.pointerY = 0;
michael@0 332 }
michael@0 333
michael@0 334 fadeOutAndReleaseAllSpotsLocked();
michael@0 335 }
michael@0 336
michael@0 337 if (mLocked.displayOrientation != orientation) {
michael@0 338 // Apply offsets to convert from the pixel top-left corner position to the pixel center.
michael@0 339 // This creates an invariant frame of reference that we can easily rotate when
michael@0 340 // taking into account that the pointer may be located at fractional pixel offsets.
michael@0 341 float x = mLocked.pointerX + 0.5f;
michael@0 342 float y = mLocked.pointerY + 0.5f;
michael@0 343 float temp;
michael@0 344
michael@0 345 // Undo the previous rotation.
michael@0 346 switch (mLocked.displayOrientation) {
michael@0 347 case DISPLAY_ORIENTATION_90:
michael@0 348 temp = x;
michael@0 349 x = mLocked.displayWidth - y;
michael@0 350 y = temp;
michael@0 351 break;
michael@0 352 case DISPLAY_ORIENTATION_180:
michael@0 353 x = mLocked.displayWidth - x;
michael@0 354 y = mLocked.displayHeight - y;
michael@0 355 break;
michael@0 356 case DISPLAY_ORIENTATION_270:
michael@0 357 temp = x;
michael@0 358 x = y;
michael@0 359 y = mLocked.displayHeight - temp;
michael@0 360 break;
michael@0 361 }
michael@0 362
michael@0 363 // Perform the new rotation.
michael@0 364 switch (orientation) {
michael@0 365 case DISPLAY_ORIENTATION_90:
michael@0 366 temp = x;
michael@0 367 x = y;
michael@0 368 y = mLocked.displayWidth - temp;
michael@0 369 break;
michael@0 370 case DISPLAY_ORIENTATION_180:
michael@0 371 x = mLocked.displayWidth - x;
michael@0 372 y = mLocked.displayHeight - y;
michael@0 373 break;
michael@0 374 case DISPLAY_ORIENTATION_270:
michael@0 375 temp = x;
michael@0 376 x = mLocked.displayHeight - y;
michael@0 377 y = temp;
michael@0 378 break;
michael@0 379 }
michael@0 380
michael@0 381 // Apply offsets to convert from the pixel center to the pixel top-left corner position
michael@0 382 // and save the results.
michael@0 383 mLocked.pointerX = x - 0.5f;
michael@0 384 mLocked.pointerY = y - 0.5f;
michael@0 385 mLocked.displayOrientation = orientation;
michael@0 386 }
michael@0 387
michael@0 388 updatePointerLocked();
michael@0 389 }
michael@0 390
michael@0 391 void PointerController::setPointerIcon(const SpriteIcon& icon) {
michael@0 392 AutoMutex _l(mLock);
michael@0 393
michael@0 394 mLocked.pointerIcon = icon.copy();
michael@0 395 mLocked.pointerIconChanged = true;
michael@0 396
michael@0 397 updatePointerLocked();
michael@0 398 }
michael@0 399
michael@0 400 void PointerController::handleMessage(const Message& message) {
michael@0 401 switch (message.what) {
michael@0 402 case MSG_ANIMATE:
michael@0 403 doAnimate();
michael@0 404 break;
michael@0 405 case MSG_INACTIVITY_TIMEOUT:
michael@0 406 doInactivityTimeout();
michael@0 407 break;
michael@0 408 }
michael@0 409 }
michael@0 410
michael@0 411 void PointerController::doAnimate() {
michael@0 412 AutoMutex _l(mLock);
michael@0 413
michael@0 414 bool keepAnimating = false;
michael@0 415 mLocked.animationPending = false;
michael@0 416 nsecs_t frameDelay = systemTime(SYSTEM_TIME_MONOTONIC) - mLocked.animationTime;
michael@0 417
michael@0 418 // Animate pointer fade.
michael@0 419 if (mLocked.pointerFadeDirection < 0) {
michael@0 420 mLocked.pointerAlpha -= float(frameDelay) / POINTER_FADE_DURATION;
michael@0 421 if (mLocked.pointerAlpha <= 0.0f) {
michael@0 422 mLocked.pointerAlpha = 0.0f;
michael@0 423 mLocked.pointerFadeDirection = 0;
michael@0 424 } else {
michael@0 425 keepAnimating = true;
michael@0 426 }
michael@0 427 updatePointerLocked();
michael@0 428 } else if (mLocked.pointerFadeDirection > 0) {
michael@0 429 mLocked.pointerAlpha += float(frameDelay) / POINTER_FADE_DURATION;
michael@0 430 if (mLocked.pointerAlpha >= 1.0f) {
michael@0 431 mLocked.pointerAlpha = 1.0f;
michael@0 432 mLocked.pointerFadeDirection = 0;
michael@0 433 } else {
michael@0 434 keepAnimating = true;
michael@0 435 }
michael@0 436 updatePointerLocked();
michael@0 437 }
michael@0 438
michael@0 439 // Animate spots that are fading out and being removed.
michael@0 440 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 441 Spot* spot = mLocked.spots.itemAt(i);
michael@0 442 if (spot->id == Spot::INVALID_ID) {
michael@0 443 spot->alpha -= float(frameDelay) / SPOT_FADE_DURATION;
michael@0 444 if (spot->alpha <= 0) {
michael@0 445 mLocked.spots.removeAt(i--);
michael@0 446 releaseSpotLocked(spot);
michael@0 447 } else {
michael@0 448 spot->sprite->setAlpha(spot->alpha);
michael@0 449 keepAnimating = true;
michael@0 450 }
michael@0 451 }
michael@0 452 }
michael@0 453
michael@0 454 if (keepAnimating) {
michael@0 455 startAnimationLocked();
michael@0 456 }
michael@0 457 }
michael@0 458
michael@0 459 void PointerController::doInactivityTimeout() {
michael@0 460 fade(TRANSITION_GRADUAL);
michael@0 461 }
michael@0 462
michael@0 463 void PointerController::startAnimationLocked() {
michael@0 464 if (!mLocked.animationPending) {
michael@0 465 mLocked.animationPending = true;
michael@0 466 mLocked.animationTime = systemTime(SYSTEM_TIME_MONOTONIC);
michael@0 467 mLooper->sendMessageDelayed(ANIMATION_FRAME_INTERVAL, mHandler, Message(MSG_ANIMATE));
michael@0 468 }
michael@0 469 }
michael@0 470
michael@0 471 void PointerController::resetInactivityTimeoutLocked() {
michael@0 472 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
michael@0 473
michael@0 474 nsecs_t timeout = mLocked.inactivityTimeout == INACTIVITY_TIMEOUT_SHORT
michael@0 475 ? INACTIVITY_TIMEOUT_DELAY_TIME_SHORT : INACTIVITY_TIMEOUT_DELAY_TIME_NORMAL;
michael@0 476 mLooper->sendMessageDelayed(timeout, mHandler, MSG_INACTIVITY_TIMEOUT);
michael@0 477 }
michael@0 478
michael@0 479 void PointerController::removeInactivityTimeoutLocked() {
michael@0 480 mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
michael@0 481 }
michael@0 482
michael@0 483 void PointerController::updatePointerLocked() {
michael@0 484 mSpriteController->openTransaction();
michael@0 485
michael@0 486 mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
michael@0 487 mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
michael@0 488
michael@0 489 if (mLocked.pointerAlpha > 0) {
michael@0 490 mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
michael@0 491 mLocked.pointerSprite->setVisible(true);
michael@0 492 } else {
michael@0 493 mLocked.pointerSprite->setVisible(false);
michael@0 494 }
michael@0 495
michael@0 496 if (mLocked.pointerIconChanged || mLocked.presentationChanged) {
michael@0 497 mLocked.pointerSprite->setIcon(mLocked.presentation == PRESENTATION_POINTER
michael@0 498 ? mLocked.pointerIcon : mResources.spotAnchor);
michael@0 499 mLocked.pointerIconChanged = false;
michael@0 500 mLocked.presentationChanged = false;
michael@0 501 }
michael@0 502
michael@0 503 mSpriteController->closeTransaction();
michael@0 504 }
michael@0 505
michael@0 506 PointerController::Spot* PointerController::getSpotLocked(uint32_t id) {
michael@0 507 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 508 Spot* spot = mLocked.spots.itemAt(i);
michael@0 509 if (spot->id == id) {
michael@0 510 return spot;
michael@0 511 }
michael@0 512 }
michael@0 513 return NULL;
michael@0 514 }
michael@0 515
michael@0 516 PointerController::Spot* PointerController::createAndAddSpotLocked(uint32_t id) {
michael@0 517 // Remove spots until we have fewer than MAX_SPOTS remaining.
michael@0 518 while (mLocked.spots.size() >= MAX_SPOTS) {
michael@0 519 Spot* spot = removeFirstFadingSpotLocked();
michael@0 520 if (!spot) {
michael@0 521 spot = mLocked.spots.itemAt(0);
michael@0 522 mLocked.spots.removeAt(0);
michael@0 523 }
michael@0 524 releaseSpotLocked(spot);
michael@0 525 }
michael@0 526
michael@0 527 // Obtain a sprite from the recycled pool.
michael@0 528 sp<Sprite> sprite;
michael@0 529 if (! mLocked.recycledSprites.isEmpty()) {
michael@0 530 sprite = mLocked.recycledSprites.top();
michael@0 531 mLocked.recycledSprites.pop();
michael@0 532 } else {
michael@0 533 sprite = mSpriteController->createSprite();
michael@0 534 }
michael@0 535
michael@0 536 // Return the new spot.
michael@0 537 Spot* spot = new Spot(id, sprite);
michael@0 538 mLocked.spots.push(spot);
michael@0 539 return spot;
michael@0 540 }
michael@0 541
michael@0 542 PointerController::Spot* PointerController::removeFirstFadingSpotLocked() {
michael@0 543 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 544 Spot* spot = mLocked.spots.itemAt(i);
michael@0 545 if (spot->id == Spot::INVALID_ID) {
michael@0 546 mLocked.spots.removeAt(i);
michael@0 547 return spot;
michael@0 548 }
michael@0 549 }
michael@0 550 return NULL;
michael@0 551 }
michael@0 552
michael@0 553 void PointerController::releaseSpotLocked(Spot* spot) {
michael@0 554 spot->sprite->clearIcon();
michael@0 555
michael@0 556 if (mLocked.recycledSprites.size() < MAX_RECYCLED_SPRITES) {
michael@0 557 mLocked.recycledSprites.push(spot->sprite);
michael@0 558 }
michael@0 559
michael@0 560 delete spot;
michael@0 561 }
michael@0 562
michael@0 563 void PointerController::fadeOutAndReleaseSpotLocked(Spot* spot) {
michael@0 564 if (spot->id != Spot::INVALID_ID) {
michael@0 565 spot->id = Spot::INVALID_ID;
michael@0 566 startAnimationLocked();
michael@0 567 }
michael@0 568 }
michael@0 569
michael@0 570 void PointerController::fadeOutAndReleaseAllSpotsLocked() {
michael@0 571 for (size_t i = 0; i < mLocked.spots.size(); i++) {
michael@0 572 Spot* spot = mLocked.spots.itemAt(i);
michael@0 573 fadeOutAndReleaseSpotLocked(spot);
michael@0 574 }
michael@0 575 }
michael@0 576
michael@0 577 void PointerController::loadResources() {
michael@0 578 mPolicy->loadPointerResources(&mResources);
michael@0 579 }
michael@0 580
michael@0 581
michael@0 582 // --- PointerController::Spot ---
michael@0 583
michael@0 584 void PointerController::Spot::updateSprite(const SpriteIcon* icon, float x, float y) {
michael@0 585 sprite->setLayer(Sprite::BASE_LAYER_SPOT + id);
michael@0 586 sprite->setAlpha(alpha);
michael@0 587 sprite->setTransformationMatrix(SpriteTransformationMatrix(scale, 0.0f, 0.0f, scale));
michael@0 588 sprite->setPosition(x, y);
michael@0 589
michael@0 590 this->x = x;
michael@0 591 this->y = y;
michael@0 592
michael@0 593 if (icon != lastIcon) {
michael@0 594 lastIcon = icon;
michael@0 595 if (icon) {
michael@0 596 sprite->setIcon(*icon);
michael@0 597 sprite->setVisible(true);
michael@0 598 } else {
michael@0 599 sprite->setVisible(false);
michael@0 600 }
michael@0 601 }
michael@0 602 }
michael@0 603
michael@0 604 } // namespace android

mercurial