TinyMaterialDesign
Loading...
Searching...
No Matches
Bounds.h
Go to the documentation of this file.
1#pragma once
2#include <stddef.h>
3#include <stdint.h>
4
5namespace tinymd {
6
7/// Clamps a signed widget-layout coordinate to a non-negative pixel index
8/// before it's passed into one of TinyGPU's size_t-based drawing calls.
9/// Needed because Bounds (this file) is deliberately signed while ISurface
10/// drawing calls take size_t - passing a negative value straight through
11/// would silently wrap to a huge unsigned value instead of clipping.
12inline size_t toPx(int32_t v) { return v < 0 ? 0 : static_cast<size_t>(v); }
13
14/**
15 * @brief Axis-aligned pixel rectangle used for widget bounds and hit-testing.
16 *
17 * Deliberately signed (unlike TinyGPU's size_t-based drawing calls) so
18 * widget layout math (x - radius, centering, ...) can't silently underflow;
19 * clamp to >=0 only at the point of calling into ISurface.
20 */
21struct Bounds {
22 int32_t x = 0;
23 int32_t y = 0;
24 int32_t w = 0;
25 int32_t h = 0;
26
27 Bounds() = default;
28 Bounds(int32_t x, int32_t y, int32_t w, int32_t h) : x(x), y(y), w(w), h(h) {}
29
30 int32_t right() const { return x + w; }
31 int32_t bottom() const { return y + h; }
32 int32_t centerX() const { return x + w / 2; }
33 int32_t centerY() const { return y + h / 2; }
34
35 bool contains(int32_t px, int32_t py) const {
36 return px >= x && py >= y && px < right() && py < bottom();
37 }
38
39 /// Returns a rect inset by `amount` on every side (negative to grow).
40 Bounds inset(int32_t amount) const {
41 return Bounds(x + amount, y + amount, w - 2 * amount, h - 2 * amount);
42 }
43};
44
45} // namespace tinymd
Definition: AnchorLayout.h:4
size_t toPx(int32_t v)
Definition: Bounds.h:12
Axis-aligned pixel rectangle used for widget bounds and hit-testing.
Definition: Bounds.h:21
int32_t right() const
Definition: Bounds.h:30
int32_t bottom() const
Definition: Bounds.h:31
int32_t w
Definition: Bounds.h:24
Bounds(int32_t x, int32_t y, int32_t w, int32_t h)
Definition: Bounds.h:28
int32_t centerY() const
Definition: Bounds.h:33
int32_t centerX() const
Definition: Bounds.h:32
int32_t h
Definition: Bounds.h:25
bool contains(int32_t px, int32_t py) const
Definition: Bounds.h:35
Bounds inset(int32_t amount) const
Returns a rect inset by amount on every side (negative to grow).
Definition: Bounds.h:40
int32_t y
Definition: Bounds.h:23
int32_t x
Definition: Bounds.h:22
Bounds()=default