aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/animator/SkDisplayPost.cpp
blob: cc45b216ba5ddf9e950c74b6931e8a019345eaaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298

/*
 * Copyright 2006 The Android Open Source Project
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkDisplayPost.h"
#include "SkAnimateMaker.h"
#include "SkAnimator.h"
#include "SkDisplayMovie.h"
#include "SkPostParts.h"
#include "SkScript.h"
#ifdef SK_DEBUG
#include "SkDump.h"
#include "SkTime.h"
#endif

enum SkPost_Properties {
    SK_PROPERTY(target),
    SK_PROPERTY(type)
};

#if SK_USE_CONDENSED_INFO == 0

const SkMemberInfo SkPost::fInfo[] = {
    SK_MEMBER(delay, MSec),
//  SK_MEMBER(initialized, Boolean),
    SK_MEMBER(mode, EventMode),
    SK_MEMBER(sink, String),
    SK_MEMBER_PROPERTY(target, String),
    SK_MEMBER_PROPERTY(type, String)
};

#endif

DEFINE_GET_MEMBER(SkPost);

SkPost::SkPost() : delay(0), /*initialized(SkBool(-1)), */ mode(kImmediate), fMaker(NULL),
    fSinkID(0), fTargetMaker(NULL), fChildHasID(false), fDirty(false) {
}

SkPost::~SkPost() {
    for (SkDataInput** part = fParts.begin(); part < fParts.end();  part++)
        delete *part;
}

bool SkPost::addChild(SkAnimateMaker& , SkDisplayable* child) {
    SkASSERT(child && child->isDataInput());
    SkDataInput* part = (SkDataInput*) child;
    *fParts.append() = part;
    return true;
}

bool SkPost::childrenNeedDisposing() const {
    return false;
}

void SkPost::dirty() {
    fDirty = true;
}

#ifdef SK_DUMP_ENABLED
void SkPost::dump(SkAnimateMaker* maker) {
    dumpBase(maker);
    SkString* eventType = new SkString();
    fEvent.getType(eventType);
    if (eventType->equals("user")) {
        const char* target = fEvent.findString("id");
        SkDebugf("target=\"%s\" ", target);
    }
    else
        SkDebugf("type=\"%s\" ", eventType->c_str());
    delete eventType;

    if (delay > 0) {
        SkDebugf("delay=\"%g\" ", SkScalarToFloat(SkScalarDiv(delay, 1000)));
    }
//  if (initialized == false)
//      SkDebugf("(uninitialized) ");
    SkString string;
    SkDump::GetEnumString(SkType_EventMode, mode, &string);
    if (!string.equals("immediate"))
        SkDebugf("mode=\"%s\" ", string.c_str());
    // !!! could enhance this to search through make hierarchy to show name of sink
    if (sink.size() > 0) {
        SkDebugf("sink=\"%s\" sinkID=\"%d\" ", sink.c_str(), fSinkID);
    } else if (fSinkID != maker->getAnimator()->getSinkID() && fSinkID != 0) {
        SkDebugf("sinkID=\"%d\" ", fSinkID);
    }
    const SkMetaData& meta = fEvent.getMetaData();
    SkMetaData::Iter iter(meta);
    SkMetaData::Type    type;
    int number;
    const char* name;
    bool closedYet = false;
    SkDisplayList::fIndent += 4;
    //this seems to work, but kinda hacky
    //for some reason the last part is id, which i don't want
    //and the parts seem to be in the reverse order from the one in which we find the
    //data itself
    //SkDataInput** ptr = fParts.end();
    //SkDataInput* data;
    //const char* ID;
    while ((name = iter.next(&type, &number)) != NULL) {
        //ptr--;
        if (strcmp(name, "id") == 0)
            continue;
        if (closedYet == false) {
            SkDebugf(">\n");
            closedYet = true;
        }
        //data = *ptr;
        //if (data->id)
        //    ID = data->id;
        //else
        //    ID = "";
        SkDebugf("%*s<data name=\"%s\" ", SkDisplayList::fIndent, "", name);
        switch (type) {
            case SkMetaData::kS32_Type: {
                int32_t s32;
                meta.findS32(name, &s32);
                SkDebugf("int=\"%d\" ", s32);
                } break;
            case SkMetaData::kScalar_Type: {
                SkScalar scalar;
                meta.findScalar(name, &scalar);
                SkDebugf("float=\"%g\" ", SkScalarToFloat(scalar));
                } break;
            case SkMetaData::kString_Type:
                SkDebugf("string=\"%s\" ", meta.findString(name));
                break;
            case SkMetaData::kPtr_Type: {//when do we have a pointer
                    void* ptr;
                    meta.findPtr(name, &ptr);
                    SkDebugf("0x%08x ", ptr);
                } break;
            case SkMetaData::kBool_Type: {
                bool boolean;
                meta.findBool(name, &boolean);
                SkDebugf("boolean=\"%s\" ", boolean ? "true " : "false ");
                } break;
            default:
                break;
        }
        SkDebugf("/>\n");
        //ptr++;
/*      perhaps this should only be done in the case of a pointer?
        SkDisplayable* displayable;
        if (maker->find(name, &displayable))
            displayable->dump(maker);
        else
            SkDebugf("\n");*/
    }
    SkDisplayList::fIndent -= 4;
    if (closedYet)
        dumpEnd(maker);
    else
        SkDebugf("/>\n");

}
#endif

