Skip to content

Commit

Permalink
Page size support + lots of improvements
Browse files Browse the repository at this point in the history
- `run` replaced by `Runner` builder pattern.
- now supports customisation of default param (e.g. page size, locked page size, etc.)
- `PageSize` and `Unit` *much* improved
- added `Draw::scale_unit()`
- updated examples
- etc.
  • Loading branch information
abey79 committed Sep 24, 2023
1 parent a9d1d91 commit 5b4632d
Show file tree
Hide file tree
Showing 21 changed files with 1,032 additions and 422 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions vsvg-sketch/examples/app_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ impl Default for MySketch {

impl App for MySketch {
fn update(&mut self, sketch: &mut Sketch, ctx: &mut Context) -> anyhow::Result<()> {
sketch.page_size(PageSize::new(200.0, 200.0));

for i in 0..self.num_circle {
sketch.circle(
100.0,
Expand All @@ -41,13 +39,7 @@ impl App for MySketch {
}

fn main() -> Result {
run_default::<MySketch>()

// or you can use this instead of implementing [`Default`]:
// run(MySketch {
// rate: 3.0,
// num_circle: 10,
// unused_text: "Hello".to_string(),
// irrelevant: String::new(),
// })
Runner::new(MySketch::default())
.with_page_size(PageSize::new(200.0, 200.0))
.run()
}
11 changes: 6 additions & 5 deletions vsvg-sketch/examples/asteroid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,9 @@ impl Default for AsteroidSketch {

impl App for AsteroidSketch {
fn update(&mut self, sketch: &mut Sketch, ctx: &mut Context) -> anyhow::Result<()> {
let page_size = PageSize::new(12. * Units::CM, 12. * Units::CM);
sketch
.page_size(page_size)
.translate(page_size.w / 2.0, page_size.h / 2.0)
.scale(4.0 * Units::CM)
.translate(sketch.width() / 2., sketch.height() / 2.)
.scale_unit(4.0 * Unit::CM)
.color(Color::DARK_BLUE);

let poly = generate_polygon(
Expand Down Expand Up @@ -260,5 +258,8 @@ fn voronoi(
}

fn main() -> Result {
run_default::<AsteroidSketch>()
Runner::new(AsteroidSketch::default())
.with_page_size(PageSize::custom(12., 12., Unit::CM))
.with_time_enabled(false)
.run()
}
4 changes: 2 additions & 2 deletions vsvg-sketch/examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use vsvg_sketch::prelude::*;

fn main() -> Result {
Sketch::new()
.page_size(PageSize::A5)
.scale(Units::CM)
.page_size(PageSize::A5V)
.scale_unit(Unit::CM)
.translate(7.0, 6.0)
.circle(0.0, 0.0, 2.5)
.translate(1.0, 4.0)
Expand Down
8 changes: 5 additions & 3 deletions vsvg-sketch/examples/bezpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//! This example demonstrates how to build complex shapes by building [`kurbo::BezPath`] instances
//! manually.

//TODO: convert to sketch

use vsvg_sketch::prelude::*;

fn main() -> Result {
let page_size = PageSize::A5;
let page_size = PageSize::A5V;
let mut sketch = Sketch::new();
sketch.page_size(page_size);

Expand All @@ -25,8 +27,8 @@ fn main() -> Result {
let path3 = kurbo::BezPath::from_svg("M 0 0 A 2 1 0 0 0 3 2 Z")?;

sketch
.translate(page_size.w / 2.0, 0.0)
.scale(Units::CM)
.translate(sketch.width() / 2.0, 0.0)
.scale_unit(Unit::CM)
.translate(0.0, 7.0)
.add_path(path)
.translate(0.0, 6.0)
Expand Down
7 changes: 4 additions & 3 deletions vsvg-sketch/examples/custom_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ struct CustomUISketch {

impl App for CustomUISketch {
fn update(&mut self, sketch: &mut Sketch, _ctx: &mut Context) -> anyhow::Result<()> {
sketch.page_size(PageSize::new(200.0, 200.0));

sketch.color(self.color);
for i in 0..5 {
sketch.circle(100.0, 100.0, 30.0 + 40.0 + i as f64 * 3.0);
Expand All @@ -99,10 +97,13 @@ impl App for CustomUISketch {
}

fn main() -> Result {
run(CustomUISketch {
Runner::new(CustomUISketch {
color: GrayRed {
red: 0.5,
gray: 0.5,
},
})
.with_page_size(PageSize::new(200.0, 200.0))
.with_time_enabled(false)
.run()
}
14 changes: 3 additions & 11 deletions vsvg-sketch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
pub mod context;
pub mod prelude;
mod runner;
pub mod sketch;
mod sketch_runner;
pub mod widgets;

pub type Result = anyhow::Result<()>;

pub use sketch::Sketch;

pub use runner::Runner;

/// This is the trait that your sketch app must implement.
pub trait App {
fn update(&mut self, sketch: &mut Sketch, ctx: &mut context::Context) -> anyhow::Result<()>;
Expand All @@ -25,13 +27,3 @@ pub trait SketchUI {
}

pub trait SketchApp: App + SketchUI {}

pub fn run_default<APP: SketchApp + Default + 'static>() -> anyhow::Result<()> {
vsvg_viewer::show_with_viewer_app(Box::new(sketch_runner::SketchRunner::new(
Box::<APP>::default(),
)))
}

pub fn run<APP: SketchApp + 'static>(app: APP) -> anyhow::Result<()> {
vsvg_viewer::show_with_viewer_app(Box::new(sketch_runner::SketchRunner::new(Box::new(app))))
}
5 changes: 2 additions & 3 deletions vsvg-sketch/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub use crate::{
context::Context, register_widget_ui, run, run_default, sketch::Sketch, widgets::Widget, App,
Result,
context::Context, register_widget_ui, sketch::Sketch, widgets::Widget, App, Result, Runner,
};
pub use vsvg::{Draw, IntoBezPath, IntoBezPathTolerance, PageSize, Transforms, Units};
pub use vsvg::{Draw, IntoBezPath, IntoBezPathTolerance, PageSize, Transforms, Unit};
pub use vsvg_sketch_derive::Sketch;

#[cfg(feature = "viewer")]
Expand Down
Loading

0 comments on commit 5b4632d

Please sign in to comment.