1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//! Bounds (x,y,w,h) and functionality
use super::*;

#[derive(Clone,Copy,Default)]
pub struct Offset {
    pub x: i32,
    pub y: i32,
}
#[derive(Clone,Copy,Default)]
pub struct Dims {
    pub w: u32,
    pub h: u32,
}
#[derive(Clone,Copy,Default)]
pub struct Bounds {
    pub off: Offset,
    pub size: Dims,
}

impl Bounds {
    pub const fn from_xywh(x: i32, y: i32, w: u32, h: u32) -> Self {
        Self{
            off: Offset{
                x,
                y,
            },
            size: Dims{
                w,
                h,
            }
        }
    }
    pub const fn from_xy(x: i32, y: i32) -> Self {
        Self::from_xywh(x,y,0,0)
    }
    pub const fn from_wh(w: u32, h: u32) -> Self {
        Self::from_xywh(0,0,w,h)
    }
    pub const fn from_off(off: Offset) -> Self {
        Self::from_xywh(off.x,off.y,0,0)
    }
    pub const fn from_size(size: Dims) -> Self {
        Self::from_xywh(0,0,size.w,size.h)
    }

    #[inline] pub fn x(&self) -> i32 { self.off.x }
    #[inline] pub fn y(&self) -> i32 { self.off.y }
    #[inline] pub fn w(&self) -> u32 { self.size.w }
    #[inline] pub fn h(&self) -> u32 { self.size.h }

    #[inline] pub fn x_mut(&mut self) -> &mut i32 { &mut self.off.x }
    #[inline] pub fn y_mut(&mut self) -> &mut i32 { &mut self.off.y }
    #[inline] pub fn w_mut(&mut self) -> &mut u32 { &mut self.size.w }
    #[inline] pub fn h_mut(&mut self) -> &mut u32 { &mut self.size.h }

    #[inline] pub fn x1(&self) -> i32 { self.off.x + (self.size.w as i32) }
    #[inline] pub fn y1(&self) -> i32 { self.off.y + (self.size.h as i32) }

    /// get the bounds inside this bound (subtract border)
    pub fn inside_border(&self, b: &Border) -> Self {
        let mut s = *self;
        s.off += b.inner();
        s.size -= b.border_effective();
        s
    }
    /// b is the inner and relative to self
    /// get the part of the inner which also is inside this bound
    pub fn slice(&self, inner_relative: &Bounds) -> Self {
        let b = inner_relative;
        Self{
            off: &self.off + &b.off,
            size: Dims{
                w: b.size.w .min( (self.size.w as i32).saturating_sub(b.off.x) as u32 ),
                h: b.size.h .min( (self.size.h as i32).saturating_sub(b.off.y) as u32 ),
            }
        }
    }
    //TODO doc
    pub fn step(&self, step: i32) -> Self {
        let mut senf = *self;
        senf.off.x += step;
        senf.off.y += step;
        senf.size.w = (senf.size.w as i32 - step*2).max(0) as u32;
        senf.size.h = (senf.size.h as i32 - step*2).max(0) as u32;
        senf
    }
    /// get bound with size s and centered relative to self
    pub fn inner_centered(&self, s: Dims) -> Self {
        let nx = (self.size.w as i32 - s.w as i32)/2;
        let ny = (self.size.h as i32 - s.h as i32)/2;
        Self{
            off: Offset{
                x: self.off.x + nx,
                y: self.off.y + ny,
            },
            size: s,
        }
    }

    pub fn from_ori(par_off: i32, unpar_off: i32, par_size: u32, unpar_size: u32, o: Orientation) -> Self {
        match o {
            Orientation::Horizontal => Self::from_xywh(par_off,unpar_off,par_size,unpar_size),
            Orientation::Vertical => Self::from_xywh(unpar_off,par_off,unpar_size,par_size),
        }
    }

    pub fn par(&self, o: Orientation) -> (i32,u32) { //TODO improve with negate orientation
        match o {
            Orientation::Horizontal => (self.off.x,self.size.w),
            Orientation::Vertical => (self.off.y,self.size.h),
        }
    }
    pub fn unpar(&self, o: Orientation) -> (i32,u32) {
        match o {
            Orientation::Horizontal => (self.off.y,self.size.h),
            Orientation::Vertical => (self.off.x,self.size.w),
        }
    }
}

impl Offset {
    /// if the offset is inside the bound b
    pub fn is_inside(&self, b: &Bounds) -> bool {
        self.x >= b.x() && self.x < b.x1() &&
        self.y >= b.y() && self.y < b.y1()
    }

    pub fn par(&self, o: Orientation) -> i32 {
        match o {
            Orientation::Horizontal => self.x,
            Orientation::Vertical => self.y,
        }
    }
    pub fn unpar(&self, o: Orientation) -> i32 {
        match o {
            Orientation::Horizontal => self.y,
            Orientation::Vertical => self.x,
        }
    }
}

impl Dims {
    pub fn par(&self, o: Orientation) -> u32 {
        match o {
            Orientation::Horizontal => self.w,
            Orientation::Vertical => self.h,
        }
    }
    pub fn unpar(&self, o: Orientation) -> u32 {
        match o {
            Orientation::Horizontal => self.h,
            Orientation::Vertical => self.w,
        }
    }
}

impl<T,U> From<(T,U)> for Dims where T: Into<u32>, U: Into<u32> {
    fn from(s: (T,U)) -> Self {
        Self{w: s.0.into(), h: s.1.into()}
    }
}

impl<T,U> From<(T,U)> for Offset where T: Into<i32>, U: Into<i32> {
    fn from(s: (T,U)) -> Self {
        Self{x: s.0.into(), y: s.1.into()}
    }
}

qwutils::opion!(add(Bounds,Offset) |s,r| {
    s.off.x += r.x;
    s.off.y += r.y;
});
qwutils::opion!(sub(Bounds,Offset) |s,r| {
    s.off.x -= r.x;
    s.off.y -= r.y;
});

qwutils::opion!(add(Offset,Offset) |s,r| {
    s.x += r.x;
    s.y += r.y;
});
qwutils::opion!(sub(Offset,Offset) |s,r| {
    s.x -= r.x;
    s.y -= r.y;
});

qwutils::opion!(add(Dims,Dims) |s,r| {
    s.w += r.w;
    s.h += r.h;
});
qwutils::opion!(sub(Dims,Dims) |s,r| {
    s.w = s.w.saturating_sub( r.w );
    s.h = s.h.saturating_sub( r.h );
});


/*qwutils::opion!(add(Bounds,Border) |s,r| {
    s.off.x += r.width();
    s.off.y += r.height();
});
qwutils::opion!(sub(Bounds,Border) |s,r| {
    s.off.x -= r.width();
    s.off.y -= r.height();
});*/

qwutils::opion!(bitand(Bounds,Bounds) |s,r| {
    let nx = s.off.x.max(r.off.x);
    let ny = s.off.y.max(r.off.y);
    *s = Bounds{
        off: Offset{
            x: nx,
            y: ny,
        },
        size: Dims{
            w: (s.x1().min(r.x1()) - nx).max(0) as u32,
            h: (s.y1().min(r.y1()) - ny).max(0) as u32,
        }
    }
});