bool SkPost::enable(SkAnimateMaker& maker ) {
    if (maker.hasError())
        return true;
    if (fDirty) {
        if (sink.size() > 0)
            findSinkID();
        if (fChildHasID) {
            SkString preserveID(fEvent.findString("id"));
            fEvent.getMetaData().reset();
            if (preserveID.size() > 0)
                fEvent.setString("id", preserveID);
            for (SkDataInput** part = fParts.begin(); part < fParts.end();  part++) {
                if ((*part)->add())
                    maker.setErrorCode(SkDisplayXMLParserError::kErrorAddingDataToPost);
            }
        }
        fDirty = false;
    }
#ifdef SK_DUMP_ENABLED
    if (maker.fDumpPosts) {
        SkDebugf("post enable: ");
        dump(&maker);
    }
#if defined SK_DEBUG_ANIMATION_TIMING
    SkString debugOut;
    SkMSec time = maker.getAppTime();
    debugOut.appendS32(time - maker.fDebugTimeBase);
    debugOut.append(" post id=");
    debugOut.append(_id);
    debugOut.append(" enable=");
    debugOut.appendS32(maker.fEnableTime - maker.fDebugTimeBase);
    debugOut.append(" delay=");
    debugOut.appendS32(delay);
#endif
#endif
//  SkMSec adjustedDelay = maker.adjustDelay(maker.fEnableTime, delay);
    SkMSec futureTime = maker.fEnableTime + delay;
    fEvent.setFast32(futureTime);
#if defined SK_DEBUG && defined SK_DEBUG_ANIMATION_TIMING
    debugOut.append(" future=");
    debugOut.appendS32(futureTime - maker.fDebugTimeBase);
    SkDebugf("%s\n", debugOut.c_str());
#endif
    SkEventSinkID targetID = fSinkID;
    bool isAnimatorEvent = true;
    SkAnimator* anim = maker.getAnimator();
    if (targetID == 0) {
        isAnimatorEvent = fEvent.findString("id") != NULL;
        if (isAnimatorEvent)
            targetID = anim->getSinkID();
        else if (maker.fHostEventSinkID)
            targetID = maker.fHostEventSinkID;
        else
            return true;
    } else
        anim = fTargetMaker->getAnimator();
    if (delay == 0) {
        if (isAnimatorEvent && mode == kImmediate)
            fTargetMaker->doEvent(fEvent);
        else
            anim->onEventPost(new SkEvent(fEvent), targetID);
    } else
        anim->onEventPostTime(new SkEvent(fEvent), targetID, futureTime);
    return true;
}

void SkPost::findSinkID() {
    // get the next delimiter '.' if any
    fTargetMaker = fMaker;
    const char* ch = sink.c_str();
    do {
        const char* end = strchr(ch, '.');
        size_t len = end ? (size_t) (end - ch) : strlen(ch);
        SkDisplayable* displayable = NULL;
        if (SK_LITERAL_STR_EQUAL("parent", ch, len)) {
            if (fTargetMaker->fParentMaker)
                fTargetMaker = fTargetMaker->fParentMaker;
            else {
                fTargetMaker->setErrorCode(SkDisplayXMLParserError::kNoParentAvailable);
                return;
            }
        } else {
            fTargetMaker->find(ch, len, &displayable);
            if (displayable == NULL || displayable->getType() != SkType_Movie) {
                fTargetMaker->setErrorCode(SkDisplayXMLParserError::kExpectedMovie);
                return;
            }
            SkDisplayMovie* movie = (SkDisplayMovie*) displayable;
            fTargetMaker = movie->fMovie.fMaker;
        }
        if (end == NULL)
            break;
        ch = ++end;
    } while (true);
    SkAnimator* anim = fTargetMaker->getAnimator();
    fSinkID = anim->getSinkID();
}

bool SkPost::hasEnable() const {
    return true;
}

void SkPost::onEndElement(SkAnimateMaker& maker) {
    fTargetMaker = fMaker = &maker;
    if (fChildHasID == false) {
        for (SkDataInput** part = fParts.begin(); part < fParts.end();  part++)
            delete *part;
        fParts.reset();
    }
}

void SkPost::setChildHasID() {
    fChildHasID = true;
}

bool SkPost::setProperty(int index, SkScriptValue& value) {
    SkASSERT(value.fType == SkType_String);
    SkString* string = value.fOperand.fString;
    switch(index) {
        case SK_PROPERTY(target): {
            fEvent.setType("user");
            fEvent.setString("id", *string);
            mode = kImmediate;
            } break;
        case SK_PROPERTY(type):
            fEvent.setType(*string);
            break;
        default:
            SkASSERT(0);
            return false;
    }
    return true;
}