gfx_types 0.2.0

Core graphics types shared between kernel and userspace - RedstoneOS
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! # Rect Types
//!
//! Retângulos definidos por posição e tamanho.

use super::{Point, PointF, Size, SizeF};

// =============================================================================
// RECT (Integer)
// =============================================================================

/// Retângulo definido por posição e tamanho.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Rect {
    pub x: i32,
    pub y: i32,
    pub width: u32,
    pub height: u32,
}

impl Rect {
    /// Cria novo retângulo.
    #[inline]
    pub const fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Retângulo vazio na origem.
    pub const ZERO: Self = Self {
        x: 0,
        y: 0,
        width: 0,
        height: 0,
    };

    /// Cria retângulo a partir de tamanho (posição = origem).
    #[inline]
    pub const fn from_size(size: Size) -> Self {
        Self {
            x: 0,
            y: 0,
            width: size.width,
            height: size.height,
        }
    }

    /// Cria retângulo a partir de ponto e tamanho.
    #[inline]
    pub const fn from_point_size(point: Point, size: Size) -> Self {
        Self {
            x: point.x,
            y: point.y,
            width: size.width,
            height: size.height,
        }
    }

    /// Cria a partir de dois pontos (canto superior esquerdo e inferior direito).
    #[inline]
    pub fn from_points(p1: Point, p2: Point) -> Self {
        let x1 = p1.x.min(p2.x);
        let y1 = p1.y.min(p2.y);
        let x2 = p1.x.max(p2.x);
        let y2 = p1.y.max(p2.y);
        Self {
            x: x1,
            y: y1,
            width: (x2 - x1) as u32,
            height: (y2 - y1) as u32,
        }
    }

    /// Retorna o canto superior esquerdo.
    #[inline]
    pub const fn origin(&self) -> Point {
        Point::new(self.x, self.y)
    }

    /// Retorna o tamanho.
    #[inline]
    pub const fn size(&self) -> Size {
        Size::new(self.width, self.height)
    }

    /// Coordenada X da borda esquerda (alias para x).
    #[inline]
    pub const fn left(&self) -> i32 {
        self.x
    }

    /// Coordenada Y da borda superior (alias para y).
    #[inline]
    pub const fn top(&self) -> i32 {
        self.y
    }

    /// Coordenada X da borda direita (exclusivo).
    #[inline]
    pub const fn right(&self) -> i32 {
        self.x.saturating_add(self.width as i32)
    }

    /// Coordenada Y da borda inferior (exclusivo).
    #[inline]
    pub const fn bottom(&self) -> i32 {
        self.y.saturating_add(self.height as i32)
    }

    /// Centro do retângulo.
    #[inline]
    pub const fn center(&self) -> Point {
        Point {
            x: self.x + (self.width as i32 / 2),
            y: self.y + (self.height as i32 / 2),
        }
    }

    /// Verifica se o retângulo é vazio.
    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.width == 0 || self.height == 0
    }

    /// Área do retângulo.
    #[inline]
    pub const fn area(&self) -> u64 {
        self.width as u64 * self.height as u64
    }

    /// Verifica se contém um ponto.
    #[inline]
    pub fn contains_point(&self, p: Point) -> bool {
        p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
    }

    /// Verifica se contém outro retângulo.
    #[inline]
    pub fn contains_rect(&self, other: &Rect) -> bool {
        other.x >= self.x
            && other.y >= self.y
            && other.right() <= self.right()
            && other.bottom() <= self.bottom()
    }

    /// Verifica se intersecta outro retângulo.
    #[inline]
    pub fn intersects(&self, other: &Rect) -> bool {
        self.x < other.right()
            && self.right() > other.x
            && self.y < other.bottom()
            && self.bottom() > other.y
    }

    /// Calcula a interseção de dois retângulos.
    pub fn intersection(&self, other: &Rect) -> Option<Rect> {
        let x1 = self.x.max(other.x);
        let y1 = self.y.max(other.y);
        let x2 = self.right().min(other.right());
        let y2 = self.bottom().min(other.bottom());

        if x1 < x2 && y1 < y2 {
            Some(Rect::new(x1, y1, (x2 - x1) as u32, (y2 - y1) as u32))
        } else {
            None
        }
    }

    /// Calcula a união (bounding box) de dois retângulos.
    pub fn union(&self, other: &Rect) -> Rect {
        if self.is_empty() {
            return *other;
        }
        if other.is_empty() {
            return *self;
        }

        let x1 = self.x.min(other.x);
        let y1 = self.y.min(other.y);
        let x2 = self.right().max(other.right());
        let y2 = self.bottom().max(other.bottom());

        Rect::new(x1, y1, (x2 - x1) as u32, (y2 - y1) as u32)
    }

    /// Move o retângulo por um offset.
    #[inline]
    pub const fn offset(&self, dx: i32, dy: i32) -> Self {
        Self {
            x: self.x + dx,
            y: self.y + dy,
            width: self.width,
            height: self.height,
        }
    }

    /// Expande o retângulo em todas as direções.
    #[inline]
    pub fn expand(&self, amount: i32) -> Self {
        Self {
            x: self.x - amount,
            y: self.y - amount,
            width: (self.width as i32 + amount * 2).max(0) as u32,
            height: (self.height as i32 + amount * 2).max(0) as u32,
        }
    }

    /// Encolhe o retângulo em todas as direções.
    #[inline]
    pub fn shrink(&self, amount: i32) -> Self {
        self.expand(-amount)
    }

    /// Divide horizontalmente em duas partes.
    #[inline]
    pub fn split_horizontal(&self, at: u32) -> (Rect, Rect) {
        let at = at.min(self.width);
        (
            Rect::new(self.x, self.y, at, self.height),
            Rect::new(
                self.x + at as i32,
                self.y,
                self.width.saturating_sub(at),
                self.height,
            ),
        )
    }

    /// Divide verticalmente em duas partes.
    #[inline]
    pub fn split_vertical(&self, at: u32) -> (Rect, Rect) {
        let at = at.min(self.height);
        (
            Rect::new(self.x, self.y, self.width, at),
            Rect::new(
                self.x,
                self.y + at as i32,
                self.width,
                self.height.saturating_sub(at),
            ),
        )
    }

    /// Converte para RectF.
    #[inline]
    pub const fn to_float(&self) -> RectF {
        RectF {
            x: self.x as f32,
            y: self.y as f32,
            width: self.width as f32,
            height: self.height as f32,
        }
    }
}

