File indexing completed on 2024-05-12 16:23:27

0001 /***************************************************************************
0002  *   Copyright (C) 2013 by Linuxstopmotion contributors;                   *
0003  *   see the AUTHORS file for details.                                     *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
0019  ***************************************************************************/
0020 
0021 #include "commandmove.h"
0022 
0023 #include "src/domain/animation/animationimpl.h"
0024 #include "src/domain/undo/command.h"
0025 
0026 class ErrorHandler;
0027 
0028 CommandMove::CommandMove(AnimationImpl& model, int fromScene, int fromFrame, int count,
0029         int toScene, int toFrame)
0030         : sv(model), fromSc(fromScene), fromFr(fromFrame), frameCount(count),
0031           toSc(toScene), toFr(toFrame) {
0032 }
0033 
0034 CommandMove::~CommandMove() {
0035 }
0036 
0037 template<typename T> void swap(T& a, T& b) {
0038     T t(a);
0039     a = b;
0040     b = t;
0041 }
0042 
0043 Command* CommandMove::execute() {
0044     sv.moveFrames(fromSc, fromFr, frameCount, toSc, toFr);
0045     if (fromSc != toSc) {
0046         swap(fromSc, toSc);
0047         swap(fromFr, toFr);
0048     } else if (fromFr + frameCount < toFr) {
0049         int t = toFr;
0050         toFr = fromFr;
0051         fromFr = t - frameCount;
0052     } else if (toFr < fromFr) {
0053         int t = toFr;
0054         toFr = fromFr + frameCount;
0055         fromFr = t;
0056     }
0057     // else it is a command that does nothing; which is it's own inverse!
0058     return this;
0059 }
0060 
0061 CommandMoveFactory::CommandMoveFactory(AnimationImpl& model) : sv(model) {
0062 }
0063 
0064 CommandMoveFactory::~CommandMoveFactory() {
0065 }
0066 
0067 Command* CommandMoveFactory::create(Parameters& ps, ErrorHandler&) {
0068     int sceneCount = sv.sceneCount();
0069     if (sceneCount == 0)
0070         return 0;
0071     int fs = ps.getInteger(0, sceneCount - 1);
0072     int framesInScene = sv.frameCount(fs);
0073     if (framesInScene == 0)
0074         return 0;
0075     int ff = ps.getInteger(0, framesInScene - 1);
0076     int fc = ps.getInteger(0, framesInScene - ff);
0077     int ts = ps.getInteger(0, sceneCount - 1);
0078     int tf = ps.getInteger(0, sv.frameCount(ts));
0079     return new CommandMove(sv, fs, ff, fc, ts, tf);
0080 }