summaryrefslogtreecommitdiff
path: root/src/layer.cpp
blob: f1ce0f4c08ca9d235266d0b55e776e8ea442a22e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405

static property_channel
Property_InitFloat(real32 Val, real32 ScrubVal, real32 MinVal = PROPERTY_REAL_MIN, real32 MaxVal = PROPERTY_REAL_MAX, bool32 AlwaysInteger = 0);

static block_layer *
Layer_Init(project_data *File, memory *Memory)
{
    if (File->Layer_Count + 1 > MAX_LAYERS) {
        Assert(0);
    }
    block_layer *Layer = (block_layer *)Memory_Block_AllocateAddress(Memory, F_Layers);
    History_Action_Block_Swap(Memory, F_Layers, Layer);

    *Layer = {};
    Layer->Occupied = 1;

    Layer->Block_String_Index = Memory_Block_AllocateNew(Memory, F_Strings);
    block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Layer->Block_String_Index, 0);
    sprintf(String->Char, "Layer %i", File->Layer_Count + 1);  // CSbros...
    History_Action_Swap(Memory, F_File, sizeof(String->Occupied), &String->Occupied);
    String->Occupied = 1;

    Layer->x =        Property_InitFloat(0.0f, 1.0f);
    Layer->y =        Property_InitFloat(0.0f, 1.0f);
    Layer->ax =       Property_InitFloat(0.5f, 0.005f);
    Layer->ay =       Property_InitFloat(0.5f, 0.005f);
    Layer->scale =    Property_InitFloat(1.0f, 0.005f);
    Layer->rotation = Property_InitFloat(0.0f, 1.0f);
    Layer->opacity =  Property_InitFloat(1.0f, 0.005f, 0.0f, 1.0f);
    Layer->time =     Property_InitFloat(0.0f, 1.0f, 0, 100000, 1);

    Layer->IsVisible = 1;

    History_Action_Swap(Memory, F_File, sizeof(File->Layer_Count), &File->Layer_Count);
    File->Layer_Count++;

    return Layer;
}

// lots of cleanup...
static void
Layer_Delete(project_data *File, project_state *State, memory *Memory, uint32 Index)
{
    block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index);
    History_Action_Block_Swap(Memory, F_Layers, Layer);
    Layer->Occupied = 0;

    block_string *String = (block_string *)Memory_Block_AddressAtIndex(Memory, F_Strings, Layer->Block_String_Index, 0);
    History_Action_Block_Swap(Memory, F_Strings, String);
    String->Occupied = 0;

    for (int i = 0; i < Layer->Block_Effect_Count; i++) {
        block_effect *Effect = (block_effect *)Memory_Block_AddressAtIndex(Memory, F_Effects, Layer->Block_Effect_Index[i]);
        header_effect *EffectHeader = Effect_EntryFromID(State, Effect->ID);
        for (int h = 0; h < EffectHeader->Property_Count; h++) {
            header_property ChannelHeader = State->Property[EffectHeader->PropertyStartIndex + h];
            property_channel *Property = (property_channel *)Memory_Block_AddressAtIndex(Memory, F_Properties, Effect->Block_Property_Index[h]);
            if (Property->Block_Bezier_Count) {
                block_bezier *Bezier = (block_bezier *)Memory_Block_AddressAtIndex(Memory, F_Bezier, Property->Block_Bezier_Index[i], 0);
                History_Action_Block_Swap(Memory, F_Bezier, Bezier);
                Bezier->Occupied = 0;
            }
            History_Action_Block_Swap(Memory, F_Properties, Property);
            Property->Occupied = 0;
        }
        History_Action_Block_Swap(Memory, F_Effects, Effect);
        Effect->Occupied = 0;
    }
    History_Action_Swap(Memory, F_File, sizeof(File->Layer_Count), &File->Layer_Count);
    File->Layer_Count--;
}

static layer_transforms
Layer_GetTransforms(block_layer *Layer) {
    return { Layer->x.CurrentValue, Layer->y.CurrentValue, Layer->ax.CurrentValue, Layer->ay.CurrentValue, Layer->rotation.CurrentValue, Layer->scale.CurrentValue };
}