// =============================================================================
// RECTF (Floating Point)
// =============================================================================

/// Retângulo com coordenadas de ponto flutuante.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RectF {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl RectF {
    /// Cria novo retângulo.
    #[inline]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Retângulo zero.
    pub const ZERO: Self = Self {
        x: 0.0,
        y: 0.0,
        width: 0.0,
        height: 0.0,
    };

    /// Cria a partir de tamanho.
    #[inline]
    pub const fn from_size(size: SizeF) -> Self {
        Self {
            x: 0.0,
            y: 0.0,
            width: size.width,
            height: size.height,
        }
    }

    /// Origem.
    #[inline]
    pub const fn origin(&self) -> PointF {
        PointF::new(self.x, self.y)
    }

    /// Tamanho.
    #[inline]
    pub const fn size(&self) -> SizeF {
        SizeF::new(self.width, self.height)
    }

    /// Borda direita.
    #[inline]
    pub fn right(&self) -> f32 {
        self.x + self.width
    }

    /// Borda inferior.
    #[inline]
    pub fn bottom(&self) -> f32 {
        self.y + self.height
    }

    /// Centro.
    #[inline]
    pub fn center(&self) -> PointF {
        PointF {
            x: self.x + self.width * 0.5,
            y: self.y + self.height * 0.5,
        }
    }

    /// Verifica se é vazio.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.width <= 0.0 || self.height <= 0.0
    }

    /// Contém ponto.
    #[inline]
    pub fn contains_point(&self, p: PointF) -> bool {
        p.x >= self.x && p.x < self.right() && p.y >= self.y && p.y < self.bottom()
    }

    /// Offset.
    #[inline]
    pub fn offset(&self, dx: f32, dy: f32) -> Self {
        Self {
            x: self.x + dx,
            y: self.y + dy,
            width: self.width,
            height: self.height,
        }
    }

    /// Interpolação linear.
    #[inline]
    pub fn lerp(&self, other: &RectF, t: f32) -> Self {
        Self {
            x: self.x + (other.x - self.x) * t,
            y: self.y + (other.y - self.y) * t,
            width: self.width + (other.width - self.width) * t,
            height: self.height + (other.height - self.height) * t,
        }
    }

    /// Arredonda para Rect inteiro.
    #[inline]
    pub fn round(&self) -> Rect {
        Rect {
            x: rdsmath::roundf(self.x) as i32,
            y: rdsmath::roundf(self.y) as i32,
            width: rdsmath::roundf(self.width) as u32,
            height: rdsmath::roundf(self.height) as u32,
        }
    }
}

impl From<Rect> for RectF {
    #[inline]
    fn from(r: Rect) -> Self {
        r.to_float()
    }
}

// =============================================================================
// ROUNDED RECT
// =============================================================================

/// Retângulo com cantos arredondados.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct RoundedRect {
    /// Retângulo base.
    pub rect: RectF,
    /// Raio dos cantos (igual para todos).
    pub radius: f32,
}

impl RoundedRect {
    /// Cria novo retângulo arredondado.
    #[inline]
    pub const fn new(rect: RectF, radius: f32) -> Self {
        Self { rect, radius }
    }

    /// Cria a partir de coordenadas.
    #[inline]
    pub const fn from_coords(x: f32, y: f32, width: f32, height: f32, radius: f32) -> Self {
        Self {
            rect: RectF::new(x, y, width, height),
            radius,
        }
    }

    /// Raio máximo permitido (metade do menor lado).
    #[inline]
    pub fn max_radius(&self) -> f32 {
        let min_side = if self.rect.width < self.rect.height {
            self.rect.width
        } else {
            self.rect.height
        };
        min_side * 0.5
    }

    /// Clamp do raio para o máximo permitido.
    #[inline]
    pub fn clamped_radius(&self) -> f32 {
        let max = self.max_radius();
        if self.radius > max {
            max
        } else {
            self.radius
        }
    }

    /// Retorna o retângulo interno (sem os cantos).
    #[inline]
    pub fn inner_rect(&self) -> RectF {
        let r = self.clamped_radius();
        RectF {
            x: self.rect.x + r,
            y: self.rect.y + r,
            width: self.rect.width - r * 2.0,
            height: self.rect.height - r * 2.0,
        }
    }
}