murrelet_draw 0.1.2

drawing functions for murrelet, a livecode framework
Documentation
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#![allow(dead_code)]
use crate::{curve_drawer::CurveDrawer, draw::*, transform2d::*};
use glam::*;
use lerpable::Lerpable;
use md5::{Digest, Md5};
use murrelet_common::*;
use murrelet_livecode::{lazy::ControlLazyNodeF32, livecode::ControlF32, types::ControlVecElement};
use murrelet_livecode_derive::Livecode;

fn _black() -> [ControlF32; 4] {
    [
        ControlF32::Raw(0.0),
        ControlF32::Raw(0.0),
        ControlF32::Raw(0.0),
        ControlF32::Raw(1.0),
    ]
}

fn _black_lazy() -> Vec<ControlVecElement<ControlLazyNodeF32>> {
    vec![
        ControlVecElement::Single(ControlLazyNodeF32::Float(0.0)),
        ControlVecElement::Single(ControlLazyNodeF32::Float(0.0)),
        ControlVecElement::Single(ControlLazyNodeF32::Float(0.0)),
        ControlVecElement::Single(ControlLazyNodeF32::Float(1.0)),
    ]
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleFilled {
    pub color: MurreletColor, // fill color
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
    #[livecode(serde_default = "_black")]
    pub stroke_color: MurreletColor,
}
impl MurreletStyleFilled {
    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: true,
            filled: true,
            color: MurreletColorStyle::color(self.color),
            stroke_weight: self.stroke_weight,
            stroke_color: MurreletColorStyle::color(self.stroke_color),
            ..Default::default()
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct StyledPathSvgFill {
    pub src: StrId, // for drawing a gpu generated texture
    pub transform: Mat4,
    pub alpha: f32,
    pub width: f32, // % of 100
    pub height: f32,
}

pub fn fixed_pt_f32_to_str(x: f32) -> String {
    FixedPointF32::new(x).to_str()
}

fn find_center_and_size<F: IsPolyline>(points: &F) -> (Vec2, f32, f32) {
    let mut p = points.into_iter_vec2();
    // hmmm
    let s = points
        .into_iter_vec2()
        .fold(Vec2::ZERO, |acc, vec| acc + vec);
    let center = s / p.len() as f32;
    let size = points
        .into_iter_vec2()
        .map(|x| x.distance(center))
        .max_by(|a, b| a.partial_cmp(b).unwrap())
        .unwrap_or(0.01);
    let first_loc = p.next().unwrap();
    let first_point = first_loc - center;
    let angle = f32::atan2(first_point.y, first_point.x);

    (center, size, angle)
}

impl StyledPathSvgFill {
    // for nannou
    pub fn to_points_textured<F: IsPolyline>(&self, raw_points: &F) -> Vec<(Vec2, Vec2)> {
        // so using this to center it
        let (center, size, _angle) = find_center_and_size(raw_points);
        let transform = self.transform; // * mat4_from_mat3_transform(Mat3::from_angle(-angle));
        let points = raw_points
            .into_iter_vec2()
            .map(|x| {
                let y = (x - center) / size;
                let z = transform.transform_vec2(y);
                (x, z)
            })
            .collect::<Vec<_>>();

        points
    }

    // for svg
    pub fn hash(&self) -> String {
        let mut hasher = Md5::new();
        hasher.update(self.src.as_str());

        // okay so borrowing from svg, we only care about a few numbers here...
        let [[a, b, _, _], [c, d, _, _], _, [e, f, _, _]] = self.transform.to_cols_array_2d();
        for &v_f32 in &[a, b, c, d, e, f] {
            hasher.update(fixed_pt_f32_to_str(v_f32));
        }
        hasher.update(fixed_pt_f32_to_str(self.alpha));
        hasher.update(fixed_pt_f32_to_str(self.width));
        hasher.update(fixed_pt_f32_to_str(self.height));

        let result = hasher.finalize();

        hex::encode(result)[..6].to_owned()
    }

    pub fn new(src: StrId, transform: Mat4, alpha: f32, width: f32, height: f32) -> Self {
        Self {
            src,
            transform,
            alpha,
            width,
            height,
        }
    }

    pub fn with_alpha(&self, alpha: f32) -> StyledPathSvgFill {
        let mut p = self.clone();
        p.alpha = alpha;
        p
    }
}

#[derive(Clone, Debug, Livecode, Lerpable)]
pub struct MurreletStyleFilledSvg {
    #[livecode(kind = "none")]
    pub pattern_id: String, // reference to canvas
    #[livecode(serde_default = "default")]
    pub transform: Transform2d,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
    #[livecode(serde_default = "ones")]
    pub alpha: f32,
    #[livecode(serde_default = "ones")]
    pub width: f32,
    #[livecode(serde_default = "ones")]
    pub height: f32,
}
impl MurreletStyleFilledSvg {
    fn to_color_style(&self) -> StyledPathSvgFill {
        StyledPathSvgFill {
            src: StrId::new(&self.pattern_id),
            transform: self.transform.to_mat4(),
            alpha: self.alpha,
            width: self.width,
            height: self.height,
        }
    }

    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: true,
            filled: true,
            color: MurreletColorStyle::SvgFill(self.to_color_style()),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleRGBAFill {
    #[lerpable(func = "lerpify_vec3")]
    pub rgb: Vec3, // red and green, can be negative
    #[livecode(serde_default = "ones")]
    pub a: f32,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleRGBAFill {
    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: true,
            filled: true,
            color: MurreletColorStyle::rgbafill(self.rgb, self.a),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleRGBALine {
    #[lerpable(func = "lerpify_vec3")]
    pub rgb: Vec3, // red and green, can be negative
    #[livecode(serde_default = "ones")]
    pub a: f32,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleRGBALine {
    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: false,
            filled: false,
            color: MurreletColorStyle::rgbafill(self.rgb, self.a),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleDAFill {
    #[lerpable(func = "lerpify_vec3")]
    pub rgb: Vec3, // red and green, can be negative
    #[livecode(serde_default = "zeros")]
    a: f32,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleDAFill {
    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: true,
            filled: true,
            color: MurreletColorStyle::rgbafill(self.rgb, self.a),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleOutlined {
    pub color: MurreletColor, // fill color
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleOutlined {
    pub fn new(color: MurreletColor, stroke_weight: f32) -> Self {
        Self {
            color,
            stroke_weight,
        }
    }

    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: true,
            filled: false,
            color: MurreletColorStyle::color(self.color),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }

    fn black() -> MurreletStyleOutlined {
        MurreletStyleOutlined {
            color: MurreletColor::hsva(0.0, 0.0, 0.0, 1.0),
            stroke_weight: 0.0,
        }
    }

    // fn to_style_points(&self) -> MurreletStyle {
    //     MurreletStyle {
    //         points: true,
    //         closed: false,
    //         filled: false,
    //         color: MurreletColorStyle::color(self.color),
    //         stroke_weight: self.stroke_weight,
    //     }
    // }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStylePoints {
    pub color: MurreletColor, // fill color
    pub shape: PixelShape,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStylePoints {
    pub fn new(color: MurreletColor, shape: PixelShape, stroke_weight: f32) -> Self {
        Self {
            color,
            shape,
            stroke_weight,
        }
    }

    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            points: Some(self.shape),
            closed: true,
            filled: false,
            color: MurreletColorStyle::color(self.color),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleRGBAPoints {
    #[lerpable(func = "lerpify_vec3")]
    pub rgb: Vec3, // fill color
    pub a: f32,
    pub shape: PixelShape,
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleRGBAPoints {
    // pub fn new(color: MurreletColor, shape: PixelShape, stroke_weight: f32) -> Self {
    //     Self { color, shape, stroke_weight }
    // }

    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            points: Some(self.shape),
            closed: true,
            filled: false,
            color: MurreletColorStyle::rgbafill(self.rgb, self.a),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

#[derive(Copy, Clone, Debug, Livecode, Lerpable, Default)]
pub struct MurreletStyleLined {
    pub color: MurreletColor, // fill color
    #[livecode(serde_default = "zeros")]
    pub stroke_weight: f32,
}
impl MurreletStyleLined {
    fn to_style(&self) -> MurreletStyle {
        MurreletStyle {
            closed: false,
            filled: false,
            color: MurreletColorStyle::color(self.color),
            stroke_weight: self.stroke_weight,
            ..Default::default()
        }
    }
}

// type DrawingThing<'a, T> = Drawing<'a, T>;

pub mod styleconf {
    use murrelet_livecode_derive::Livecode;

    use super::*;

    // use this one, so you can get the shortcuts
    #[derive(Clone, Debug, Livecode, Lerpable)]
    pub enum StyleConf {
        // Verbose(MurreletStyle),
        Texture(MurreletStyleFilledSvg),
        Fill(MurreletStyleFilled),
        Outline(MurreletStyleOutlined),
        Points(MurreletStylePoints),
        Line(MurreletStyleLined),
        ThickLine,
        RGBAFill(MurreletStyleRGBAFill),
        RGBALine(MurreletStyleRGBALine),
        RGBAOutline(MurreletStyleRGBALine),
        RGBAPoints(MurreletStyleRGBAPoints),
    }
    impl StyleConf {
        pub fn to_style(&self) -> MurreletStyle {
            match self {
                // StyleConf::Verbose(a) => *a,
                StyleConf::Fill(a) => a.to_style(),
                StyleConf::Outline(a) => a.to_style(),
                StyleConf::Line(a) => a.to_style(),
                StyleConf::ThickLine => MurreletStyleOutlined::black().to_style(),
                StyleConf::RGBAFill(a) => a.to_style(),
                StyleConf::RGBALine(a) => a.to_style(),
                StyleConf::RGBAOutline(a) => a.to_style().with_no_fill(),
                StyleConf::Points(a) => a.to_style(),
                StyleConf::RGBAPoints(a) => a.to_style(),
                StyleConf::Texture(a) => a.to_style(),
            }
        }

        pub fn color(&self) -> MurreletColor {
            self.to_style().color.as_color()
        }

        pub fn outline(color: MurreletColor, stroke_weight: f32) -> Self {
            Self::Outline(MurreletStyleOutlined {
                color,
                stroke_weight,
            })
        }

        pub fn line(color: MurreletColor, stroke_weight: f32) -> Self {
            Self::Line(MurreletStyleLined {
                color,
                stroke_weight,
            })
        }

        pub fn fill(color: MurreletColor) -> Self {
            Self::Fill(MurreletStyleFilled {
                color,
                stroke_weight: 0.0,
                stroke_color: MurreletColor::black(),
            })
        }
    }

    impl Default for StyleConf {
        fn default() -> Self {
            StyleConf::Fill(MurreletStyleFilled::default())
        }
    }
}

// this one attaches a transform to the curve.
// you can _try_ to apply it using to_curve_maker, but this
// will act funny for non-affine
#[derive(Debug, Clone)]
pub struct MurreletCurve {
    cd: CurveDrawer,
    t: Mat4,
}

impl MurreletCurve {
    pub fn new(cd: CurveDrawer) -> Self {
        Self {
            cd,
            t: Mat4::IDENTITY,
        }
    }

    pub fn transform_with_mat4_after(&self, t: Mat4) -> MurreletCurve {
        Self {
            cd: self.cd.clone(),
            t: t * self.t,
        }
    }

    pub fn transform_with_mat4_before(&self, t: Mat4) -> MurreletCurve {
        Self {
            cd: self.cd.clone(),
            t: self.t * t,
        }
    }

    pub fn mat4(&self) -> Mat4 {
        self.t
    }

    pub fn curve(&self) -> &CurveDrawer {
        &self.cd
    }
}

#[derive(Debug, Clone)]
pub enum MurreletPath {
    Polyline(Polyline),
    Curve(MurreletCurve),
}
impl MurreletPath {
    pub fn polyline<F: IsPolyline>(path: F) -> Self {
        Self::Polyline(path.as_polyline())
    }

    pub fn curve(cd: CurveDrawer) -> Self {
        Self::Curve(MurreletCurve::new(cd))
    }

    pub fn as_curve(&self) -> MurreletCurve {
        match self {
            MurreletPath::Polyline(c) => MurreletCurve {
                cd: CurveDrawer::new_simple_points(c.clone_to_vec(), false),
                t: Mat4::IDENTITY,
            },
            MurreletPath::Curve(c) => c.clone(),
        }
    }

    pub fn transform_with<T: TransformVec2>(&self, t: &T) -> Self {
        match self {
            MurreletPath::Polyline(x) => MurreletPath::Polyline(x.transform_with(t)),
            MurreletPath::Curve(_) => todo!(), // i'm not sure how i want to handle this yet
        }
    }

    pub fn transform_with_mat4_after(&self, t: Mat4) -> MurreletPath {
        match self {
            MurreletPath::Polyline(_) => self.transform_with(&t),
            MurreletPath::Curve(c) => MurreletPath::Curve(c.transform_with_mat4_after(t)),
        }
    }

    pub fn transform_with_mat4_before(&self, t: Mat4) -> MurreletPath {
        match self {
            MurreletPath::Polyline(_) => self.transform_with(&t),
            MurreletPath::Curve(c) => MurreletPath::Curve(c.transform_with_mat4_before(t)),
        }
    }

    pub fn transform(&self) -> Option<Mat4> {
        match self {
            MurreletPath::Polyline(_) => None,
            MurreletPath::Curve(c) => Some(c.t),
        }
    }
}

#[derive(Debug, Clone)]
pub struct StyledPath {
    pub path: MurreletPath,
    pub style: MurreletStyle,
}
impl StyledPath {
    pub fn new_from_path(path: MurreletPath, style: MurreletStyle) -> Self {
        Self { path, style }
    }

    pub fn new<F: IsPolyline>(path: F, style: MurreletStyle) -> Self {
        Self {
            path: MurreletPath::Polyline(path.as_polyline()),
            style,
        }
    }

    pub fn from_path<P: IsPolyline>(path: P) -> StyledPath {
        StyledPath {
            path: MurreletPath::Polyline(path.as_polyline()),
            style: MurreletStyle::default(),
        }
    }

    pub fn transform_path<T: TransformVec2>(&self, t: &T) -> Self {
        StyledPath {
            path: self.path.transform_with(t),
            ..self.clone()
        }
    }

    pub fn transform_with_mat4_after(&self, t: Mat4) -> Self {
        StyledPath {
            path: self.path.transform_with_mat4_after(t),
            ..self.clone()
        }
    }
}