Skip to content

Commit

Permalink
Merge pull request #175 from Nazariglez/f/shapes
Browse files Browse the repository at this point in the history
Allow to fill and stroke shapes using the same DrawBuilder
  • Loading branch information
Nazariglez authored Nov 1, 2022
2 parents c6f92bc + 4f9165c commit a48440c
Show file tree
Hide file tree
Showing 10 changed files with 667 additions and 81 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ All notable changes to this project will be documented in this file.
- Added mipmapping support with `TextureBuilder::generate_mipmaps`.
- Added `WindowBackend::position` and `WindowBackend::set_position`.
- Fix lint warning `notan_main` macro.
- Added methods `.fill_color` and `stroke_color` for the Draw2d shapes to allow to stroke and fill with the same builder.
- Added method `Draw::star(spikes, outser_radius, inner_radius)` to draw stars.
- Added method `Draw::polygon(sides, radius)` to draw regular polygons.

## v0.7.1 - 08/10/2022

Expand Down
14 changes: 14 additions & 0 deletions crates/notan_draw/src/shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ mod geometry;
mod line;
mod painter;
mod path;
mod polygon;
mod rect;
mod star;
mod tess;
mod triangle;

Expand All @@ -16,7 +18,9 @@ pub use line::Line;
pub use painter::create_shape_pipeline;
pub(crate) use painter::*;
pub use path::Path;
pub use polygon::Polygon;
pub use rect::Rectangle;
pub use star::Star;
pub use triangle::Triangle;

