Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite and integrate ui module with iced #88

Open
wants to merge 2 commits into
base: improvement/new-winit-event-loop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ debug = []
image = "0.21"
nalgebra = "0.18"
rayon = "1.0"
stretch = "0.2"
twox-hash = "1.3"
iced = { version = "0.1.0-alpha", features = ["winit"] }
lyon_tessellation = "0.13"
gilrs = "0.7"
winit = "0.20.0-alpha3"
Expand Down
4 changes: 2 additions & 2 deletions examples/counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ impl UserInterface for Counter {

fn layout(&mut self, window: &Window) -> Element<Message> {
Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.align_items(Align::Center)
.justify_content(Justify::Center)
.spacing(20)
Expand Down
4 changes: 2 additions & 2 deletions examples/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ impl UserInterface for GamepadExample {

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.align_items(Align::Center)
.justify_content(Justify::Center)
.push(
Expand Down
24 changes: 8 additions & 16 deletions examples/image.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use coffee::graphics::{
Color, Frame, HorizontalAlignment, VerticalAlignment, Window,
WindowSettings, self,
self, Color, Frame, HorizontalAlignment, VerticalAlignment, Window,
WindowSettings,
};
use coffee::load::Task;
use coffee::ui::{
Align, Column, Element, Justify, Renderer, Text, UserInterface, Image,
Align, Column, Element, Image, Justify, Renderer, Text, UserInterface,
};
use coffee::{Game, Result, Timer};

Expand All @@ -27,11 +27,7 @@ impl Game for ImageScreen {

fn load(_window: &Window) -> Task<ImageScreen> {
graphics::Image::load("resources/ui.png")
.map(|image| {
ImageScreen {
image,
}
})
.map(|image| ImageScreen { image })
}

fn draw(&mut self, frame: &mut Frame, _timer: &Timer) {
Expand All @@ -48,13 +44,12 @@ impl UserInterface for ImageScreen {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _message: ()) {
}
fn react(&mut self, _message: ()) {}

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.align_items(Align::Center)
.justify_content(Justify::Center)
.spacing(20)
Expand All @@ -65,10 +60,7 @@ impl UserInterface for ImageScreen {
.horizontal_alignment(HorizontalAlignment::Center)
.vertical_alignment(VerticalAlignment::Center),
)
.push(
Image::new(&self.image)
.height(250)
)
.push(Image::new(&self.image).height(250))
.into()
}
}
4 changes: 2 additions & 2 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl UserInterface for InputExample {
));

Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.padding(20)
.align_items(Align::Center)
.justify_content(Justify::Center)
Expand Down
16 changes: 10 additions & 6 deletions examples/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,24 @@ impl Game for Example {
});

let mut mesh = Mesh::new();
let (x, y) = (
f32::from(frame.width()) / 4.0,
f32::from(frame.height()) / 2.0,
);

let shape = match self.shape {
ShapeOption::Rectangle => Shape::Rectangle(Rectangle {
x: frame.width() / 4.0 - 100.0,
y: frame.height() / 2.0 - 50.0,
x: x - 100.0,
y: y - 50.0,
width: 200.0,
height: 100.0,
}),
ShapeOption::Circle => Shape::Circle {
center: Point::new(frame.width() / 4.0, frame.height() / 2.0),
center: Point::new(x, y),
radius: self.radius,
},
ShapeOption::Ellipse => Shape::Ellipse {
center: Point::new(frame.width() / 4.0, frame.height() / 2.0),
center: Point::new(x, y),
horizontal_radius: self.radius,
vertical_radius: self.vertical_radius,
rotation: 0.0,
Expand Down Expand Up @@ -210,8 +214,8 @@ impl UserInterface for Example {
controls.push(color_sliders(&mut self.color_sliders, self.color));

Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.padding(20)
.align_items(Align::End)
.justify_content(Justify::SpaceBetween)
Expand Down
12 changes: 6 additions & 6 deletions examples/particles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl Particles {
const G: f32 = 6.674;
const CENTER_MASS: f32 = 200.0;

fn generate(max_x: f32, max_y: f32) -> Task<Vec<Particle>> {
fn generate(max_x: u16, max_y: u16) -> Task<Vec<Particle>> {
Task::succeed(move || {
let rng = &mut rand::thread_rng();

Expand Down Expand Up @@ -179,8 +179,8 @@ impl UserInterface for Particles {
Column::new()
.padding(20)
.spacing(20)
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.justify_content(Justify::End)
.push(Checkbox::new(
self.interpolate,
Expand All @@ -204,11 +204,11 @@ struct Particle {
}

impl Particle {
fn random<R: Rng>(max_x: f32, max_y: f32, rng: &mut R) -> Particle {
fn random<R: Rng>(max_x: u16, max_y: u16, rng: &mut R) -> Particle {
Particle {
position: Point::new(
rng.gen_range(0.0, max_x),
rng.gen_range(0.0, max_y),
f32::from(rng.gen_range(0, max_x)),
f32::from(rng.gen_range(0, max_y)),
),
velocity: Vector::new(0.0, 0.0),
acceleration: Vector::new(0.0, 0.0),
Expand Down
19 changes: 6 additions & 13 deletions examples/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use coffee::graphics::{
};
use coffee::load::Task;
use coffee::ui::{
Align, Column, Element, Justify, Renderer, Text,
UserInterface, ProgressBar,
Align, Column, Element, Justify, ProgressBar, Renderer, Text, UserInterface,
};
use coffee::{Game, Result, Timer};

Expand All @@ -27,9 +26,7 @@ impl Game for Progress {
type LoadingScreen = ();

fn load(_window: &Window) -> Task<Progress> {
Task::succeed(|| Progress {
value: 0.0,
})
Task::succeed(|| Progress { value: 0.0 })
}

fn draw(&mut self, frame: &mut Frame, timer: &Timer) {
Expand All @@ -53,13 +50,12 @@ impl UserInterface for Progress {
type Message = ();
type Renderer = Renderer;

fn react(&mut self, _message: ()) {
}
fn react(&mut self, _message: ()) {}

fn layout(&mut self, window: &Window) -> Element<()> {
Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.align_items(Align::Center)
.justify_content(Justify::Center)
.spacing(20)
Expand All @@ -70,10 +66,7 @@ impl UserInterface for Progress {
.horizontal_alignment(HorizontalAlignment::Center)
.vertical_alignment(VerticalAlignment::Center),
)
.push(
ProgressBar::new(self.value)
.width(400),
)
.push(ProgressBar::new(self.value).width(400))
.into()
}
}
4 changes: 2 additions & 2 deletions examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ impl UserInterface for Tour {
.push(controls);

Column::new()
.width(window.width() as u32)
.height(window.height() as u32)
.width(window.width())
.height(window.height())
.padding(20)
.align_items(Align::Center)
.justify_content(Justify::Center)
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ impl Canvas {
Target::with_transformation(
gpu,
self.drawable.target().clone(),
texture.width() as f32,
texture.height() as f32,
texture.width(),
texture.height(),
texture::Drawable::render_transformation(),
)
}
Expand Down
16 changes: 12 additions & 4 deletions src/graphics/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ impl Rectangle<f32> {
/// [`Point`]: type.Point.html
/// [`Rectangle`]: struct.Rectangle.html
pub fn center(&self) -> Point {
Point::new(
self.x + self.width / 2.0,
self.y + self.height / 2.0,
)
Point::new(self.x + self.width / 2.0, self.y + self.height / 2.0)
}
}

impl From<iced::Rectangle> for Rectangle<f32> {
fn from(rectangle: iced::Rectangle) -> Rectangle<f32> {
Rectangle {
x: rectangle.x,
y: rectangle.y,
width: rectangle.width,
height: rectangle.height,
}
}
}
13 changes: 8 additions & 5 deletions src/graphics/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,24 @@ impl<'a> Target<'a> {
pub(super) fn new(
gpu: &mut Gpu,
view: TargetView,
width: f32,
height: f32,
width: u16,
height: u16,
) -> Target<'_> {
Target {
gpu,
view,
transformation: Transformation::orthographic(width, height),
transformation: Transformation::orthographic(
f32::from(width),
f32::from(height),
),
}
}

pub(super) fn with_transformation(
gpu: &mut Gpu,
view: TargetView,
width: f32,
height: f32,
width: u16,
height: u16,
transformation: Transformation,
) -> Target<'_> {
let mut target = Self::new(gpu, view, width, height);
Expand Down
56 changes: 56 additions & 0 deletions src/graphics/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,59 @@ pub enum VerticalAlignment {
/// Align bottom
Bottom,
}

impl From<HorizontalAlignment> for iced::text::HorizontalAlignment {
fn from(
horizontal_alignment: HorizontalAlignment,
) -> iced::text::HorizontalAlignment {
match horizontal_alignment {
HorizontalAlignment::Left => iced::text::HorizontalAlignment::Left,
HorizontalAlignment::Center => {
iced::text::HorizontalAlignment::Center
}
HorizontalAlignment::Right => {
iced::text::HorizontalAlignment::Right
}
}
}
}

impl From<iced::text::HorizontalAlignment> for HorizontalAlignment {
fn from(
horizontal_alignment: iced::text::HorizontalAlignment,
) -> HorizontalAlignment {
match horizontal_alignment {
iced::text::HorizontalAlignment::Left => HorizontalAlignment::Left,
iced::text::HorizontalAlignment::Center => {
HorizontalAlignment::Center
}
iced::text::HorizontalAlignment::Right => {
HorizontalAlignment::Right
}
}
}
}

impl From<VerticalAlignment> for iced::text::VerticalAlignment {
fn from(
vertical_alignment: VerticalAlignment,
) -> iced::text::VerticalAlignment {
match vertical_alignment {
VerticalAlignment::Top => iced::text::VerticalAlignment::Top,
VerticalAlignment::Center => iced::text::VerticalAlignment::Center,
VerticalAlignment::Bottom => iced::text::VerticalAlignment::Bottom,
}
}
}

impl From<iced::text::VerticalAlignment> for VerticalAlignment {
fn from(
vertical_alignment: iced::text::VerticalAlignment,
) -> VerticalAlignment {
match vertical_alignment {
iced::text::VerticalAlignment::Top => VerticalAlignment::Top,
iced::text::VerticalAlignment::Center => VerticalAlignment::Center,
iced::text::VerticalAlignment::Bottom => VerticalAlignment::Bottom,
}
}
}
Loading