File indexing completed on 2024-12-22 04:16:52

0001 /*
0002  *  SPDX-FileCopyrightText: 2016 Jouni Pentikäinen <joupent@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "test_animated_transform_parameters.h"
0008 
0009 #include "kis_transform_mask.h"
0010 #include <testutil.h>
0011 #include "kistest.h"
0012 #include "tool_transform_args.h"
0013 #include "commands_new/KisSimpleModifyTransformMaskCommand.h"
0014 #include "commands_new/KisLazyCreateTransformMaskKeyframesCommand.h"
0015 #include "kis_image_animation_interface.h"
0016 #include "kis_transform_mask_params_interface.h"
0017 #include "KisAnimatedTransformMaskParamsHolder.h"
0018 #include "kis_keyframe_channel.h"
0019 
0020 #include <KoToolRegistry.h>
0021 
0022 void KisAnimatedTransformParametersTest::initTestCase()
0023 {
0024     KoToolRegistry::instance();
0025 }
0026 
0027 QSharedPointer<KisTransformMaskAdapter> adapterFromParams(KisTransformMaskParamsInterfaceSP params)
0028 {
0029     return params.dynamicCast<KisTransformMaskAdapter>();
0030 }
0031 
0032 ToolTransformArgs argsFromParams(KisTransformMaskParamsInterfaceSP params) {
0033     return *adapterFromParams(params)->transformArgs();
0034 }
0035 
0036 void KisAnimatedTransformParametersTest::testTransformKeyframing()
0037 {
0038     TestUtil::MaskParent p;
0039     KisTransformMaskSP mask = new KisTransformMask(p.image, "mask");
0040     p.image->addNode(mask, p.layer);
0041 
0042     // Make mask animated
0043     QList<KoID> ids = {
0044         KisKeyframeChannel::PositionX,
0045         KisKeyframeChannel::PositionY,
0046         KisKeyframeChannel::ScaleX,
0047         KisKeyframeChannel::ScaleY,
0048         KisKeyframeChannel::ShearX,
0049         KisKeyframeChannel::ShearY,
0050         KisKeyframeChannel::RotationX,
0051         KisKeyframeChannel::RotationY,
0052         KisKeyframeChannel::RotationZ
0053     };
0054 
0055     Q_FOREACH( const KoID& koid, ids ) {
0056         mask->getKeyframeChannel(koid.id(), true);
0057         QVERIFY(mask->getKeyframeChannel(koid.id(), false));
0058     }
0059 
0060     QVERIFY(!adapterFromParams(mask->transformParams())->isInitialized());
0061 
0062     ToolTransformArgs args;
0063 
0064     KUndo2Command firstFrameCommand;
0065     KUndo2Command secondFrameCommand;
0066 
0067     {
0068         p.image->animationInterface()->switchCurrentTimeAsync(0);
0069         p.image->waitForDone();
0070 
0071         args.setMode(ToolTransformArgs::FREE_TRANSFORM);
0072         args.setScaleX(0.75);
0073 
0074         new KisLazyCreateTransformMaskKeyframesCommand(mask, &firstFrameCommand);
0075         new KisSimpleModifyTransformMaskCommand(mask, toQShared(new KisTransformMaskAdapter(args)), {}, &firstFrameCommand);
0076         firstFrameCommand.redo();
0077 
0078         QVERIFY(adapterFromParams(mask->transformParams())->isInitialized());
0079         QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 0.75);
0080     }
0081 
0082     {
0083         p.image->animationInterface()->switchCurrentTimeAsync(10);
0084         p.image->waitForDone();
0085 
0086         args.setScaleX(0.5);
0087 
0088         new KisLazyCreateTransformMaskKeyframesCommand(mask, &secondFrameCommand);
0089         new KisSimpleModifyTransformMaskCommand(mask, toQShared(new KisTransformMaskAdapter(args)), {}, &secondFrameCommand);
0090         secondFrameCommand.redo();
0091 
0092         QVERIFY(adapterFromParams(mask->transformParams())->isInitialized());
0093         QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 0.5);
0094     }
0095 
0096     p.image->animationInterface()->switchCurrentTimeAsync(0);
0097     p.image->waitForDone();
0098     QVERIFY(p.image->animationInterface()->currentTime() == 0);
0099     QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 0.75);
0100 
0101     p.image->animationInterface()->switchCurrentTimeAsync(10);
0102     p.image->waitForDone();
0103     QVERIFY(p.image->animationInterface()->currentTime() == 10);
0104     QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 0.5);
0105 
0106     secondFrameCommand.undo();
0107     QVERIFY(p.image->animationInterface()->currentTime() == 10);
0108     QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 0.75);
0109 
0110     firstFrameCommand.undo();
0111     QVERIFY(p.image->animationInterface()->currentTime() == 10);
0112     QCOMPARE(argsFromParams(mask->transformParams()).scaleX(), 1.0);
0113 
0114     mask->forceUpdateTimedNode();
0115     p.image->waitForDone();
0116 }
0117 
0118 
0119 KISTEST_MAIN(KisAnimatedTransformParametersTest)