From ae94b4b9fc5b4443f6d9eb6bb450de1def108cdb Mon Sep 17 00:00:00 2001 From: Fox Caminiti Date: Wed, 24 Aug 2022 20:40:39 -0400 Subject: fixes for gl core; create/delete developing --- bezier.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 64 insertions(+), 13 deletions(-) (limited to 'bezier.cpp') diff --git a/bezier.cpp b/bezier.cpp index f41bd06..e5f9bf9 100644 --- a/bezier.cpp +++ b/bezier.cpp @@ -177,15 +177,52 @@ Mask_PushPoint(mask *Mask, v2 Pos) Mask->NumberOfPoints++; } -// Mask_DeletePoint(mask *Mask, uint32 p) -// { -// } +static void +Mask_ShiftPointers(mask *Mask, int16 Increment, int16 StopAt) { + if (Increment > 0) { + int16 i = Mask->NumberOfPoints - 1; + while (i >= StopAt) { + mask_point *CurrentPoint = &Mask->Point[i]; + mask_point *NextPoint = &Mask->Point[i + Increment]; + *NextPoint = *CurrentPoint; + i--; + } + } else { + int16 i = StopAt; + while (i <= Mask->NumberOfPoints - 1) { + mask_point *CurrentPoint = &Mask->Point[i]; + mask_point *NextPoint = &Mask->Point[i - Increment]; + *CurrentPoint = *NextPoint; + i++; + } + } +} + +static void +Mask_DeletePoint(memory *Memory, mask *Mask, uint16 Index) +{ + History_Entry_Commit(Memory, action_entry_default, "Delete keyframe"); + mask_point *MaskPointIndex = &Mask->Point[Index]; + History_Action_StoreData(Memory, MaskPointIndex, sizeof(mask_point)); + + History_Action_Change_Decrement(Memory, &Mask->NumberOfPoints, action_type_change_u16); + // History_Action_Shift(Memory, action_type_shift_bezier, Mask, -1, Index); + void *StartingAddress = &Mask->Point[0]; + History_Action_Shift_2(Memory, StartingAddress, sizeof(mask_point), Mask->NumberOfPoints, -1, Index); + // Mask_ShiftPointers(Mask, -1, Index); + History_Entry_End(Memory); +} +// It's more useful to input the ratio here instead of the cursor position +// since we have to use it to calculate the four new handle lengths (two on the +// new point and one on each edge). static void -Mask_AddPointToCurve(mask *Mask, uint16 Index, real32 ratio) +Mask_AddPointToCurve(memory *Memory, mask *Mask, uint16 Index, real32 ratio) { mask_point *Point0 = &Mask->Point[Index]; mask_point *Point1 = &Mask->Point[Index+1]; + if (Index + 1 == Mask->NumberOfPoints) + Point1 = &Mask->Point[0]; v2 Point0_Pos_Right = Point0->Pos + Point0->TangentRight; v2 Point1_Pos_Left = Point1->Pos + Point1->TangentLeft; @@ -196,18 +233,32 @@ Mask_AddPointToCurve(mask *Mask, uint16 Index, real32 ratio) v2 NewHandleRight = Line_RatioToPoint(Top_Half, Handle1_Half, ratio); v2 NewPos = Line_RatioToPoint(NewHandleLeft, NewHandleRight, ratio); - Point0->TangentRight = -(Point0->Pos - Handle0_Half); - Point1->TangentLeft = -(Point1->Pos - Handle1_Half); + History_Entry_Commit(Memory, action_entry_default, "Add point to curve"); + + v2 NewPoint0Pos = -(Point0->Pos - Handle0_Half); + v2 NewPoint1Pos = -(Point1->Pos - Handle1_Half); + History_Action_Change_V2(Memory, &Point0->TangentRight, &Point0->TangentRight, &NewPoint0Pos); + History_Action_Change_V2(Memory, &Point1->TangentLeft, &Point1->TangentLeft, &NewPoint1Pos); + + void *StartingAddress = &Mask->Point[0]; + History_Action_Shift_2(Memory, StartingAddress, sizeof(mask_point), Mask->NumberOfPoints, 1, Index); - for (int i = Mask->NumberOfPoints - 1; i > Index; i--) { - Mask->Point[i+1] = Mask->Point[i]; - } mask_point *PointToAdd = &Mask->Point[Index+1]; - PointToAdd->Pos = NewPos; - PointToAdd->TangentLeft = -(NewPos - NewHandleLeft); - PointToAdd->TangentRight = -(NewPos - NewHandleRight); - Mask->NumberOfPoints++; + // NOTE(fox): The above shift duplicates the keyframe at Index into where + // we're writing, Index+1. I'm using the Change action (which is normally + // for changing values that already exist) on this intermediate keyframe + // slot to save having to write a bunch of special actions for shifting and + // adding new data. + + History_Action_Change_V2(Memory, &PointToAdd->Pos, &PointToAdd->Pos, &NewPos); + v2 NewLeftPos = -(NewPos - NewHandleLeft); + v2 NewRightPos = -(NewPos - NewHandleRight); + History_Action_Change_V2(Memory, &PointToAdd->TangentLeft, &PointToAdd->TangentLeft, &NewLeftPos); + History_Action_Change_V2(Memory, &PointToAdd->TangentRight, &PointToAdd->TangentRight, &NewRightPos); + + History_Action_Change_Increment(Memory, &Mask->NumberOfPoints, action_type_change_u16); + History_Entry_End(Memory); } static void -- cgit v1.2.3