1
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkDisplayEvents.h"
11#include "SkAnimateMaker.h"
12#include "SkAnimator.h"
13#include "SkDisplayEvent.h"
14#include "SkDisplayMovie.h"
15#include "SkDrawable.h"
16#ifdef SK_DEBUG
17#include "SkDump.h"
18#endif
19
20SkEventState::SkEventState() : fCode(0), fDisable(false), fDisplayable(0), fX(0), fY(0) {
21}
22
23SkEvents::SkEvents() {
24}
25
26SkEvents::~SkEvents() {
27}
28
29bool SkEvents::doEvent(SkAnimateMaker& maker, SkDisplayEvent::Kind kind, SkEventState* state) {
30/*#ifdef SK_DUMP_ENABLED
31    if (maker.fDumpEvents) {
32        SkDebugf("doEvent: ");
33        SkString str;
34        SkDump::GetEnumString(SkType_EventKind, kind, &str);
35        SkDebugf("kind=%s ", str.c_str());
36        if (state && state->fDisplayable)
37            state->fDisplayable->SkDisplayable::dump(&maker);
38        else
39            SkDebugf("\n");
40    }
41#endif*/
42    bool handled = false;
43    SkDisplayable** firstMovie = maker.fMovies.begin();
44    SkDisplayable** endMovie = maker.fMovies.end();
45    for (SkDisplayable** ptr = firstMovie; ptr < endMovie; ptr++) {
46        SkDisplayMovie* movie = (SkDisplayMovie*) *ptr;
47        if (kind != SkDisplayEvent::kOnload)
48            movie->doEvent(kind, state);
49    }
50    SkDisplayable* displayable = state ? state->fDisplayable : NULL;
51    int keyCode = state ? state->fCode : 0;
52    int count = fEvents.count();
53    for (int index = 0; index < count; index++) {
54        SkDisplayEvent* evt = fEvents[index];
55        if (evt->disable)
56            continue;
57        if (evt->kind != kind)
58            continue;
59        if (evt->code != (SkKey) -1) {
60            if ((int) evt->code > keyCode || (int) (evt->fMax != (SkKey) -1 ? evt->fMax : evt->code) < keyCode)
61                continue;
62            evt->fLastCode = (SkKey) keyCode;
63        }
64        if (evt->fTarget != NULL && evt->fTarget != displayable)
65            continue;
66        if (state == NULL || state->fDisable == 0) {
67            if (kind >= SkDisplayEvent::kMouseDown && kind <= SkDisplayEvent::kMouseUp) {
68                evt->x = state->fX;
69                evt->y = state->fY;
70            }
71            if (evt->enableEvent(maker))
72                fError = true;
73        }
74        handled = true;
75    }
76    return handled;
77}
78
79#ifdef SK_DUMP_ENABLED
80void SkEvents::dump(SkAnimateMaker& maker) {
81    int index;
82    SkTDDrawableArray& drawArray = maker.fDisplayList.fDrawList;
83    int count = drawArray.count();
84    for (index = 0; index < count; index++) {
85        SkDrawable* drawable = drawArray[index];
86        drawable->dumpEvents();
87    }
88    count = fEvents.count();
89    for (index = 0; index < count; index++) {
90        SkDisplayEvent* evt = fEvents[index];
91        evt->dumpEvent(&maker);
92    }
93}
94#endif
95
96// currently this only removes onLoad events
97void SkEvents::removeEvent(SkDisplayEvent::Kind kind, SkEventState* state) {
98    int keyCode = state ? state->fCode : 0;
99    SkDisplayable* displayable = state ? state->fDisplayable : NULL;
100    for (SkDisplayEvent** evtPtr = fEvents.begin(); evtPtr < fEvents.end(); evtPtr++) {
101        SkDisplayEvent* evt = *evtPtr;
102        if (evt->kind != kind)
103            continue;
104        if (evt->code != (SkKey) -1) {
105            if ((int) evt->code > keyCode || (int) (evt->fMax != (SkKey) -1 ? evt->fMax : evt->code) < keyCode)
106                continue;
107        }
108        if (evt->fTarget != NULL && evt->fTarget != displayable)
109            continue;
110        int index = fEvents.find(evt);
111        fEvents.remove(index);
112    }
113}
114