Home | History | Annotate | Download | only in animation
      1 /*
      2  * Copyright (C) 2013 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 
     32 #include "config.h"
     33 #include "core/animation/Player.h"
     34 
     35 #include "core/animation/DocumentTimeline.h"
     36 #include "core/animation/TimedItem.h"
     37 
     38 namespace WebCore {
     39 
     40 PassRefPtr<Player> Player::create(DocumentTimeline* timeline, TimedItem* content)
     41 {
     42     ASSERT(timeline);
     43     return adoptRef(new Player(timeline, content));
     44 }
     45 
     46 Player::Player(DocumentTimeline* timeline, TimedItem* content)
     47     : m_pauseStartTime(nullValue())
     48     , m_playbackRate(1)
     49     , m_timeDrift(0)
     50     , m_startTime(effectiveTime(timeline->currentTime()))
     51     , m_content(content)
     52     , m_timeline(timeline)
     53 {
     54     ASSERT(m_startTime >= 0);
     55     if (m_content)
     56         m_content->attach(this);
     57     update();
     58 }
     59 
     60 Player::~Player()
     61 {
     62     if (m_content)
     63         m_content->detach();
     64 }
     65 
     66 double Player::currentTimeBeforeDrift() const
     67 {
     68     return (effectiveTime(m_timeline->currentTime()) - m_startTime) * m_playbackRate;
     69 }
     70 
     71 double Player::pausedTimeDrift() const
     72 {
     73     ASSERT(paused());
     74     return currentTimeBeforeDrift() - m_pauseStartTime;
     75 }
     76 
     77 double Player::timeDrift() const
     78 {
     79     return paused() ? pausedTimeDrift() : m_timeDrift;
     80 }
     81 
     82 double Player::currentTime() const
     83 {
     84     return currentTimeBeforeDrift() - timeDrift();
     85 }
     86 
     87 bool Player::update()
     88 {
     89     if (!m_content)
     90         return false;
     91 
     92     double newTime = isNull(m_timeline->currentTime()) ? nullValue() : currentTime();
     93     m_content->updateInheritedTime(newTime);
     94     return m_content->isCurrent() || m_content->isInEffect();
     95 }
     96 
     97 void Player::cancel()
     98 {
     99     if (!m_content)
    100         return;
    101 
    102     m_content->detach();
    103     m_content = 0;
    104 }
    105 
    106 void Player::setCurrentTime(double seekTime)
    107 {
    108     if (paused())
    109         m_pauseStartTime = seekTime;
    110     else
    111         m_timeDrift = currentTimeBeforeDrift() - seekTime;
    112 
    113     update();
    114 }
    115 
    116 void Player::setPaused(bool newValue)
    117 {
    118     if (paused() == newValue)
    119         return;
    120 
    121     if (newValue)
    122         m_pauseStartTime = currentTime();
    123     else {
    124         m_timeDrift = pausedTimeDrift();
    125         m_pauseStartTime = nullValue();
    126     }
    127 }
    128 
    129 void Player::setPlaybackRate(double newRate)
    130 {
    131     double previousTime = currentTime();
    132     m_playbackRate = newRate;
    133     setCurrentTime(previousTime);
    134 }
    135 
    136 } // namespace
    137