Chromium Code Reviews| Index: Source/core/animation/InterpolableValue.h |
| diff --git a/Source/core/animation/InterpolableValue.h b/Source/core/animation/InterpolableValue.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..906e74867185d37a4c33fd9632214273be6986c6 |
| --- /dev/null |
| +++ b/Source/core/animation/InterpolableValue.h |
| @@ -0,0 +1,173 @@ |
| +/* |
| + * Copyright (C) 2014 Google Inc. All rights reserved. |
|
dstockwell
2014/02/25 02:14:51
Use the new/shorter license header.
shans
2014/02/25 03:26:43
Done.
|
| + * |
| + * Redistribution and use in source and binary forms, with or without |
| + * modification, are permitted provided that the following conditions are |
| + * met: |
| + * |
| + * * Redistributions of source code must retain the above copyright |
| + * notice, this list of conditions and the following disclaimer. |
| + * * Redistributions in binary form must reproduce the above |
| + * copyright notice, this list of conditions and the following disclaimer |
| + * in the documentation and/or other materials provided with the |
| + * distribution. |
| + * * Neither the name of Google Inc. nor the names of its |
| + * contributors may be used to endorse or promote products derived from |
| + * this software without specific prior written permission. |
| + * |
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + */ |
| + |
| +#ifndef InterpolableValue_h |
| +#define InterpolableValue_h |
| + |
| +#include "wtf/OwnPtr.h" |
| + |
| +namespace WebCore { |
| + |
| +class InterpolableValue { |
| +public: |
| + virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) = 0; |
| + virtual PassOwnPtr<InterpolableValue> clone() = 0; |
| + |
| + virtual bool isNumber() const { return false; } |
| + virtual bool isBool() const { return false; } |
| + virtual bool isList() const { return false; } |
| + |
| + virtual ~InterpolableValue() { } |
| +}; |
| + |
| +class InterpolableNumber; |
| +class InterpolableList; |
| + |
| +InterpolableNumber* toInterpolableNumber(InterpolableValue*); |
| +InterpolableList* toInterpolableList(InterpolableValue*); |
| + |
| +class InterpolableNumber : public InterpolableValue { |
| +public: |
| + static PassOwnPtr<InterpolableNumber> create(double value) |
| + { |
| + return adoptPtr(new InterpolableNumber(value)); |
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { |
|
dstockwell
2014/02/25 02:14:51
percentage -> progress
dstockwell
2014/02/25 02:14:51
these non-trivial functions should move to cpp
shans
2014/02/25 03:26:43
Done.
shans
2014/02/25 03:26:43
Done.
|
| + InterpolableNumber *otherNumber = toInterpolableNumber(&other); |
| + if (!percentage) |
| + return create(m_value); |
| + if (percentage == 1) |
| + return create(otherNumber->m_value); |
| + return create(m_value * (1 - percentage) + otherNumber->m_value * percentage); |
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create(m_value); } |
| + |
| + virtual bool isNumber() const OVERRIDE FINAL { return true; } |
| + |
| + double value() { return m_value; } |
| + |
| +private: |
| + double m_value; |
| + |
| + InterpolableNumber(double value) |
| + : m_value(value) |
| + { } |
| + |
| +}; |
| + |
| +class InterpolableBool : public InterpolableValue { |
| +public: |
| + PassOwnPtr<InterpolableBool> create(bool value) |
| + { |
| + return adoptPtr(new InterpolableBool(value)); |
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { |
| + if (percentage < 0.5) { |
| + return clone(); |
| + } |
| + return (other.clone()); |
|
dstockwell
2014/02/25 02:14:51
remove parens here
shans
2014/02/25 03:26:43
Done.
|
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create(m_value); } |
| + |
| + virtual bool isBool() const OVERRIDE FINAL { return true; } |
| + |
| + bool value() { return m_value; } |
| + |
| +private: |
| + bool m_value; |
| + |
| + InterpolableBool(bool value) |
| + : m_value(value) |
| + { } |
| +}; |
| + |
| +class InterpolableList : public InterpolableValue { |
| +public: |
| + static PassOwnPtr<InterpolableList> create(InterpolableList &other) |
| + { |
| + return adoptPtr(new InterpolableList(other)); |
| + } |
| + |
| + static PassOwnPtr<InterpolableList> create(size_t size) |
| + { |
| + return adoptPtr(new InterpolableList(size)); |
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> interpolate(InterpolableValue &other, double percentage) OVERRIDE FINAL { |
| + InterpolableList *otherList = toInterpolableList(&other); |
|
dstockwell
2014/02/25 02:14:51
assert same size
shans
2014/02/25 03:26:43
Done.
|
| + if (!percentage) { |
| + return create(*this); |
| + } |
| + if (percentage == 1) { |
| + return InterpolableList::create(*otherList); |
| + } |
| + |
| + OwnPtr<InterpolableList> result = create(m_size); |
| + for (size_t i = 0; i < m_size; i++) |
| + result->set(m_values.get()[i]->interpolate(*(otherList->m_values.get()[i]), percentage), i); |
| + return result.release(); |
| + } |
| + |
| + virtual PassOwnPtr<InterpolableValue> clone() OVERRIDE FINAL { return create(*this); } |
| + |
| + virtual bool isList() const OVERRIDE FINAL { return true; } |
| + |
| + void set(PassOwnPtr<InterpolableValue> value, size_t position) { m_values.get()[position] = value; } |
|
dstockwell
2014/02/25 02:14:51
ASSERT position < m_size? Or is the size mutable?
shans
2014/02/25 03:26:43
Done.
|
| + |
| +private: |
| + InterpolableList(size_t size) |
| + : m_size(size) |
| + { |
| + m_values = adoptArrayPtr(new OwnPtr<InterpolableValue>[size]); |
| + } |
| + |
| + InterpolableList(InterpolableList& other) |
| + : m_size(other.m_size) |
| + { |
| + m_values = adoptArrayPtr(new OwnPtr<InterpolableValue>[m_size]); |
| + for (size_t i = 0; i < m_size; i++) |
| + set(other.m_values.get()[i]->clone(), i); |
| + } |
| + |
| + size_t m_size; |
| + OwnPtr<OwnPtr<InterpolableValue>[]> m_values; |
| +}; |
| + |
| +DEFINE_TYPE_CASTS(InterpolableNumber, InterpolableValue, value, value->isNumber(), value.isNumber()); |
| +DEFINE_TYPE_CASTS(InterpolableBool, InterpolableValue, value, value->isBool(), value.isBool()); |
| +DEFINE_TYPE_CASTS(InterpolableList, InterpolableValue, value, value->isList(), value.isList()); |
| + |
| +} |
| + |
| +#endif |