grafo/shape.rs
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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
//! The `shape` module provides structures and methods for creating and managing graphical shapes
//! within the Grafo library. It supports both simple and complex shapes, including rectangles and
//! custom paths with fill and stroke properties.
//!
//! # Examples
//!
//! Creating and using different shapes:
//!
//! ```rust
//! use grafo::Color;
//! use grafo::Stroke;
//! use grafo::{Shape, ShapeBuilder, BorderRadii};
//!
//! // Create a simple rectangle
//! let rect = Shape::rect(
//! [(0.0, 0.0), (100.0, 50.0)],
//! Color::rgb(255, 0, 0), // Red fill
//! Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
//! );
//!
//! // Create a rounded rectangle
//! let rounded_rect = Shape::rounded_rect(
//! [(0.0, 0.0), (100.0, 50.0)],
//! BorderRadii::new(10.0),
//! Color::rgba(0, 255, 0, 128), // Semi-transparent green fill
//! Stroke::new(1.5, Color::BLACK), // Black stroke with width 1.5
//! );
//!
//! // Build a custom shape using ShapeBuilder
//! let custom_shape = Shape::builder()
//! .fill(Color::rgb(0, 0, 255)) // Blue fill
//! .stroke(Stroke::new(3.0, Color::BLACK)) // Black stroke with width 3.0
//! .begin((0.0, 0.0))
//! .line_to((50.0, 10.0))
//! .line_to((50.0, 50.0))
//! .close()
//! .build();
//! ```
use crate::renderer::depth;
use crate::vertex::CustomVertex;
use crate::{Color, Stroke};
use lyon::lyon_tessellation::{
BuffersBuilder, FillOptions, FillTessellator, FillVertex, VertexBuffers,
};
use lyon::path::Winding;
use lyon::tessellation::FillVertexConstructor;
use wgpu::util::DeviceExt;
/// Represents a graphical shape, which can be either a custom path or a simple rectangle.
///
/// # Variants
///
/// - `Path(PathShape)`: A custom path shape defined using Bézier curves and lines.
/// - `Rect(RectShape)`: A simple rectangular shape with optional rounded corners.
///
/// # Examples
///
/// ```rust
/// use grafo::Color;
/// use grafo::Stroke;
/// use grafo::{Shape, BorderRadii};
///
/// // Create a simple rectangle
/// let rect = Shape::rect(
/// [(0.0, 0.0), (100.0, 50.0)],
/// Color::rgb(255, 0, 0), // Red fill
/// Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
/// );
///
/// // Create a custom path shape
/// let custom_path = Shape::builder()
/// .fill(Color::rgb(0, 255, 0))
/// .stroke(Stroke::new(1.0, Color::BLACK))
/// .begin((0.0, 0.0))
/// .line_to((50.0, 10.0))
/// .line_to((50.0, 50.0))
/// .close()
/// .build();
/// ```
#[derive(Debug, Clone)]
pub enum Shape {
/// A custom path shape defined using Bézier curves and lines.
Path(PathShape),
/// A simple rectangular shape.
Rect(RectShape),
}
impl Shape {
/// Creates a new [`ShapeBuilder`] for constructing complex shapes.
///
/// # Examples
///
/// ```rust
/// use grafo::Shape;
///
/// let builder = Shape::builder();
/// ```
pub fn builder() -> ShapeBuilder {
ShapeBuilder::new()
}
/// Creates a simple rectangle shape with the specified coordinates, fill color, and stroke.
///
/// # Parameters
///
/// - `rect`: An array containing two tuples representing the top-left and bottom-right
/// coordinates of the rectangle.
/// - `fill_color`: The fill color of the rectangle.
/// - `stroke`: The stroke properties of the rectangle.
///
/// # Examples
///
/// ```rust
/// use grafo::Color;
/// use grafo::Stroke;
/// use grafo::Shape;
///
/// let rect = Shape::rect(
/// [(0.0, 0.0), (100.0, 50.0)],
/// Color::rgb(255, 0, 0), // Red fill
/// Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
/// );
/// ```
pub fn rect(rect: [(f32, f32); 2], fill_color: Color, stroke: Stroke) -> Shape {
let rect_shape = RectShape::new(rect, fill_color, stroke);
Shape::Rect(rect_shape)
}
/// Creates a rectangle shape with rounded corners.
///
/// # Parameters
///
/// - `rect`: An array containing two tuples representing the top-left and bottom-right
/// coordinates of the rectangle.
/// - `border_radii`: The radii for each corner of the rectangle.
/// - `fill_color`: The fill color of the rectangle.
/// - `stroke`: The stroke properties of the rectangle.
///
/// # Examples
///
/// ```rust
/// use grafo::Color;
/// use grafo::Stroke;
/// use grafo::{Shape, BorderRadii};
///
/// let rounded_rect = Shape::rounded_rect(
/// [(0.0, 0.0), (100.0, 50.0)],
/// BorderRadii::new(10.0),
/// Color::rgba(0, 255, 0, 128), // Semi-transparent green fill
/// Stroke::new(1.5, Color::BLACK), // Black stroke with width 1.5
/// );
/// ```
pub fn rounded_rect(
rect: [(f32, f32); 2],
border_radii: BorderRadii,
fill_color: Color,
stroke: Stroke,
) -> Shape {
let mut path_builder = lyon::path::Path::builder();
let box2d = lyon::math::Box2D::new(rect[0].into(), rect[1].into());
path_builder.add_rounded_rectangle(&box2d, &border_radii.into(), Winding::Positive);
let path = path_builder.build();
let path_shape = PathShape {
path,
fill: fill_color,
stroke,
};
Shape::Path(path_shape)
}
}
impl From<PathShape> for Shape {
fn from(value: PathShape) -> Self {
Shape::Path(value)
}
}
impl From<RectShape> for Shape {
fn from(value: RectShape) -> Self {
Shape::Rect(value)
}
}
/// Represents a simple rectangular shape with a fill color and stroke.
///
/// You typically do not need to use `RectShape` directly; instead, use the [`Shape::rect`] method.
///
/// # Fields
///
/// - `rect`: An array containing two tuples representing the top-left and bottom-right
/// coordinates of the rectangle.
/// - `fill`: The fill color of the rectangle.
/// - `stroke`: The stroke properties of the rectangle.
///
/// # Examples
///
/// ```rust
/// use grafo::RectShape;
/// use grafo::Color;
/// use grafo::Stroke;
///
/// let rect_shape = RectShape::new(
/// [(0.0, 0.0), (100.0, 50.0)],
/// Color::rgb(255, 0, 0), // Red fill
/// Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
/// );
/// ```
#[derive(Debug, Clone)]
pub struct RectShape {
/// An array containing two tuples representing the top-left and bottom-right coordinates
/// of the rectangle.
pub(crate) rect: [(f32, f32); 2],
/// The fill color of the rectangle.
pub(crate) fill: Color,
/// The stroke properties of the rectangle.
#[allow(unused)]
pub(crate) stroke: Stroke,
}
impl RectShape {
/// Creates a new `RectShape` with the specified coordinates, fill color, and stroke.
///
/// # Parameters
///
/// - `rect`: An array containing two tuples representing the top-left and bottom-right
/// coordinates of the rectangle.
/// - `fill`: The fill color of the rectangle.
/// - `stroke`: The stroke properties of the rectangle.
///
/// # Examples
///
/// ```rust
/// use grafo::RectShape;
/// use grafo::Color;
/// use grafo::Stroke;
///
/// let rect_shape = RectShape::new(
/// [(0.0, 0.0), (100.0, 50.0)],
/// Color::rgb(255, 0, 0), // Red fill
/// Stroke::new(2.0, Color::BLACK), // Black stroke with width 2.0
/// );
/// ```
pub fn new(rect: [(f32, f32); 2], fill: Color, stroke: Stroke) -> Self {
Self { rect, fill, stroke }
}
}
/// Represents a custom path shape with a fill color and stroke.
///
/// You typically do not need to use `PathShape` directly; instead, use the [`Shape::builder`]
/// method to construct complex shapes.
///
/// # Fields
///
/// - `path`: The geometric path defining the shape.
/// - `fill`: The fill color of the shape.
/// - `stroke`: The stroke properties of the shape.
///
/// # Examples
///
/// ```rust
/// use grafo::{Shape, PathShape};
/// use grafo::Color;
/// use grafo::Stroke;
///
/// // Replace this with your own path
/// let path = lyon::path::Path::builder().build();
///
/// let path_shape = PathShape::new(
/// path,
/// Color::rgb(0, 255, 0), // Green fill
/// Stroke::new(1.0, Color::BLACK), // Black stroke with width 1.0
/// );
///
/// let shape = Shape::Path(path_shape);
/// ```
#[derive(Clone, Debug)]
pub struct PathShape {
/// The geometric path defining the shape.
pub(crate) path: lyon::path::Path,
/// The fill color of the shape.
pub(crate) fill: Color,
/// The stroke properties of the shape.
#[allow(unused)]
pub(crate) stroke: Stroke,
}
struct VertexConverter {
depth: f32,
color: [f32; 4],
}
impl VertexConverter {
fn new(depth: f32, color: [f32; 4]) -> Self {
Self { depth, color }
}
}
impl FillVertexConstructor<CustomVertex> for VertexConverter {
fn new_vertex(&mut self, vertex: FillVertex) -> CustomVertex {
CustomVertex {
position: vertex.position().to_array(),
depth: self.depth,
color: self.color,
}
}
}
impl PathShape {
/// Creates a new `PathShape` with the specified path, fill color, and stroke.
///
/// # Parameters
///
/// - `path`: The geometric path defining the shape.
/// - `fill`: The fill color of the shape.
/// - `stroke`: The stroke properties of the shape.
///
/// # Examples
///
/// ```rust
/// use grafo::PathShape;
/// use grafo::Color;
/// use grafo::Stroke;
/// use lyon::path::Path;
///
/// let path = Path::builder().build();
/// let path_shape = PathShape::new(path, Color::rgb(0, 255, 0), Stroke::default());
/// ```
pub fn new(path: lyon::path::Path, fill: Color, stroke: Stroke) -> Self {
Self { path, fill, stroke }
}
/// Tessellates the path shape into vertex and index buffers for rendering.
///
/// # Parameters
///
/// - `depth`: The depth value used for rendering order.
///
/// # Returns
///
/// A `VertexBuffers` structure containing the tessellated vertices and indices.
/// ```
pub(crate) fn tessellate(&self, depth: f32) -> VertexBuffers<CustomVertex, u16> {
let mut buffers: VertexBuffers<CustomVertex, u16> = VertexBuffers::new();
let mut tessellator = FillTessellator::new();
let options = FillOptions::default().with_tolerance(0.01);
let color = self.fill.normalize();
let vertex_converter = VertexConverter::new(depth, color);
tessellator
.tessellate_path(
&self.path,
&options,
&mut BuffersBuilder::new(&mut buffers, vertex_converter),
)
.unwrap();
buffers
}
}
/// Contains the data required to draw a shape, including vertex and index buffers.
///
/// This struct is used internally by the renderer and typically does not need to be used
/// directly by library users.
#[derive(Debug)]
pub(crate) struct ShapeDrawData {
/// The vertex buffer containing the shape's vertices.
pub(crate) vertex_buffer: Option<wgpu::Buffer>,
/// The index buffer containing the shape's indices.
pub(crate) index_buffer: Option<wgpu::Buffer>,
/// The number of indices in the index buffer.
pub(crate) num_indices: Option<u32>,
/// An optional index of another shape to clip to.
pub(crate) clip_to_shape: Option<usize>,
/// The shape associated with this draw data.
pub(crate) shape: Shape,
}
impl ShapeDrawData {
pub fn new(shape: impl Into<Shape>, clip_to_shape: Option<usize>) -> Self {
let shape = shape.into();
ShapeDrawData {
vertex_buffer: None,
index_buffer: None,
num_indices: None,
clip_to_shape,
shape,
}
}
fn shape_data_to_buffers(
&self,
device: &wgpu::Device,
depth: f32,
) -> (wgpu::Buffer, wgpu::Buffer, u32) {
match &self.shape {
Shape::Path(path_shape) => {
let vertex_buffers = path_shape.tessellate(depth);
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None,
contents: bytemuck::cast_slice(&vertex_buffers.vertices),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None,
contents: bytemuck::cast_slice(&vertex_buffers.indices),
usage: wgpu::BufferUsages::INDEX,
});
(
vertex_buffer,
index_buffer,
vertex_buffers.indices.len() as u32,
)
}
Shape::Rect(rect_shape) => {
let min_width = rect_shape.rect[0].0;
let min_height = rect_shape.rect[0].1;
let max_width = rect_shape.rect[1].0;
let max_height = rect_shape.rect[1].1;
let color = rect_shape.fill.normalize();
let quad = [
CustomVertex {
position: [min_width, min_height],
color,
depth,
},
CustomVertex {
position: [max_width, min_height],
color,
depth,
},
CustomVertex {
position: [min_width, max_height],
color,
depth,
},
CustomVertex {
position: [min_width, max_height],
color,
depth,
},
CustomVertex {
position: [max_width, min_height],
color,
depth,
},
CustomVertex {
position: [max_width, max_height],
color,
depth,
},
];
let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None,
contents: bytemuck::cast_slice(&quad),
usage: wgpu::BufferUsages::VERTEX,
});
let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {
label: None,
contents: bytemuck::cast_slice(&[0u16, 1, 2, 3, 4, 5]),
usage: wgpu::BufferUsages::INDEX,
});
(vertex_buffer, index_buffer, 6)
}
}
}
/// Prepares the GPU buffers for rendering the shape.
///
/// # Parameters
///
/// - `device`: The WGPU device used to create the buffers.
/// - `shape_id`: The identifier for the shape, used to calculate depth.
/// - `max_shape_id`: The maximum number of shapes, used to normalize depth.
///
/// # Examples
///
/// ```rust
/// // Assuming `device` is a valid wgpu::Device instance and shape_id/max_shape_id are set
/// // shape_draw_data.prepare_buffers(&device, shape_id, max_shape_id);
/// ```
pub fn prepare_buffers(&mut self, device: &wgpu::Device, shape_id: usize, max_shape_id: usize) {
let depth = depth(shape_id, max_shape_id);
let (vertex_buffer, index_buffer, num_indices) = self.shape_data_to_buffers(device, depth);
self.vertex_buffer = Some(vertex_buffer);
self.index_buffer = Some(index_buffer);
self.num_indices = Some(num_indices);
}
}
/// A builder for creating complex shapes using a fluent interface.
///
/// The `ShapeBuilder` allows you to define the fill color, stroke, and path of a shape
/// using method chaining. You also can get it from the [`Shape::builder`] method.
///
/// # Examples
///
/// ```rust
/// use grafo::Color;
/// use grafo::Stroke;
/// use grafo::ShapeBuilder;
///
/// let custom_shape = ShapeBuilder::new()
/// .fill(Color::rgb(0, 0, 255)) // Blue fill
/// .stroke(Stroke::new(3.0, Color::BLACK)) // Black stroke with width 3.0
/// .begin((0.0, 0.0))
/// .line_to((50.0, 10.0))
/// .line_to((50.0, 50.0))
/// .close()
/// .build();
/// ```
#[derive(Clone)]
pub struct ShapeBuilder {
/// The fill color of the shape.
color: Color,
/// The stroke properties of the shape.
stroke: Stroke,
/// The path builder used to construct the shape's geometric path.
path_builder: lyon::path::Builder,
}
impl Default for ShapeBuilder {
/// Creates a default `ShapeBuilder` with black fill and stroke.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::default();
/// ```
fn default() -> Self {
Self::new()
}
}
impl ShapeBuilder {
/// Creates a new `ShapeBuilder` with default fill color (black) and stroke.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new();
/// ```
pub fn new() -> Self {
Self {
color: Color::rgb(0, 0, 0),
stroke: Stroke::new(1.0, Color::rgb(0, 0, 0)),
path_builder: lyon::path::Path::builder(),
}
}
/// Sets the fill color of the shape.
///
/// # Parameters
///
/// - `color`: The desired fill color.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::Color;
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new().fill(Color::rgb(255, 0, 0)); // Red fill
/// ```
pub fn fill(mut self, color: Color) -> Self {
self.color = color;
self
}
/// Sets the stroke properties of the shape.
///
/// # Parameters
///
/// - `stroke`: The desired stroke properties.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::Stroke;
/// use grafo::Color;
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new().stroke(Stroke::new(2.0, Color::BLACK)); // Black stroke with width 2.0
/// ```
pub fn stroke(mut self, stroke: Stroke) -> Self {
self.stroke = stroke;
self
}
/// Begin path at point
///
/// # Parameters
///
/// - `point`: The start point of the shape.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new().begin((0.0, 0.0));
/// ```
pub fn begin(mut self, point: (f32, f32)) -> Self {
self.path_builder.begin(point.into());
self
}
/// Draws a line from the current point to the specified point.
///
/// # Parameters
///
/// - `point`: The end point of the line.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new().begin((0.0, 0.0)).line_to((50.0, 10.0));
/// ```
pub fn line_to(mut self, point: (f32, f32)) -> Self {
self.path_builder.line_to(point.into());
self
}
/// Draws a cubic Bézier curve from the current point to the specified end point.
///
/// # Parameters
///
/// - `ctrl`: The first control point.
/// - `ctrl2`: The second control point.
/// - `to`: The end point of the curve.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new()
/// .begin((0.0, 0.0))
/// .cubic_bezier_to((20.0, 30.0), (40.0, 30.0), (50.0, 10.0));
/// ```
pub fn cubic_bezier_to(mut self, ctrl: (f32, f32), ctrl2: (f32, f32), to: (f32, f32)) -> Self {
self.path_builder
.cubic_bezier_to(ctrl.into(), ctrl2.into(), to.into());
self
}
/// Draws a quadratic Bézier curve from the current point to the specified end point.
///
/// # Parameters
///
/// - `ctrl`: The control point.
/// - `to`: The end point of the curve.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new()
/// .begin((0.0, 0.0))
/// .quadratic_bezier_to((25.0, 40.0), (50.0, 10.0));
/// ```
pub fn quadratic_bezier_to(mut self, ctrl: (f32, f32), to: (f32, f32)) -> Self {
self.path_builder
.quadratic_bezier_to(ctrl.into(), to.into());
self
}
/// Closes the current sub-path by drawing a line back to the starting point.
///
/// # Returns
///
/// The updated `ShapeBuilder` instance.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let builder = ShapeBuilder::new().begin((0.0, 0.0)).close();
/// ```
pub fn close(mut self) -> Self {
self.path_builder.close();
self
}
/// Builds the [`Shape`] from the accumulated path, fill color, and stroke.
///
/// # Returns
///
/// A `Shape` instance representing the constructed shape.
///
/// # Examples
///
/// ```rust
/// use grafo::ShapeBuilder;
///
/// let shape = ShapeBuilder::new()
/// .begin((0.0, 0.0))
/// .line_to((50.0, 10.0))
/// .line_to((50.0, 50.0))
/// .close()
/// .build();
/// ```
pub fn build(self) -> Shape {
let path = self.path_builder.build();
Shape::Path(PathShape {
path,
fill: self.color,
stroke: self.stroke,
})
}
}
impl From<ShapeBuilder> for Shape {
fn from(value: ShapeBuilder) -> Self {
value.build()
}
}
/// A set of border radii for a rounded rectangle
#[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Default)]
pub struct BorderRadii {
pub top_left: f32,
pub top_right: f32,
pub bottom_left: f32,
pub bottom_right: f32,
}
/// Represents the radii of each corner for a rounded rectangle.
///
/// # Fields
///
/// - `top_left`: Radius of the top-left corner.
/// - `top_right`: Radius of the top-right corner.
/// - `bottom_left`: Radius of the bottom-left corner.
/// - `bottom_right`: Radius of the bottom-right corner.
///
/// # Examples
///
/// Creating uniform and non-uniform border radii:
///
/// ```rust
/// use grafo::BorderRadii;
///
/// // Uniform border radii
/// let uniform_radii = BorderRadii::new(10.0);
///
/// // Custom border radii
/// let custom_radii = BorderRadii {
/// top_left: 5.0,
/// top_right: 10.0,
/// bottom_left: 15.0,
/// bottom_right: 20.0,
/// };
/// ```
impl BorderRadii {
/// Creates a new `BorderRadii` with the same radius for all corners.
///
/// # Parameters
///
/// - `radius`: The radius to apply to all corners.
///
/// # Returns
///
/// A `BorderRadii` instance with uniform corner radii.
///
/// # Examples
///
/// ```rust
/// use grafo::BorderRadii;
///
/// let radii = BorderRadii::new(10.0);
/// ```
pub fn new(radius: f32) -> Self {
let r = radius.abs();
BorderRadii {
top_left: r,
top_right: r,
bottom_left: r,
bottom_right: r,
}
}
}
impl core::fmt::Display for BorderRadii {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// In the order of a well known convention (CSS) clockwise from top left
write!(
f,
"BorderRadii({}, {}, {}, {})",
self.top_left, self.top_right, self.bottom_left, self.bottom_right
)
}
}
impl From<BorderRadii> for lyon::path::builder::BorderRadii {
fn from(val: BorderRadii) -> Self {
lyon::path::builder::BorderRadii {
top_left: val.top_left,
top_right: val.top_right,
bottom_left: val.bottom_left,
bottom_right: val.bottom_right,
}
}
}