1use crate::{
5 BasicFrontResource, BasicFrontResourceConfig, BorderKind, DisplayInfo, PositionSizeConfig,
6 RustConstructorResource,
7};
8#[cfg(feature = "bevy")]
9use egui_bevy::TextureHandle;
10#[cfg(feature = "standard")]
11use egui_standard::TextureHandle;
12use std::{
13 any::Any,
14 fmt::{Debug, Formatter},
15};
16
17#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
26pub struct CustomRectConfig {
27 pub position_size_config: Option<PositionSizeConfig>,
31
32 pub clip_rect: Option<Option<PositionSizeConfig>>,
36
37 pub hidden: Option<bool>,
41
42 pub ignore_render_layer: Option<bool>,
46
47 pub rounding: Option<f32>,
51
52 pub color: Option<[u8; 3]>,
56
57 pub alpha: Option<u8>,
61
62 pub overlay_color: Option<[u8; 3]>,
66
67 pub overlay_alpha: Option<Option<u8>>,
71
72 pub border_width: Option<f32>,
76
77 pub border_color: Option<[u8; 3]>,
81
82 pub border_alpha: Option<u8>,
86
87 pub overlay_border_color: Option<[u8; 3]>,
91
92 pub overlay_border_alpha: Option<Option<u8>>,
96
97 pub border_kind: Option<BorderKind>,
101
102 pub tags: Option<Vec<[String; 2]>>,
106}
107
108impl CustomRectConfig {
109 pub fn from_custom_rect(custom_rect: &CustomRect) -> Self {
110 Self {
111 position_size_config: Some(
112 custom_rect.basic_front_resource_config.position_size_config,
113 ),
114 clip_rect: Some(custom_rect.basic_front_resource_config.clip_rect),
115 hidden: Some(custom_rect.display_info.hidden),
116 ignore_render_layer: Some(custom_rect.display_info.ignore_render_layer),
117 rounding: Some(custom_rect.rounding),
118 color: Some(custom_rect.color),
119 alpha: Some(custom_rect.alpha),
120 overlay_color: Some(custom_rect.overlay_color),
121 overlay_alpha: Some(custom_rect.overlay_alpha),
122 border_width: Some(custom_rect.border_width),
123 border_color: Some(custom_rect.border_color),
124 border_alpha: Some(custom_rect.border_alpha),
125 overlay_border_color: Some(custom_rect.overlay_border_color),
126 overlay_border_alpha: Some(custom_rect.overlay_border_alpha),
127 border_kind: Some(custom_rect.border_kind),
128 tags: Some(custom_rect.tags.clone()),
129 }
130 }
131
132 #[inline]
133 pub fn position_size_config(
134 mut self,
135 position_size_config: Option<PositionSizeConfig>,
136 ) -> Self {
137 self.position_size_config = position_size_config;
138 self
139 }
140
141 #[inline]
142 pub fn clip_rect(mut self, clip_rect: Option<Option<PositionSizeConfig>>) -> Self {
143 self.clip_rect = clip_rect;
144 self
145 }
146
147 #[inline]
148 pub fn hidden(mut self, hidden: Option<bool>) -> Self {
149 self.hidden = hidden;
150 self
151 }
152
153 #[inline]
154 pub fn ignore_render_layer(mut self, ignore_render_layer: Option<bool>) -> Self {
155 self.ignore_render_layer = ignore_render_layer;
156 self
157 }
158
159 #[inline]
160 pub fn rounding(mut self, rounding: Option<f32>) -> Self {
161 self.rounding = rounding;
162 self
163 }
164
165 #[inline]
166 pub fn color(mut self, color: Option<[u8; 3]>) -> Self {
167 self.color = color;
168 self
169 }
170
171 #[inline]
172 pub fn alpha(mut self, alpha: Option<u8>) -> Self {
173 self.alpha = alpha;
174 self
175 }
176
177 #[inline]
178 pub fn overlay_color(mut self, overlay_color: Option<[u8; 3]>) -> Self {
179 self.overlay_color = overlay_color;
180 self
181 }
182
183 #[inline]
184 pub fn overlay_alpha(mut self, overlay_alpha: Option<Option<u8>>) -> Self {
185 self.overlay_alpha = overlay_alpha;
186 self
187 }
188
189 #[inline]
190 pub fn border_width(mut self, border_width: Option<f32>) -> Self {
191 self.border_width = border_width;
192 self
193 }
194
195 #[inline]
196 pub fn border_color(mut self, border_color: Option<[u8; 3]>) -> Self {
197 self.border_color = border_color;
198 self
199 }
200
201 #[inline]
202 pub fn border_alpha(mut self, border_alpha: Option<u8>) -> Self {
203 self.border_alpha = border_alpha;
204 self
205 }
206
207 #[inline]
208 pub fn overlay_border_color(mut self, overlay_border_color: Option<[u8; 3]>) -> Self {
209 self.overlay_border_color = overlay_border_color;
210 self
211 }
212
213 #[inline]
214 pub fn overlay_border_alpha(mut self, overlay_border_alpha: Option<Option<u8>>) -> Self {
215 self.overlay_border_alpha = overlay_border_alpha;
216 self
217 }
218
219 #[inline]
220 pub fn border_kind(mut self, border_kind: Option<BorderKind>) -> Self {
221 self.border_kind = border_kind;
222 self
223 }
224
225 #[inline]
226 pub fn tags(mut self, tags: Option<Vec<[String; 2]>>) -> Self {
227 self.tags = tags;
228 self
229 }
230}
231
232#[derive(Debug, Clone, PartialEq, PartialOrd)]
236pub struct CustomRect {
237 pub basic_front_resource_config: BasicFrontResourceConfig,
241
242 pub position: [f32; 2],
246
247 pub size: [f32; 2],
251
252 pub display_info: DisplayInfo,
256
257 pub rounding: f32,
261
262 pub color: [u8; 3],
266
267 pub alpha: u8,
271
272 pub overlay_color: [u8; 3],
276
277 pub overlay_alpha: Option<u8>,
281
282 pub border_width: f32,
286
287 pub border_color: [u8; 3],
291
292 pub border_alpha: u8,
296
297 pub overlay_border_color: [u8; 3],
301
302 pub overlay_border_alpha: Option<u8>,
306
307 pub border_kind: BorderKind,
311
312 pub tags: Vec<[String; 2]>,
316}
317
318impl RustConstructorResource for CustomRect {
319 fn as_any(&self) -> &dyn Any {
320 self
321 }
322
323 fn as_any_mut(&mut self) -> &mut dyn Any {
324 self
325 }
326
327 fn display_display_info(&self) -> Option<DisplayInfo> {
328 Some(self.display_info)
329 }
330
331 fn modify_display_info(&mut self, display_info: DisplayInfo) {
332 self.display_info = display_info;
333 }
334
335 fn display_tags(&self) -> Vec<[String; 2]> {
336 self.tags.clone()
337 }
338
339 fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
340 if replace {
341 self.tags = tags.to_owned();
342 } else {
343 for tag in tags {
344 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
345 self.tags.remove(index);
346 };
347 }
348 self.tags.extend(tags.iter().cloned());
349 };
350 }
351}
352
353impl BasicFrontResource for CustomRect {
354 fn display_basic_front_resource_config(&self) -> BasicFrontResourceConfig {
355 self.basic_front_resource_config.clone()
356 }
357
358 fn display_position_size_config(&self) -> PositionSizeConfig {
359 self.basic_front_resource_config.position_size_config
360 }
361
362 fn display_clip_rect(&self) -> Option<PositionSizeConfig> {
363 self.basic_front_resource_config.clip_rect
364 }
365
366 fn display_position(&self) -> [f32; 2] {
367 self.position
368 }
369
370 fn display_size(&self) -> [f32; 2] {
371 self.size
372 }
373
374 fn modify_basic_front_resource_config(
375 &mut self,
376 basic_front_resource_config: BasicFrontResourceConfig,
377 ) {
378 self.basic_front_resource_config = basic_front_resource_config;
379 }
380
381 fn modify_position_size_config(&mut self, position_size_config: PositionSizeConfig) {
382 self.basic_front_resource_config.position_size_config = position_size_config;
383 }
384
385 fn modify_clip_rect(&mut self, clip_rect: Option<PositionSizeConfig>) {
386 self.basic_front_resource_config.clip_rect = clip_rect;
387 }
388}
389
390impl Default for CustomRect {
391 fn default() -> Self {
392 Self {
393 basic_front_resource_config: BasicFrontResourceConfig::default(),
394 position: [0_f32, 0_f32],
395 size: [0_f32, 0_f32],
396 display_info: DisplayInfo::default(),
397 rounding: 2_f32,
398 color: [255, 255, 255],
399 alpha: 255,
400 overlay_border_color: [255, 255, 255],
401 overlay_alpha: None,
402 border_width: 2_f32,
403 border_color: [0, 0, 0],
404 border_alpha: 255,
405 overlay_color: [255, 255, 255],
406 overlay_border_alpha: None,
407 border_kind: BorderKind::default(),
408 tags: Vec::new(),
409 }
410 }
411}
412
413impl CustomRect {
414 pub fn from_config(mut self, config: &CustomRectConfig) -> Self {
415 if let Some(position_size_config) = config.position_size_config {
416 self.basic_front_resource_config.position_size_config = position_size_config;
417 };
418 if let Some(clip_rect) = config.clip_rect {
419 self.basic_front_resource_config.clip_rect = clip_rect;
420 };
421 if let Some(hidden) = config.hidden {
422 self.display_info.hidden = hidden;
423 };
424 if let Some(ignore_render_layer) = config.ignore_render_layer {
425 self.display_info.ignore_render_layer = ignore_render_layer;
426 };
427 if let Some(rounding) = config.rounding {
428 self.rounding = rounding;
429 };
430 if let Some(color) = config.color {
431 self.color = color;
432 };
433 if let Some(alpha) = config.alpha {
434 self.alpha = alpha;
435 };
436 if let Some(overlay_color) = config.overlay_color {
437 self.overlay_color = overlay_color;
438 };
439 if let Some(overlay_alpha) = config.overlay_alpha {
440 self.overlay_alpha = overlay_alpha;
441 };
442 if let Some(border_width) = config.border_width {
443 self.border_width = border_width;
444 };
445 if let Some(border_color) = config.border_color {
446 self.border_color = border_color;
447 };
448 if let Some(border_alpha) = config.border_alpha {
449 self.border_alpha = border_alpha;
450 };
451 if let Some(overlay_border_color) = config.overlay_border_color {
452 self.overlay_border_color = overlay_border_color;
453 };
454 if let Some(overlay_border_alpha) = config.overlay_border_alpha {
455 self.overlay_border_alpha = overlay_border_alpha;
456 };
457 if let Some(border_kind) = config.border_kind {
458 self.border_kind = border_kind;
459 };
460 if let Some(tags) = config.tags.clone() {
461 self.tags = tags;
462 };
463 self
464 }
465
466 #[inline]
467 pub fn basic_front_resource_config(
468 mut self,
469 basic_front_resource_config: &BasicFrontResourceConfig,
470 ) -> Self {
471 self.basic_front_resource_config = basic_front_resource_config.clone();
472 self
473 }
474
475 #[inline]
476 pub fn hidden(mut self, hidden: bool) -> Self {
477 self.display_info.hidden = hidden;
478 self
479 }
480
481 #[inline]
482 pub fn ignore_render_layer(mut self, ignore_render_layer: bool) -> Self {
483 self.display_info.ignore_render_layer = ignore_render_layer;
484 self
485 }
486
487 #[inline]
488 pub fn rounding(mut self, rounding: f32) -> Self {
489 self.rounding = rounding;
490 self
491 }
492
493 #[inline]
494 pub fn color(mut self, r: u8, g: u8, b: u8) -> Self {
495 self.color = [r, g, b];
496 self
497 }
498
499 #[inline]
500 pub fn alpha(mut self, alpha: u8) -> Self {
501 self.alpha = alpha;
502 self
503 }
504
505 #[inline]
506 pub fn overlay_color(mut self, r: u8, g: u8, b: u8) -> Self {
507 self.overlay_color = [r, g, b];
508 self
509 }
510
511 #[inline]
512 pub fn overlay_alpha(mut self, overlay_alpha: Option<u8>) -> Self {
513 self.overlay_alpha = overlay_alpha;
514 self
515 }
516
517 #[inline]
518 pub fn border_width(mut self, border_width: f32) -> Self {
519 self.border_width = border_width;
520 self
521 }
522
523 #[inline]
524 pub fn border_color(mut self, r: u8, g: u8, b: u8) -> Self {
525 self.border_color = [r, g, b];
526 self
527 }
528
529 #[inline]
530 pub fn border_alpha(mut self, border_alpha: u8) -> Self {
531 self.border_alpha = border_alpha;
532 self
533 }
534
535 #[inline]
536 pub fn overlay_border_color(mut self, r: u8, g: u8, b: u8) -> Self {
537 self.overlay_border_color = [r, g, b];
538 self
539 }
540
541 #[inline]
542 pub fn overlay_border_alpha(mut self, overlay_border_alpha: Option<u8>) -> Self {
543 self.overlay_border_alpha = overlay_border_alpha;
544 self
545 }
546
547 #[inline]
548 pub fn border_kind(mut self, border_kind: BorderKind) -> Self {
549 self.border_kind = border_kind;
550 self
551 }
552
553 #[inline]
554 pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
555 if replace {
556 self.tags = tags.to_owned();
557 } else {
558 for tag in tags {
559 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
560 self.tags.remove(index);
561 };
562 }
563 self.tags.extend(tags.iter().cloned());
564 };
565 self
566 }
567}
568
569#[derive(Clone, PartialEq, Eq, Hash)]
573pub struct DebugTextureHandle(pub TextureHandle);
574
575impl Debug for DebugTextureHandle {
576 fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
577 f.debug_struct("DebugTextureHandle").finish()
579 }
580}
581
582impl DebugTextureHandle {
583 pub fn new(texture_handle: &TextureHandle) -> Self {
584 Self(texture_handle.clone())
585 }
586}
587
588#[derive(Debug, Clone, PartialEq, Eq, Hash)]
592pub enum ImageLoadMethod {
593 ByPath((String, [bool; 2])),
597
598 ByTexture(DebugTextureHandle),
602}
603
604#[derive(Debug, Default, Clone, PartialEq)]
608pub struct ImageConfig {
609 pub position_size_config: Option<PositionSizeConfig>,
613
614 pub clip_rect: Option<Option<PositionSizeConfig>>,
618
619 pub hidden: Option<bool>,
623
624 pub ignore_render_layer: Option<bool>,
628
629 pub alpha: Option<u8>,
633
634 pub overlay_color: Option<[u8; 3]>,
638
639 pub overlay_alpha: Option<u8>,
643
644 pub background_color: Option<[u8; 3]>,
648
649 pub background_alpha: Option<u8>,
653
654 pub rotate_angle: Option<f32>,
658
659 pub rotate_center: Option<[f32; 2]>,
663
664 pub image_load_method: Option<ImageLoadMethod>,
668
669 pub tags: Option<Vec<[String; 2]>>,
673}
674
675impl ImageConfig {
676 pub fn from_image(image: &Image) -> Self {
677 Self {
678 position_size_config: Some(image.basic_front_resource_config.position_size_config),
679 clip_rect: Some(image.basic_front_resource_config.clip_rect),
680 hidden: Some(image.display_info.hidden),
681 ignore_render_layer: Some(image.display_info.ignore_render_layer),
682 alpha: Some(image.alpha),
683 overlay_color: Some(image.overlay_color),
684 overlay_alpha: Some(image.overlay_alpha),
685 background_color: Some(image.background_color),
686 background_alpha: Some(image.background_alpha),
687 rotate_angle: Some(image.rotate_angle),
688 rotate_center: Some(image.rotate_center),
689 image_load_method: Some(image.image_load_method.clone()),
690 tags: Some(image.tags.clone()),
691 }
692 }
693
694 #[inline]
695 pub fn position_size_config(
696 mut self,
697 position_size_config: Option<PositionSizeConfig>,
698 ) -> Self {
699 self.position_size_config = position_size_config;
700 self
701 }
702
703 #[inline]
704 pub fn clip_rect(mut self, clip_rect: Option<Option<PositionSizeConfig>>) -> Self {
705 self.clip_rect = clip_rect;
706 self
707 }
708
709 #[inline]
710 pub fn hidden(mut self, hidden: Option<bool>) -> Self {
711 self.hidden = hidden;
712 self
713 }
714
715 #[inline]
716 pub fn ignore_render_layer(mut self, ignore_render_layer: Option<bool>) -> Self {
717 self.ignore_render_layer = ignore_render_layer;
718 self
719 }
720
721 #[inline]
722 pub fn alpha(mut self, alpha: Option<u8>) -> Self {
723 self.alpha = alpha;
724 self
725 }
726
727 #[inline]
728 pub fn overlay_color(mut self, overlay_color: Option<[u8; 3]>) -> Self {
729 self.overlay_color = overlay_color;
730 self
731 }
732
733 #[inline]
734 pub fn overlay_alpha(mut self, overlay_alpha: Option<u8>) -> Self {
735 self.overlay_alpha = overlay_alpha;
736 self
737 }
738
739 #[inline]
740 pub fn background_color(mut self, background_color: Option<[u8; 3]>) -> Self {
741 self.background_color = background_color;
742 self
743 }
744
745 #[inline]
746 pub fn background_alpha(mut self, background_alpha: Option<u8>) -> Self {
747 self.background_alpha = background_alpha;
748 self
749 }
750
751 #[inline]
752 pub fn rotate_angle(mut self, rotate_angle: Option<f32>) -> Self {
753 self.rotate_angle = rotate_angle;
754 self
755 }
756
757 #[inline]
758 pub fn rotate_center(mut self, rotate_center: Option<[f32; 2]>) -> Self {
759 self.rotate_center = rotate_center;
760 self
761 }
762
763 #[inline]
764 pub fn image_load_method(mut self, image_load_method: Option<ImageLoadMethod>) -> Self {
765 self.image_load_method = image_load_method;
766 self
767 }
768
769 #[inline]
770 pub fn tags(mut self, tags: Option<Vec<[String; 2]>>) -> Self {
771 self.tags = tags;
772 self
773 }
774}
775
776#[derive(Debug, Clone, PartialEq)]
780pub struct Image {
781 pub basic_front_resource_config: BasicFrontResourceConfig,
785
786 pub position: [f32; 2],
790
791 pub size: [f32; 2],
795
796 pub display_info: DisplayInfo,
800
801 pub texture: Option<DebugTextureHandle>,
805
806 pub alpha: u8,
810
811 pub overlay_color: [u8; 3],
815
816 pub overlay_alpha: u8,
820
821 pub background_color: [u8; 3],
825
826 pub background_alpha: u8,
830
831 pub rotate_angle: f32,
835
836 pub rotate_center: [f32; 2],
840
841 pub image_load_method: ImageLoadMethod,
845
846 pub last_frame_path: String,
850
851 pub tags: Vec<[String; 2]>,
855}
856
857impl RustConstructorResource for Image {
858 fn as_any(&self) -> &dyn Any {
859 self
860 }
861
862 fn as_any_mut(&mut self) -> &mut dyn Any {
863 self
864 }
865
866 fn display_display_info(&self) -> Option<DisplayInfo> {
867 Some(self.display_info)
868 }
869
870 fn modify_display_info(&mut self, display_info: DisplayInfo) {
871 self.display_info = display_info;
872 }
873
874 fn display_tags(&self) -> Vec<[String; 2]> {
875 self.tags.clone()
876 }
877
878 fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
879 if replace {
880 self.tags = tags.to_owned();
881 } else {
882 for tag in tags {
883 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
884 self.tags.remove(index);
885 };
886 }
887 self.tags.extend(tags.iter().cloned());
888 };
889 }
890}
891
892impl BasicFrontResource for Image {
893 fn display_basic_front_resource_config(&self) -> BasicFrontResourceConfig {
894 self.basic_front_resource_config.clone()
895 }
896
897 fn display_position_size_config(&self) -> PositionSizeConfig {
898 self.basic_front_resource_config.position_size_config
899 }
900
901 fn display_clip_rect(&self) -> Option<PositionSizeConfig> {
902 self.basic_front_resource_config.clip_rect
903 }
904
905 fn display_position(&self) -> [f32; 2] {
906 self.position
907 }
908
909 fn display_size(&self) -> [f32; 2] {
910 self.size
911 }
912
913 fn modify_basic_front_resource_config(
914 &mut self,
915 basic_front_resource_config: BasicFrontResourceConfig,
916 ) {
917 self.basic_front_resource_config = basic_front_resource_config;
918 }
919
920 fn modify_position_size_config(&mut self, position_size_config: PositionSizeConfig) {
921 self.basic_front_resource_config.position_size_config = position_size_config;
922 }
923
924 fn modify_clip_rect(&mut self, clip_rect: Option<PositionSizeConfig>) {
925 self.basic_front_resource_config.clip_rect = clip_rect;
926 }
927}
928
929impl Default for Image {
930 fn default() -> Self {
931 Self {
932 basic_front_resource_config: BasicFrontResourceConfig::default(),
933 position: [0_f32, 0_f32],
934 size: [0_f32, 0_f32],
935 display_info: DisplayInfo::default(),
936 texture: None,
937 alpha: 255,
938 overlay_color: [255, 255, 255],
939 overlay_alpha: 255,
940 background_color: [0, 0, 0],
941 background_alpha: 0,
942 rotate_angle: 0_f32,
943 rotate_center: [0_f32, 0_f32],
944 image_load_method: ImageLoadMethod::ByPath((String::new(), [false, false])),
945 last_frame_path: String::new(),
946 tags: Vec::new(),
947 }
948 }
949}
950
951impl Image {
952 pub fn from_config(mut self, config: &ImageConfig) -> Self {
953 if let Some(position_size_config) = config.position_size_config {
954 self.basic_front_resource_config.position_size_config = position_size_config;
955 };
956 if let Some(clip_rect) = config.clip_rect {
957 self.basic_front_resource_config.clip_rect = clip_rect;
958 };
959 if let Some(hidden) = config.hidden {
960 self.display_info.hidden = hidden;
961 };
962 if let Some(ignore_render_layer) = config.ignore_render_layer {
963 self.display_info.ignore_render_layer = ignore_render_layer;
964 };
965 if let Some(alpha) = config.alpha {
966 self.alpha = alpha;
967 };
968 if let Some(overlay_color) = config.overlay_color {
969 self.overlay_color = overlay_color;
970 };
971 if let Some(overlay_alpha) = config.overlay_alpha {
972 self.overlay_alpha = overlay_alpha;
973 };
974 if let Some(background_color) = config.background_color {
975 self.background_color = background_color;
976 };
977 if let Some(background_alpha) = config.background_alpha {
978 self.background_alpha = background_alpha;
979 };
980 if let Some(rotate_angle) = config.rotate_angle {
981 self.rotate_angle = rotate_angle;
982 };
983 if let Some(rotate_center) = config.rotate_center {
984 self.rotate_center = rotate_center;
985 };
986 if let Some(image_load_method) = config.image_load_method.clone() {
987 self.image_load_method = image_load_method;
988 };
989 if let Some(tags) = config.tags.clone() {
990 self.tags = tags;
991 };
992 self
993 }
994
995 #[inline]
996 pub fn basic_front_resource_config(
997 mut self,
998 basic_front_resource_config: &BasicFrontResourceConfig,
999 ) -> Self {
1000 self.basic_front_resource_config = basic_front_resource_config.clone();
1001 self
1002 }
1003
1004 #[inline]
1005 pub fn hidden(mut self, hidden: bool) -> Self {
1006 self.display_info.hidden = hidden;
1007 self
1008 }
1009
1010 #[inline]
1011 pub fn ignore_render_layer(mut self, ignore_render_layer: bool) -> Self {
1012 self.display_info.ignore_render_layer = ignore_render_layer;
1013 self
1014 }
1015
1016 #[inline]
1017 pub fn alpha(mut self, alpha: u8) -> Self {
1018 self.alpha = alpha;
1019 self
1020 }
1021
1022 #[inline]
1023 pub fn overlay_color(mut self, r: u8, g: u8, b: u8) -> Self {
1024 self.overlay_color = [r, g, b];
1025 self
1026 }
1027
1028 #[inline]
1029 pub fn overlay_alpha(mut self, overlay_alpha: u8) -> Self {
1030 self.overlay_alpha = overlay_alpha;
1031 self
1032 }
1033
1034 #[inline]
1035 pub fn background_color(mut self, r: u8, g: u8, b: u8) -> Self {
1036 self.background_color = [r, g, b];
1037 self
1038 }
1039
1040 #[inline]
1041 pub fn background_alpha(mut self, background_alpha: u8) -> Self {
1042 self.background_alpha = background_alpha;
1043 self
1044 }
1045
1046 #[inline]
1047 pub fn rotate_angle(mut self, rotate_angle: f32) -> Self {
1048 self.rotate_angle = rotate_angle;
1049 self
1050 }
1051
1052 #[inline]
1053 pub fn rotate_center(mut self, x: f32, y: f32) -> Self {
1054 self.rotate_center = [x, y];
1055 self
1056 }
1057
1058 #[inline]
1059 pub fn image_load_method(mut self, image_load_method: &ImageLoadMethod) -> Self {
1060 self.image_load_method = image_load_method.clone();
1061 self
1062 }
1063
1064 #[inline]
1065 pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
1066 if replace {
1067 self.tags = tags.to_owned();
1068 } else {
1069 for tag in tags {
1070 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
1071 self.tags.remove(index);
1072 };
1073 }
1074 self.tags.extend(tags.iter().cloned());
1075 };
1076 self
1077 }
1078}
1079
1080#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
1084pub enum HyperlinkSelectMethod {
1085 All(String),
1089 Segment(Vec<(usize, String)>),
1093}
1094
1095#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
1099pub struct TextConfig {
1100 pub position_size_config: Option<PositionSizeConfig>,
1104
1105 pub clip_rect: Option<Option<PositionSizeConfig>>,
1109
1110 pub hidden: Option<bool>,
1114
1115 pub ignore_render_layer: Option<bool>,
1119
1120 pub content: Option<String>,
1124
1125 pub font_size: Option<f32>,
1129
1130 pub color: Option<[u8; 3]>,
1134
1135 pub alpha: Option<u8>,
1139
1140 pub background_color: Option<[u8; 3]>,
1144
1145 pub background_alpha: Option<u8>,
1149
1150 pub background_rounding: Option<f32>,
1154
1155 pub font: Option<String>,
1159
1160 pub selectable: Option<bool>,
1164
1165 pub hyperlink_text: Option<Vec<(String, HyperlinkSelectMethod)>>,
1169
1170 pub auto_fit: Option<[bool; 2]>,
1174
1175 pub tags: Option<Vec<[String; 2]>>,
1179}
1180
1181impl TextConfig {
1182 pub fn from_text(text: &Text) -> Self {
1183 Self {
1184 position_size_config: Some(text.basic_front_resource_config.position_size_config),
1185 clip_rect: Some(text.basic_front_resource_config.clip_rect),
1186 hidden: Some(text.display_info.hidden),
1187 ignore_render_layer: Some(text.display_info.ignore_render_layer),
1188 content: Some(text.content.clone()),
1189 font_size: Some(text.font_size),
1190 color: Some(text.color),
1191 alpha: Some(text.alpha),
1192 background_color: Some(text.background_color),
1193 background_alpha: Some(text.background_alpha),
1194 background_rounding: Some(text.background_rounding),
1195 font: Some(text.font.clone()),
1196 selectable: Some(text.selectable),
1197 hyperlink_text: Some(text.hyperlink_text.clone()),
1198 auto_fit: Some(text.auto_fit),
1199 tags: Some(text.tags.clone()),
1200 }
1201 }
1202
1203 #[inline]
1204 pub fn position_size_config(
1205 mut self,
1206 position_size_config: Option<PositionSizeConfig>,
1207 ) -> Self {
1208 self.position_size_config = position_size_config;
1209 self
1210 }
1211
1212 #[inline]
1213 pub fn clip_rect(mut self, clip_rect: Option<Option<PositionSizeConfig>>) -> Self {
1214 self.clip_rect = clip_rect;
1215 self
1216 }
1217
1218 #[inline]
1219 pub fn hidden(mut self, hidden: Option<bool>) -> Self {
1220 self.hidden = hidden;
1221 self
1222 }
1223
1224 #[inline]
1225 pub fn ignore_render_layer(mut self, ignore_render_layer: Option<bool>) -> Self {
1226 self.ignore_render_layer = ignore_render_layer;
1227 self
1228 }
1229
1230 #[inline]
1231 pub fn content(mut self, content: Option<String>) -> Self {
1232 self.content = content;
1233 self
1234 }
1235
1236 #[inline]
1237 pub fn font_size(mut self, font_size: Option<f32>) -> Self {
1238 self.font_size = font_size;
1239 self
1240 }
1241
1242 #[inline]
1243 pub fn color(mut self, color: Option<[u8; 3]>) -> Self {
1244 self.color = color;
1245 self
1246 }
1247
1248 #[inline]
1249 pub fn alpha(mut self, alpha: Option<u8>) -> Self {
1250 self.alpha = alpha;
1251 self
1252 }
1253
1254 #[inline]
1255 pub fn background_color(mut self, background_color: Option<[u8; 3]>) -> Self {
1256 self.background_color = background_color;
1257 self
1258 }
1259
1260 #[inline]
1261 pub fn background_alpha(mut self, background_alpha: Option<u8>) -> Self {
1262 self.background_alpha = background_alpha;
1263 self
1264 }
1265
1266 #[inline]
1267 pub fn background_rounding(mut self, background_rounding: Option<f32>) -> Self {
1268 self.background_rounding = background_rounding;
1269 self
1270 }
1271
1272 #[inline]
1273 pub fn font(mut self, font: Option<String>) -> Self {
1274 self.font = font;
1275 self
1276 }
1277
1278 #[inline]
1279 pub fn selectable(mut self, selectable: Option<bool>) -> Self {
1280 self.selectable = selectable;
1281 self
1282 }
1283
1284 #[inline]
1285 pub fn hyperlink_text(
1286 mut self,
1287 hyperlink_text: Option<Vec<(String, HyperlinkSelectMethod)>>,
1288 ) -> Self {
1289 self.hyperlink_text = hyperlink_text;
1290 self
1291 }
1292
1293 #[inline]
1294 pub fn auto_fit(mut self, auto_fit: Option<[bool; 2]>) -> Self {
1295 self.auto_fit = auto_fit;
1296 self
1297 }
1298
1299 #[inline]
1300 pub fn tags(mut self, tags: Option<Vec<[String; 2]>>) -> Self {
1301 self.tags = tags;
1302 self
1303 }
1304}
1305
1306#[derive(Debug, Clone, PartialEq, PartialOrd)]
1310pub struct Text {
1311 pub basic_front_resource_config: BasicFrontResourceConfig,
1315
1316 pub position: [f32; 2],
1320
1321 pub size: [f32; 2],
1325
1326 pub display_info: DisplayInfo,
1330
1331 pub content: String,
1335
1336 pub font_size: f32,
1340
1341 pub color: [u8; 3],
1345
1346 pub alpha: u8,
1350
1351 pub background_color: [u8; 3],
1355
1356 pub background_alpha: u8,
1360
1361 pub background_rounding: f32,
1365
1366 pub font: String,
1370
1371 pub selectable: bool,
1375
1376 pub hyperlink_text: Vec<(String, HyperlinkSelectMethod)>,
1380
1381 pub hyperlink_index: Vec<(usize, usize, String)>,
1385
1386 pub auto_fit: [bool; 2],
1390
1391 pub last_frame_content: String,
1395
1396 pub selection: Option<(usize, usize)>,
1400
1401 pub truncate_size: [f32; 2],
1405
1406 pub actual_size: [f32; 2],
1410
1411 pub tags: Vec<[String; 2]>,
1415}
1416
1417impl RustConstructorResource for Text {
1418 fn as_any(&self) -> &dyn Any {
1419 self
1420 }
1421
1422 fn as_any_mut(&mut self) -> &mut dyn Any {
1423 self
1424 }
1425
1426 fn display_display_info(&self) -> Option<DisplayInfo> {
1427 Some(self.display_info)
1428 }
1429
1430 fn modify_display_info(&mut self, display_info: DisplayInfo) {
1431 self.display_info = display_info;
1432 }
1433
1434 fn display_tags(&self) -> Vec<[String; 2]> {
1435 self.tags.clone()
1436 }
1437
1438 fn modify_tags(&mut self, tags: &[[String; 2]], replace: bool) {
1439 if replace {
1440 self.tags = tags.to_owned();
1441 } else {
1442 for tag in tags {
1443 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
1444 self.tags.remove(index);
1445 };
1446 }
1447 self.tags.extend(tags.iter().cloned());
1448 };
1449 }
1450}
1451
1452impl BasicFrontResource for Text {
1453 fn display_basic_front_resource_config(&self) -> BasicFrontResourceConfig {
1454 self.basic_front_resource_config.clone()
1455 }
1456
1457 fn display_position_size_config(&self) -> PositionSizeConfig {
1458 self.basic_front_resource_config.position_size_config
1459 }
1460
1461 fn display_clip_rect(&self) -> Option<PositionSizeConfig> {
1462 self.basic_front_resource_config.clip_rect
1463 }
1464
1465 fn display_position(&self) -> [f32; 2] {
1466 self.position
1467 }
1468
1469 fn display_size(&self) -> [f32; 2] {
1470 self.size
1471 }
1472
1473 fn modify_basic_front_resource_config(
1474 &mut self,
1475 basic_front_resource_config: BasicFrontResourceConfig,
1476 ) {
1477 self.basic_front_resource_config = basic_front_resource_config;
1478 }
1479
1480 fn modify_position_size_config(&mut self, position_size_config: PositionSizeConfig) {
1481 self.basic_front_resource_config.position_size_config = position_size_config;
1482 }
1483
1484 fn modify_clip_rect(&mut self, clip_rect: Option<PositionSizeConfig>) {
1485 self.basic_front_resource_config.clip_rect = clip_rect;
1486 }
1487}
1488
1489impl Default for Text {
1490 fn default() -> Self {
1491 Self {
1492 basic_front_resource_config: BasicFrontResourceConfig::default(),
1493 position: [0_f32, 0_f32],
1494 size: [0_f32, 0_f32],
1495 display_info: DisplayInfo::default(),
1496 content: String::from("Hello world"),
1497 font_size: 16_f32,
1498 color: [255, 255, 255],
1499 alpha: 255,
1500 background_color: [0, 0, 0],
1501 background_alpha: 0,
1502 background_rounding: 2_f32,
1503 font: String::new(),
1504 selectable: true,
1505 auto_fit: [true, true],
1506 hyperlink_text: Vec::new(),
1507 hyperlink_index: Vec::new(),
1508 last_frame_content: String::from(""),
1509 selection: None,
1510 truncate_size: [0_f32, 0_f32],
1511 actual_size: [0_f32, 0_f32],
1512 tags: Vec::new(),
1513 }
1514 }
1515}
1516
1517impl Text {
1518 pub fn from_config(mut self, config: &TextConfig) -> Self {
1519 if let Some(position_size_config) = config.position_size_config {
1520 self.basic_front_resource_config.position_size_config = position_size_config;
1521 };
1522 if let Some(clip_rect) = config.clip_rect {
1523 self.basic_front_resource_config.clip_rect = clip_rect;
1524 };
1525 if let Some(hidden) = config.hidden {
1526 self.display_info.hidden = hidden;
1527 };
1528 if let Some(ignore_render_layer) = config.ignore_render_layer {
1529 self.display_info.ignore_render_layer = ignore_render_layer;
1530 };
1531 if let Some(content) = config.content.clone() {
1532 self.content = content;
1533 };
1534 if let Some(font_size) = config.font_size {
1535 self.font_size = font_size;
1536 };
1537 if let Some(color) = config.color {
1538 self.color = color;
1539 };
1540 if let Some(alpha) = config.alpha {
1541 self.alpha = alpha;
1542 };
1543 if let Some(background_color) = config.background_color {
1544 self.background_color = background_color;
1545 };
1546 if let Some(background_alpha) = config.background_alpha {
1547 self.background_alpha = background_alpha;
1548 };
1549 if let Some(background_rounding) = config.background_rounding {
1550 self.background_rounding = background_rounding;
1551 };
1552 if let Some(font) = config.font.clone() {
1553 self.font = font;
1554 };
1555 if let Some(selectable) = config.selectable {
1556 self.selectable = selectable;
1557 };
1558 if let Some(hyperlink_text) = config.hyperlink_text.clone() {
1559 self.hyperlink_text = hyperlink_text;
1560 };
1561 if let Some(auto_fit) = config.auto_fit {
1562 self.auto_fit = auto_fit;
1563 };
1564 if let Some(tags) = config.tags.clone() {
1565 self.tags = tags;
1566 };
1567 self
1568 }
1569
1570 #[inline]
1571 pub fn basic_front_resource_config(
1572 mut self,
1573 basic_front_resource_config: &BasicFrontResourceConfig,
1574 ) -> Self {
1575 self.basic_front_resource_config = basic_front_resource_config.clone();
1576 self
1577 }
1578
1579 #[inline]
1580 pub fn hidden(mut self, hidden: bool) -> Self {
1581 self.display_info.hidden = hidden;
1582 self
1583 }
1584
1585 #[inline]
1586 pub fn ignore_render_layer(mut self, ignore_render_layer: bool) -> Self {
1587 self.display_info.ignore_render_layer = ignore_render_layer;
1588 self
1589 }
1590
1591 #[inline]
1592 pub fn content(mut self, content: &str) -> Self {
1593 self.content = content.to_string();
1594 self
1595 }
1596
1597 #[inline]
1598 pub fn font_size(mut self, font_size: f32) -> Self {
1599 self.font_size = font_size;
1600 self
1601 }
1602
1603 #[inline]
1604 pub fn color(mut self, r: u8, g: u8, b: u8) -> Self {
1605 self.color = [r, g, b];
1606 self
1607 }
1608
1609 #[inline]
1610 pub fn alpha(mut self, alpha: u8) -> Self {
1611 self.alpha = alpha;
1612 self
1613 }
1614
1615 #[inline]
1616 pub fn background_color(mut self, r: u8, g: u8, b: u8) -> Self {
1617 self.background_color = [r, g, b];
1618 self
1619 }
1620
1621 #[inline]
1622 pub fn background_alpha(mut self, alpha: u8) -> Self {
1623 self.background_alpha = alpha;
1624 self
1625 }
1626
1627 #[inline]
1628 pub fn background_rounding(mut self, background_rounding: f32) -> Self {
1629 self.background_rounding = background_rounding;
1630 self
1631 }
1632
1633 #[inline]
1634 pub fn font(mut self, font: &str) -> Self {
1635 self.font = font.to_string();
1636 self
1637 }
1638
1639 #[inline]
1640 pub fn selectable(mut self, selectable: bool) -> Self {
1641 self.selectable = selectable;
1642 self
1643 }
1644
1645 #[inline]
1646 pub fn push_hyperlink_text(
1647 mut self,
1648 target_text: &str,
1649 select_method: HyperlinkSelectMethod,
1650 ) -> Self {
1651 self.hyperlink_text
1652 .push((target_text.to_string(), select_method));
1653 self
1654 }
1655
1656 #[inline]
1657 pub fn hyperlink_text(mut self, hyperlink_text: Vec<(String, HyperlinkSelectMethod)>) -> Self {
1658 self.hyperlink_text = hyperlink_text;
1659 self
1660 }
1661
1662 #[inline]
1663 pub fn auto_fit(mut self, x: bool, y: bool) -> Self {
1664 self.auto_fit = [x, y];
1665 self
1666 }
1667
1668 #[inline]
1669 pub fn tags(mut self, tags: &[[String; 2]], replace: bool) -> Self {
1670 if replace {
1671 self.tags = tags.to_owned();
1672 } else {
1673 for tag in tags {
1674 if let Some(index) = self.tags.iter().position(|x| x[0] == tag[0]) {
1675 self.tags.remove(index);
1676 };
1677 }
1678 self.tags.extend(tags.iter().cloned());
1679 };
1680 self
1681 }
1682}