Skip to content

Commit

Permalink
Merge branch 'master' into atlas
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Feb 25, 2020
2 parents 74bfb0c + 69c81aa commit 2c066a6
Show file tree
Hide file tree
Showing 76 changed files with 2,542 additions and 271 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ categories = ["gui"]
image = ["iced_wgpu/image"]
# Enables the `Svg` widget
svg = ["iced_wgpu/svg"]
# Enables the `Canvas` widget
canvas = ["iced_wgpu/canvas"]
# Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"]
# Enables `tokio` as the `executor::Default` on native platforms
Expand All @@ -36,13 +38,15 @@ members = [
"wgpu",
"winit",
"examples/bezier_tool",
"examples/clock",
"examples/counter",
"examples/custom_widget",
"examples/events",
"examples/geometry",
"examples/integration",
"examples/pokedex",
"examples/progress_bar",
"examples/solar_system",
"examples/stopwatch",
"examples/styling",
"examples/svg",
Expand Down
9 changes: 8 additions & 1 deletion core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,18 @@ impl Color {
///
/// [`Color`]: struct.Color.html
pub fn from_rgb8(r: u8, g: u8, b: u8) -> Color {
Color::from_rgba8(r, g, b, 1.0)
}

/// Creates a [`Color`] from its RGB8 components and an alpha value.
///
/// [`Color`]: struct.Color.html
pub fn from_rgba8(r: u8, g: u8, b: u8, a: f32) -> Color {
Color {
r: f32::from(r) / 255.0,
g: f32::from(g) / 255.0,
b: f32::from(b) / 255.0,
a: 1.0,
a,
}
}

Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod font;
mod length;
mod point;
mod rectangle;
mod size;
mod vector;

pub use align::{Align, HorizontalAlignment, VerticalAlignment};
Expand All @@ -31,4 +32,5 @@ pub use font::Font;
pub use length::Length;
pub use point::Point;
pub use rectangle::Rectangle;
pub use size::Size;
pub use vector::Vector;
7 changes: 6 additions & 1 deletion core/src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ pub struct Point {
}

impl Point {
/// The origin (i.e. a [`Point`] with both X=0 and Y=0).
///
/// [`Point`]: struct.Point.html
pub const ORIGIN: Point = Point::new(0.0, 0.0);

/// Creates a new [`Point`] with the given coordinates.
///
/// [`Point`]: struct.Point.html
pub fn new(x: f32, y: f32) -> Self {
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
}
Expand Down
50 changes: 50 additions & 0 deletions core/src/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,34 @@ impl Rectangle<f32> {
&& self.y <= point.y
&& point.y <= self.y + self.height
}

/// Computes the intersection with the given [`Rectangle`].
///
/// [`Rectangle`]: struct.Rectangle.html
pub fn intersection(
&self,
other: &Rectangle<f32>,
) -> Option<Rectangle<f32>> {
let x = self.x.max(other.x);
let y = self.y.max(other.y);

let lower_right_x = (self.x + self.width).min(other.x + other.width);
let lower_right_y = (self.y + self.height).min(other.y + other.height);

let width = lower_right_x - x;
let height = lower_right_y - y;

if width > 0.0 && height > 0.0 {
Some(Rectangle {
x,
y,
width,
height,
})
} else {
None
}
}
}

impl std::ops::Mul<f32> for Rectangle<u32> {
Expand All @@ -41,3 +69,25 @@ impl std::ops::Mul<f32> for Rectangle<u32> {
}
}
}

impl From<Rectangle<u32>> for Rectangle<f32> {
fn from(rectangle: Rectangle<u32>) -> Rectangle<f32> {
Rectangle {
x: rectangle.x as f32,
y: rectangle.y as f32,
width: rectangle.width as f32,
height: rectangle.height as f32,
}
}
}

impl From<Rectangle<f32>> for Rectangle<u32> {
fn from(rectangle: Rectangle<f32>) -> Rectangle<u32> {
Rectangle {
x: rectangle.x as u32,
y: rectangle.y as u32,
width: rectangle.width.ceil() as u32,
height: rectangle.height.ceil() as u32,
}
}
}
File renamed without changes.
4 changes: 3 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,16 @@ cargo run --package styling
## Extras
A bunch of simpler examples exist:

- [`bezier_tool`](bezier_tool), a Paint-like tool for drawing Bezier curves using [`lyon`].
- [`bezier_tool`](bezier_tool), a Paint-like tool for drawing Bézier curves using [`lyon`].
- [`clock`](clock), an application that uses the `Canvas` widget to draw a clock and its hands to display the current time.
- [`counter`](counter), the classic counter example explained in the [`README`](../README.md).
- [`custom_widget`](custom_widget), a demonstration of how to build a custom widget that draws a circle.
- [`events`](events), a log of native events displayed using a conditional `Subscription`.
- [`geometry`](geometry), a custom widget showcasing how to draw geometry with the `Mesh2D` primitive in [`iced_wgpu`](../wgpu).
- [`integration`](integration), a demonstration of how to integrate Iced in an existing graphical application.
- [`pokedex`](pokedex), an application that displays a random Pokédex entry (sprite included!) by using the [PokéAPI].
- [`progress_bar`](progress_bar), a simple progress bar that can be filled by using a slider.
- [`solar_system`](solar_system), an animated solar system drawn using the `Canvas` widget and showcasing how to compose different transforms.
- [`stopwatch`](stopwatch), a watch with start/stop and reset buttons showcasing how to listen to time.
- [`svg`](svg), an application that renders the [Ghostscript Tiger] by leveraging the `Svg` widget.

Expand Down
19 changes: 19 additions & 0 deletions examples/bezier_tool/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Bézier tool

A Paint-like tool for drawing Bézier curves using [`lyon`].

The __[`main`]__ file contains all the code of the example.

<div align="center">
<a href="https://gfycat.com/soulfulinfiniteantbear">
<img src="https://thumbs.gfycat.com/SoulfulInfiniteAntbear-small.gif">
</a>
</div>

You can run it with `cargo run`:
```
cargo run --package bezier_tool
```

[`main`]: src/main.rs
[`lyon`]: https://github.com/nical/lyon
59 changes: 26 additions & 33 deletions examples/bezier_tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,15 @@ mod bezier {
layout: Layout<'_>,
cursor_position: Point,
) -> (Primitive, MouseCursor) {
let mut buffer: VertexBuffers<Vertex2D, u16> = VertexBuffers::new();
let mut buffer: VertexBuffers<Vertex2D, u32> = VertexBuffers::new();
let mut path_builder = lyon::path::Path::builder();

let bounds = layout.bounds();

// Draw rectangle border with lyon.
basic_shapes::stroke_rectangle(
&lyon::math::Rect::new(
lyon::math::Point::new(bounds.x + 0.5, bounds.y + 0.5),
lyon::math::Point::new(0.5, 0.5),
lyon::math::Size::new(
bounds.width - 1.0,
bounds.height - 1.0,
Expand All @@ -121,48 +121,35 @@ mod bezier {

for curve in self.curves {
path_builder.move_to(lyon::math::Point::new(
curve.from.x + bounds.x,
curve.from.y + bounds.y,
curve.from.x,
curve.from.y,
));

path_builder.quadratic_bezier_to(
lyon::math::Point::new(
curve.control.x + bounds.x,
curve.control.y + bounds.y,
),
lyon::math::Point::new(
curve.to.x + bounds.x,
curve.to.y + bounds.y,
),
lyon::math::Point::new(curve.control.x, curve.control.y),
lyon::math::Point::new(curve.to.x, curve.to.y),
);
}

match self.state.pending {
None => {}
Some(Pending::One { from }) => {
path_builder.move_to(lyon::math::Point::new(
from.x + bounds.x,
from.y + bounds.y,
));
path_builder
.move_to(lyon::math::Point::new(from.x, from.y));
path_builder.line_to(lyon::math::Point::new(
cursor_position.x,
cursor_position.y,
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
));
}
Some(Pending::Two { from, to }) => {
path_builder.move_to(lyon::math::Point::new(
from.x + bounds.x,
from.y + bounds.y,
));
path_builder
.move_to(lyon::math::Point::new(from.x, from.y));
path_builder.quadratic_bezier_to(
lyon::math::Point::new(
cursor_position.x,
cursor_position.y,
),
lyon::math::Point::new(
to.x + bounds.x,
to.y + bounds.y,
cursor_position.x - bounds.x,
cursor_position.y - bounds.y,
),
lyon::math::Point::new(to.x, to.y),
);
}
}
Expand All @@ -186,10 +173,13 @@ mod bezier {
)
.unwrap();

let mesh = Primitive::Mesh2D(Arc::new(Mesh2D {
vertices: buffer.vertices,
indices: buffer.indices,
}));
let mesh = Primitive::Mesh2D {
origin: Point::new(bounds.x, bounds.y),
buffers: Arc::new(Mesh2D {
vertices: buffer.vertices,
indices: buffer.indices,
}),
};

(
Primitive::Clip {
Expand Down Expand Up @@ -296,7 +286,10 @@ use iced::{
};

pub fn main() {
Example::run(Settings::default())
Example::run(Settings {
antialiasing: true,
..Settings::default()
});
}

#[derive(Default)]
Expand Down
15 changes: 15 additions & 0 deletions examples/clock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "clock"
version = "0.1.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
publish = false

[features]
canvas = []

[dependencies]
iced = { path = "../..", features = ["canvas", "async-std", "debug"] }
iced_native = { path = "../../native" }
chrono = "0.4"
async-std = { version = "1.0", features = ["unstable"] }
16 changes: 16 additions & 0 deletions examples/clock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## Clock

An application that uses the `Canvas` widget to draw a clock and its hands to display the current time.

The __[`main`]__ file contains all the code of the example.

<div align="center">
<img src="https://user-images.githubusercontent.com/518289/74716344-a3e6b300-522e-11ea-8aea-3cc0a5100a2e.gif">
</div>

You can run it with `cargo run`:
```
cargo run --package clock
```

[`main`]: src/main.rs
Loading

0 comments on commit 2c066a6

Please sign in to comment.