static int
Layer_GetTopOffset(project_data *File, memory *Memory)
{
    if (File->Layer_Count == 0)
        return 11;
    int TopOffset = 9999;
    int h = 0, c = 0, i = 0;
    while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i);
        if (Layer->Block_Composition_Index == File->PrincipalCompIndex) {
            TopOffset = (Layer->Vertical_Offset < TopOffset) ? Layer->Vertical_Offset : TopOffset;
        }
    }
    return TopOffset;
}


static void
Layer_UpdateMasksEffects(project_state *State, block_layer *Layer, memory *Memory, void *EffectBitmapAddress,
        int Width, int Height, int BytesPerPixel)
{
    uint64 Size = Width*Height*BytesPerPixel;

    // We need two of these: one with multisampling enabled and a
    // non-multisampled one that we can blit to.
    gl_effect_layer TestL = {};
    gl_effect_layer TestM = {};

    GL_UpdateTexture(&TestL, EffectBitmapAddress, Width, Height, BytesPerPixel, 0);
    GL_UpdateTexture(&TestM, EffectBitmapAddress, Width, Height, BytesPerPixel, 1);

    for (int i = 0; i < Layer->Block_Effect_Count; i++)
    {
        block_effect Effect = *(block_effect *)Memory_Block_AddressAtIndex(Memory, F_Effects, Layer->Block_Effect_Index[i]);
        header_effect *EffectEntry = Effect_EntryFromID(State, Effect.ID);

        if (Effect.IsToggled) {
            uint64 Size = (sizeof(real32) * MAX_PROPERTIES_PER_EFFECT) + (sizeof(real32) * 10);
            real32 *Data;
            if (EffectEntry->DisplayType == effect_display_type_curves) {
                Data = (real32 *)Memory_PushScratch(Memory, Size);
                uint16 SortedPointIndex[MAX_PROPERTIES_PER_EFFECT];
                v2 *SortedPointValues = (v2 *)(Data + 5);
                for (int c = 0; c < 5; c++) {
                    *(Data + c) = Effect.ExtraData[c];
                    uint32 Shift = MAX_PROPERTIES_PER_EFFECT / 5 * c;
                    uint16 *SortedPointIndexPlayhead = SortedPointIndex + Shift;
                    v2 *SortedPointValuesPlayhead = SortedPointValues + Shift;
                    Effect_Curves_Sort(Memory, &Effect, SortedPointIndexPlayhead, c);
                    for (int a = 0; a < Effect.ExtraData[c]; a++) {
                        *SortedPointValuesPlayhead = Effect_V2(Memory, &Effect, SortedPointIndexPlayhead[a]);
                        SortedPointValuesPlayhead++;
                    }
                }
            } else {
                Data = (real32 *)Memory_PushScratch(Memory, Size);
                for (int c = 0; c < EffectEntry->Property_Count; c++) {
                    property_channel *Property = (property_channel *)Memory_Block_AddressAtIndex(Memory, F_Properties, Effect.Block_Property_Index[c]);
                    Data[c] = Property->CurrentValue;
                }
            }
            EffectEntry->func(Data, Width, Height, BytesPerPixel, EffectBitmapAddress, EffectEntry->GLShaderIndex);
            Memory_PopScratch(Memory, Size);
        }
    }
    /*
    if (Layer->NumberOfMasks) {
        for (int i = 0; i < Layer->NumberOfMasks; i++) {
            file_mask_header *MaskHeader = (file_mask_header *)((uint8 *)Layer + sizeof(file_layer) + MaskOffset);
            if (MaskHeader->IsClosed && MaskHeader->IsToggled) {
                mask_point *Point = (mask_point *)((uint8 *)MaskHeader + sizeof(file_mask_header));
                Mask_TriangulateAndRasterize(TestM, TestL, Memory, MaskHeader, Point, Source->Width, Source->Height, Source->BytesPerPixel, EffectBitmapAddress);
            }
        }
        Bitmap_StencilAlpha(SourceBitmapAddress, EffectBitmapAddress, Source->BytesPerPixel, Size);
    }

    Layer->OutputBitmapLocation = EffectBitmapAddress;
    */

    GL_DeleteHWBuffer(&TestL);
    GL_DeleteHWBuffer(&TestM);
}

