makepad_platform/
live_prims.rs

1use {
2    std::{str, sync::Arc},
3    crate::{
4        makepad_live_compiler::*,
5        makepad_math::*,
6        cx::Cx,
7        live_traits::*,
8        animator::Animator
9    }
10};
11
12#[macro_export]
13macro_rules!get_component {
14    ( $ comp_id: expr, $ ty: ty, $ frame: expr) => {
15        $ frame.get_component( $ comp_id).map_or(None, | v | v.cast_mut::< $ ty>())
16    }
17}
18 
19#[macro_export]
20macro_rules!live_primitive {
21    ( $ ty: ty, $ default: expr, $ apply: item, $ to_live_value: item) => {
22        impl LiveHook for $ ty {}
23        impl ToLiveValue for $ ty {
24            $ to_live_value
25        }
26        impl LiveRead for $ ty {
27            fn live_read_to(&self, id:LiveId, out:&mut Vec<LiveNode>){
28                out.push(LiveNode::from_id_value(id, self.to_live_value()));
29            } 
30        }
31        impl LiveApply for $ ty {
32            //fn type_id(&self) -> TypeId {
33            //    TypeId::of::< $ ty>()
34            // }
35            $ apply
36        }
37        impl LiveNew for $ ty {
38            fn live_design_with(_cx:&mut Cx){}
39            fn new(_cx: &mut Cx) -> Self {
40                $ default
41            }
42            
43            fn live_type_info(_cx: &mut Cx) -> LiveTypeInfo {
44                LiveTypeInfo {
45                    module_id: LiveModuleId::from_str(&module_path!()).unwrap(),
46                    live_type: LiveType::of::<Self>(),
47                    fields: Vec::new(),
48                    live_ignore: true,
49                    type_name: LiveId::from_str_with_lut(stringify!( $ ty)).unwrap(),
50                    //kind: LiveTypeKind::Primitive
51                }
52            }
53        }
54    }
55}
56
57live_primitive!(
58    LiveValue,
59    LiveValue::None,
60    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
61        if nodes[index].is_array() {
62            if let Some(_) = Animator::last_keyframe_value_from_array(index, nodes) {
63                self.apply(cx, apply, index, nodes);
64            }
65            nodes.skip_node(index)
66        }
67        else if nodes[index].value.is_open() { // cant use this
68            nodes.skip_node(index)
69        }
70        else {
71            *self = nodes[index].value.clone();
72            index + 1
73        }
74    },
75    fn to_live_value(&self) -> LiveValue {
76        self.clone()
77    }
78);
79
80live_primitive!(
81    LiveId,
82    LiveId::empty(),
83    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
84        match &nodes[index].value {
85            LiveValue::Id(id) => {
86                *self = *id;
87                index + 1
88            }
89            LiveValue::BareEnum(id)=>{
90                *self = *id;
91                index + 1
92            }
93            LiveValue::Array => {
94                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
95                    self.apply(cx, apply, index, nodes);
96                }
97                nodes.skip_node(index)
98            }
99            _ => {
100                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "LiveId");
101                nodes.skip_node(index)
102            }
103        }
104    },
105    fn to_live_value(&self) -> LiveValue {
106        LiveValue::Id(*self)
107    }
108);
109
110live_primitive!(
111    bool,
112    false,
113    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
114        match &nodes[index].value {
115            LiveValue::Bool(val) => {
116                *self = *val;
117                index + 1
118            }
119            LiveValue::Uint64(val) => {
120                *self = *val != 0;
121                index + 1
122            }
123            LiveValue::Int64(val) => {
124                *self = *val != 0;
125                index + 1
126            }
127            LiveValue::Float64(val) => {
128                *self = val.abs()>0.00001;
129                index + 1
130            }
131            LiveValue::Float32(val) => {
132                *self = val.abs()>0.00001;
133                index + 1
134            }
135            LiveValue::Array => {
136                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
137                    self.apply(cx, apply, index, nodes);
138                }
139                nodes.skip_node(index)
140            }
141            LiveValue::Expr {..} => {
142                panic!("Expr node found whilst deserialising DSL")
143            },
144            LiveValue::DSL {..} => nodes.skip_node(index),
145            _ => {
146                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "bool");
147                nodes.skip_node(index)
148            }
149        }
150    },
151    fn to_live_value(&self) -> LiveValue {
152        LiveValue::Bool(*self)
153    }
154);
155
156
157live_primitive!(
158    f32,
159    0.0f32,
160    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
161        match &nodes[index].value {
162            LiveValue::Float32(val) => {
163                *self = *val;
164                index + 1
165            }
166            LiveValue::Float64(val) => {
167                *self = *val as f32;
168                index + 1
169            }
170            LiveValue::Uint64(val) => {
171                *self = *val as f32;
172                index + 1
173            }
174            LiveValue::Int64(val) => {
175                *self = *val as f32;
176                index + 1
177            }
178            LiveValue::Expr {..} => {
179                panic!("Expr node found whilst deserialising DSL")
180            },
181            LiveValue::Array => {
182                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
183                    self.apply(cx, apply, index, nodes);
184                }
185                nodes.skip_node(index)
186            }
187            LiveValue::DSL {..} => nodes.skip_node(index),
188            _ => {
189                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "f32");
190                nodes.skip_node(index)
191            }
192        }
193    },
194    fn to_live_value(&self) -> LiveValue {
195        LiveValue::Float32(*self)
196    }
197);
198
199live_primitive!(
200    f64,
201    0.0f64,
202    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
203        match &nodes[index].value {
204            LiveValue::Float32(val) => {
205                *self = *val as f64;
206                index + 1
207            }
208            LiveValue::Float64(val) => {
209                *self = *val;
210                index + 1
211            }
212            LiveValue::Int64(val) => {
213                *self = *val as f64;
214                index + 1
215            }
216            LiveValue::Uint64(val) => {
217                *self = *val as f64;
218                index + 1
219            }
220            LiveValue::Expr {..} => {
221                panic!("Expr node found whilst deserialising DSL")
222            },
223            LiveValue::Array => {
224                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
225                    self.apply(cx, apply, index, nodes);
226                }
227                nodes.skip_node(index)
228            }
229            LiveValue::DSL {..} => nodes.skip_node(index),
230            _ => {
231                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "f64");
232                nodes.skip_node(index)
233            }
234        }
235    },
236    fn to_live_value(&self) -> LiveValue {
237        LiveValue::Float64(*self as f64)
238    }
239);
240
241live_primitive!(
242    i64,
243    0i64,
244    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
245        match &nodes[index].value {
246            LiveValue::Float32(val) => {
247                *self = *val as i64;
248                index + 1
249            }
250            LiveValue::Float64(val) => {
251                *self = *val as i64;
252                index + 1
253            }
254            LiveValue::Int64(val) => {
255                *self = *val as i64;
256                index + 1
257            }
258            LiveValue::Uint64(val) => {
259                *self = *val as i64;
260                index + 1
261            }
262            LiveValue::Expr {..} => {
263                panic!("Expr node found whilst deserialising DSL")
264            },
265            LiveValue::Array => {
266                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
267                    self.apply(cx, apply, index, nodes);
268                }
269                nodes.skip_node(index)
270            }
271            LiveValue::DSL {..} => nodes.skip_node(index),
272            _ => {
273                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "i64");
274                nodes.skip_node(index)
275            }
276        }
277    },
278    fn to_live_value(&self) -> LiveValue {
279        LiveValue::Int64(*self)
280    }
281);
282
283live_primitive!(
284    u64,
285    0u64,
286    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
287        match &nodes[index].value {
288            LiveValue::Float32(val) => {
289                *self = *val as u64;
290                index + 1
291            }
292            LiveValue::Float64(val) => {
293                *self = *val as u64;
294                index + 1
295            }
296            LiveValue::Int64(val) => {
297                *self = *val as u64;
298                index + 1
299            }
300            LiveValue::Uint64(val) => {
301                *self = *val as u64;
302                index + 1
303            }
304            LiveValue::Expr {..} => {
305                panic!("Expr node found whilst deserialising DSL")
306            },
307            LiveValue::Array => {
308                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
309                    self.apply(cx, apply, index, nodes);
310                }
311                nodes.skip_node(index)
312            }
313            LiveValue::DSL {..} => nodes.skip_node(index),
314            _ => {
315                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "i64");
316                nodes.skip_node(index)
317            }
318        }
319    },
320    fn to_live_value(&self) -> LiveValue {
321        LiveValue::Uint64(*self)
322    }
323);
324
325live_primitive!(
326    i32,
327    0i32,
328    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
329        match &nodes[index].value {
330            LiveValue::Float32(val) => {
331                *self = *val as i32;
332                index + 1
333            }
334            LiveValue::Float64(val) => {
335                *self = *val as i32;
336                index + 1
337            }
338            LiveValue::Int64(val) => {
339                *self = *val as i32;
340                index + 1
341            }
342            LiveValue::Uint64(val) => {
343                *self = *val as i32;
344                index + 1
345            }
346            LiveValue::Expr {..} => {
347                panic!("Expr node found whilst deserialising DSL")
348            },
349            LiveValue::Array => {
350                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
351                    self.apply(cx, apply, index, nodes);
352                }
353                nodes.skip_node(index)
354            }
355            LiveValue::DSL {..} => nodes.skip_node(index),
356            _ => {
357                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "i64");
358                nodes.skip_node(index)
359            }
360        }
361    },
362    fn to_live_value(&self) -> LiveValue {
363        LiveValue::Int64(*self as i64)
364    }
365);
366
367live_primitive!(
368    u32,
369    0u32,
370    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
371        match &nodes[index].value {
372            LiveValue::Float32(val) => {
373                *self = *val as u32;
374                index + 1
375            }
376            LiveValue::Float64(val) => {
377                *self = *val as u32;
378                index + 1
379            }
380            LiveValue::Int64(val) => {
381                *self = *val as u32;
382                index + 1
383            }
384            LiveValue::Uint64(val) => {
385                *self = *val as u32;
386                index + 1
387            }
388            LiveValue::Expr {..} => {
389                panic!("Expr node found whilst deserialising DSL")
390            },
391            LiveValue::Array => {
392                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
393                    self.apply(cx, apply, index, nodes);
394                }
395                nodes.skip_node(index)
396            }
397            LiveValue::DSL {..} => nodes.skip_node(index),
398            _ => {
399                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "i64");
400                nodes.skip_node(index)
401            }
402        }
403    },
404    fn to_live_value(&self) -> LiveValue {
405        LiveValue::Int64(*self as i64)
406    }
407);
408
409live_primitive!(
410    usize,
411    0usize,
412    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
413        match &nodes[index].value {
414            LiveValue::Float32(val) => {
415                *self = *val as usize;
416                index + 1
417            }
418            LiveValue::Float64(val) => {
419                *self = *val as usize;
420                index + 1
421            }
422            LiveValue::Int64(val) => {
423                *self = *val as usize;
424                index + 1
425            }
426            LiveValue::Uint64(val) => {
427                *self = *val as usize;
428                index + 1
429            }
430            LiveValue::Expr {..} => {
431                panic!("Expr node found whilst deserialising DSL")
432            },
433            LiveValue::Array => {
434                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
435                    self.apply(cx, apply, index, nodes);
436                }
437                nodes.skip_node(index)
438            }
439            LiveValue::DSL {..} => nodes.skip_node(index),
440            _ => {
441                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "i64");
442                nodes.skip_node(index)
443            }
444        }
445    },
446    fn to_live_value(&self) -> LiveValue {
447        LiveValue::Int64(*self as i64)
448    }
449);
450
451
452live_primitive!(
453    DVec2,
454    DVec2::default(),
455    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
456        match &nodes[index].value {
457            LiveValue::Uint64(v) => {
458                *self = DVec2::all(*v as f64);
459                index + 1
460            }
461            LiveValue::Int64(v) => {
462                *self = DVec2::all(*v as f64);
463                index + 1
464            }
465            LiveValue::Float32(v) => {
466                *self = DVec2::all(*v as f64);
467                index + 1
468            }
469            LiveValue::Float64(v) => {
470                *self = DVec2::all(*v);
471                index + 1
472            }
473            LiveValue::Vec2(val) => {
474                *self = val.clone().into();
475                index + 1
476            }
477            LiveValue::Array => {
478                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
479                    self.apply(cx, apply, index, nodes);
480                }
481                nodes.skip_node(index)
482            }
483            LiveValue::Expr {..} => {
484                panic!("Expr node found whilst deserialising DSL")
485            },
486            LiveValue::DSL {..} => nodes.skip_node(index),
487            _ => {
488                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Vec2");
489                nodes.skip_node(index)
490            }
491        }
492    },
493    fn to_live_value(&self) -> LiveValue {
494        LiveValue::Vec2(self.clone().into())
495    }
496);
497
498live_primitive!(
499    Vec2,
500    Vec2::default(),
501    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
502        match &nodes[index].value {
503            LiveValue::Uint64(v) => {
504                *self = Vec2::all(*v as f32);
505                index + 1
506            }
507            LiveValue::Int64(v) => {
508                *self = Vec2::all(*v as f32);
509                index + 1
510            }
511            LiveValue::Float32(v) => {
512                *self = Vec2::all(*v as f32);
513                index + 1
514            }
515            LiveValue::Float64(v) => {
516                *self = Vec2::all(*v as f32);
517                index + 1
518            }
519            LiveValue::Vec2(val) => {
520                *self = *val;
521                index + 1
522            }
523            LiveValue::Array => {
524                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
525                    self.apply(cx, apply, index, nodes);
526                }
527                nodes.skip_node(index)
528            }
529            LiveValue::Expr {..} => {
530                panic!("Expr node found whilst deserialising DSL")
531            },
532            LiveValue::DSL {..} => nodes.skip_node(index),
533            _ => {
534                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Vec2");
535                nodes.skip_node(index)
536            }
537        }
538    },
539    fn to_live_value(&self) -> LiveValue {
540        LiveValue::Vec2(*self)
541    }
542);
543
544live_primitive!(
545    Vec3,
546    Vec3::default(),
547    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
548        match &nodes[index].value {
549            LiveValue::Vec2(v) => {
550                *self = Vec3{x:v.x, y:v.y, z:0.0};
551                index + 1
552            }
553            LiveValue::Uint64(v) => {
554                *self = Vec3::all(*v as f32);
555                index + 1
556            }            
557            LiveValue::Int64(v) => {
558                *self = Vec3::all(*v as f32);
559                index + 1
560            }
561            LiveValue::Float32(v) => {
562                *self = Vec3::all(*v as f32);
563                index + 1
564            }
565            LiveValue::Float64(v) => {
566                *self = Vec3::all(*v as f32);
567                index + 1
568            }
569            LiveValue::Vec3(val) => {
570                *self = *val;
571                index + 1
572            }
573            LiveValue::Array => {
574                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
575                    self.apply(cx, apply, index, nodes);
576                }
577                nodes.skip_node(index)
578            }
579            LiveValue::Expr {..} => {
580                panic!("Expr node found whilst deserialising DSL")
581            },
582            LiveValue::DSL {..} => nodes.skip_node(index),
583            _ => {
584                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Vec3");
585                nodes.skip_node(index)
586            }
587        }
588    },
589    fn to_live_value(&self) -> LiveValue {
590        LiveValue::Vec3(*self)
591    }
592);
593
594live_primitive!(
595    Vec4,
596    Vec4::default(),
597    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
598        match &nodes[index].value {
599            LiveValue::Vec2(v) => {
600                *self = Vec4{x:v.x, y:v.y, z:v.x, w:v.y};
601                index + 1
602            }
603            LiveValue::Vec3(v) => {
604                *self = Vec4{x:v.x, y:v.y, z:v.z, w:1.0};
605                index + 1
606            }
607            LiveValue::Vec4(v) => {
608                *self = Vec4{x:v.x, y:v.y, z:v.z, w:v.w};
609                index + 1
610            }
611            LiveValue::Uint64(v) => {
612                *self = Vec4::all(*v as f32);
613                index + 1
614            }  
615            LiveValue::Int64(v) => {
616                *self = Vec4::all(*v as f32);
617                index + 1
618            }
619            LiveValue::Float32(v) => {
620                *self = Vec4::all(*v as f32);
621                index + 1
622            }
623            LiveValue::Float64(v) => {
624                *self = Vec4::all(*v as f32);
625                index + 1
626            }
627            LiveValue::Color(v) => {
628                *self = Vec4::from_u32(*v);
629                index + 1
630            }
631            LiveValue::Array => {
632                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
633                    self.apply(cx, apply, index, nodes);
634                }
635                nodes.skip_node(index)
636            }
637            LiveValue::Expr {..} => {
638                panic!("Expr node found whilst deserialising DSL")
639            },
640            LiveValue::DSL {..} => nodes.skip_node(index),
641            _ => {
642                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Vec4");
643                nodes.skip_node(index)
644            }
645        }
646    },
647    fn to_live_value(&self) -> LiveValue {
648        LiveValue::Color(self.to_u32())
649    }
650);
651
652
653live_primitive!(
654    Mat4,
655    Mat4::default(),
656    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
657        match &nodes[index].value {
658            LiveValue::Array => {
659                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
660                    self.apply(cx, apply, index, nodes);
661                }
662                nodes.skip_node(index)
663            }
664            LiveValue::Expr {..} => {
665                panic!("Expr node found whilst deserialising DSL")
666            },
667            LiveValue::DSL {..} => nodes.skip_node(index),
668            _ => {
669                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Vec4");
670                nodes.skip_node(index)
671            }
672        }
673    },
674    fn to_live_value(&self) -> LiveValue {
675        LiveValue::None
676    }
677);
678
679live_primitive!(
680    String,
681    String::default(),
682    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
683        match &nodes[index].value {
684            LiveValue::Str(v) => {
685                self.clear();
686                self.push_str(v);
687                index + 1
688            }
689            LiveValue::String(v) => {
690                self.clear();
691                self.push_str(v.as_str());
692                index + 1
693            }
694            LiveValue::InlineString(v) => {
695                self.clear();
696                self.push_str(v.as_str());
697                index + 1
698            }
699            LiveValue::Expr {..} => {
700                panic!("Expr node found whilst deserialising DSL")
701            },
702            LiveValue::Array => {
703                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
704                    self.apply(cx, apply, index, nodes);
705                }
706                nodes.skip_node(index)
707            }
708            LiveValue::Dependency(path) => {
709                match cx.get_dependency(path) {
710                    Ok(bytes) => {
711                        let string = String::from_utf8_lossy(&bytes);
712                        self.push_str(&string);
713                    }
714                    Err(_) => {
715                        cx.apply_error_resource_not_found(live_error_origin!(), index, nodes, path);
716                    }
717                }
718                index + 1
719            }
720            _ => {
721                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "String");
722                nodes.skip_node(index)
723            }
724        }
725    },
726    fn to_live_value(&self) -> LiveValue {
727        // lets check our byte size and choose a storage mode appropriately.
728        //let bytes = self.as_bytes();
729        if let Some(inline_str) = InlineString::from_str(&self) {
730            LiveValue::InlineString(inline_str)
731        }
732        else {
733            LiveValue::String(Arc::new(self.clone()))
734        }
735    }
736);
737
738pub enum ArcStringMut{
739    Arc(Arc<String>),
740    String(String)
741}
742
743impl Default for ArcStringMut{
744    fn default()->Self{Self::String(String::new())}
745}
746
747impl ArcStringMut{
748    pub fn as_arc(&self)->Arc<String>{
749        match self{
750            Self::Arc(rc)=>{
751                return rc.clone();
752            }
753            Self::String(s)=>{
754                return Arc::new(s.clone())
755            }
756        }
757    }
758    pub fn as_mut(&mut self)->&mut String{
759        match self{
760            Self::Arc(rc)=>{
761                *self = Self::String(rc.to_string());
762                return self.as_mut();
763            }
764            Self::String(s)=>{
765                return s
766            }
767        }
768    }
769    pub fn as_mut_empty(&mut self)->&mut String{
770        match self{
771            Self::Arc(_)=>{
772                *self = Self::String(String::new());
773                return self.as_mut();
774            }
775            Self::String(s)=>{
776                s.clear();
777                return s
778            }
779        }
780    }
781
782    pub fn set(&mut self, v:&str){
783        match self{
784            Self::Arc(_rc)=>{
785                *self = Self::String(v.to_string());
786            }
787            Self::String(s)=>{
788                s.clear();
789                s.push_str(v);
790            }
791        }
792    }
793
794    pub fn as_ref(&self)->&str{
795        match self{
796            Self::Arc(rc)=>{
797                &*rc
798            }
799            Self::String(s)=>{
800                return &s
801            }
802        }
803    }
804}
805
806
807live_primitive!(
808    ArcStringMut,
809    Default::default(),
810    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
811        match &nodes[index].value {
812            LiveValue::Str(v) => {
813                *self = ArcStringMut::String(v.to_string());
814                index + 1
815            }
816            LiveValue::String(v) => {
817                *self = ArcStringMut::Arc(v.clone());
818                index + 1
819            }
820            LiveValue::InlineString(v) => {
821                *self = ArcStringMut::String(v.as_str().to_string());
822                index + 1
823            }
824            LiveValue::Expr {..} => {
825                panic!("Expr node found whilst deserialising DSL")
826            },
827            LiveValue::Array => {
828                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
829                    self.apply(cx, apply, index, nodes);
830                }
831                nodes.skip_node(index)
832            }
833            LiveValue::Dependency(path) => {
834                match cx.get_dependency(path) {
835                    Ok(bytes) => {
836                        let string = String::from_utf8_lossy(&bytes);
837                        *self = ArcStringMut::String(string.to_string());
838                    }
839                    Err(_) => {
840                        cx.apply_error_resource_not_found(live_error_origin!(), index, nodes, path);
841                    }
842                }
843                index + 1
844            }
845            _ => {
846                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "String");
847                nodes.skip_node(index)
848            }
849        }
850    },
851    fn to_live_value(&self) -> LiveValue {
852        // lets check our byte size and choose a storage mode appropriately.
853        //let bytes = self.as_bytes();
854        if let Some(inline_str) = InlineString::from_str(&self.as_ref()) {
855            LiveValue::InlineString(inline_str)
856        }
857        else {
858            match self{
859                ArcStringMut::Arc(rc)=>LiveValue::String(rc.clone()),
860                ArcStringMut::String(v)=>LiveValue::String(Arc::new(v.clone()))
861            }
862        }
863    }
864);
865/*
866live_primitive!(
867    Arc<String>,
868    Default::default(),
869    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
870        match &nodes[index].value {
871            LiveValue::Str(v) => {
872                *self = Arc::new(v.to_string());
873                index + 1
874            }
875            LiveValue::String(v) => {
876                *self = v.clone();
877                index + 1
878            }
879            LiveValue::InlineString(v) => {
880                *self = Arc::new(v.as_str().to_string());
881                index + 1
882            }
883            LiveValue::Expr {..} => {
884                panic!("Expr node found whilst deserialising DSL")
885            },
886            LiveValue::Array => {
887                if let Some(index) = Animator::last_keyframe_value_from_array(index, nodes) {
888                    self.apply(cx, apply, index, nodes);
889                }
890                nodes.skip_node(index)
891            }
892            _ => {
893                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "String");
894                nodes.skip_node(index)
895            }
896        }
897    },
898    fn to_live_value(&self) -> LiveValue {
899        // lets check our byte size and choose a storage mode appropriately.
900        //let bytes = self.as_bytes();
901        if let Some(inline_str) = InlineString::from_str(&self) {
902            LiveValue::InlineString(inline_str)
903        }
904        else {
905            LiveValue::String(self.clone())
906        }
907    }
908);*/
909
910impl ToLiveValue for &str{
911    fn to_live_value(&self) -> LiveValue {
912        // lets check our byte size and choose a storage mode appropriately.
913        //let bytes = self.as_bytes();
914        if let Some(inline_str) = InlineString::from_str(self) {
915            LiveValue::InlineString(inline_str)
916        }
917        else {
918            LiveValue::String(Arc::new(self.to_string()))
919        }
920    }
921}
922
923/*
924pub trait LiveIdToEnum{
925    fn to_enum(&self) -> LiveValue;
926}
927
928impl LiveIdToEnum for &[LiveId;1]{
929    fn to_enum(&self) -> LiveValue {
930        LiveValue::BareEnum(self[0])
931    }
932}*/
933
934#[derive(Debug, Default, Clone)]
935pub struct LiveDependency(Arc<String>);
936
937impl LiveDependency{
938    pub fn as_str(&self)->&str{&self.0}
939    pub fn as_ref(&self)->&Arc<String>{&self.0}
940}
941
942
943live_primitive!(
944    LiveDependency,
945    LiveDependency::default(),
946    fn apply(&mut self, cx: &mut Cx, _applyl: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
947        match &nodes[index].value {
948            LiveValue::Dependency (dep)=> {
949                *self = Self(dep.clone());
950                index + 1
951            }
952            LiveValue::Expr {..} => {
953                panic!("Expr node found whilst deserialising DSL")
954            },
955
956            _ => {
957                cx.apply_error_wrong_value_type_for_primitive(live_error_origin!(), index, nodes, "Dependency");
958                nodes.skip_node(index)
959            }
960        }
961    },
962    fn to_live_value(&self) -> LiveValue { panic!() }
963);
964
965
966live_primitive!(
967    LivePtr,
968    LivePtr {file_id: LiveFileId(0), index: 0, generation: Default::default()},
969    fn apply(&mut self, cx: &mut Cx, apply: &mut Apply, index: usize, nodes: &[LiveNode]) -> usize {
970        if let Some(file_id) = apply.from.file_id() {
971            *self = cx.live_registry.borrow().file_id_index_to_live_ptr(file_id, index);
972        }
973        nodes.skip_node(index)
974    },
975    fn to_live_value(&self) -> LiveValue {
976        panic!()
977    }
978);
979