1use std::fmt::Display;
10
11#[derive(Clone, Debug, Default)]
12pub struct ViewParameters {
13 x: Option<i32>,
14 y: Option<i32>,
15 width: u32,
16 height: u32,
17 vx: i32,
18 vy: i32,
19 vw: u32,
20 vh: u32,
21}
22
23impl ViewParameters {
24 pub fn new(vx: i32, vy: i32, vw: u32, vh: u32, width: u32, height: u32) -> Self {
25 ViewParameters {
26 vx,
27 vy,
28 vw,
29 vh,
30 x: None,
31 y: None,
32 width,
33 height,
34 }
35 }
36
37 pub fn view_box(&self) -> (i32, i32, u32, u32) {
38 (self.vx, self.vy, self.vw, self.vh)
39 }
40
41 pub fn view_box_str(&self) -> String {
42 format!("{} {} {} {}", self.vx, self.vy, self.vw, self.vh)
43 }
44
45 pub fn set_view_box(&mut self, vx: i32, vy: i32, vw: u32, vh: u32) {
46 self.vx = vx;
47 self.vy = vy;
48 self.vw = vw;
49 self.vh = vh;
50 }
51
52 pub fn x(&self) -> Option<i32> {
54 self.x
55 }
56
57 pub fn set_x(&mut self, x: i32) {
58 self.x = Some(x);
59 }
60
61 pub fn y(&self) -> Option<i32> {
63 self.y
64 }
65
66 pub fn set_y(&mut self, y: i32) {
67 self.y = Some(y);
68 }
69
70 pub fn width(&self) -> u32 {
71 self.width
72 }
73
74 pub fn set_width(&mut self, width: u32) {
75 self.width = width;
76 }
77
78 pub fn height(&self) -> u32 {
79 self.height
80 }
81
82 pub fn set_height(&mut self, height: u32) {
83 self.height = height;
84 }
85
86 pub fn extend(&mut self, other: &ViewParameters) {
87 self.vx = self.vx.min(other.vx);
88 self.vy = self.vy.min(other.vy);
89 self.vw = self.vw.max(other.vw);
90 self.vh = self.vh.max(other.vh);
91 }
92
93 pub fn extend_with_pos(&mut self, other: &ViewParameters, x: i32, y: i32) {
94 self.vx = self.vx.min(other.vx);
95 self.vy = self.vy.min(other.vy);
96 self.vw = self.vw.max((other.vw as i32 + x) as u32);
97 self.vh = self.vh.max((other.vh as i32 + y) as u32);
98 }
99
100 pub fn extend_with_margin(&mut self, other: &ViewParameters, margin: i32) {
101 self.vx = self.vx.min(other.vx);
102 self.vy = self.vy.min(other.vy);
103 self.vw = self.vw.max((other.vw as i32 + 2 * margin) as u32);
104 self.vh = self.vh.max((other.vh as i32 + 2 * margin) as u32);
105 }
106}
107
108impl Display for ViewParameters {
109 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
110 write!(f, "{} {} {} {}", self.vx, self.vy, self.vw, self.vh)
111 }
112}
113
114impl From<ViewParameters> for (i32, i32, u32, u32) {
115 fn from(value: ViewParameters) -> Self {
116 (value.vx, value.vy, value.vw, value.vh)
117 }
118}