Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

UX/Animation: Switch to active KEYFRAME when inserting/removing hold frames.

Emmet O'Neillddf8746f628d
diff --git a/libs/command/kis_command_ids.h b/libs/command/kis_command_ids.h
index 1b8e48f577..4ce7488e04 100644
--- a/libs/command/kis_command_ids.h
+++ b/libs/command/kis_command_ids.h
@@ -27,6 +27,7 @@ enum CommandId {
ChangeNodeNameId,
ChangeNodeCompositeOpId,
ChangeCurrentTimeId,
+ ChangeCurrentTimeToKeyId,
DisableUIUpdatesCommandId,
UpdateCommandId,
EmitImageSignalsCommandId,
diff --git a/libs/image/commands_new/kis_switch_current_time_command.cpp b/libs/image/commands_new/kis_switch_current_time_command.cpp
index 6c6b2180ac..9a35604e31 100644
--- a/libs/image/commands_new/kis_switch_current_time_command.cpp
+++ b/libs/image/commands_new/kis_switch_current_time_command.cpp
@@ -9,6 +9,8 @@
#include "kis_image.h"
#include "kis_image_animation_interface.h"
#include "kis_command_ids.h"
+#include "kis_keyframe_channel.h"
+#include "kis_node.h"
KisSwitchCurrentTimeCommand::KisSwitchCurrentTimeCommand(KisImageAnimationInterface *animation, int oldTime, int newTime, KUndo2Command *parent)
@@ -50,3 +52,52 @@ void KisSwitchCurrentTimeCommand::undo()
{
m_animation->requestTimeSwitchNonGUI(m_oldTime);
}
+
+// ====================================//
+
+KisSwitchCurrentTimeToKeyframeCommand::KisSwitchCurrentTimeToKeyframeCommand(KisImageAnimationInterface *animation, int oldTime, KisNodeSP node, KoID channelId, KisKeyframeSP targetKeyframe, KUndo2Command *parent)
+ : KUndo2Command(kundo2_i18n("Switch current time to keyframe"), parent),
+ m_animation(animation),
+ m_oldTime(oldTime),
+ m_node(node),
+ m_channelId(channelId),
+ m_targetKeyframe(targetKeyframe)
+{
+}
+
+KisSwitchCurrentTimeToKeyframeCommand::~KisSwitchCurrentTimeToKeyframeCommand()
+{
+}
+
+int KisSwitchCurrentTimeToKeyframeCommand::id() const
+{
+ return KisCommandUtils::ChangeCurrentTimeToKeyId;
+}
+
+bool KisSwitchCurrentTimeToKeyframeCommand::mergeWith(const KUndo2Command* command)
+{
+ // Note: Maybe we could merge this with a regular time switch command with a bit more logic.
+ const KisSwitchCurrentTimeToKeyframeCommand *other =
+ dynamic_cast<const KisSwitchCurrentTimeToKeyframeCommand*>(command);
+
+ if (!other || other->id() != id()) {
+ return false;
+ }
+
+ m_targetKeyframe = other->m_targetKeyframe;
+ return true;
+}
+
+void KisSwitchCurrentTimeToKeyframeCommand::redo()
+{
+ KisKeyframeChannel *channel = m_node->getKeyframeChannel(m_channelId.id());
+ if (channel) {
+ int newTime = channel->lookupKeyframeTime(m_targetKeyframe);
+ m_animation->requestTimeSwitchNonGUI(newTime);
+ }
+}
+
+void KisSwitchCurrentTimeToKeyframeCommand::undo()
+{
+ m_animation->requestTimeSwitchNonGUI(m_oldTime);
+}
diff --git a/libs/image/commands_new/kis_switch_current_time_command.h b/libs/image/commands_new/kis_switch_current_time_command.h
index 650d07ed75..2b7c1d82f8 100644
--- a/libs/image/commands_new/kis_switch_current_time_command.h
+++ b/libs/image/commands_new/kis_switch_current_time_command.h
@@ -9,6 +9,7 @@
#include "kritaimage_export.h"
#include "kis_types.h"
+#include "KoID.h"
#include <kundo2command.h>
@@ -33,4 +34,24 @@ private:
int m_newTime;
};
+class KRITAIMAGE_EXPORT KisSwitchCurrentTimeToKeyframeCommand : public KUndo2Command
+{
+public:
+ KisSwitchCurrentTimeToKeyframeCommand(KisImageAnimationInterface *animation, int oldTime, KisNodeSP node, KoID channelId, KisKeyframeSP targetKeyframe, KUndo2Command *parent = 0);
+ ~KisSwitchCurrentTimeToKeyframeCommand() override;
+
+ void redo() override;
+ void undo() override;
+
+ int id() const override;
+ bool mergeWith(const KUndo2Command* command) override;
+
+private:
+ KisImageAnimationInterface *m_animation;
+ int m_oldTime;
+ KisNodeSP m_node;
+ KoID m_channelId;
+ KisKeyframeSP m_targetKeyframe;
+};
+
#endif /* __KIS_SWITCH_CURRENT_TIME_COMMAND_H */
diff --git a/libs/image/kis_keyframe_channel.cpp b/libs/image/kis_keyframe_channel.cpp
index 2716050cd7..89c0520881 100644
--- a/libs/image/kis_keyframe_channel.cpp
+++ b/libs/image/kis_keyframe_channel.cpp
@@ -248,6 +248,13 @@ int KisKeyframeChannel::activeKeyframeTime(int time) const
return iter.key();
}
+int KisKeyframeChannel::lookupKeyframeTime(KisKeyframeSP toLookup)
+{
+ int time = m_d->keys.key(toLookup, -1);
+ KIS_ASSERT(time >= 0);
+ return time;
+}
+
int KisKeyframeChannel::firstKeyframeTime() const
{
if (m_d->keys.isEmpty()) {
diff --git a/libs/image/kis_keyframe_channel.h b/libs/image/kis_keyframe_channel.h
index 902a4edfee..066b7b8d57 100644
--- a/libs/image/kis_keyframe_channel.h
+++ b/libs/image/kis_keyframe_channel.h
@@ -102,6 +102,13 @@ public:
int activeKeyframeTime(int time) const;
int activeKeyframeTime() const { return activeKeyframeTime(currentTime()); }
+ /**
+ * @brief Search for the time for a given keyframe. (Reverse map lookup, so use sparingly.)
+ * @param toLookup - the keyframe to look up the time for.
+ * @return Returns the time of the keyframe.
+ */
+ int lookupKeyframeTime(KisKeyframeSP toLookup);
+
int firstKeyframeTime() const;
int previousKeyframeTime(const int time) const;
int nextKeyframeTime(const int time) const;
diff --git a/plugins/dockers/animation/KisAnimTimelineFramesModel.cpp b/plugins/dockers/animation/KisAnimTimelineFramesModel.cpp
index 0a22d10b6b..45c8261f12 100644
--- a/plugins/dockers/animation/KisAnimTimelineFramesModel.cpp
+++ b/plugins/dockers/animation/KisAnimTimelineFramesModel.cpp
@@ -966,18 +966,18 @@ bool KisAnimTimelineFramesModel::insertFrames(int dstColumn, const QList<int> &d
return true;
}
-bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selectedIndexes, int count)
+bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selectedIndexes, int insertCount)
{
- if (selectedIndexes.isEmpty() || count == 0) return true;
+ if (selectedIndexes.isEmpty() || insertCount == 0) return true;
- QScopedPointer<KUndo2Command> parentCommand(new KUndo2Command(kundo2_i18np("Insert frame", "Insert %1 frames", count)));
+ QScopedPointer<KUndo2Command> parentCommand(new KUndo2Command(kundo2_i18np("Insert frame", "Insert %1 frames", insertCount)));
{
KisImageBarrierLock locker(m_d->image);
+ // Find Keyframes in selection...
QSet<TimelineSelectionEntry> uniqueKeyframesInSelection;
-
- int minSelectedTime = std::numeric_limits<int>::max();
+ int earliestAffectedTime = std::numeric_limits<int>::max();
Q_FOREACH (const QModelIndex &index, selectedIndexes) {
KisNodeSP node = nodeAt(index);
@@ -986,7 +986,7 @@ bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selecte
KisRasterKeyframeChannel *channel = dynamic_cast<KisRasterKeyframeChannel*>(node->getKeyframeChannel(KisKeyframeChannel::Raster.id()));
if (!channel) continue;
- minSelectedTime = qMin(minSelectedTime, index.column());
+ earliestAffectedTime = qMin(earliestAffectedTime, index.column()); // Earliest of selection... (to be continued.)
int time = channel->activeKeyframeTime(index.column());
KisRasterKeyframeSP keyframe = channel->activeKeyframeAt<KisRasterKeyframe>(index.column());
@@ -996,6 +996,7 @@ bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selecte
}
}
+ // Determine which keyframes need to move and sort by time...
QList<TimelineSelectionEntry> keyframesToMove;
for (auto it = uniqueKeyframesInSelection.begin(); it != uniqueKeyframesInSelection.end(); ++it) {
@@ -1017,25 +1018,39 @@ bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selecte
if (keyframesToMove.isEmpty()) return true;
+ const int oldTime = m_d->image->animationInterface()->currentUITime();
const int maxColumn = columnCount();
- if (count > 0) {
- setLastVisibleFrame(columnCount() + count);
+ // Store the original active keyframe, so we can switch to its new time at the end.
+ // (This helps to keep artist drawing on the right keyframe even while adding and removing hold frames.)
+ QModelIndex activeIndex = index(m_d->activeLayerIndex, oldTime);
+ KisNodeSP activeNode = nodeAt(activeIndex);
+ KisRasterKeyframeChannel *activeChannel = dynamic_cast<KisRasterKeyframeChannel*>(activeNode->getKeyframeChannel(KisKeyframeChannel::Raster.id()));
+
+ KisRasterKeyframeSP originalActiveKeyframe = nullptr;
+ if (activeChannel) {
+ originalActiveKeyframe = activeChannel->activeKeyframeAt<KisRasterKeyframe>(oldTime);
+ }
+
+ // Create keyframe movement commands...
+ if (insertCount > 0) {
+ setLastVisibleFrame(columnCount() + insertCount);
}
Q_FOREACH (TimelineSelectionEntry entry, keyframesToMove) {
- int plannedFrameMove = count;
+ int plannedFrameMove = insertCount;
- if (count < 0) {
+ if (insertCount < 0) {
KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(entry.time > 0, false);
int prevKeyframeTime = entry.channel->previousKeyframeTime(entry.time);
KisRasterKeyframeSP prevFrame = entry.channel->keyframeAt<KisRasterKeyframe>(prevKeyframeTime);
KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(prevFrame, false);
- plannedFrameMove = qMax(count, prevKeyframeTime - entry.time + 1);
+ // Clamp values so that they never exceed or overlap the previous frame
+ plannedFrameMove = qMax(insertCount, prevKeyframeTime - entry.time + 1);
- minSelectedTime = qMin(minSelectedTime, prevKeyframeTime);
+ earliestAffectedTime = qMin(earliestAffectedTime, prevKeyframeTime); // No longer limited to selection.
}
KisNodeDummy *dummy = m_d->dummiesFacade->dummyForNode(entry.channel->node());
@@ -1055,6 +1070,15 @@ bool KisAnimTimelineFramesModel::insertHoldFrames(const QModelIndexList &selecte
true,
parentCommand.data());
}
+
+ if (originalActiveKeyframe) {
+ new KisSwitchCurrentTimeToKeyframeCommand(m_d->image->animationInterface(),
+ oldTime,
+ activeNode,
+ KisKeyframeChannel::Raster,
+ originalActiveKeyframe,
+ parentCommand.data());
+ }
}
KisProcessingApplicator::runSingleCommandStroke(m_d->image, parentCommand.take(),
diff --git a/plugins/dockers/animation/KisAnimTimelineFramesModel.h b/plugins/dockers/animation/KisAnimTimelineFramesModel.h
index 345276648a..92950c380e 100644
--- a/plugins/dockers/animation/KisAnimTimelineFramesModel.h
+++ b/plugins/dockers/animation/KisAnimTimelineFramesModel.h
@@ -73,7 +73,7 @@ public:
void makeClonesUnique(const QModelIndexList &indices);
bool insertFrames(int dstColumn, const QList<int> &dstRows, int count, int timing = 1);
- bool insertHoldFrames(const QModelIndexList &selectedIndexes, int count);
+ bool insertHoldFrames(const QModelIndexList &selectedIndexes, int insertCount);
QString audioChannelFileName() const;
void setAudioChannelFileName(const QFileInfo &fileName);