Home | History | Annotate | Download | only in animator
      1 /* libs/graphics/animator/SkDisplayEvents.cpp
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #include "SkDisplayEvents.h"
     19 #include "SkAnimateMaker.h"
     20 #include "SkAnimator.h"
     21 #include "SkDisplayEvent.h"
     22 #include "SkDisplayMovie.h"
     23 #include "SkDrawable.h"
     24 #ifdef SK_DEBUG
     25 #include "SkDump.h"
     26 #endif
     27 
     28 SkEventState::SkEventState() : fCode(0), fDisable(false), fDisplayable(0), fX(0), fY(0) {
     29 }
     30 
     31 SkEvents::SkEvents() {
     32 }
     33 
     34 SkEvents::~SkEvents() {
     35 }
     36 
     37 bool SkEvents::doEvent(SkAnimateMaker& maker, SkDisplayEvent::Kind kind, SkEventState* state) {
     38 /*#ifdef SK_DUMP_ENABLED
     39     if (maker.fDumpEvents) {
     40         SkDebugf("doEvent: ");
     41         SkString str;
     42         SkDump::GetEnumString(SkType_EventKind, kind, &str);
     43         SkDebugf("kind=%s ", str.c_str());
     44         if (state && state->fDisplayable)
     45             state->fDisplayable->SkDisplayable::dump(&maker);
     46         else
     47             SkDebugf("\n");
     48     }
     49 #endif*/
     50     bool handled = false;
     51     SkDisplayable** firstMovie = maker.fMovies.begin();
     52     SkDisplayable** endMovie = maker.fMovies.end();
     53     for (SkDisplayable** ptr = firstMovie; ptr < endMovie; ptr++) {
     54         SkDisplayMovie* movie = (SkDisplayMovie*) *ptr;
     55         if (kind != SkDisplayEvent::kOnload)
     56             movie->doEvent(kind, state);
     57     }
     58     SkDisplayable* displayable = state ? state->fDisplayable : NULL;
     59     int keyCode = state ? state->fCode : 0;
     60     int count = fEvents.count();
     61     for (int index = 0; index < count; index++) {
     62         SkDisplayEvent* evt = fEvents[index];
     63         if (evt->disable)
     64             continue;
     65         if (evt->kind != kind)
     66             continue;
     67         if (evt->code != (SkKey) -1) {
     68             if ((int) evt->code > keyCode || (int) (evt->fMax != (SkKey) -1 ? evt->fMax : evt->code) < keyCode)
     69                 continue;
     70             evt->fLastCode = (SkKey) keyCode;
     71         }
     72         if (evt->fTarget != NULL && evt->fTarget != displayable)
     73             continue;
     74         if (state == NULL || state->fDisable == 0) {
     75             if (kind >= SkDisplayEvent::kMouseDown && kind <= SkDisplayEvent::kMouseUp) {
     76                 evt->x = state->fX;
     77                 evt->y = state->fY;
     78             }
     79             if (evt->enableEvent(maker))
     80                 fError = true;
     81         }
     82         handled = true;
     83     }
     84     return handled;
     85 }
     86 
     87 #ifdef SK_DUMP_ENABLED
     88 void SkEvents::dump(SkAnimateMaker& maker) {
     89     int index;
     90     SkTDDrawableArray& drawArray = maker.fDisplayList.fDrawList;
     91     int count = drawArray.count();
     92     for (index = 0; index < count; index++) {
     93         SkDrawable* drawable = drawArray[index];
     94         drawable->dumpEvents();
     95     }
     96     count = fEvents.count();
     97     for (index = 0; index < count; index++) {
     98         SkDisplayEvent* evt = fEvents[index];
     99         evt->dumpEvent(&maker);
    100     }
    101 }
    102 #endif
    103 
    104 // currently this only removes onLoad events
    105 void SkEvents::removeEvent(SkDisplayEvent::Kind kind, SkEventState* state) {
    106     int keyCode = state ? state->fCode : 0;
    107     SkDisplayable* displayable = state ? state->fDisplayable : NULL;
    108     for (SkDisplayEvent** evtPtr = fEvents.begin(); evtPtr < fEvents.end(); evtPtr++) {
    109         SkDisplayEvent* evt = *evtPtr;
    110         if (evt->kind != kind)
    111             continue;
    112         if (evt->code != (SkKey) -1) {
    113             if ((int) evt->code > keyCode || (int) (evt->fMax != (SkKey) -1 ? evt->fMax : evt->code) < keyCode)
    114                 continue;
    115         }
    116         if (evt->fTarget != NULL && evt->fTarget != displayable)
    117             continue;
    118         int index = fEvents.find(evt);
    119         fEvents.remove(index);
    120     }
    121 }
    122