1use std::{
2 cell::{
3 Cell,
4 Ref,
5 RefCell,
6 RefMut,
7 },
8 marker::PhantomData,
9 mem,
10 ops::{
11 Deref,
12 DerefMut,
13 },
14};
15
16use console_display_macros::{
17 DynamicWidget,
18 SingleWidget,
19 StaticWidget,
20};
21use num_traits::NumCast;
22
23use crate::{
24 color::TerminalColor,
25 console_display::DynamicConsoleDisplay,
26 constraint,
27 drawing::DynamicCanvas,
28 impl_getters,
29 impl_new,
30 impl_setters,
31 or,
32 pixel::{
33 Pixel,
34 character_pixel::CharacterPixel,
35 },
36 widget::{
37 DynamicWidget,
38 StringData,
39 },
40};
41
42use super::StaticWidget;
43
44#[const_trait]
45pub trait SingleWidget<T: DynamicWidget>:
46 DynamicWidget + Deref + DerefMut
47{
48 type Borrowed<'a>: Deref<Target = T>
49 where
50 T: 'a,
51 Self: 'a;
52 type BorrowedMut<'a>: DerefMut<Target = T>
53 where
54 T: 'a,
55 Self: 'a;
56
57 fn child(&self) -> Self::Borrowed<'_>;
58 fn child_mut(&mut self) -> Self::BorrowedMut<'_>;
59}
60
61#[derive(StaticWidget, DynamicWidget)]
62pub struct UvWidget<T: DynamicConsoleDisplay<S>, S: Pixel> {
63 pixel_type: PhantomData<S>,
64 child: T,
65 uv_x_min: f32,
66 uv_x_max: f32,
67 uv_y_min: f32,
68 uv_y_max: f32,
69}
70
71impl<T: DynamicConsoleDisplay<S> + StaticWidget, S: Pixel> DynamicCanvas<S>
72 for UvWidget<T, S>
73{
74 type A = f32;
75
76 fn pixel(
84 &self,
85 x: Self::A,
86 y: Self::A,
87 ) -> Result<<S as Pixel>::U, String>
88 where
89 [(); <S as Pixel>::WIDTH * <S as Pixel>::HEIGHT]:,
90 {
91 let display = self.child();
92 let uv = (
93 Self::uv_to_texture(
94 x,
95 self.uv_x_min,
96 self.uv_x_max,
97 display.width(),
98 ),
99 Self::uv_to_texture(
100 y,
101 self.uv_y_min,
102 self.uv_y_max,
103 display.width(),
104 ),
105 );
106 display.pixel(
107 NumCast::from(uv.0).unwrap(),
108 NumCast::from(uv.1).unwrap(),
109 )
110 }
111
112 fn set_pixel(
118 &mut self,
119 x: Self::A,
120 y: Self::A,
121 value: <S as Pixel>::U,
122 ) -> Result<(), String>
123 where
124 [(); <S as Pixel>::WIDTH * <S as Pixel>::HEIGHT]:,
125 {
126 let display = self.child();
127 if x < self.uv_x_min.min(self.uv_x_max) ||
130 x > self.uv_x_max.max(self.uv_x_min)
131 {
132 return Err("x is outside the uv bounds.".to_owned());
133 }
134 if y < self.uv_y_min.min(self.uv_y_max) ||
135 y > self.uv_y_max.max(self.uv_y_min)
136 {
137 return Err("y is outside the uv bounds.".to_owned());
138 }
139 let uv = (
140 Self::uv_to_texture(
141 x,
142 self.uv_x_min,
143 self.uv_x_max,
144 display.width(),
145 ),
146 Self::uv_to_texture(
147 y,
148 self.uv_y_min,
149 self.uv_y_max,
150 display.height(),
151 ),
152 );
153 self.child_mut().set_pixel(
154 NumCast::from(uv.0).unwrap(),
155 NumCast::from(uv.1).unwrap(),
156 value,
157 )
158 }
159
160 fn draw<D: crate::drawing::DynamicDrawable<N>, const N: usize>(
161 &mut self,
162 drawable: &D,
163 value: <S as Pixel>::U,
164 ) where
165 Self: Sized,
166 [(); <S as Pixel>::WIDTH * <S as Pixel>::HEIGHT]:,
167 {
168 let drawable = drawable.transform(|(x, y)| {
169 (
170 Self::uv_to_texture_f32(
171 x,
172 self.uv_x_min,
173 self.uv_x_max,
174 self.width() as f32,
175 ),
176 Self::uv_to_texture_f32(
177 y,
178 self.uv_y_min,
179 self.uv_y_max,
180 self.height() as f32,
181 ),
182 )
183 });
184 drawable.draw(self.child_mut(), value);
185 }
186}
187
188impl<T: DynamicConsoleDisplay<S>, S: Pixel> UvWidget<T, S> {
189 pub fn new(child: T) -> Self {
190 let (width, height) = (child.width(), child.height());
191 Self {
192 pixel_type: PhantomData::<S>,
193 child,
194 uv_x_min: 0.0,
195 uv_x_max: width as f32,
196 uv_y_min: 0.0,
197 uv_y_max: height as f32,
198 }
199 }
200}
201
202impl<S: Pixel, T: DynamicConsoleDisplay<S> + StaticWidget> UvWidget<T, S> {
203 impl_setters!(pub const uv_x_min: f32, pub const uv_x_max: f32, pub const uv_y_min: f32, pub const uv_y_max: f32);
204
205 #[must_use]
206 pub fn uv_x_to_texture(&self, x: f32) -> usize {
207 Self::uv_to_texture(
208 x,
209 self.uv_x_min,
210 self.uv_x_max,
211 self.child().width(),
212 )
213 }
214
215 #[must_use]
216 pub fn uv_y_to_texture(&self, y: f32) -> usize {
217 Self::uv_to_texture(
218 y,
219 self.uv_y_min,
220 self.uv_y_max,
221 self.child().height(),
222 )
223 }
224
225 #[must_use]
226 pub fn texture_to_uv_x(&self, x: usize) -> f32 {
227 Self::texture_to_uv(
228 x,
229 self.child().width(),
230 self.uv_x_min,
231 self.uv_x_max,
232 )
233 }
234
235 #[must_use]
236 pub fn texture_to_uv_y(&self, y: usize) -> f32 {
237 Self::texture_to_uv(
238 y,
239 self.child().height(),
240 self.uv_y_min,
241 self.uv_y_max,
242 )
243 }
244
245 #[allow(clippy::cast_possible_truncation)]
246 #[allow(clippy::cast_sign_loss)]
247 fn uv_to_texture(
248 uv: f32,
249 uv_min: f32,
250 uv_max: f32,
251 texture_coordinate_max: usize,
252 ) -> usize {
253 ((uv - uv_min) / (uv_max - uv_min))
254 .mul_add(texture_coordinate_max as f32, -0.5)
255 .round() as usize
256 }
257
258 fn uv_to_texture_f32(
259 uv: f32,
260 uv_min: f32,
261 uv_max: f32,
262 texture_coordinate_max: f32,
263 ) -> f32 {
264 ((uv - uv_min) / (uv_max - uv_min))
265 .mul_add(texture_coordinate_max, -0.5)
266 .round()
267 }
268
269 fn texture_to_uv(
270 texture_coordinate: usize,
271 texture_coordinate_max: usize,
272 uv_min: f32,
273 uv_max: f32,
274 ) -> f32 {
275 ((texture_coordinate as f32 + 0.5) / texture_coordinate_max as f32)
276 .mul_add(uv_max - uv_min, uv_min)
277 }
278
279 pub fn x_values(&self) -> impl Iterator<Item = f32> {
305 let width = self.child().width();
306 (0..width).map(|x| self.texture_to_uv_x(x))
307 }
308
309 pub fn y_values(&self) -> impl Iterator<Item = f32> {
335 let height = self.child().height();
336 (0..height).map(|x| self.texture_to_uv_y(x))
337 }
338}
339
340impl<T: DynamicConsoleDisplay<S> + StaticWidget, S: Pixel> const
341 SingleWidget<T> for UvWidget<T, S>
342{
343 type Borrowed<'a>
344 = &'a T
345 where
346 T: 'a,
347 S: 'a;
348
349 type BorrowedMut<'a>
350 = &'a mut T
351 where
352 T: 'a,
353 Self: 'a;
354
355 fn child(&self) -> &T {
356 &self.child
357 }
358
359 fn child_mut(&mut self) -> &mut T {
360 &mut self.child
361 }
362}
363
364impl<T: DynamicConsoleDisplay<S>, S: Pixel> const Deref
365 for UvWidget<T, S>
366{
367 type Target = T;
368
369 fn deref(&self) -> &Self::Target {
370 &self.child
371 }
372}
373
374impl<T: DynamicConsoleDisplay<S>, S: Pixel> const DerefMut
375 for UvWidget<T, S>
376{
377 fn deref_mut(&mut self) -> &mut Self::Target {
378 &mut self.child
379 }
380}
381
382#[derive(StaticWidget)]
383pub struct DoubleBufferWidget<T: DynamicConsoleDisplay<S>, S: Pixel> {
384 pixel_type: PhantomData<S>,
385 child: RefCell<T>,
386 backbuffer: RefCell<Box<[S]>>,
387 is_write: Cell<bool>,
388}
389
390impl<T: DynamicConsoleDisplay<S>, S: Pixel> DoubleBufferWidget<T, S> {
391 pub fn new(child: T) -> Self
392 where
393 [(); S::WIDTH * S::HEIGHT]:,
394 {
395 let pixels = child.data().to_vec().into_boxed_slice();
396 Self {
397 pixel_type: PhantomData::<S>,
398 child: RefCell::new(child),
399 backbuffer: RefCell::new(pixels),
400 is_write: false.into(),
401 }
402 }
403
404 #[allow(clippy::swap_with_temporary)]
405 pub fn swap_buffers(&self) {
406 mem::swap(
407 self.child.borrow_mut().data_mut(),
408 &mut self.backbuffer.borrow_mut(),
409 );
410 }
411}
412
413impl<T: DynamicConsoleDisplay<S>, S: Pixel> DynamicWidget
414 for DoubleBufferWidget<T, S>
415{
416 fn width_characters(&self) -> usize {
417 self.child.borrow().width_characters()
418 }
419
420 fn height_characters(&self) -> usize {
421 self.child.borrow().height_characters()
422 }
423
424 fn string_data(&self) -> StringData {
425 if self.is_write.get() {
426 self.swap_buffers();
427 self.is_write.set(false);
428 }
429 self.child.borrow().string_data()
430 }
431}
432
433impl<T: DynamicConsoleDisplay<S>, S: Pixel> const SingleWidget<T>
434 for DoubleBufferWidget<T, S>
435{
436 type Borrowed<'a>
437 = Ref<'a, T>
438 where
439 T: 'a,
440 Self: 'a;
441
442 type BorrowedMut<'a>
443 = RefMut<'a, T>
444 where
445 T: 'a,
446 Self: 'a;
447
448 fn child(&self) -> Ref<'_, T> {
449 self.child.borrow()
450 }
451
452 fn child_mut(&mut self) -> RefMut<'_, T> {
453 self.child.borrow_mut()
454 }
455}
456
457impl<T: DynamicConsoleDisplay<S>, S: Pixel> Deref
458 for DoubleBufferWidget<T, S>
459{
460 type Target = T;
461
462 fn deref(&self) -> &Self::Target {
463 if self.is_write.get() {
464 self.swap_buffers();
465 self.is_write.set(false);
466 }
467 unsafe { &*self.child.as_ptr() }
469 }
470}
471
472impl<T: DynamicConsoleDisplay<S>, S: Pixel> DerefMut
473 for DoubleBufferWidget<T, S>
474{
475 fn deref_mut(&mut self) -> &mut Self::Target {
476 if !self.is_write.get() {
477 self.swap_buffers();
478 self.is_write.set(true);
479 }
480 self.child.get_mut()
481 }
482}
483
484#[derive(SingleWidget)]
485pub struct PaddingWidget<T: DynamicWidget> {
486 child: T,
487 padding_left: usize,
488 padding_right: usize,
489 padding_top: usize,
490 padding_bottom: usize,
491}
492
493impl<T: DynamicWidget> PaddingWidget<T> {
494 impl_new!(pub const PaddingWidget<T>, child: T, padding_left: usize, padding_right: usize, padding_top: usize, padding_bottom: usize);
495
496 impl_setters!(pub const padding_left: usize, pub const padding_right: usize, pub const padding_top: usize, pub const padding_bottom: usize);
497}
498
499impl<T: DynamicWidget> DynamicWidget for PaddingWidget<T> {
500 fn width_characters(&self) -> usize {
501 self.child.width_characters() +
502 self.padding_left +
503 self.padding_right
504 }
505
506 fn height_characters(&self) -> usize {
507 self.child.height_characters() +
508 self.padding_top +
509 self.padding_bottom
510 }
511
512 fn string_data(&self) -> StringData {
513 let mut data = self.child.string_data().data;
514 let padding_top = vec![
515 vec![
516 CharacterPixel::default().into();
517 self.width_characters()
518 ];
519 self.padding_top
520 ];
521 let padding_bottom = vec![
522 vec![
523 CharacterPixel::default().into();
524 self.width_characters()
525 ];
526 self.padding_bottom
527 ];
528 data = data
529 .into_iter()
530 .map(|line| {
531 [
532 vec![
533 CharacterPixel::default().into();
534 self.padding_left
535 ],
536 line,
537 vec![
538 CharacterPixel::default().into();
539 self.padding_right
540 ],
541 ]
542 .concat()
543 })
544 .collect();
545 StringData {
546 data: [padding_top, data, padding_bottom].concat(),
547 }
548 }
549}
550
551impl<T: DynamicWidget> const Deref for PaddingWidget<T> {
552 type Target = T;
553
554 fn deref(&self) -> &Self::Target {
555 &self.child
556 }
557}
558
559impl<T: DynamicWidget> const DerefMut for PaddingWidget<T> {
560 fn deref_mut(&mut self) -> &mut Self::Target {
561 &mut self.child
562 }
563}
564
565#[const_trait]
566pub trait Border {
567 fn border_at(
568 &self,
569 width: usize,
570 height: usize,
571 ) -> impl Fn(usize, usize) -> CharacterPixel;
572 fn width_top(&self) -> usize;
573 fn width_left(&self) -> usize;
574 fn width_bottom(&self) -> usize;
575 fn width_right(&self) -> usize;
576}
577
578pub struct BorderDefault {
579 top: CharacterPixel,
580 top_left: CharacterPixel,
581 left: CharacterPixel,
582 bottom_left: CharacterPixel,
583 bottom: CharacterPixel,
584 bottom_right: CharacterPixel,
585 right: CharacterPixel,
586 top_right: CharacterPixel,
587}
588
589impl BorderDefault {
590 impl_new!(
591 #[allow(clippy::too_many_arguments)]
592 #[must_use] pub const BorderDefault,
593 top: CharacterPixel,
594 top_left: CharacterPixel,
595 left: CharacterPixel,
596 bottom_left: CharacterPixel,
597 bottom: CharacterPixel,
598 bottom_right: CharacterPixel,
599 right: CharacterPixel,
600 top_right: CharacterPixel
601 );
602
603 impl_getters!(
604 #[must_use] pub const top: CharacterPixel,
605 #[must_use] pub const top_left: CharacterPixel,
606 #[must_use] pub const left: CharacterPixel,
607 #[must_use] pub const bottom_left: CharacterPixel,
608 #[must_use] pub const bottom: CharacterPixel,
609 #[must_use] pub const bottom_right: CharacterPixel,
610 #[must_use] pub const right: CharacterPixel,
611 #[must_use] pub const top_right: CharacterPixel
612 );
613
614 pub fn symmetric(
618 horizontal: char,
619 vertical: char,
620 corner: char,
621 foreground: TerminalColor,
622 background: TerminalColor,
623 ) -> Result<Self, String> {
624 let horizontal =
625 CharacterPixel::build(horizontal, foreground, background)?;
626 let vertical =
627 CharacterPixel::build(vertical, foreground, background)?;
628 let corner =
629 CharacterPixel::build(corner, foreground, background)?;
630
631 Ok(Self::new(
632 horizontal, corner, vertical, corner, horizontal, corner,
633 vertical, corner,
634 ))
635 }
636
637 #[must_use]
638 pub fn symmetric_static<
639 const HORIZONTAL: char,
640 const VERTICAL: char,
641 const CORNER: char,
642 >(
643 foreground: TerminalColor,
644 background: TerminalColor,
645 ) -> Self
646 where
647 constraint!(HORIZONTAL >= '\u{20}'):, constraint!(or!(HORIZONTAL < '\u{7F}', HORIZONTAL >= '\u{A0}')):, constraint!(VERTICAL >= '\u{20}'):, constraint!(or!(VERTICAL < '\u{7F}', VERTICAL >= '\u{A0}')):, constraint!(CORNER >= '\u{20}'):, constraint!(or!(CORNER < '\u{7F}', CORNER >= '\u{A0}')):, {
654 let horizontal =
655 CharacterPixel::new::<HORIZONTAL>(foreground, background);
656 let vertical =
657 CharacterPixel::new::<VERTICAL>(foreground, background);
658 let corner = CharacterPixel::new::<CORNER>(foreground, background);
659
660 Self::new(
661 horizontal, corner, vertical, corner, horizontal, corner,
662 vertical, corner,
663 )
664 }
665
666 #[must_use]
667 pub fn ascii(
668 foreground: TerminalColor,
669 background: TerminalColor,
670 ) -> Self {
671 Self::symmetric_static::<'-', '|', '+'>(foreground, background)
672 }
673
674 #[must_use]
675 pub fn overdrawn(
676 foreground: TerminalColor,
677 background: TerminalColor,
678 ) -> Self {
679 Self::symmetric_static::<'─', '│', '┼'>(
680 foreground, background,
681 )
682 }
683
684 #[allow(clippy::too_many_arguments)]
688 pub fn same_color(
689 top: char,
690 top_left: char,
691 left: char,
692 bottom_left: char,
693 bottom: char,
694 bottom_right: char,
695 right: char,
696 top_right: char,
697 foreground: TerminalColor,
698 background: TerminalColor,
699 ) -> Result<Self, String> {
700 let top = CharacterPixel::build(top, foreground, background)?;
701 let top_left =
702 CharacterPixel::build(top_left, foreground, background)?;
703 let left = CharacterPixel::build(left, foreground, background)?;
704 let bottom_left =
705 CharacterPixel::build(bottom_left, foreground, background)?;
706 let bottom =
707 CharacterPixel::build(bottom, foreground, background)?;
708 let bottom_right =
709 CharacterPixel::build(bottom_right, foreground, background)?;
710 let right = CharacterPixel::build(right, foreground, background)?;
711 let top_right =
712 CharacterPixel::build(top_right, foreground, background)?;
713
714 Ok(Self::new(
715 top,
716 top_left,
717 left,
718 bottom_left,
719 bottom,
720 bottom_right,
721 right,
722 top_right,
723 ))
724 }
725
726 #[must_use]
727 pub fn same_color_static<
728 const TOP: char,
729 const TOP_LEFT: char,
730 const LEFT: char,
731 const BOTTOM_LEFT: char,
732 const BOTTOM: char,
733 const BOTTOM_RIGHT: char,
734 const RIGHT: char,
735 const TOP_RIGHT: char,
736 >(
737 foreground: TerminalColor,
738 background: TerminalColor,
739 ) -> Self
740 where
741 constraint!(TOP >= '\u{20}'):, constraint!(or!(TOP < '\u{7F}', TOP >= '\u{A0}')):, constraint!(TOP_LEFT >= '\u{20}'):, constraint!(or!(TOP_LEFT < '\u{7F}', TOP_LEFT >= '\u{A0}')):, constraint!(LEFT >= '\u{20}'):, constraint!(or!(LEFT < '\u{7F}', LEFT >= '\u{A0}')):, constraint!(BOTTOM_LEFT >= '\u{20}'):, constraint!(or!(BOTTOM_LEFT < '\u{7F}', BOTTOM_LEFT >= '\u{A0}')):, constraint!(BOTTOM >= '\u{20}'):, constraint!(or!(BOTTOM < '\u{7F}', BOTTOM >= '\u{A0}')):, constraint!(BOTTOM_RIGHT >= '\u{20}'):, constraint!(or!(
754 BOTTOM_RIGHT < '\u{7F}',
755 BOTTOM_RIGHT >= '\u{A0}'
756 )):, constraint!(RIGHT >= '\u{20}'):, constraint!(or!(RIGHT < '\u{7F}', RIGHT >= '\u{A0}')):, constraint!(TOP_RIGHT >= '\u{20}'):, constraint!(or!(TOP_RIGHT < '\u{7F}', TOP_RIGHT >= '\u{A0}')):, {
762 let top = CharacterPixel::new::<TOP>(foreground, background);
763 let top_left =
764 CharacterPixel::new::<TOP_LEFT>(foreground, background);
765 let left = CharacterPixel::new::<LEFT>(foreground, background);
766 let bottom_left =
767 CharacterPixel::new::<BOTTOM_LEFT>(foreground, background);
768 let bottom = CharacterPixel::new::<BOTTOM>(foreground, background);
769 let bottom_right =
770 CharacterPixel::new::<BOTTOM_RIGHT>(foreground, background);
771 let right = CharacterPixel::new::<RIGHT>(foreground, background);
772 let top_right =
773 CharacterPixel::new::<TOP_RIGHT>(foreground, background);
774
775 Self::new(
776 top,
777 top_left,
778 left,
779 bottom_left,
780 bottom,
781 bottom_right,
782 right,
783 top_right,
784 )
785 }
786
787 #[must_use]
788 pub fn single_stroke(
789 foreground: TerminalColor,
790 background: TerminalColor,
791 ) -> Self {
792 Self::same_color_static::<'─', '┌', '│', '└', '─', '┘', '│', '┐'>(
793 foreground, background,
794 )
795 }
796
797 #[must_use]
798 pub fn rounded_corners(
799 foreground: TerminalColor,
800 background: TerminalColor,
801 ) -> Self {
802 Self::same_color_static::<'─', '╭', '│', '╰', '─', '╯', '│', '╮'>(
803 foreground, background,
804 )
805 }
806
807 #[must_use]
808 pub fn double_stroke(
809 foreground: TerminalColor,
810 background: TerminalColor,
811 ) -> Self {
812 Self::same_color_static::<'═', '╔', '║', '╚', '═', '╝', '║', '╗'>(
813 foreground, background,
814 )
815 }
816
817 #[must_use]
818 pub fn bold_stroke(
819 foreground: TerminalColor,
820 background: TerminalColor,
821 ) -> Self {
822 Self::same_color_static::<'━', '┏', '┃', '┗', '━', '┛', '┃', '┓'>(
823 foreground, background,
824 )
825 }
826}
827
828impl Border for BorderDefault {
829 fn border_at(
830 &self,
831 width: usize,
832 height: usize,
833 ) -> impl Fn(usize, usize) -> CharacterPixel {
834 move |x: usize, y: usize| match (x, y) {
835 (0, 0) => self.top_left,
836 (0, y) if y == height - 1 => self.bottom_left,
837 (x, y) if x == width - 1 && y == height - 1 => {
838 self.bottom_right
839 }
840 (x, 0) if x == width - 1 => self.top_right,
841 (_, 0) => self.top,
842 (0, _) => self.left,
843 (_, y) if y == height - 1 => self.bottom,
844 (x, _) if x == width - 1 => self.right,
845 (_, _) => CharacterPixel::default(),
846 }
847 }
848
849 fn width_top(&self) -> usize {
850 1
851 }
852
853 fn width_left(&self) -> usize {
854 1
855 }
856
857 fn width_bottom(&self) -> usize {
858 1
859 }
860
861 fn width_right(&self) -> usize {
862 1
863 }
864}
865
866pub struct BorderWidget<T: DynamicWidget, S: Border> {
867 child: T,
868 border: S,
869}
870
871impl<T: DynamicWidget, S: Border> BorderWidget<T, S> {
872 impl_new!(pub const BorderWidget<T, S>, child: T, border: S);
873}
874
875impl<T: DynamicWidget, S: Border> DynamicWidget for BorderWidget<T, S> {
876 fn width_characters(&self) -> usize {
877 self.child.width_characters() +
878 self.border.width_left() +
879 self.border.width_right()
880 }
881
882 fn height_characters(&self) -> usize {
883 self.child.height_characters() +
884 self.border.width_top() +
885 self.border.width_bottom()
886 }
887
888 fn string_data(&self) -> StringData {
889 let border_at = self
890 .border
891 .border_at(self.width_characters(), self.height_characters());
892 let mut data = self.child.string_data().data;
893 let border_top = (0..self.border.width_top())
894 .map(|y| {
895 (0..self.width_characters())
896 .map(|x| border_at(x, y).into())
897 .collect()
898 })
899 .collect();
900 let border_bottom = (self.height_characters() -
901 self.border.width_bottom()..
902 self.height_characters())
903 .map(|y| {
904 (0..self.width_characters())
905 .map(|x| border_at(x, y).into())
906 .collect()
907 })
908 .collect();
909 data = data
910 .into_iter()
911 .enumerate()
912 .map(|(y, line)| {
913 [
914 (0..self.border.width_left())
915 .map(|x| {
916 border_at(x, y + self.border.width_top())
917 .into()
918 })
919 .collect(),
920 line,
921 (0..self.border.width_right())
922 .map(|x| {
923 border_at(x, y + self.border.width_top())
924 .into()
925 })
926 .collect(),
927 ]
928 .concat()
929 })
930 .collect();
931 StringData {
932 data: [border_top, data, border_bottom].concat(),
933 }
934 }
935}
936
937impl<T: DynamicWidget, S: Border> const Deref for BorderWidget<T, S> {
938 type Target = T;
939
940 fn deref(&self) -> &Self::Target {
941 &self.child
942 }
943}
944
945impl<T: DynamicWidget, S: Border> const DerefMut for BorderWidget<T, S> {
946 fn deref_mut(&mut self) -> &mut Self::Target {
947 &mut self.child
948 }
949}
950
951pub struct InsetWidget<T: DynamicWidget> {
952 child: T,
953 inset_left: usize,
954 inset_right: usize,
955 inset_top: usize,
956 inset_bottom: usize,
957}
958
959impl<T: DynamicWidget> InsetWidget<T> {
960 impl_new!(pub const InsetWidget<T>, child: T, inset_left: usize, inset_right: usize, inset_top: usize, inset_bottom: usize);
961
962 impl_getters!(pub const child: T);
963}
964
965impl<T: DynamicWidget> DynamicWidget for InsetWidget<T> {
966 fn width_characters(&self) -> usize {
967 self.child
968 .width_characters()
969 .saturating_sub(self.inset_left)
970 .saturating_sub(self.inset_right)
971 }
972
973 fn height_characters(&self) -> usize {
974 self.child
975 .height_characters()
976 .saturating_sub(self.inset_top)
977 .saturating_sub(self.inset_bottom)
978 }
979
980 fn string_data(&self) -> StringData {
981 let mut data = self.child.string_data().data;
982
983 data = data
984 .into_iter()
985 .skip(self.inset_top)
986 .take(self.height_characters())
987 .collect::<Vec<_>>();
988
989 StringData {
990 data: data
991 .into_iter()
992 .map(|line| {
993 line[self.inset_left..
994 self.width_characters() + self.inset_left]
995 .to_vec()
996 })
997 .collect(),
998 }
999 }
1000}
1001
1002impl<T: DynamicWidget> const Deref for InsetWidget<T> {
1003 type Target = T;
1004
1005 fn deref(&self) -> &Self::Target {
1006 &self.child
1007 }
1008}
1009
1010impl<T: DynamicWidget> const DerefMut for InsetWidget<T> {
1011 fn deref_mut(&mut self) -> &mut Self::Target {
1012 &mut self.child
1013 }
1014}
1015
1016#[cfg(test)]
1017mod tests {
1018 use crate::{
1019 pixel::monochrome_pixel::SinglePixel,
1020 pixel_display::StaticPixelDisplay,
1021 };
1022
1023 use super::*;
1024
1025 mod uv_widget {
1026 use super::*;
1027
1028 #[test]
1029 fn texture_to_uv() {
1030 let expected = 0.0005;
1031 let actual = UvWidget::<
1032 StaticPixelDisplay<SinglePixel, 1, 1>,
1033 SinglePixel,
1034 >::texture_to_uv(500, 1000, -0.5, 0.5);
1035 let error = expected * 0.0001;
1036 assert!((actual - 0.0005).abs() < error);
1037 }
1038
1039 #[test]
1040 fn uv_to_texture() {
1041 let expected = 1500;
1042 let actual = UvWidget::<
1043 StaticPixelDisplay<SinglePixel, 1, 1>,
1044 SinglePixel,
1045 >::uv_to_texture(0.5, -1.0, 1.0, 2000);
1046 assert_eq!(actual, expected);
1047 }
1048 }
1049
1050 mod double_buffer_widget {
1051 use crate::console_display::StaticConsoleDisplay;
1052
1053 use super::*;
1054
1055 #[test]
1056 fn buffer_swap() {
1057 let mut widget =
1058 DoubleBufferWidget::new(StaticPixelDisplay::<
1059 SinglePixel,
1060 1,
1061 1,
1062 >::new(false));
1063 widget.set_pixel_static::<0, 0>(true);
1064 let buffer1 = widget.backbuffer.clone();
1065 widget.swap_buffers();
1066 let buffer2 = widget.backbuffer;
1067 assert_ne!(
1068 buffer1.borrow()[0].character().to_string(),
1069 buffer2.borrow()[0].character().to_string()
1070 );
1071 }
1072 }
1073 mod padding_widget {
1074 use super::*;
1075
1076 #[test]
1077 fn dimensions() {
1078 let widget = PaddingWidget::new(
1079 StaticPixelDisplay::<SinglePixel, 1, 1>::new(false),
1080 10,
1081 20,
1082 30,
1083 40,
1084 );
1085 assert_eq!(widget.width_characters(), 31);
1086 assert_eq!(widget.height_characters(), 71);
1087 }
1088 }
1089}