summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/README.md84
-rw-r--r--examples/resources/Roboto-LICENSE (renamed from examples/tour/resources/Roboto-LICENSE)0
-rw-r--r--examples/resources/Roboto-Regular.ttf (renamed from examples/tour/resources/Roboto-Regular.ttf)bin171272 -> 171272 bytes
-rw-r--r--examples/resources/ferris.png (renamed from examples/tour/resources/ferris.png)bin33061 -> 33061 bytes
-rw-r--r--examples/resources/ui.png (renamed from examples/tour/resources/ui.png)bin16691 -> 16691 bytes
-rw-r--r--examples/tour.html (renamed from examples/tour/index.html)5
-rw-r--r--examples/tour.rs (renamed from examples/tour/src/tour.rs)122
-rw-r--r--examples/tour/Cargo.toml33
-rw-r--r--examples/tour/README.md66
-rw-r--r--examples/tour/src/iced_ggez.rs6
-rw-r--r--examples/tour/src/iced_ggez/renderer.rs77
-rw-r--r--examples/tour/src/iced_ggez/renderer/button.rs154
-rw-r--r--examples/tour/src/iced_ggez/renderer/checkbox.rs94
-rw-r--r--examples/tour/src/iced_ggez/renderer/debugger.rs32
-rw-r--r--examples/tour/src/iced_ggez/renderer/image.rs76
-rw-r--r--examples/tour/src/iced_ggez/renderer/radio.rs92
-rw-r--r--examples/tour/src/iced_ggez/renderer/slider.rs93
-rw-r--r--examples/tour/src/iced_ggez/renderer/text.rs110
-rw-r--r--examples/tour/src/iced_ggez/widget.rs12
-rw-r--r--examples/tour/src/lib.rs11
-rw-r--r--examples/tour/src/main.rs191
-rw-r--r--examples/tour/src/web.rs33
-rw-r--r--examples/tour/src/widget.rs5
23 files changed, 140 insertions, 1156 deletions
diff --git a/examples/README.md b/examples/README.md
index 4e83faf1..0a06a012 100644
--- a/examples/README.md
+++ b/examples/README.md
@@ -1,77 +1,63 @@
# Examples
-
__Iced moves fast and the `master` branch can contain breaking changes!__ If
you want to learn about a specific release, check out [the release list].
[the release list]: https://github.com/hecrj/iced/releases
-## [Tour](tour)
-
-A simple UI tour showcasing different widgets that can be built using Iced. It
-also shows how the library can be integrated into an existing system.
+## [Tour](tour.rs)
+A simple UI tour showcasing different widgets that can be built using Iced.
The example can run both on native and web platforms, using the same GUI code!
-The native renderer of the example is built on top of [`ggez`], a game library
-for Rust. Currently, it is using a [personal fork] to [add a `FontCache` type]
-and [fix some issues with HiDPI].
+[![Tour - Iced][gui_gif]][gui_gfycat]
-The web version uses `iced_web` directly. This crate is still a work in
-progress. In particular, the styling of elements is not finished yet
-(text color, alignment, sizing, etc).
+[gui_gif]: https://thumbs.gfycat.com/VeneratedSourAurochs-small.gif
+[gui_gfycat]: https://gfycat.com/veneratedsouraurochs
-The implementation consists of different modules:
- - __[`tour`]__ contains the actual cross-platform GUI code: __state__,
- __messages__, __update logic__ and __view logic__.
- - __[`iced_ggez`]__ implements a simple renderer for each of the used widgets
- on top of the graphics module of [`ggez`].
- - __[`widget`]__ conditionally re-exposes the correct platform widgets based
- on the target architecture.
- - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with
- the native [`renderer`].
- - __[`lib`]__ exposes the [`tour`] types and conditionally implements the
- WebAssembly entrypoint in the [`web`] module.
+On native, the example uses:
+ - [`iced_winit`], as a bridge between [`iced_native`] and [`winit`].
+ - [`iced_wgpu`], a WIP Iced renderer built on top of [`wgpu`] and supporting
+ Vulkan, Metal, D3D11, and D3D12 (OpenGL and WebGL soon!).
-The conditional compilation awkwardness from targetting both native and web
-platforms should be handled seamlessly by the `iced` crate in the near future!
+The web version uses [`iced_web`], which is still a work in progress. In
+particular, the styling of elements is not finished yet (text color, alignment,
+sizing, etc).
-If you want to run it as a native app:
+The __[`tour`]__ file contains all the code of the example! All the
+cross-platform GUI is defined in terms of __state__, __messages__,
+__update logic__ and __view logic__.
-```
-cd examples/tour
-cargo run
-```
+[`tour`]: tour.rs
+[`iced_winit`]: ../winit
+[`iced_native`]: ../native
+[`iced_wgpu`]: ../wgpu
+[`iced_web`]: ../web
+[`winit`]: https://github.com/rust-windowing/winit
+[`wgpu`]: https://github.com/gfx-rs/wgpu-rs
-If you want to run it on web, you will need [`wasm-pack`]:
+#### Running the native version
+Use [Cargo](https://doc.rust-lang.org/cargo/reference/manifest.html#examples)
+to run the example:
```
-cd examples/tour
-wasm-pack build --target web
+cargo run --example tour
```
-Then, simply serve the directory with any HTTP server. For instance:
+#### Running the web version
+Build using the `wasm32-unknown-unknown` target and use the [`wasm-bindgen`] CLI
+to generate appropriate bindings in a `tour` directory.
```
-python3 -m http.server
+cd examples
+cargo build --example tour --target wasm32-unknown-unknown
+wasm-bindgen ../target/wasm32-unknown-unknown/debug/examples/tour.wasm --out-dir tour --web
```
-[![Tour - Iced][gui_gif]][gui_gfycat]
+Finally, serve the `examples` directory using an HTTP server and access the
+`tour.html` file.
-[`ggez`]: https://github.com/ggez/ggez
-[`tour`]: tour/src/tour.rs
-[`iced_ggez`]: tour/src/iced_ggez
-[`renderer`]: src/iced_ggez/renderer
-[`widget`]: tour/src/widget.rs
-[`main`]: tour/src/main.rs
-[`lib`]: tour/src/lib.rs
-[`web`]: tour/src/web.rs
-[`wasm-pack`]: https://rustwasm.github.io/wasm-pack/installer/
-[personal fork]: https://github.com/hecrj/ggez
-[add a `FontCache` type]: https://github.com/ggez/ggez/pull/679
-[fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1
-[gui_gif]: https://thumbs.gfycat.com/VeneratedSourAurochs-small.gif
-[gui_gfycat]: https://gfycat.com/veneratedsouraurochs
+[`wasm-bindgen`]: https://github.com/rustwasm/wasm-bindgen
## [Coffee]
diff --git a/examples/tour/resources/Roboto-LICENSE b/examples/resources/Roboto-LICENSE
index 75b52484..75b52484 100644
--- a/examples/tour/resources/Roboto-LICENSE
+++ b/examples/resources/Roboto-LICENSE
diff --git a/examples/tour/resources/Roboto-Regular.ttf b/examples/resources/Roboto-Regular.ttf
index 2b6392ff..2b6392ff 100644
--- a/examples/tour/resources/Roboto-Regular.ttf
+++ b/examples/resources/Roboto-Regular.ttf
Binary files differ
diff --git a/examples/tour/resources/ferris.png b/examples/resources/ferris.png
index ebce1a14..ebce1a14 100644
--- a/examples/tour/resources/ferris.png
+++ b/examples/resources/ferris.png
Binary files differ
diff --git a/examples/tour/resources/ui.png b/examples/resources/ui.png
index 4fd3beb3..4fd3beb3 100644
--- a/examples/tour/resources/ui.png
+++ b/examples/resources/ui.png
Binary files differ
diff --git a/examples/tour/index.html b/examples/tour.html
index b17ac4a2..35360e59 100644
--- a/examples/tour/index.html
+++ b/examples/tour.html
@@ -6,8 +6,9 @@
</head>
<body>
<script type="module">
- import init from "./pkg/iced_tour.js";
- init("./pkg/iced_tour_bg.wasm");
+ import init from "./tour/tour.js";
+
+ init('./tour/tour_bg.wasm');
</script>
</body>
</html>
diff --git a/examples/tour/src/tour.rs b/examples/tour.rs
index 60a251d2..59a8c525 100644
--- a/examples/tour/src/tour.rs
+++ b/examples/tour.rs
@@ -1,8 +1,17 @@
-use crate::widget::{
- button, slider, text::HorizontalAlignment, Align, Button, Checkbox, Color,
- Column, Element, Image, Length, Radio, Row, Slider, Text,
+use iced::{
+ button, slider, text::HorizontalAlignment, Align, Application, Background,
+ Button, Checkbox, Color, Column, Element, Image, Justify, Length, Radio,
+ Row, Slider, Text,
};
+pub fn main() {
+ env_logger::init();
+
+ let tour = Tour::new();
+
+ tour.run();
+}
+
pub struct Tour {
steps: Steps,
back_button: button::State,
@@ -19,8 +28,12 @@ impl Tour {
debug: false,
}
}
+}
+
+impl Application for Tour {
+ type Message = Message;
- pub fn update(&mut self, event: Message) {
+ fn update(&mut self, event: Message) {
match event {
Message::BackPressed => {
self.steps.go_back();
@@ -34,7 +47,7 @@ impl Tour {
}
}
- pub fn view(&mut self) -> Element<Message> {
+ fn view(&mut self) -> Element<Message> {
let Tour {
steps,
back_button,
@@ -46,9 +59,8 @@ impl Tour {
if steps.has_previous() {
controls = controls.push(
- Button::new(back_button, "Back")
- .on_press(Message::BackPressed)
- .class(button::Class::Secondary),
+ secondary_button(back_button, "Back")
+ .on_press(Message::BackPressed),
);
}
@@ -56,22 +68,32 @@ impl Tour {
if steps.can_continue() {
controls = controls.push(
- Button::new(next_button, "Next").on_press(Message::NextPressed),
+ primary_button(next_button, "Next")
+ .on_press(Message::NextPressed),
);
}
let element: Element<_> = Column::new()
- .max_width(Length::Units(500))
+ .max_width(Length::Units(540))
.spacing(20)
+ .padding(20)
.push(steps.view(self.debug).map(Message::StepMessage))
.push(controls)
.into();
- if self.debug {
+ let element = if self.debug {
element.explain(Color::BLACK)
} else {
element
- }
+ };
+
+ Column::new()
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .align_items(Align::Center)
+ .justify_content(Justify::Center)
+ .push(element)
+ .into()
}
}
@@ -286,7 +308,7 @@ impl<'a> Step {
that can be easily implemented on top of Iced.",
))
.push(Text::new(
- "Iced is a renderer-agnostic GUI library for Rust focused on \
+ "Iced is a cross-platform GUI library for Rust focused on \
simplicity and type-safety. It is heavily inspired by Elm.",
))
.push(Text::new(
@@ -294,9 +316,9 @@ impl<'a> Step {
2D game engine for Rust.",
))
.push(Text::new(
- "Iced does not provide a built-in renderer. On native \
- platforms, this example runs on a fairly simple renderer \
- built on top of ggez, another game library.",
+ "On native platforms, Iced provides by default a renderer \
+ built on top of wgpu, a graphics library supporting Vulkan, \
+ Metal, DX11, and DX12.",
))
.push(Text::new(
"Additionally, this tour can also run on WebAssembly thanks \
@@ -304,7 +326,7 @@ impl<'a> Step {
))
.push(Text::new(
"You will need to interact with the UI in order to reach the \
- end of this tour!",
+ end!",
))
}
@@ -481,9 +503,18 @@ impl<'a> Step {
Self::container("Image")
.push(Text::new("An image that tries to keep its aspect ratio."))
.push(
- Image::new("resources/ferris.png")
- .width(Length::Units(width))
- .align_self(Align::Center),
+ // This should go away once we unify resource loading on native
+ // platforms
+ if cfg!(target_arch = "wasm32") {
+ Image::new("resources/ferris.png")
+ } else {
+ Image::new(format!(
+ "{}/examples/resources/ferris.png",
+ env!("CARGO_MANIFEST_DIR")
+ ))
+ }
+ .width(Length::Units(width))
+ .align_self(Align::Center),
)
.push(Slider::new(
slider,
@@ -524,6 +555,44 @@ impl<'a> Step {
}
}
+fn button<'a, Message>(
+ state: &'a mut button::State,
+ label: &str,
+) -> Button<'a, Message> {
+ Button::new(
+ state,
+ Text::new(label)
+ .color(Color::WHITE)
+ .horizontal_alignment(HorizontalAlignment::Center),
+ )
+ .padding(12)
+ .border_radius(12)
+}
+
+fn primary_button<'a, Message>(
+ state: &'a mut button::State,
+ label: &str,
+) -> Button<'a, Message> {
+ button(state, label).background(Background::Color(Color {
+ r: 0.11,
+ g: 0.42,
+ b: 0.87,
+ a: 1.0,
+ }))
+}
+
+fn secondary_button<'a, Message>(
+ state: &'a mut button::State,
+ label: &str,
+) -> Button<'a, Message> {
+ button(state, label).background(Background::Color(Color {
+ r: 0.4,
+ g: 0.4,
+ b: 0.4,
+ a: 1.0,
+ }))
+}
+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Language {
Rust,
@@ -565,3 +634,16 @@ pub enum Layout {
Row,
Column,
}
+
+// This should be gracefully handled by Iced in the future. Probably using our
+// own proc macro, or maybe the whole process is streamlined by `wasm-pack` at
+// some point.
+#[cfg(target_arch = "wasm32")]
+mod wasm {
+ use wasm_bindgen::prelude::*;
+
+ #[wasm_bindgen(start)]
+ pub fn run() {
+ super::main()
+ }
+}
diff --git a/examples/tour/Cargo.toml b/examples/tour/Cargo.toml
deleted file mode 100644
index 2c79cbf7..00000000
--- a/examples/tour/Cargo.toml
+++ /dev/null
@@ -1,33 +0,0 @@
-[package]
-name = "iced_tour"
-version = "0.0.0"
-authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
-description = "Tour example for Iced"
-license = "MIT"
-repository = "https://github.com/hecrj/iced"
-edition = "2018"
-publish = false
-
-[lib]
-crate-type = ["cdylib", "rlib"]
-
-[[bin]]
-name = "main"
-path = "src/main.rs"
-
-[dependencies]
-futures-preview = "=0.3.0-alpha.18"
-
-[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
-iced_native = { version = "0.1.0-alpha", path = "../../native" }
-# A personal `ggez` fork that introduces a `FontCache` type to measure text
-# efficiently and fixes HiDPI issues.
-ggez = { version = "0.5", git = "https://github.com/hecrj/ggez.git" }
-env_logger = "0.6"
-
-[target.'cfg(target_arch = "wasm32")'.dependencies]
-iced_web = { path = "../../web" }
-wasm-bindgen = "0.2.50"
-log = "0.4"
-console_error_panic_hook = "0.1.6"
-console_log = "0.1.2"
diff --git a/examples/tour/README.md b/examples/tour/README.md
deleted file mode 100644
index 7ef1a212..00000000
--- a/examples/tour/README.md
+++ /dev/null
@@ -1,66 +0,0 @@
-# Tour
-
-A simple UI tour showcasing different widgets that can be built using Iced. It
-also shows how the library can be integrated into an existing system.
-
-The example can run both on native and web platforms, using the same GUI code!
-
-The native renderer of the example is built on top of [`ggez`], a game library
-for Rust. Currently, it is using a [personal fork] to [add a `FontCache` type]
-and [fix some issues with HiDPI].
-
-The web version uses `iced_web` directly. This crate is still a work in
-progress. In particular, the styling of elements is not finished yet
-(text color, alignment, sizing, etc).
-
-The implementation consists of different modules:
- - __[`tour`]__ contains the actual cross-platform GUI code: __state__,
- __messages__, __update logic__ and __view logic__.
- - __[`iced_ggez`]__ implements a simple renderer for each of the used widgets
- on top of the graphics module of [`ggez`].
- - __[`widget`]__ conditionally re-exposes the correct platform widgets based
- on the target architecture.
- - __[`main`]__ integrates Iced with [`ggez`] and connects the [`tour`] with
- the native [`renderer`].
- - __[`lib`]__ exposes the [`tour`] types and conditionally implements the
- WebAssembly entrypoint in the [`web`] module.
-
-The conditional compilation awkwardness from targetting both native and web
-platforms should be handled seamlessly by the `iced` crate in the near future!
-
-If you want to run it as a native app:
-
-```
-cd examples/tour
-cargo run
-```
-
-If you want to run it on web, you will need [`wasm-pack`]:
-
-```
-cd examples/tour
-wasm-pack build --target web
-```
-
-Then, simply serve the directory with any HTTP server. For instance:
-
-```
-python3 -m http.server
-```
-
-[![Tour - Iced][gui_gif]][gui_gfycat]
-
-[`ggez`]: https://github.com/ggez/ggez
-[`tour`]: src/tour.rs
-[`iced_ggez`]: src/iced_ggez
-[`renderer`]: src/iced_ggez/renderer
-[`widget`]: src/widget.rs
-[`main`]: src/main.rs
-[`lib`]: src/lib.rs
-[`web`]: src/web.rs
-[`wasm-pack`]: https://rustwasm.github.io/wasm-pack/installer/
-[personal fork]: https://github.com/hecrj/ggez
-[add a `FontCache` type]: https://github.com/ggez/ggez/pull/679
-[fix some issues with HiDPI]: https://github.com/hecrj/ggez/commit/dfe2fd2423c51a6daf42c75f66dfaeaacd439fb1
-[gui_gif]: https://thumbs.gfycat.com/VeneratedSourAurochs-small.gif
-[gui_gfycat]: https://gfycat.com/veneratedsouraurochs
diff --git a/examples/tour/src/iced_ggez.rs b/examples/tour/src/iced_ggez.rs
deleted file mode 100644
index 4a9c0ef4..00000000
--- a/examples/tour/src/iced_ggez.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-mod renderer;
-mod widget;
-
-pub use renderer::Cache as ImageCache;
-pub use renderer::Renderer;
-pub use widget::*;
diff --git a/examples/tour/src/iced_ggez/renderer.rs b/examples/tour/src/iced_ggez/renderer.rs
deleted file mode 100644
index c0e6d559..00000000
--- a/examples/tour/src/iced_ggez/renderer.rs
+++ /dev/null
@@ -1,77 +0,0 @@
-mod button;
-mod checkbox;
-mod debugger;
-mod image;
-mod radio;
-mod slider;
-mod text;
-
-use ggez::graphics::{
- self, spritebatch::SpriteBatch, Font, Image, MeshBuilder,
-};
-use ggez::Context;
-
-pub use image::Cache;
-
-pub struct Renderer<'a> {
- pub context: &'a mut Context,
- pub images: &'a mut image::Cache,
- pub sprites: SpriteBatch,
- pub spritesheet: Image,
- pub font: Font,
- font_size: f32,
- debug_mesh: Option<MeshBuilder>,
-}
-
-impl<'a> Renderer<'a> {
- pub fn new(
- context: &'a mut Context,
- images: &'a mut image::Cache,
- spritesheet: Image,
- font: Font,
- ) -> Renderer<'a> {
- Renderer {
- context,
- images,
- sprites: SpriteBatch::new(spritesheet.clone()),
- spritesheet,
- font,
- font_size: 20.0,
- debug_mesh: None,
- }
- }
-
- pub fn flush(&mut self) {
- graphics::draw(
- self.context,
- &self.sprites,
- graphics::DrawParam::default(),
- )
- .expect("Draw sprites");
-
- graphics::draw_queued_text(
- self.context,
- graphics::DrawParam::default(),
- Default::default(),
- graphics::FilterMode::Linear,
- )
- .expect("Draw text");
-
- if let Some(debug_mesh) = self.debug_mesh.take() {
- let mesh =
- debug_mesh.build(self.context).expect("Build debug mesh");
-
- graphics::draw(self.context, &mesh, graphics::DrawParam::default())
- .expect("Draw debug mesh");
- }
- }
-}
-
-pub fn into_color(color: iced_native::Color) -> graphics::Color {
- graphics::Color {
- r: color.r,
- g: color.g,
- b: color.b,
- a: color.a,
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/button.rs b/examples/tour/src/iced_ggez/renderer/button.rs
deleted file mode 100644
index 78a5de07..00000000
--- a/examples/tour/src/iced_ggez/renderer/button.rs
+++ /dev/null
@@ -1,154 +0,0 @@
-use super::Renderer;
-use ggez::graphics::{
- self, Align, Color, DrawParam, Rect, Scale, Text, TextFragment, WHITE,
-};
-use iced_native::{button, Button, Layout, Length, MouseCursor, Node, Style};
-
-const LEFT: Rect = Rect {
- x: 0.0,
- y: 34.0,
- w: 6.0,
- h: 49.0,
-};
-
-const BACKGROUND: Rect = Rect {
- x: LEFT.w,
- y: LEFT.y,
- w: 1.0,
- h: LEFT.h,
-};
-
-const RIGHT: Rect = Rect {
- x: LEFT.h - LEFT.w,
- y: LEFT.y,
- w: LEFT.w,
- h: LEFT.h,
-};
-
-impl button::Renderer for Renderer<'_> {
- fn node<Message>(&self, button: &Button<'_, Message>) -> Node {
- let style = Style::default()
- .width(button.width)
- .height(Length::Units(LEFT.h as u16))
- .min_width(Length::Units(100))
- .align_self(button.align_self);
-
- Node::new(style)
- }
-
- fn draw<Message>(
- &mut self,
- button: &Button<'_, Message>,
- layout: Layout<'_>,
- cursor_position: iced_native::Point,
- ) -> MouseCursor {
- let mut bounds = layout.bounds();
- let mouse_over = bounds.contains(cursor_position);
-
- let mut state_offset = 0.0;
-
- if mouse_over {
- if button.state.is_pressed() {
- bounds.y += 4.0;
- state_offset = RIGHT.x + RIGHT.w;
- } else {
- bounds.y -= 1.0;
- }
- }
-
- let class_index = match button.class {
- button::Class::Primary => 0,
- button::Class::Secondary => 1,
- button::Class::Positive => 2,
- };
-
- let width = self.spritesheet.width() as f32;
- let height = self.spritesheet.height() as f32;
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (LEFT.x + state_offset) / width,
- y: (LEFT.y + class_index as f32 * LEFT.h) / height,
- w: LEFT.w / width,
- h: LEFT.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (BACKGROUND.x + state_offset) / width,
- y: (BACKGROUND.y + class_index as f32 * BACKGROUND.h) / height,
- w: BACKGROUND.w / width,
- h: BACKGROUND.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x + LEFT.w,
- y: bounds.y,
- },
- scale: ggez::mint::Vector2 {
- x: bounds.width - LEFT.w - RIGHT.w,
- y: 1.0,
- },
- ..DrawParam::default()
- });
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (RIGHT.x + state_offset) / width,
- y: (RIGHT.y + class_index as f32 * RIGHT.h) / height,
- w: RIGHT.w / width,
- h: RIGHT.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x + bounds.width - RIGHT.w,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
-
- let mut text = Text::new(TextFragment {
- text: button.label.clone(),
- font: Some(self.font),
- scale: Some(Scale { x: 20.0, y: 20.0 }),
- ..Default::default()
- });
-
- text.set_bounds(
- ggez::mint::Point2 {
- x: bounds.width,
- y: bounds.height,
- },
- Align::Center,
- );
-
- graphics::queue_text(
- self.context,
- &text,
- ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y + BACKGROUND.h / 4.0,
- },
- Some(if mouse_over {
- WHITE
- } else {
- Color {
- r: 0.9,
- g: 0.9,
- b: 0.9,
- a: 1.0,
- }
- }),
- );
-
- if mouse_over {
- MouseCursor::Pointer
- } else {
- MouseCursor::OutOfBounds
- }
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/checkbox.rs b/examples/tour/src/iced_ggez/renderer/checkbox.rs
deleted file mode 100644
index 807185d9..00000000
--- a/examples/tour/src/iced_ggez/renderer/checkbox.rs
+++ /dev/null
@@ -1,94 +0,0 @@
-use super::Renderer;
-
-use ggez::graphics::{DrawParam, Rect};
-use iced_native::{
- checkbox, text, Align, Checkbox, Column, Layout, Length, MouseCursor, Node,
- Row, Text, Widget,
-};
-
-const SPRITE: Rect = Rect {
- x: 98.0,
- y: 0.0,
- w: 28.0,
- h: 28.0,
-};
-
-impl checkbox::Renderer for Renderer<'_>
-where
- Self: text::Renderer,
-{
- fn node<Message>(&mut self, checkbox: &Checkbox<Message>) -> Node {
- Row::<(), Self>::new()
- .spacing(15)
- .align_items(Align::Center)
- .push(
- Column::new()
- .width(Length::Units(SPRITE.w as u16))
- .height(Length::Units(SPRITE.h as u16)),
- )
- .push(Text::new(&checkbox.label))
- .node(self)
- }
-
- fn draw<Message>(
- &mut self,
- checkbox: &Checkbox<Message>,
- layout: Layout<'_>,
- cursor_position: iced_native::Point,
- ) -> MouseCursor {
- let bounds = layout.bounds();
- let children: Vec<_> = layout.children().collect();
- let text_bounds = children[1].bounds();
-
- let mut text = Text::new(&checkbox.label);
-
- if let Some(label_color) = checkbox.label_color {
- text = text.color(label_color);
- }
-
- text::Renderer::draw(self, &text, children[1]);
-
- let mouse_over = bounds.contains(cursor_position)
- || text_bounds.contains(cursor_position);
-
- let width = self.spritesheet.width() as f32;
- let height = self.spritesheet.height() as f32;
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 }))
- / width,
- y: SPRITE.y / height,
- w: SPRITE.w / width,
- h: SPRITE.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
-
- if checkbox.is_checked {
- self.sprites.add(DrawParam {
- src: Rect {
- x: (SPRITE.x + SPRITE.w * 2.0) / width,
- y: SPRITE.y / height,
- w: SPRITE.w / width,
- h: SPRITE.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
- }
-
- if mouse_over {
- MouseCursor::Pointer
- } else {
- MouseCursor::OutOfBounds
- }
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/debugger.rs b/examples/tour/src/iced_ggez/renderer/debugger.rs
deleted file mode 100644
index ffb658af..00000000
--- a/examples/tour/src/iced_ggez/renderer/debugger.rs
+++ /dev/null
@@ -1,32 +0,0 @@
-use super::{into_color, Renderer};
-use ggez::graphics::{DrawMode, MeshBuilder, Rect};
-
-impl iced_native::renderer::Debugger for Renderer<'_> {
- fn explain(
- &mut self,
- layout: &iced_native::Layout<'_>,
- color: iced_native::Color,
- ) {
- let bounds = layout.bounds();
-
- let mut debug_mesh =
- self.debug_mesh.take().unwrap_or(MeshBuilder::new());
-
- debug_mesh.rectangle(
- DrawMode::stroke(1.0),
- Rect {
- x: bounds.x,
- y: bounds.y,
- w: bounds.width,
- h: bounds.height,
- },
- into_color(color),
- );
-
- self.debug_mesh = Some(debug_mesh);
-
- for child in layout.children() {
- self.explain(&child, color);
- }
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/image.rs b/examples/tour/src/iced_ggez/renderer/image.rs
deleted file mode 100644
index b12b65c3..00000000
--- a/examples/tour/src/iced_ggez/renderer/image.rs
+++ /dev/null
@@ -1,76 +0,0 @@
-use super::Renderer;
-
-use ggez::{graphics, nalgebra};
-use iced_native::{image, Image, Layout, Length, Style};
-
-pub struct Cache {
- images: std::collections::HashMap<String, graphics::Image>,
-}
-
-impl Cache {
- pub fn new() -> Self {
- Self {
- images: std::collections::HashMap::new(),
- }
- }
-
- fn get<'a>(
- &mut self,
- name: &'a str,
- context: &mut ggez::Context,
- ) -> graphics::Image {
- if let Some(image) = self.images.get(name) {
- return image.clone();
- }
-
- let mut image = graphics::Image::new(context, &format!("/{}", name))
- .expect("Load ferris image");
-
- image.set_filter(graphics::FilterMode::Linear);
-
- self.images.insert(name.to_string(), image.clone());
-
- image
- }
-}
-
-impl<'a> image::Renderer<&'a str> for Renderer<'_> {
- fn node(&mut self, image: &Image<&'a str>) -> iced_native::Node {
- let ggez_image = self.images.get(image.handle, self.context);
-
- let aspect_ratio =
- ggez_image.width() as f32 / ggez_image.height() as f32;
-
- let mut style = Style::default().align_self(image.align_self);
-
- style = match (image.width, image.height) {
- (Length::Units(width), _) => style.width(image.width).height(
- Length::Units((width as f32 / aspect_ratio).round() as u16),
- ),
- (_, _) => style
- .width(Length::Units(ggez_image.width()))
- .height(Length::Units(ggez_image.height())),
- };
-
- iced_native::Node::new(style)
- }
-
- fn draw(&mut self, image: &Image<&'a str>, layout: Layout<'_>) {
- let image = self.images.get(image.handle, self.context);
- let bounds = layout.bounds();
-
- // We should probably use batches to draw images efficiently and keep
- // draw side-effect free, but this is good enough for the example.
- graphics::draw(
- self.context,
- &image,
- graphics::DrawParam::new()
- .dest(nalgebra::Point2::new(bounds.x, bounds.y))
- .scale(nalgebra::Vector2::new(
- bounds.width / image.width() as f32,
- bounds.height / image.height() as f32,
- )),
- )
- .expect("Draw image");
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/radio.rs b/examples/tour/src/iced_ggez/renderer/radio.rs
deleted file mode 100644
index dbd29ecd..00000000
--- a/examples/tour/src/iced_ggez/renderer/radio.rs
+++ /dev/null
@@ -1,92 +0,0 @@
-use super::Renderer;
-
-use ggez::graphics::{DrawParam, Rect};
-use iced_native::{
- radio, text, Align, Column, Layout, Length, MouseCursor, Node, Point,
- Radio, Row, Text, Widget,
-};
-
-const SPRITE: Rect = Rect {
- x: 98.0,
- y: 28.0,
- w: 28.0,
- h: 28.0,
-};
-
-impl radio::Renderer for Renderer<'_>
-where
- Self: text::Renderer,
-{
- fn node<Message>(&mut self, radio: &Radio<Message>) -> Node {
- Row::<(), Self>::new()
- .spacing(15)
- .align_items(Align::Center)
- .push(
- Column::new()
- .width(Length::Units(SPRITE.w as u16))
- .height(Length::Units(SPRITE.h as u16)),
- )
- .push(Text::new(&radio.label))
- .node(self)
- }
-
- fn draw<Message>(
- &mut self,
- radio: &Radio<Message>,
- layout: Layout<'_>,
- cursor_position: Point,
- ) -> MouseCursor {
- let children: Vec<_> = layout.children().collect();
-
- let mut text = Text::new(&radio.label);
-
- if let Some(label_color) = radio.label_color {
- text = text.color(label_color);
- }
-
- text::Renderer::draw(self, &text, children[1]);
-
- let bounds = layout.bounds();
- let mouse_over = bounds.contains(cursor_position);
-
- let width = self.spritesheet.width() as f32;
- let height = self.spritesheet.height() as f32;
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (SPRITE.x + (if mouse_over { SPRITE.w } else { 0.0 }))
- / width,
- y: SPRITE.y / height,
- w: SPRITE.w / width,
- h: SPRITE.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
-
- if radio.is_selected {
- self.sprites.add(DrawParam {
- src: Rect {
- x: (SPRITE.x + SPRITE.w * 2.0) / width,
- y: SPRITE.y / height,
- w: SPRITE.w / width,
- h: SPRITE.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- ..DrawParam::default()
- });
- }
-
- if mouse_over {
- MouseCursor::Pointer
- } else {
- MouseCursor::OutOfBounds
- }
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/slider.rs b/examples/tour/src/iced_ggez/renderer/slider.rs
deleted file mode 100644
index 60c40c55..00000000
--- a/examples/tour/src/iced_ggez/renderer/slider.rs
+++ /dev/null
@@ -1,93 +0,0 @@
-use super::Renderer;
-
-use ggez::graphics::{DrawParam, Rect};
-use iced_native::{
- slider, Layout, Length, MouseCursor, Node, Point, Slider, Style,
-};
-
-const RAIL: Rect = Rect {
- x: 98.0,
- y: 56.0,
- w: 1.0,
- h: 4.0,
-};
-
-const MARKER: Rect = Rect {
- x: RAIL.x + 28.0,
- y: RAIL.y,
- w: 16.0,
- h: 24.0,
-};
-
-impl slider::Renderer for Renderer<'_> {
- fn node<Message>(&self, slider: &Slider<'_, Message>) -> Node {
- let style = Style::default()
- .width(slider.width)
- .height(Length::Units(25))
- .min_width(Length::Units(100));
-
- Node::new(style)
- }
-
- fn draw<Message>(
- &mut self,
- slider: &Slider<'_, Message>,
- layout: Layout<'_>,
- cursor_position: Point,
- ) -> MouseCursor {
- let bounds = layout.bounds();
- let width = self.spritesheet.width() as f32;
- let height = self.spritesheet.height() as f32;
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: RAIL.x / width,
- y: RAIL.y / height,
- w: RAIL.w / width,
- h: RAIL.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x + MARKER.w as f32 / 2.0,
- y: bounds.y + 12.5,
- },
- scale: ggez::mint::Vector2 {
- x: bounds.width - MARKER.w as f32,
- y: 1.0,
- },
- ..DrawParam::default()
- });
-
- let (range_start, range_end) = slider.range.clone().into_inner();
-
- let marker_offset = (bounds.width - MARKER.w as f32)
- * ((slider.value - range_start)
- / (range_end - range_start).max(1.0));
-
- let mouse_over = bounds.contains(cursor_position);
- let is_active = slider.state.is_dragging() || mouse_over;
-
- self.sprites.add(DrawParam {
- src: Rect {
- x: (MARKER.x + (if is_active { MARKER.w } else { 0.0 }))
- / width,
- y: MARKER.y / height,
- w: MARKER.w / width,
- h: MARKER.h / height,
- },
- dest: ggez::mint::Point2 {
- x: bounds.x + marker_offset.round(),
- y: bounds.y
- + (if slider.state.is_dragging() { 2.0 } else { 0.0 }),
- },
- ..DrawParam::default()
- });
-
- if slider.state.is_dragging() {
- MouseCursor::Grabbing
- } else if mouse_over {
- MouseCursor::Grab
- } else {
- MouseCursor::OutOfBounds
- }
- }
-}
diff --git a/examples/tour/src/iced_ggez/renderer/text.rs b/examples/tour/src/iced_ggez/renderer/text.rs
deleted file mode 100644
index b51cc220..00000000
--- a/examples/tour/src/iced_ggez/renderer/text.rs
+++ /dev/null
@@ -1,110 +0,0 @@
-use super::{into_color, Renderer};
-use ggez::graphics::{self, mint, Align, Scale, Text, TextFragment};
-
-use iced_native::{text, Layout, Node, Style};
-use std::cell::RefCell;
-use std::f32;
-
-impl text::Renderer for Renderer<'_> {
- fn node(&self, text: &iced_native::Text) -> Node {
- let font = self.font;
- let font_cache = graphics::font_cache(self.context);
- let content = String::from(&text.content);
-
- // TODO: Investigate why stretch tries to measure this MANY times
- // with every ancestor's bounds.
- // Bug? Using the library wrong? I should probably open an issue on
- // the stretch repository.
- // I noticed that the first measure is the one that matters in
- // practice. Here, we use a RefCell to store the cached measurement.
- let measure = RefCell::new(None);
- let size = text.size.map(f32::from).unwrap_or(self.font_size);
-
- let style = Style::default().width(text.width);
-
- iced_native::Node::with_measure(style, move |bounds| {
- let mut measure = measure.borrow_mut();
-
- if measure.is_none() {
- let bounds = (
- match bounds.width {
- iced_native::Number::Undefined => f32::INFINITY,
- iced_native::Number::Defined(w) => w,
- },
- match bounds.height {
- iced_native::Number::Undefined => f32::INFINITY,
- iced_native::Number::Defined(h) => h,
- },
- );
-
- let mut text = Text::new(TextFragment {
- text: content.clone(),
- font: Some(font),
- scale: Some(Scale { x: size, y: size }),
- ..Default::default()
- });
-
- text.set_bounds(
- mint::Point2 {
- x: bounds.0,
- y: bounds.1,
- },
- Align::Left,
- );
-
- let (width, height) = font_cache.dimensions(&text);
-
- let size = iced_native::Size {
- width: width as f32,
- height: height as f32,
- };
-
- // If the text has no width boundary we avoid caching as the
- // layout engine may just be measuring text in a row.
- if bounds.0 == f32::INFINITY {
- return size;
- } else {
- *measure = Some(size);
- }
- }
-
- measure.unwrap()
- })
- }
-
- fn draw(&mut self, text: &iced_native::Text, layout: Layout<'_>) {
- let size = text.size.map(f32::from).unwrap_or(self.font_size);
- let bounds = layout.bounds();
-
- let mut ggez_text = Text::new(TextFragment {
- text: text.content.clone(),
- font: Some(self.font),
- scale: Some(Scale { x: size, y: size }),
- ..Default::default()
- });
-
- ggez_text.set_bounds(
- mint::Point2 {
- x: bounds.width,
- y: bounds.height,
- },
- match text.horizontal_alignment {
- text::HorizontalAlignment::Left => graphics::Align::Left,
- text::HorizontalAlignment::Center => graphics::Align::Center,
- text::HorizontalAlignment::Right => graphics::Align::Right,
- },
- );
-
- graphics::queue_text(
- self.context,
- &ggez_text,
- mint::Point2 {
- x: bounds.x,
- y: bounds.y,
- },
- text.color
- .or(Some(iced_native::Color::BLACK))
- .map(into_color),
- );
- }
-}
diff --git a/examples/tour/src/iced_ggez/widget.rs b/examples/tour/src/iced_ggez/widget.rs
deleted file mode 100644
index 948f9fc6..00000000
--- a/examples/tour/src/iced_ggez/widget.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-use super::Renderer;
-
-pub use iced_native::{
- button, slider, text, Align, Button, Checkbox, Color, Length, Radio,
- Slider, Text,
-};
-
-pub type Image<'a> = iced_native::Image<&'a str>;
-
-pub type Column<'a, Message> = iced_native::Column<'a, Message, Renderer<'a>>;
-pub type Row<'a, Message> = iced_native::Row<'a, Message, Renderer<'a>>;
-pub type Element<'a, Message> = iced_native::Element<'a, Message, Renderer<'a>>;
diff --git a/examples/tour/src/lib.rs b/examples/tour/src/lib.rs
deleted file mode 100644
index eb41fcd9..00000000
--- a/examples/tour/src/lib.rs
+++ /dev/null
@@ -1,11 +0,0 @@
-pub mod tour;
-
-pub use tour::{Message, Tour};
-
-mod widget;
-
-#[cfg(target_arch = "wasm32")]
-mod web;
-
-#[cfg(not(target_arch = "wasm32"))]
-pub mod iced_ggez;
diff --git a/examples/tour/src/main.rs b/examples/tour/src/main.rs
deleted file mode 100644
index a34d3298..00000000
--- a/examples/tour/src/main.rs
+++ /dev/null
@@ -1,191 +0,0 @@
-use iced_tour::{iced_ggez, Tour};
-
-use ggez;
-use ggez::event;
-use ggez::filesystem;
-use ggez::graphics;
-use ggez::input::mouse;
-
-pub fn main() -> ggez::GameResult {
- env_logger::init();
-
- let (context, event_loop) = {
- &mut ggez::ContextBuilder::new("iced", "ggez")
- .window_mode(ggez::conf::WindowMode {
- width: 1280.0,
- height: 1024.0,
- resizable: true,
- ..ggez::conf::WindowMode::default()
- })
- .build()?
- };
-
- filesystem::mount(
- context,
- std::path::Path::new(env!("CARGO_MANIFEST_DIR")),
- true,
- );
-
- let state = &mut Game::new(context)?;
-
- event::run(context, event_loop, state)
-}
-
-struct Game {
- spritesheet: graphics::Image,
- font: graphics::Font,
- images: iced_ggez::ImageCache,
- tour: Tour,
-
- events: Vec<iced_native::Event>,
- cache: Option<iced_native::Cache>,
-}
-
-impl Game {
- fn new(context: &mut ggez::Context) -> ggez::GameResult<Game> {
- graphics::set_default_filter(context, graphics::FilterMode::Nearest);
-
- Ok(Game {
- spritesheet: graphics::Image::new(context, "/resources/ui.png")
- .unwrap(),
- font: graphics::Font::new(context, "/resources/Roboto-Regular.ttf")
- .unwrap(),
- images: iced_ggez::ImageCache::new(),
- tour: Tour::new(),
-
- events: Vec::new(),
- cache: Some(iced_native::Cache::default()),
- })
- }
-}
-
-impl event::EventHandler for Game {
- fn update(&mut self, _ctx: &mut ggez::Context) -> ggez::GameResult {
- Ok(())
- }
-
- fn mouse_button_down_event(
- &mut self,
- _context: &mut ggez::Context,
- _button: mouse::MouseButton,
- _x: f32,
- _y: f32,
- ) {
- self.events.push(iced_native::Event::Mouse(
- iced_native::input::mouse::Event::Input {
- state: iced_native::input::ButtonState::Pressed,
- button: iced_native::input::mouse::Button::Left, // TODO: Map `button`
- },
- ));
- }
-
- fn mouse_button_up_event(
- &mut self,
- _context: &mut ggez::Context,
- _button: mouse::MouseButton,
- _x: f32,
- _y: f32,
- ) {
- self.events.push(iced_native::Event::Mouse(
- iced_native::input::mouse::Event::Input {
- state: iced_native::input::ButtonState::Released,
- button: iced_native::input::mouse::Button::Left, // TODO: Map `button`
- },
- ));
- }
-
- fn mouse_motion_event(
- &mut self,
- _context: &mut ggez::Context,
- x: f32,
- y: f32,
- _dx: f32,
- _dy: f32,
- ) {
- self.events.push(iced_native::Event::Mouse(
- iced_native::input::mouse::Event::CursorMoved { x, y },
- ));
- }
-
- fn resize_event(
- &mut self,
- context: &mut ggez::Context,
- width: f32,
- height: f32,
- ) {
- graphics::set_screen_coordinates(
- context,
- graphics::Rect {
- x: 0.0,
- y: 0.0,
- w: width,
- h: height,
- },
- )
- .expect("Set screen coordinates");
- }
-
- fn draw(&mut self, context: &mut ggez::Context) -> ggez::GameResult {
- graphics::clear(context, graphics::WHITE);
-
- let screen = graphics::screen_coordinates(context);
-
- let (messages, cursor) = {
- let view = self.tour.view();
-
- let content = iced_ggez::Column::new()
- .width(iced_native::Length::Units(screen.w as u16))
- .height(iced_native::Length::Units(screen.h as u16))
- .padding(20)
- .align_items(iced_native::Align::Center)
- .justify_content(iced_native::Justify::Center)
- .push(view);
-
- let renderer = &mut iced_ggez::Renderer::new(
- context,
- &mut self.images,
- self.spritesheet.clone(),
- self.font,
- );
-
- let mut ui = iced_native::UserInterface::build(
- content,
- self.cache.take().unwrap(),
- renderer,
- );
-
- let messages = ui.update(self.events.drain(..));
- let cursor = ui.draw(renderer);
-
- self.cache = Some(ui.into_cache());
-
- renderer.flush();
-
- (messages, cursor)
- };
-
- for message in messages {
- self.tour.update(message);
- }
-
- let cursor_type = into_cursor_type(cursor);
-
- if mouse::cursor_type(context) != cursor_type {
- mouse::set_cursor_type(context, cursor_type);
- }
-
- graphics::present(context)?;
- Ok(())
- }
-}
-
-fn into_cursor_type(cursor: iced_native::MouseCursor) -> mouse::MouseCursor {
- match cursor {
- iced_native::MouseCursor::OutOfBounds => mouse::MouseCursor::Default,
- iced_native::MouseCursor::Idle => mouse::MouseCursor::Default,
- iced_native::MouseCursor::Pointer => mouse::MouseCursor::Hand,
- iced_native::MouseCursor::Working => mouse::MouseCursor::Progress,
- iced_native::MouseCursor::Grab => mouse::MouseCursor::Grab,
- iced_native::MouseCursor::Grabbing => mouse::MouseCursor::Grabbing,
- }
-}
diff --git a/examples/tour/src/web.rs b/examples/tour/src/web.rs
deleted file mode 100644
index a0a3060f..00000000
--- a/examples/tour/src/web.rs
+++ /dev/null
@@ -1,33 +0,0 @@
-use futures::Future;
-use iced_web::UserInterface;
-use wasm_bindgen::prelude::*;
-
-use crate::tour::{self, Tour};
-
-#[wasm_bindgen(start)]
-pub fn run() {
- console_error_panic_hook::set_once();
- console_log::init_with_level(log::Level::Trace)
- .expect("Initialize logging");
-
- let tour = Tour::new();
-
- tour.run();
-}
-
-impl iced_web::UserInterface for Tour {
- type Message = tour::Message;
-
- fn update(
- &mut self,
- message: tour::Message,
- ) -> Option<Box<dyn Future<Output = tour::Message>>> {
- self.update(message);
-
- None
- }
-
- fn view(&mut self) -> iced_web::Element<tour::Message> {
- self.view()
- }
-}
diff --git a/examples/tour/src/widget.rs b/examples/tour/src/widget.rs
deleted file mode 100644
index 9c2c4d5b..00000000
--- a/examples/tour/src/widget.rs
+++ /dev/null
@@ -1,5 +0,0 @@
-#[cfg(target_arch = "wasm32")]
-pub use iced_web::*;
-
-#[cfg(not(target_arch = "wasm32"))]
-pub use crate::iced_ggez::*;