pub trait DrawShapes {
Expand All @@ -26,6 +30,8 @@ pub trait DrawShapes {
fn rect(&mut self, position: (f32, f32), size: (f32, f32)) -> DrawBuilder<Rectangle>;
fn circle(&mut self, radius: f32) -> DrawBuilder<Circle>;
fn ellipse(&mut self, position: (f32, f32), size: (f32, f32)) -> DrawBuilder<Ellipse>;
fn star(&mut self, spikes: u8, outer_radius: f32, inner_radius: f32) -> DrawBuilder<Star>;
fn polygon(&mut self, sides: u8, radius: f32) -> DrawBuilder<Polygon>;
}

impl DrawShapes for Draw {
Expand All @@ -52,4 +58,12 @@ impl DrawShapes for Draw {
fn ellipse(&mut self, position: (f32, f32), size: (f32, f32)) -> DrawBuilder<Ellipse> {
DrawBuilder::new(self, Ellipse::new(position, size))
}

fn star(&mut self, spikes: u8, outer_radius: f32, inner_radius: f32) -> DrawBuilder<Star> {
DrawBuilder::new(self, Star::new(spikes, outer_radius, inner_radius))
}

fn polygon(&mut self, sides: u8, radius: f32) -> DrawBuilder<Polygon> {
DrawBuilder::new(self, Polygon::new(sides, radius))
}
}
57 changes: 45 additions & 12 deletions crates/notan_draw/src/shapes/circle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ pub struct Circle {
color: Color,
pos: (f32, f32),
radius: f32,
mode: TessMode,
stroke_width: f32,
alpha: f32,
matrix: Option<Mat3>,
tolerance: f32,
blend_mode: Option<BlendMode>,
modes: [Option<TessMode>; 2],
mode_index: usize,
fill_color: Option<Color>,
stroke_color: Option<Color>,
}

impl Circle {
Expand All @@ -27,12 +30,15 @@ impl Circle {
color: Color::WHITE,
pos: (0.0, 0.0),
radius,
mode: TessMode::Fill,
stroke_width: 1.0,
alpha: 1.0,
matrix: None,
tolerance: StrokeOptions::DEFAULT_TOLERANCE,
blend_mode: None,
modes: [None; 2],
mode_index: 0,
fill_color: None,
stroke_color: None,
}
}

Expand All @@ -46,6 +52,16 @@ impl Circle {
self
}

pub fn fill_color(&mut self, color: Color) -> &mut Self {
self.fill_color = Some(color);
self
}

pub fn stroke_color(&mut self, color: Color) -> &mut Self {
self.stroke_color = Some(color);
self
}

pub fn color(&mut self, color: Color) -> &mut Self {
self.color = color;
self
Expand All @@ -57,13 +73,15 @@ impl Circle {
}

pub fn fill(&mut self) -> &mut Self {
self.mode = TessMode::Fill;
self.modes[self.mode_index] = Some(TessMode::Fill);
self.mode_index = (self.mode_index + 1) % 2;
self
}

pub fn stroke(&mut self, width: f32) -> &mut Self {
self.mode = TessMode::Stroke;
self.modes[self.mode_index] = Some(TessMode::Stroke);
self.stroke_width = width;
self.mode_index = (self.mode_index + 1) % 2;
self
}

Expand All @@ -81,14 +99,23 @@ impl DrawTransform for Circle {

impl DrawProcess for Circle {
fn draw_process(self, draw: &mut Draw) {
match self.mode {
TessMode::Fill => fill(self, draw),
TessMode::Stroke => stroke(self, draw),
}
let modes = self.modes;
modes.iter().enumerate().for_each(|(i, mode)| match mode {
None => {
if i == 0 {
// fill by default
fill(&self, draw);
}
}
Some(mode) => match mode {
TessMode::Fill => fill(&self, draw),
TessMode::Stroke => stroke(&self, draw),
},
});
}
}

fn stroke(circle: Circle, draw: &mut Draw) {
fn stroke(circle: &Circle, draw: &mut Draw) {
let Circle {
color,
pos: (x, y),
Expand All @@ -98,12 +125,15 @@ fn stroke(circle: Circle, draw: &mut Draw) {
matrix,
tolerance,
blend_mode,
stroke_color,
..
} = circle;
} = *circle;

let stroke_options = StrokeOptions::default()
.with_line_width(stroke_width)
.with_tolerance(tolerance);

let color = stroke_color.unwrap_or(color);
let color = color.with_alpha(color.a * alpha);

let path = geometry::circle(x, y, radius);
Expand All @@ -117,7 +147,7 @@ fn stroke(circle: Circle, draw: &mut Draw) {
});
}

fn fill(circle: Circle, draw: &mut Draw) {
fn fill(circle: &Circle, draw: &mut Draw) {
let Circle {
color,
pos: (x, y),
Expand All @@ -126,10 +156,13 @@ fn fill(circle: Circle, draw: &mut Draw) {
matrix,
tolerance,
blend_mode,
fill_color,
..
} = circle;
} = *circle;

let fill_options = FillOptions::default().with_tolerance(tolerance);

let color = fill_color.unwrap_or(color);
let color = color.with_alpha(color.a * alpha);

let path = geometry::circle(x, y, radius);
Expand Down
57 changes: 45 additions & 12 deletions crates/notan_draw/src/shapes/ellipse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ pub struct Ellipse {
pos: (f32, f32),
size: (f32, f32),
rotation: f32,
mode: TessMode,
stroke_width: f32,
alpha: f32,
matrix: Option<Mat3>,
tolerance: f32,
blend_mode: Option<BlendMode>,
modes: [Option<TessMode>; 2],
mode_index: usize,
fill_color: Option<Color>,
stroke_color: Option<Color>,
}

impl Ellipse {
Expand All @@ -28,13 +31,16 @@ impl Ellipse {
color: Color::WHITE,
pos,
size,
mode: TessMode::Fill,
stroke_width: 1.0,
alpha: 1.0,
matrix: None,
tolerance: StrokeOptions::DEFAULT_TOLERANCE,
rotation: 0.0,
blend_mode: None,
modes: [None; 2],
mode_index: 0,
fill_color: None,
stroke_color: None,
}
}

Expand All @@ -53,6 +59,16 @@ impl Ellipse {
self
}

pub fn fill_color(&mut self, color: Color) -> &mut Self {
self.fill_color = Some(color);
self
}

pub fn stroke_color(&mut self, color: Color) -> &mut Self {
self.stroke_color = Some(color);
self
}

pub fn color(&mut self, color: Color) -> &mut Self {
self.color = color;
self
Expand All @@ -64,13 +80,15 @@ impl Ellipse {
}

pub fn fill(&mut self) -> &mut Self {
self.mode = TessMode::Fill;
self.modes[self.mode_index] = Some(TessMode::Fill);
self.mode_index = (self.mode_index + 1) % 2;
self
}

pub fn stroke(&mut self, width: f32) -> &mut Self {
self.mode = TessMode::Stroke;
self.modes[self.mode_index] = Some(TessMode::Stroke);
self.stroke_width = width;
self.mode_index = (self.mode_index + 1) % 2;
self
}

Expand All @@ -88,14 +106,23 @@ impl DrawTransform for Ellipse {

impl DrawProcess for Ellipse {
fn draw_process(self, draw: &mut Draw) {
match self.mode {
TessMode::Fill => fill(self, draw),
TessMode::Stroke => stroke(self, draw),
}
let modes = self.modes;
modes.iter().enumerate().for_each(|(i, mode)| match mode {
None => {
if i == 0 {
// fill by default
fill(&self, draw);
}
}
Some(mode) => match mode {
TessMode::Fill => fill(&self, draw),
TessMode::Stroke => stroke(&self, draw),
},
})
}
}

fn stroke(ellipse: Ellipse, draw: &mut Draw) {
fn stroke(ellipse: &Ellipse, draw: &mut Draw) {
let Ellipse {
color,
pos: (x, y),
Expand All @@ -106,12 +133,15 @@ fn stroke(ellipse: Ellipse, draw: &mut Draw) {
matrix,
tolerance,
blend_mode,
stroke_color,
..
} = ellipse;
} = *ellipse;

let stroke_options = StrokeOptions::default()
.with_line_width(stroke_width)
.with_tolerance(tolerance);

let color = stroke_color.unwrap_or(color);
let color = color.with_alpha(color.a * alpha);

let path = geometry::ellipse(x, y, width, height, rotation);
Expand All @@ -125,7 +155,7 @@ fn stroke(ellipse: Ellipse, draw: &mut Draw) {
});
}

fn fill(ellipse: Ellipse, draw: &mut Draw) {
fn fill(ellipse: &Ellipse, draw: &mut Draw) {
let Ellipse {
color,
pos: (x, y),
Expand All @@ -135,10 +165,13 @@ fn fill(ellipse: Ellipse, draw: &mut Draw) {
matrix,
tolerance,
blend_mode,
fill_color,
..
} = ellipse;
} = *ellipse;

let fill_options = FillOptions::default().with_tolerance(tolerance);

let color = fill_color.unwrap_or(color);
let color = color.with_alpha(color.a * alpha);

let path = geometry::ellipse(x, y, width, height, rotation);
Expand Down
Loading

0 comments on commit a48440c

Please sign in to comment.