static void
Layer_ToggleChannel(project_data *File, memory *Memory, int32 a)
{
    int h = 0, c = 0, i = 0;
    while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i))
    {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i);
        if (Layer->IsSelected)
            Layer->Property[a].IsToggled ^= 1;
    }
}

static void
Layer_Select(memory *Memory, project_state *State, int32 i)
{
    block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i);
    Layer->IsSelected = true;
    State->MostRecentlySelectedLayer = i;
    State->RecentSelectionType = selection_type_layer;
}

static void
Layer_Select_RecurseUp(memory *Memory, project_state *State, int32 i, int16 RecursionIdx[MAX_PRECOMP_RECURSIONS], uint32 Recursions)
{
    for (int a = 1; a <= Recursions; a++) {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, RecursionIdx[a]);
        Layer->IsSelected = 2;
    }
}

static void
Layer_DeselectAll(project_data *File, project_state *State, memory *Memory) {
    int h = 0, c = 0, i = 0;
    while (Block_Loop(Memory, F_Layers, File->Layer_Count, &h, &c, &i)) {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, i);
        Layer->IsSelected = false;
    }
    State->MostRecentlySelectedLayer = -1;
}

// h: index of the total amount of properties and effects
// c: index of the amount of properties in a given effect
// p: prior property's keyframe count, so we can increment the sorted keyframe array properly
static bool32
Layer_LoopChannels(project_state *State, memory *Memory, sorted_property_array **SortedProperty, uint16 **SortedKeyframe, block_layer *Layer,
                   property_channel **Property, block_effect **EffectOut, int *h, int *c, int *p)
{
    uint32 Amount = AmountOf(Layer->Property) + Layer->Block_Effect_Count;
    // Assert(Layer->Block_Effect_Count < 2);
    while (*h < Amount) {
        if (*h < AmountOf(Layer->Property) && *c == 0) {
            *Property = &Layer->Property[*h];
            if (*h != 0) {
                *SortedProperty += 1;
                *SortedKeyframe += *p;
            }
            *h += 1;
            *p = (**Property).Keyframe_Count;
            return 1;
        } else {
            uint16 EffectIdx = Layer->Block_Effect_Index[*h - AmountOf(Layer->Property)];
            block_effect *Effect = (block_effect *)Memory_Block_AddressAtIndex(Memory, F_Effects, EffectIdx);
            if (EffectOut)
                *EffectOut = Effect;
            header_effect *EffectHeader = Effect_EntryFromID(State, Effect->ID);
            while (*c < EffectHeader->Property_Count) {
                // header_property ChannelHeader = State->Property[EffectHeader->PropertyStartIndex + c];
                *Property = (property_channel *)Memory_Block_AddressAtIndex(Memory, F_Properties, Effect->Block_Property_Index[*c]);
                *SortedProperty += 1;
                *SortedKeyframe += *p;
                *p = (**Property).Keyframe_Count;
                *c += 1;
                return 1;
            }
            *h += 1;
            *c = 0;
        }
    }
    Assert(*h != (Amount - 1));
    return 0;
}

static void
Layer_ToggleAllChannels(project_state *State, memory *Memory, block_layer *Layer,
                        sorted_comp_array *SortedCompStart, sorted_layer_array *SortedLayerStart,
                        sorted_property_array *SortedPropertyStart, uint16 *SortedKeyframeArray)
{
    bool32 ToggleMode = 1;
    {
        sorted_property_array *InfoLocation = SortedPropertyStart + SortedLayerStart->SortedPropertyStart;
        uint16 *ArrayLocation = SortedKeyframeArray + SortedLayerStart->SortedKeyframeStart;
        int h = 0, c = 0, p = 0;
        property_channel *Property = NULL;
        block_effect *Effect = NULL;
        while (Layer_LoopChannels(State, Memory, &InfoLocation, &ArrayLocation, Layer, &Property, &Effect, &h, &c, &p))
        {
            if (Property->IsToggled) {
                ToggleMode = 0;
                break;
            }
        }
    }
    sorted_property_array *InfoLocation = SortedPropertyStart + SortedLayerStart->SortedPropertyStart;
    uint16 *ArrayLocation = SortedKeyframeArray + SortedLayerStart->SortedKeyframeStart;
    int h = 0, c = 0, p = 0;
    property_channel *Property = NULL;
    block_effect *Effect = NULL;
    while (Layer_LoopChannels(State, Memory, &InfoLocation, &ArrayLocation, Layer, &Property, &Effect, &h, &c, &p))
    {
        if (Property->Keyframe_Count) {
            Property->IsToggled = ToggleMode;
        }
    }
}

