summaryrefslogtreecommitdiff
path: root/keyframes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'keyframes.cpp')
-rw-r--r--keyframes.cpp161
1 files changed, 85 insertions, 76 deletions
diff --git a/keyframes.cpp b/keyframes.cpp
index c9f7813..7fb7c13 100644
--- a/keyframes.cpp
+++ b/keyframes.cpp
@@ -81,25 +81,6 @@ CheckKeyframeSort(property_channel *Property, int32 Increment, int32 b)
}
static void
-ShiftKeyframeIndex(property_channel *Property, int16 Increment, int16 StopAt) {
- /*
- if (Increment > 0) {
- int16 i = Property->NumberOfTotalKeyframes - 1;
- while (i > StopAt) {
- Property->SortedIndex[i + Increment] = Property->SortedIndex[i];
- i--;
- }
- } else {
- int16 i = StopAt;
- while (i < Property->NumberOfTotalKeyframes - 1) {
- Property->SortedIndex[i] = Property->SortedIndex[i + Increment];
- i++;
- }
- }
- */
-}
-
-static void
DeleteKeyframeFromMemory(property_channel *Property, int16 Increment, int16 StopAt) {
if (Increment > 0) {
int16 i = Property->NumberOfTotalKeyframes - 1;
@@ -176,22 +157,6 @@ DeleteSelectedKeyframes(project_data *File, memory *Memory)
}
static void
-CalculatePropertyMinMax(property_channel *Property) {
- Property->LocalMaxVal = Property->MinVal;
- Property->LocalMinVal = Property->MaxVal;
- for (int16 i = 0; i < Property->NumberOfTotalKeyframes; i++) {
- keyframe *Keyframe = KeyframeLookup(Property, i);
- Property->LocalMinVal.f = Ceil(Property->LocalMinVal.f, Keyframe->Value.f);
- Property->LocalMaxVal.f = Floor(Property->LocalMaxVal.f, Keyframe->Value.f);
- }
- if (Property->LocalMinVal.f == Property->LocalMaxVal.f)
- {
- Property->LocalMaxVal.f += 1;
- }
-}
-
-
-static void
IncrementKeyframes(property_channel *Property, int16 Increment)
{
/*
@@ -262,78 +227,123 @@ ClampSurroundingKeyframeHandles(property_channel *Property, int16 b) {
}
-
static void
-ManualKeyframeInsertF(property_channel *Property, memory *Memory, int32 CurrentFrame, real32 Val)
-{
- if (!(Property->NumberOfTotalKeyframes % MAX_KEYFRAMES_PER_BLOCK)) {
- CreateKeyframeBlock(Property, Memory);
+ShiftKeyframes(property_channel *Property, int16 Increment, int16 StopAt) {
+ if (Increment > 0) {
+ int16 i = Property->NumberOfTotalKeyframes - 1;
+ while (i >= StopAt) {
+ keyframe *NextKeyframe = KeyframeLookup(Property, i + Increment);
+ keyframe *CurrentKeyframe = KeyframeLookup(Property, i);
+ *NextKeyframe = *CurrentKeyframe;
+ i--;
+ }
+ } else {
+ int16 i = StopAt;
+ while (i <= Property->NumberOfTotalKeyframes - 1) {
+ keyframe *NextKeyframe = KeyframeLookup(Property, i - Increment);
+ keyframe *CurrentKeyframe = KeyframeLookup(Property, i);
+ *CurrentKeyframe = *NextKeyframe;
+ i++;
+ }
}
- keyframe *Keyframe = NULL;
+}
+
+static uint32
+Keyframe_FindClosestIndex(property_channel *Property, int32 CurrentFrame, bool32 *Overlapping)
+{
if (Property->NumberOfTotalKeyframes == 0) {
- Keyframe = &Property->KeyframeBlock[0]->Keyframe[0];
- Property->NumberOfTotalKeyframes++;
+ *Overlapping = false;
+ return 0;
} else {
uint32 Index = Property->NumberOfTotalKeyframes;
- bool32 Found = false;
- while (!Found) {
+ for (;;) {
keyframe *PreviousKeyframe = KeyframeLookup(Property, Index - 1);
keyframe *NextKeyframe = KeyframeLookup(Property, Index + 1);
if (PreviousKeyframe->FrameNumber < CurrentFrame) {
- if (NextKeyframe->FrameNumber >= CurrentFrame || Index == Property->NumberOfTotalKeyframes) {
+ if (NextKeyframe->FrameNumber >= CurrentFrame) {
keyframe *CurrentKeyframe = KeyframeLookup(Property, Index);
- if (CurrentKeyframe->FrameNumber == CurrentFrame || Index == Property->NumberOfTotalKeyframes) {
- Keyframe = CurrentKeyframe;
- Property->NumberOfTotalKeyframes++;
+ if (CurrentKeyframe->FrameNumber == CurrentFrame) {
+ *Overlapping = true;
+ return Index;
} else {
if (CurrentKeyframe->FrameNumber > CurrentFrame) {
- Keyframe = KeyframeLookup(Property, Index);
+ *Overlapping = false;
+ return Index;
} else {
- Keyframe = KeyframeLookup(Property, Index + 1);
+ *Overlapping = false;
+ return Index + 1;
}
}
- Found = true;
+ } else if (Index == Property->NumberOfTotalKeyframes) {
+ *Overlapping = false;
+ return Index;
} else {
Index += (Property->NumberOfTotalKeyframes - Index) / 2;
}
// We can only progress from this first if statement if
// NextKeyframe is valid, so we need to check for these conditions.
} else if (Property->NumberOfTotalKeyframes == 1) {
- Property->NumberOfTotalKeyframes++;
- Keyframe = KeyframeLookup(Property, 0);
- Found = true;
+ keyframe *Keyframe = KeyframeLookup(Property, 0);
+ *Overlapping = (Keyframe->FrameNumber == CurrentFrame);
+ Index = 0;
+ return Index;
} else if (Property->NumberOfTotalKeyframes == 2) {
- keyframe *FirstKeyframe = KeyframeLookup(Property, Index + 1);
- if (FirstKeyframe->FrameNumber > CurrentFrame)
- Keyframe = KeyframeLookup(Property, 0);
- else
- Keyframe = KeyframeLookup(Property, 1);
- Property->NumberOfTotalKeyframes++;
- Found = true;
+ // We know we're smaller than index 1, so we just need to compare 0.
+ keyframe *FirstKeyframe = KeyframeLookup(Property, 0);
+ Index = (FirstKeyframe->FrameNumber > CurrentFrame) ? 0 : 1;
+ keyframe *Keyframe = KeyframeLookup(Property, Index);
+ *Overlapping = (Keyframe->FrameNumber == CurrentFrame);
+ return Index;
} else {
Index = Index / 2;
}
}
}
- Assert(!(Keyframe == NULL))
+}
+
+static void
+Keyframe_Insert(property_channel *Property, memory *Memory, int32 CurrentFrame, real32 Val)
+{
+ if (!(Property->NumberOfTotalKeyframes % MAX_KEYFRAMES_PER_BLOCK)) {
+ CreateKeyframeBlock(Property, Memory);
+ }
+ keyframe *Keyframe = NULL;
+ bool32 Overlapping = 0;
+ uint32 Index = Keyframe_FindClosestIndex(Property, CurrentFrame, &Overlapping);
+ Keyframe = KeyframeLookup(Property, Index);
+ if (Overlapping && Val == Keyframe->Value.f)
+ return;
+ if (!Overlapping) {
+ History_Entry_Commit(Memory, action_entry_default, "Insert keyframe");
+ if (Index != Property->NumberOfTotalKeyframes) {
+ History_Action_Shift(Memory, action_type_shift_keyframe, Property, 1, Index);
+ ShiftKeyframes(Property, 1, Index);
+ }
- Keyframe->FrameNumber = CurrentFrame;
- Keyframe->Value.f = Val;
- Keyframe->Type = bezier;
- Keyframe->TangentLeft = V2(-1, 0);
- Keyframe->TangentRight = V2(1, 0);
- Keyframe->ImguiID = RandomGlobalIncrement++;
- CalculatePropertyMinMax(Property);
+ History_Action_Change_Increment(Memory, &Property->NumberOfTotalKeyframes, action_type_change_u16);
+ History_Action_Change(Memory, &Keyframe->FrameNumber, &Keyframe->FrameNumber, &CurrentFrame, action_type_change_i32);
+ History_Action_Change_Increment(Memory, &RandomGlobalIncrement, action_type_change_u32);
+ History_Action_Change(Memory, &Keyframe->ImguiID, &Keyframe->ImguiID, &RandomGlobalIncrement, action_type_change_u32);
+
+ Keyframe->Type = bezier;
+ Keyframe->TangentLeft = V2(-1, 0);
+ Keyframe->TangentRight = V2(1, 0);
+ } else {
+ History_Entry_Commit(Memory, action_entry_default, "Adjust keyframe value");
+ }
+ History_Action_Change(Memory, &Keyframe->Value.f, &Keyframe->Value.f, &Val, action_type_change_r32);
+ // Keyframe->Value.f = Val;
+
+ History_Entry_End(Memory);
}
static void
CalculateKeyframesLinearly(uint16 CurrentFrame, struct property_channel *Property)
{
- /*
- keyframe *FirstKeyframe = KeyframeLookupIndex(Property, 0);
+ keyframe *FirstKeyframe = KeyframeLookup(Property, 0);
- keyframe *LastKeyframe = KeyframeLookupIndex(Property, Property->NumberOfTotalKeyframes - 1);
+ keyframe *LastKeyframe = KeyframeLookup(Property, Property->NumberOfTotalKeyframes - 1);
if (Property->NumberOfTotalKeyframes == 0) {
// do nothing
}
@@ -347,8 +357,8 @@ CalculateKeyframesLinearly(uint16 CurrentFrame, struct property_channel *Propert
}
else if (Property->NumberOfTotalKeyframes > 1) {
for (int i = 0; i < (Property->NumberOfTotalKeyframes - 1); i++) {
- keyframe *Keyframe = KeyframeLookupIndex(Property, i);
- keyframe *NextKeyframe = KeyframeLookupIndex(Property, i+1);
+ keyframe *Keyframe = KeyframeLookup(Property, i);
+ keyframe *NextKeyframe = KeyframeLookup(Property, i+1);
if (CurrentFrame >= Keyframe->FrameNumber &&
CurrentFrame <= NextKeyframe->FrameNumber)
{
@@ -386,5 +396,4 @@ CalculateKeyframesLinearly(uint16 CurrentFrame, struct property_channel *Propert
}
}
}
- */
}