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