static void
Layer_Select_Traverse(uint16 PrincipalCompIndex, memory *Memory, project_state *State, int32 IndexToFind, sorted_comp_array *SortedCompArray, sorted_layer_array *SortedLayerArray,
                      int16 RecursionIdx[MAX_PRECOMP_RECURSIONS], int32 *Recursions)
{
    uint16 CompIndex = 0;
    if (RecursionIdx[*Recursions] == -1) {
        CompIndex = PrincipalCompIndex;
    } else {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, RecursionIdx[*Recursions]);
        CompIndex = Layer->Block_Source_Index;
    }
    sorted_layer_array *SortedLayerStart = Sorted_GetLayerStart(SortedLayerArray, SortedCompArray, CompIndex);
    sorted_comp_array SortedCompStart = SortedCompArray[CompIndex];
    uint32 RecursionsCurrent = *Recursions;
    for (int i = SortedCompStart.LayerCount - 1; i >= 0; i--)
    {
        sorted_layer_array SortEntry = SortedLayerStart[i];
        uint32 Index_Physical = SortEntry.Block_Layer_Index;
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical);
        if (Layer->IsPrecomp && Layer->IsSelected == 2) {
            *Recursions = RecursionsCurrent + 1;
            RecursionIdx[*Recursions] = Index_Physical;
            Layer_Select_Traverse(PrincipalCompIndex, Memory, State, IndexToFind, SortedCompArray, SortedLayerArray, RecursionIdx, Recursions);
        } else if (Index_Physical == IndexToFind) {
            return;
        }
    }
}

static v2
Layer_TraverseForPoint(project_data *File, project_state *State, memory *Memory, v2 PrincipalCompUV, sorted_comp_array *SortedCompArray, sorted_layer_array *SortedLayerArray)
{
    int16 RecursionIdx[MAX_PRECOMP_RECURSIONS] = {};
    RecursionIdx[0] = -1;
    int32 Recursions = 0;
    sorted_layer_array *SortedLayerStart = Sorted_GetLayerStart(SortedLayerArray, SortedCompArray, File->PrincipalCompIndex);
    sorted_comp_array SortedCompStart = SortedCompArray[File->PrincipalCompIndex];
    block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, File->PrincipalCompIndex);
    Layer_Select_Traverse(File->PrincipalCompIndex, Memory, State, State->Brush.LayerToPaint_Index, SortedCompArray, SortedLayerArray, RecursionIdx, &Recursions);
    v2 PointUV = {0, 0};
    int OuterWidth = Comp->Width, OuterHeight = Comp->Height;
    int InnerWidth = 0, InnerHeight = 0;
    if (Recursions == 0) {
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, State->Brush.LayerToPaint_Index);
        layer_transforms T = Layer_GetTransforms(Layer);
        Layer_GetDimensions(Memory, Layer, &InnerWidth, &InnerHeight);
        PointUV = T_CompUVToLayerUV(T, OuterWidth, OuterHeight, InnerWidth, InnerHeight, PrincipalCompUV);
    } else {
        for (int i = 1; i <= Recursions; i++) {
            block_layer *InnerLayer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, RecursionIdx[i]);
            layer_transforms T = Layer_GetTransforms(InnerLayer);
            Layer_GetDimensions(Memory, InnerLayer, &InnerWidth, &InnerHeight);
            PointUV = T_CompUVToLayerUV(T, OuterWidth, OuterHeight, InnerWidth, InnerHeight, PrincipalCompUV);
            OuterWidth = InnerWidth;
            OuterHeight = InnerHeight;
        }
    }
    return PointUV * V2(InnerWidth, InnerHeight);
}

