File indexing completed on 2024-12-22 04:10:03

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisLazyCreateTransformMaskKeyframesCommand.h"
0008 
0009 #include "kis_transform_mask.h"
0010 #include "kis_keyframe_channel.h"
0011 
0012 KisLazyCreateTransformMaskKeyframesCommand::KisLazyCreateTransformMaskKeyframesCommand(KisTransformMaskSP mask, KUndo2Command *parent)
0013     : KisCommandUtils::AggregateCommand(parent)
0014     , m_mask(mask)
0015 {
0016 }
0017 
0018 bool KisLazyCreateTransformMaskKeyframesCommand::maskHasAnimation(KisTransformMaskSP mask)
0019 {
0020     const QVector<QString> ids = {KisKeyframeChannel::PositionX.id(),
0021                                   KisKeyframeChannel::PositionY.id(),
0022                                   KisKeyframeChannel::ScaleX.id(),
0023                                   KisKeyframeChannel::ScaleY.id(),
0024                                   KisKeyframeChannel::ShearX.id(),
0025                                   KisKeyframeChannel::ShearY.id(),
0026                                   KisKeyframeChannel::RotationX.id(),
0027                                   KisKeyframeChannel::RotationY.id(),
0028                                   KisKeyframeChannel::RotationZ.id()};
0029 
0030     Q_FOREACH (const QString &id, ids) {
0031         if (mask->getKeyframeChannel(id)) return true;
0032     }
0033 
0034     return false;
0035 }
0036 
0037 void KisLazyCreateTransformMaskKeyframesCommand::populateChildCommands()
0038 {
0039     QScopedPointer<KUndo2Command> parentCommand(new KUndo2Command);
0040 
0041     KIS_SAFE_ASSERT_RECOVER_RETURN(m_mask->parent());
0042 
0043     const int time = m_mask->parent()->original()->defaultBounds()->currentTime();
0044 
0045     auto addKeyframe = [this, time] (const KoID &channelId, KUndo2Command *parentCommand)
0046     {
0047         KisKeyframeChannel *channel = m_mask->getKeyframeChannel(channelId.id());
0048         KIS_SAFE_ASSERT_RECOVER_RETURN(channel);
0049 
0050         if (!channel->keyframeAt(time)){
0051             const int activeTime = channel->activeKeyframeTime(time);
0052             channel->copyKeyframe(activeTime, time, parentCommand);
0053         }
0054     };
0055 
0056     addKeyframe(KisKeyframeChannel::PositionX, parentCommand.data());
0057     addKeyframe(KisKeyframeChannel::PositionY, parentCommand.data());
0058 
0059     addKeyframe(KisKeyframeChannel::ScaleX, parentCommand.data());
0060     addKeyframe(KisKeyframeChannel::ScaleY, parentCommand.data());
0061 
0062     addKeyframe(KisKeyframeChannel::ShearX, parentCommand.data());
0063     addKeyframe(KisKeyframeChannel::ShearY, parentCommand.data());
0064 
0065     addKeyframe(KisKeyframeChannel::RotationX, parentCommand.data());
0066     addKeyframe(KisKeyframeChannel::RotationY, parentCommand.data());
0067     addKeyframe(KisKeyframeChannel::RotationZ, parentCommand.data());
0068 
0069     addCommand(parentCommand.take());
0070 }