1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

Add epaint::RoundedRect primitive (#8440)

This commit is contained in:
Emil Ernerfeldt
2026-08-21 01:33:54 -07:00
committed by GitHub
parent 00dc6e814b
commit f5c9373e26
4 changed files with 173 additions and 21 deletions

View File

@@ -11,8 +11,8 @@ use emath::{
use crate::{
CircleShape, ClippedPrimitive, ClippedShape, Color32, CornerRadiusF32, CubicBezierShape,
EllipseShape, Mesh, PathShape, Primitive, QuadraticBezierShape, RectShape, Shape, Stroke,
StrokeKind, TextShape, TextureId, Vertex, color::ColorMode, emath, stroke::PathStroke,
EllipseShape, Mesh, PathShape, Primitive, QuadraticBezierShape, RectShape, RoundedRect, Shape,
Stroke, StrokeKind, TextShape, TextureId, Vertex, color::ColorMode, emath, stroke::PathStroke,
texture_atlas::PreparedDisc,
};
@@ -536,18 +536,19 @@ impl Path {
pub mod path {
//! Helpers for constructing paths
use crate::CornerRadiusF32;
use emath::{Pos2, Rect, pos2};
use crate::{CornerRadiusF32, RoundedRect};
use emath::{Pos2, pos2};
/// overwrites existing points
pub fn rounded_rectangle(path: &mut Vec<Pos2>, rect: Rect, cr: CornerRadiusF32) {
pub fn rounded_rectangle(path: &mut Vec<Pos2>, rounded_rect: RoundedRect) {
path.clear();
// The corner radius is already clamped to half the rect size by `RoundedRect`:
let (rect, cr) = rounded_rect.into_parts();
let min = rect.min;
let max = rect.max;
let cr = clamp_corner_radius(cr, rect);
if cr == CornerRadiusF32::ZERO {
path.reserve(4);
path.push(pos2(min.x, min.y)); // left top
@@ -633,14 +634,6 @@ pub mod path {
path.extend(quadrant_vertices.iter().map(|&n| center + radius * n));
}
}
// Ensures the radius of each corner is within a valid range
fn clamp_corner_radius(cr: CornerRadiusF32, rect: Rect) -> CornerRadiusF32 {
let half_width = rect.width() * 0.5;
let half_height = rect.height() * 0.5;
let max_cr = half_width.min(half_height);
cr.at_most(max_cr).at_least(0.0)
}
}
// ----------------------------------------------------------------------------
@@ -1938,7 +1931,10 @@ impl Tessellator {
let path = &mut self.scratchpad_path;
path.clear();
path::rounded_rectangle(&mut self.scratchpad_points, rect, corner_radius);
path::rounded_rectangle(
&mut self.scratchpad_points,
RoundedRect::new(rect, corner_radius),
);
// Apply rotation if angle is non-zero
if angle != 0.0 {