static int32
Layer_TestSelection(memory *Memory, project_state *State, ui *UI, sorted_comp_array *SortedCompArray, sorted_layer_array *SortedLayerArray, uint16 PrincipalIndex)
{
    block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, PrincipalIndex);
    sorted_comp_array SortedCompStart = SortedCompArray[PrincipalIndex];
    sorted_layer_array *SortedLayerStart = Sorted_GetLayerStart(SortedLayerArray, SortedCompArray, PrincipalIndex);
    int SelectionCount = 0;
    int SelectedLayerIndex = 0;
    for (int i = SortedCompStart.LayerCount - 1; i >= 0; i--) {
        sorted_layer_array SortEntry = SortedLayerStart[i];
        uint32 Index_Physical = SortEntry.Block_Layer_Index;
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical);
        block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Layer->Block_Source_Index);
        layer_transforms T = Layer_GetTransforms(Layer);
        v2 UV = T_CompUVToLayerUV(T, Comp->Width, Comp->Height, Source->Width, Source->Height, State->TempZoomRatio);
        if (UV.x <= 1.0f && UV.x >= 0.0f && UV.y <= 1.0f && UV.y >= 0.0f && Layer->IsSelected)
        {
            SelectionCount++;
            SelectedLayerIndex = i;
        }
    }
    int32 LayerIndex = -1;
    for (int i = SortedCompStart.LayerCount - 1; i >= 0; i--) {
        sorted_layer_array SortEntry = SortedLayerStart[i];
        uint32 Index_Physical = SortEntry.Block_Layer_Index;
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical);
        block_source *Source = (block_source *)Memory_Block_AddressAtIndex(Memory, F_Sources, Layer->Block_Source_Index);
        layer_transforms T = Layer_GetTransforms(Layer);
        v2 UV = T_CompUVToLayerUV(T, Comp->Width, Comp->Height, Source->Width, Source->Height, State->TempZoomRatio);
        if (UV.x <= 1.0f && UV.x >= 0.0f && UV.y <= 1.0f && UV.y >= 0.0f && !Layer->IsSelected)
        {
            if (SelectionCount == 1) {
                if (i < SelectedLayerIndex) {
                    LayerIndex = Index_Physical;
                    break;
                }
            } else {
                LayerIndex = Index_Physical;
                break;
            }
        }
        // if (Layer->IsPrecomp) {
        //     Layer_RecursiveDeselect(Memory, SortedCompArray, SortedLayerArray, TargetIndex, Layer->Block_Source_Index);
        // }
        // if (Layer->Block_Composition_Index != TargetIndex) {
        //     Layer->IsSelected = false;
        // }
    }
    return LayerIndex;
}

static void
Layer_RecursiveDeselect(memory *Memory, sorted_comp_array *SortedCompArray, sorted_layer_array *SortedLayerArray, uint16 TargetIndex, uint16 PrincipalIndex)
{
    block_composition *Comp = (block_composition *)Memory_Block_AddressAtIndex(Memory, F_Precomps, PrincipalIndex);
    sorted_comp_array SortedCompStart = SortedCompArray[PrincipalIndex];
    sorted_layer_array *SortedLayerStart = Sorted_GetLayerStart(SortedLayerArray, SortedCompArray, PrincipalIndex);
    for (int i = 0; i < SortedCompStart.LayerCount; i++) {
        sorted_layer_array SortEntry = SortedLayerStart[i];
        uint32 Index_Physical = SortEntry.Block_Layer_Index;
        block_layer *Layer = (block_layer *)Memory_Block_AddressAtIndex(Memory, F_Layers, Index_Physical);
        if (Layer->IsPrecomp) {
            Layer_RecursiveDeselect(Memory, SortedCompArray, SortedLayerArray, TargetIndex, Layer->Block_Source_Index);
        }
        if (Layer->Block_Composition_Index != TargetIndex) {
            Layer->IsSelected = false;
        }
    }
}