gfx/skia/trunk/src/animator/SkDisplayEvent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/animator/SkDisplayEvent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,252 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2006 The Android Open Source Project
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +
    1.12 +
    1.13 +#include "SkDisplayEvent.h"
    1.14 +#include "SkAnimateMaker.h"
    1.15 +#include "SkDisplayApply.h"
    1.16 +#include "SkDisplayInput.h"
    1.17 +#include "SkDisplayList.h"
    1.18 +#ifdef SK_DEBUG
    1.19 +#include "SkDump.h"
    1.20 +#endif
    1.21 +#include "SkEvent.h"
    1.22 +#include "SkDisplayInput.h"
    1.23 +#include "SkKey.h"
    1.24 +#include "SkMetaData.h"
    1.25 +#include "SkScript.h"
    1.26 +#include "SkUtils.h"
    1.27 +
    1.28 +enum SkDisplayEvent_Properties {
    1.29 +    SK_PROPERTY(key),
    1.30 +    SK_PROPERTY(keys)
    1.31 +};
    1.32 +
    1.33 +#if SK_USE_CONDENSED_INFO == 0
    1.34 +
    1.35 +const SkMemberInfo SkDisplayEvent::fInfo[] = {
    1.36 +    SK_MEMBER(code, EventCode),
    1.37 +    SK_MEMBER(disable, Boolean),
    1.38 +    SK_MEMBER_PROPERTY(key, String), // a single key (also last key pressed)
    1.39 +    SK_MEMBER_PROPERTY(keys, String), // a single key or dash-delimited range of keys
    1.40 +    SK_MEMBER(kind, EventKind),
    1.41 +    SK_MEMBER(target, String),
    1.42 +    SK_MEMBER(x, Float),
    1.43 +    SK_MEMBER(y, Float)
    1.44 +};
    1.45 +
    1.46 +#endif
    1.47 +
    1.48 +DEFINE_GET_MEMBER(SkDisplayEvent);
    1.49 +
    1.50 +SkDisplayEvent::SkDisplayEvent() : code((SkKey) -1), disable(false),
    1.51 +    kind(kUser), x(0), y(0), fLastCode((SkKey) -1), fMax((SkKey) -1), fTarget(NULL) {
    1.52 +}
    1.53 +
    1.54 +SkDisplayEvent::~SkDisplayEvent() {
    1.55 +    deleteMembers();
    1.56 +}
    1.57 +
    1.58 +bool SkDisplayEvent::addChild(SkAnimateMaker& , SkDisplayable* child) {
    1.59 +    *fChildren.append() = child;
    1.60 +    return true;
    1.61 +}
    1.62 +
    1.63 +bool SkDisplayEvent::contains(SkDisplayable* match) {
    1.64 +    for (int index = 0; index < fChildren.count(); index++) {
    1.65 +        if (fChildren[index] == match || fChildren[index]->contains(match))
    1.66 +            return true;
    1.67 +    }
    1.68 +    return false;
    1.69 +}
    1.70 +
    1.71 +SkDisplayable* SkDisplayEvent::contains(const SkString& match) {
    1.72 +    for (int index = 0; index < fChildren.count(); index++) {
    1.73 +        SkDisplayable* child = fChildren[index];
    1.74 +        if (child->contains(match))
    1.75 +            return child;
    1.76 +    }
    1.77 +    return NULL;
    1.78 +}
    1.79 +
    1.80 +void SkDisplayEvent::deleteMembers() {
    1.81 +    for (int index = 0; index < fChildren.count(); index++) {
    1.82 +        SkDisplayable* evt = fChildren[index];
    1.83 +        delete evt;
    1.84 +    }
    1.85 +}
    1.86 +
    1.87 +#ifdef SK_DUMP_ENABLED
    1.88 +void SkDisplayEvent::dumpEvent(SkAnimateMaker* maker) {
    1.89 +    dumpBase(maker);
    1.90 +    SkString str;
    1.91 +    SkDump::GetEnumString(SkType_EventKind, kind, &str);
    1.92 +    SkDebugf("kind=\"%s\" ", str.c_str());
    1.93 +    if (kind == SkDisplayEvent::kKeyPress || kind == SkDisplayEvent::kKeyPressUp) {
    1.94 +        if (code >= 0)
    1.95 +            SkDump::GetEnumString(SkType_EventCode, code, &str);
    1.96 +        else
    1.97 +            str.set("none");
    1.98 +        SkDebugf("code=\"%s\" ", str.c_str());
    1.99 +    }
   1.100 +    if (kind == SkDisplayEvent::kKeyChar) {
   1.101 +        if (fMax != (SkKey) -1 && fMax != code)
   1.102 +            SkDebugf("keys=\"%c - %c\" ", code, fMax);
   1.103 +        else
   1.104 +            SkDebugf("key=\"%c\" ", code);
   1.105 +    }
   1.106 +    if (fTarget != NULL) {
   1.107 +        SkDebugf("target=\"%s\" ", fTarget->id);
   1.108 +    }
   1.109 +    if (kind >= SkDisplayEvent::kMouseDown && kind <= SkDisplayEvent::kMouseUp) {
   1.110 +        SkDebugf("x=\"%g\" y=\"%g\" ", SkScalarToFloat(x), SkScalarToFloat(y));
   1.111 +    }
   1.112 +    if (disable)
   1.113 +        SkDebugf("disable=\"true\" ");
   1.114 +    SkDebugf("/>\n");
   1.115 +}
   1.116 +#endif
   1.117 +
   1.118 +bool SkDisplayEvent::enableEvent(SkAnimateMaker& maker)
   1.119 +{
   1.120 +    maker.fActiveEvent = this;
   1.121 +    if (fChildren.count() == 0)
   1.122 +        return false;
   1.123 +    if (disable)
   1.124 +        return false;
   1.125 +#ifdef SK_DUMP_ENABLED
   1.126 +    if (maker.fDumpEvents) {
   1.127 +        SkDebugf("enable: ");
   1.128 +        dumpEvent(&maker);
   1.129 +    }
   1.130 +#endif
   1.131 +    SkDisplayList& displayList = maker.fDisplayList;
   1.132 +    for (int index = 0; index < fChildren.count(); index++) {
   1.133 +        SkDisplayable* displayable = fChildren[index];
   1.134 +        if (displayable->isGroup()) {
   1.135 +            SkTDDrawableArray* parentList = displayList.getDrawList();
   1.136 +            *parentList->append() = (SkDrawable*) displayable;  // make it findable before children are enabled
   1.137 +        }
   1.138 +        if (displayable->enable(maker))
   1.139 +            continue;
   1.140 +        if (maker.hasError())
   1.141 +            return true;
   1.142 +        if (displayable->isDrawable() == false)
   1.143 +            return true;    // error
   1.144 +        SkDrawable* drawable = (SkDrawable*) displayable;
   1.145 +        SkTDDrawableArray* parentList = displayList.getDrawList();
   1.146 +        *parentList->append() = drawable;
   1.147 +    }
   1.148 +    return false;
   1.149 +}
   1.150 +
   1.151 +bool SkDisplayEvent::getProperty(int index, SkScriptValue* value) const {
   1.152 +    switch (index) {
   1.153 +        case SK_PROPERTY(key):
   1.154 +        case SK_PROPERTY(keys): {
   1.155 +            value->fType = SkType_String;
   1.156 +            char scratch[8];
   1.157 +            SkKey convert = index == SK_PROPERTY(keys) ? code : fLastCode;
   1.158 +            size_t size = convert > 0 ? SkUTF8_FromUnichar(convert, scratch) : 0;
   1.159 +            fKeyString.set(scratch, size);
   1.160 +            value->fOperand.fString = &fKeyString;
   1.161 +            if (index != SK_PROPERTY(keys) || fMax == (SkKey) -1 || fMax == code)
   1.162 +                break;
   1.163 +            value->fOperand.fString->append("-");
   1.164 +            size = SkUTF8_FromUnichar(fMax, scratch);
   1.165 +            value->fOperand.fString->append(scratch, size);
   1.166 +            } break;
   1.167 +        default:
   1.168 +            SkASSERT(0);
   1.169 +            return false;
   1.170 +    }
   1.171 +    return true;
   1.172 +}
   1.173 +
   1.174 +void SkDisplayEvent::onEndElement(SkAnimateMaker& maker)
   1.175 +{
   1.176 +    if (kind == kUser)
   1.177 +        return;
   1.178 +    maker.fEvents.addEvent(this);
   1.179 +    if (kind == kOnEnd) {
   1.180 +        SkDEBUGCODE(bool found = ) maker.find(target.c_str(), &fTarget);
   1.181 +        SkASSERT(found);
   1.182 +        SkASSERT(fTarget && fTarget->isAnimate());
   1.183 +        SkAnimateBase* animate = (SkAnimateBase*) fTarget;
   1.184 +        animate->setHasEndEvent();
   1.185 +    }
   1.186 +}
   1.187 +
   1.188 +void SkDisplayEvent::populateInput(SkAnimateMaker& maker, const SkEvent& fEvent) {
   1.189 +    const SkMetaData& meta = fEvent.getMetaData();
   1.190 +    SkMetaData::Iter iter(meta);
   1.191 +    SkMetaData::Type    type;
   1.192 +    int number;
   1.193 +    const char* name;
   1.194 +    while ((name = iter.next(&type, &number)) != NULL) {
   1.195 +        if (name[0] == '\0')
   1.196 +            continue;
   1.197 +        SkDisplayable* displayable;
   1.198 +        SkInput* input;
   1.199 +        for (int index = 0; index < fChildren.count(); index++) {
   1.200 +            displayable = fChildren[index];
   1.201 +            if (displayable->getType() != SkType_Input)
   1.202 +                continue;
   1.203 +            input = (SkInput*) displayable;
   1.204 +            if (input->name.equals(name))
   1.205 +                goto found;
   1.206 +        }
   1.207 +        if (!maker.find(name, &displayable) || displayable->getType() != SkType_Input)
   1.208 +            continue;
   1.209 +        input = (SkInput*) displayable;
   1.210 +    found:
   1.211 +        switch (type) {
   1.212 +            case SkMetaData::kS32_Type:
   1.213 +                meta.findS32(name, &input->fInt);
   1.214 +                break;
   1.215 +            case SkMetaData::kScalar_Type:
   1.216 +                meta.findScalar(name, &input->fFloat);
   1.217 +                break;
   1.218 +            case SkMetaData::kPtr_Type:
   1.219 +                SkASSERT(0);
   1.220 +                break; // !!! not handled for now
   1.221 +            case SkMetaData::kString_Type:
   1.222 +                input->string.set(meta.findString(name));
   1.223 +                break;
   1.224 +            default:
   1.225 +                SkASSERT(0);
   1.226 +        }
   1.227 +    }
   1.228 +    // re-evaluate all animators that may have built their values from input strings
   1.229 +    for (SkDisplayable** childPtr = fChildren.begin(); childPtr < fChildren.end(); childPtr++) {
   1.230 +        SkDisplayable* displayable = *childPtr;
   1.231 +        if (displayable->isApply() == false)
   1.232 +            continue;
   1.233 +        SkApply* apply = (SkApply*) displayable;
   1.234 +        apply->refresh(maker);
   1.235 +    }
   1.236 +}
   1.237 +
   1.238 +bool SkDisplayEvent::setProperty(int index, SkScriptValue& value) {
   1.239 +    SkASSERT(index == SK_PROPERTY(key) || index == SK_PROPERTY(keys));
   1.240 +    SkASSERT(value.fType == SkType_String);
   1.241 +    SkString* string = value.fOperand.fString;
   1.242 +    const char* chars = string->c_str();
   1.243 +    int count = SkUTF8_CountUnichars(chars);
   1.244 +    SkASSERT(count >= 1);
   1.245 +    code = (SkKey) SkUTF8_NextUnichar(&chars);
   1.246 +    fMax = code;
   1.247 +    SkASSERT(count == 1 || index == SK_PROPERTY(keys));
   1.248 +    if (--count > 0) {
   1.249 +        SkASSERT(*chars == '-');
   1.250 +        chars++;
   1.251 +        fMax = (SkKey) SkUTF8_NextUnichar(&chars);
   1.252 +        SkASSERT(fMax >= code);
   1.253 +    }
   1.254 +    return true;
   1.255 +}

mercurial