TachyonUI
0.1.0
High-performance UI for building tools.
Toggle main menu visibility
Loading...
Searching...
No Matches
gui_pixel_snap.h
Go to the documentation of this file.
1
#pragma once
2
3
// Pixel snapping of nested boxes. At fractional scales a 1-unit border or inset
4
// covers 1.5 pixels; rounding every edge to the absolute pixel grid makes such
5
// features 1 or 2 pixels depending on where they land. Snapping each edge
6
// relative to the parent's snapped edge keeps them the same everywhere, and
7
// content placed against a parent's border meets it exactly.
8
//
9
// Unsnapped rects are { x0, y0, x1, y1 } in window pixels.
10
11
#include <
gui_core/gui_base_types.h
>
12
13
#include <algorithm>
14
#include <cmath>
15
16
namespace
tachyon
17
{
20
22
inline
int32_t
SnapPx
(
float
v) {
return
int32_t(std::floor(v + 0.5f)); }
23
25
inline
IntRect
SnapAbsolute
(
float4
px)
26
{
27
const
int32_t x0 =
SnapPx
(px.
x
), y0 =
SnapPx
(px.
y
);
28
return
IntRect
{ x0, y0,
SnapPx
(px.
z
) - x0,
SnapPx
(px.
w
) - y0 };
29
}
30
32
inline
float4
PixelBounds
(
const
float3x2
& toPixels,
IntRect
r)
33
{
34
const
float2
a = toPixels.
TransformPoint
(
float2
{ float(r.x), float(r.y) });
35
const
float2
b = toPixels.
TransformPoint
(
float2
{ float(r.x + r.w), float(r.y + r.h) });
36
return
float4
{ std::min(a.
x
, b.
x
), std::min(a.
y
, b.
y
), std::max(a.
x
, b.
x
), std::max(a.
y
, b.
y
) };
37
}
38
41
inline
IntRect
SnapToParent
(
float4
parentPx,
IntRect
parentSnapped,
float4
childPx)
42
{
43
const
int32_t x0 = parentSnapped.x +
SnapPx
(childPx.
x
- parentPx.
x
);
44
const
int32_t y0 = parentSnapped.y +
SnapPx
(childPx.
y
- parentPx.
y
);
45
const
int32_t x1 = parentSnapped.x + parentSnapped.w -
SnapPx
(parentPx.
z
- childPx.
z
);
46
const
int32_t y1 = parentSnapped.y + parentSnapped.h -
SnapPx
(parentPx.
w
- childPx.
w
);
47
return
IntRect
{ x0, y0, x1 - x0, y1 - y0 };
48
}
49
50
}
tachyon::PixelBounds
float4 PixelBounds(const float3x2 &toPixels, IntRect r)
The bounds of a rect mapped to window pixels, { x0, y0, x1, y1 }.
Definition
gui_pixel_snap.h:32
tachyon::SnapPx
int32_t SnapPx(float v)
Round half up, the same rule the box shader uses.
Definition
gui_pixel_snap.h:22
tachyon::SnapToParent
IntRect SnapToParent(float4 parentPx, IntRect parentSnapped, float4 childPx)
A nested box: each edge keeps its rounded distance from the parent's snapped edge on the same side.
Definition
gui_pixel_snap.h:41
tachyon::SnapAbsolute
IntRect SnapAbsolute(float4 px)
A box with no snapped parent: each edge to the nearest pixel.
Definition
gui_pixel_snap.h:25
gui_base_types.h
tachyon
Definition
gui_logging.h:24
tachyon::IntRect
Definition
gui_base_types.h:407
tachyon::float2
Definition
gui_base_types.h:121
tachyon::float2::y
float y
Definition
gui_base_types.h:122
tachyon::float2::x
float x
Definition
gui_base_types.h:122
tachyon::float3x2
2D affine transform matrix (2 rows x 3 columns): x' = r0.x*x + r0.y*y + r0.z y' = r1....
Definition
gui_base_types.h:219
tachyon::float3x2::TransformPoint
float2 TransformPoint(float2 p) const
tachyon::float4
Definition
gui_base_types.h:174
tachyon::float4::y
float y
Definition
gui_base_types.h:175
tachyon::float4::x
float x
Definition
gui_base_types.h:175
tachyon::float4::z
float z
Definition
gui_base_types.h:175
tachyon::float4::w
float w
Definition
gui_base_types.h:175
gui_core
graphics
gui_pixel_snap.h
Generated by
1.18.0