1 // Copyright (c) 2015 Sergio Gonzalez. All rights reserved.
2 // License: https://github.com/serge-rgb/milton#license
3 
4 
5 #pragma once
6 
7 #include "vector.h"
8 #include "StrokeList.h"
9 
10 #define MAX_LAYER_NAME_LEN          64
11 
12 struct LayerEffect
13 {
14     i32 type;  // LayerEffectType enum
15     b32 enabled;
16 
17     union {
18         struct {  // Blur
19             // In order for the blur to be resolution independent, we
20             // save the zoom level at the time of creation and always
21             // multiply the kernel size by original_scale / CURRENT_SCALE
22             i32 original_scale;
23             i32 kernel_size;
24         } blur;
25     };
26 
27     LayerEffect* next;
28 };
29 
30 enum LayerFlags
31 {
32     LayerFlags_VISIBLE = (1<<0),
33 };
34 
35 struct Layer
36 {
37     i32 id;
38 
39     StrokeList strokes;
40     char    name[MAX_LAYER_NAME_LEN];
41 
42     i32     flags;  // LayerFlags
43 
44     float alpha;
45 
46     LayerEffect* effects;
47 
48     Layer* prev;
49     Layer* next;
50 };
51 
52 enum LayerEffectType
53 {
54     LayerEffectType_BLUR,
55 
56     LayerEffectType_COUNT,
57 };
58 
59 #pragma pack (push, 1)
60 
61 // IMPORTANT: CanvasView needs to be a flat structure
62 //            because the whole struct is saved to the mlt file.
63 //            It should only grow down.
64 struct CanvasView
65 {
66     u32 size;                   // Size of struct
67     v2i screen_size;            // Size in pixels
68     i64 scale;                  // Zoom
69     v2i zoom_center;            // In pixels
70     v2l pan_center;             // In canvas scale
71     v3f background_color;
72     i32 working_layer_id;
73     f32 angle;                  // Rotation
74 };
75 
76 // Used to load older MLT files.
77 struct CanvasViewPreV9
78 {
79     v2i screen_size;            // Size in pixels
80     i64 scale;                  // Zoom
81     v2i zoom_center;            // In pixels
82     v2l pan_center;             // In canvas scale
83     v3f background_color;
84     i32 working_layer_id;
85     i32 num_layers;
86     u8 padding[4];
87 };
88 struct CanvasViewPreV4
89 {
90     v2i screen_size;            // Size in pixels
91     i32 scale;                  // Zoom
92     v2i zoom_center;            // In pixels
93     v2i pan_center;             // In canvas scale
94     i32 downsampling_factor;
95     i32 canvas_radius_limit;
96     v3f background_color;
97     i32 working_layer_id;
98     i32 num_layers;
99 };
100 
101 #pragma pack(pop)
102 
103 
104 v2l     canvas_to_raster (CanvasView* view, v2l canvas_point);
105 v2l     raster_to_canvas (CanvasView* view, v2l raster_point);
106 
107 b32     stroke_point_contains_point (v2l p0, i64 r0, v2l p1, i64 r1);  // Does point p0 with radius r0 contain point p1 with radius r1?
108 Rect    bounding_box_for_stroke (Stroke* stroke);
109 Rect    bounding_box_for_last_n_points (Stroke* stroke, i32 last_n);
110 
111 Rect    raster_to_canvas_bounding_rect(CanvasView* view, i32 x, i32 y, i32 w, i32 h, i64 scale);
112 Rect    canvas_to_raster_bounding_rect(CanvasView* view, Rect rect);
113 
114 void    reset_transform_at_origin(v2l* pan_center, i64* scale, f32* angle);
115 
116 // ---- Layer functions.
117 
118 
119 namespace layer {
120     Layer*  get_topmost (Layer* root);
121     Layer*  get_by_id (Layer* root_layer, i32 id);
122     void    layer_toggle_visibility (Layer* layer);
123     b32     layer_has_blur_effect (Layer* layer);
124     Stroke* layer_push_stroke (Layer* layer, Stroke stroke);
125     i32     number_of_layers (Layer* root);
126     void    free_layers (Layer* root);
127     i64     count_strokes (Layer* root);
128     i64     count_clipped_strokes (Layer* root, i32 num_workers);
129 }
130