Skip to content

Commit

Permalink
Port bevy_ui on pipelined_rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Davier committed Oct 15, 2021
1 parent 43e8a15 commit 6507ba9
Show file tree
Hide file tree
Showing 25 changed files with 2,761 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ default = [
"bevy_sprite2",
"bevy_render2",
"bevy_pbr2",
"bevy_ui2",
"bevy_winit",
"render",
"png",
Expand Down Expand Up @@ -64,6 +65,7 @@ bevy_render2 = ["bevy_internal/bevy_render2"]
bevy_sprite2 = ["bevy_internal/bevy_sprite2"]
bevy_pbr2 = ["bevy_internal/bevy_pbr2"]
bevy_gltf2 = ["bevy_internal/bevy_gltf2"]
bevy_ui2 = ["bevy_internal/bevy_ui2"]

trace_chrome = ["bevy_internal/trace_chrome"]
trace = ["bevy_internal/trace"]
Expand Down Expand Up @@ -502,6 +504,10 @@ path = "examples/ui/text_debug.rs"
name = "ui"
path = "examples/ui/ui.rs"

[[example]]
name = "ui_pipelined"
path = "examples/ui/ui_pipelined.rs"

# Window
[[example]]
name = "clear_color"
Expand Down
1 change: 1 addition & 0 deletions crates/bevy_internal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ bevy_sprite = { path = "../bevy_sprite", optional = true, version = "0.5.0" }
bevy_sprite2 = { path = "../../pipelined/bevy_sprite2", optional = true, version = "0.5.0" }
bevy_text = { path = "../bevy_text", optional = true, version = "0.5.0" }
bevy_ui = { path = "../bevy_ui", optional = true, version = "0.5.0" }
bevy_ui2 = { path = "../../pipelined/bevy_ui2", optional = true, version = "0.5.0" }
bevy_wgpu = { path = "../bevy_wgpu", optional = true, version = "0.5.0" }
bevy_winit = { path = "../bevy_winit", optional = true, version = "0.5.0" }
bevy_gilrs = { path = "../bevy_gilrs", optional = true, version = "0.5.0" }
Expand Down
3 changes: 3 additions & 0 deletions crates/bevy_internal/src/default_plugins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ impl PluginGroup for PipelinedDefaultPlugins {

#[cfg(feature = "bevy_gltf2")]
group.add(bevy_gltf2::GltfPlugin::default());

#[cfg(feature = "bevy_ui2")]
group.add(bevy_ui2::UiPlugin::default());
}

#[cfg(feature = "bevy_winit")]
Expand Down
6 changes: 6 additions & 0 deletions crates/bevy_internal/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ pub mod ui {
pub use bevy_ui::*;
}

#[cfg(feature = "bevy_ui2")]
pub mod ui2 {
//! User interface components and widgets.
pub use bevy_ui2::*;
}

#[cfg(feature = "bevy_winit")]
pub mod winit {
pub use bevy_winit::*;
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ Example | File | Description
`text` | [`ui/text.rs`](./ui/text.rs) | Illustrates creating and updating text
`text_debug` | [`ui/text_debug.rs`](./ui/text_debug.rs) | An example for debugging text layout
`ui` | [`ui/ui.rs`](./ui/ui.rs) | Illustrates various features of Bevy UI
`ui_pipelined` | [`ui/ui_pipelined.rs`](./ui/ui_pipelined.rs) | Illustrates various features of Bevy UI

## Window

Expand Down
222 changes: 222 additions & 0 deletions examples/ui/ui_pipelined.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
use bevy::{
prelude::{App, AssetServer, BuildChildren, Commands, Rect, Res, Size},
render2::{camera::OrthographicCameraBundle, color::Color},
ui2::{
entity::ImageBundle, entity::NodeBundle, entity::UiCameraBundle, AlignItems,
JustifyContent, PositionType, Style, Val,
},
PipelinedDefaultPlugins,
};

/// This example illustrates the various features of Bevy UI.
fn main() {
App::new()
.add_plugins(PipelinedDefaultPlugins)
.add_startup_system(setup)
.run();
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// ui camera
commands.spawn_bundle(UiCameraBundle::default());
// FIXME: this is needed to clear the background root node
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
justify_content: JustifyContent::SpaceBetween,
..Default::default()
},
color: Color::NONE,
..Default::default()
})
.with_children(|parent| {
// left vertical fill (border)
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(200.0), Val::Percent(100.0)),
border: Rect::all(Val::Px(2.0)),
..Default::default()
},
color: Color::rgb(0.65, 0.65, 0.65),
..Default::default()
})
.with_children(|parent| {
// left vertical fill (content)
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
align_items: AlignItems::FlexEnd,
..Default::default()
},
color: Color::rgb(0.15, 0.15, 0.15),
..Default::default()
})
.with_children(|_parent| {
// text
// parent.spawn_bundle(TextBundle {
// style: Style {
// margin: Rect::all(Val::Px(5.0)),
// ..Default::default()
// },
// text: Text::with_section(
// "Text Example",
// TextStyle {
// font: asset_server.load("fonts/FiraSans-Bold.ttf"),
// font_size: 30.0,
// color: Color::WHITE,
// },
// Default::default(),
// ),
// ..Default::default()
// });
});
});
// right vertical fill
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(200.0), Val::Percent(100.0)),
..Default::default()
},
color: Color::rgb(0.15, 0.15, 0.15),
..Default::default()
});
// absolute positioning
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(200.0), Val::Px(200.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(210.0),
bottom: Val::Px(10.0),
..Default::default()
},
border: Rect::all(Val::Px(20.0)),
..Default::default()
},
color: Color::rgb(0.4, 0.4, 1.0),
..Default::default()
})
.with_children(|parent| {
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
..Default::default()
},
color: Color::rgb(0.8, 0.8, 1.0),
..Default::default()
});
});
// render order test: reddest in the back, whitest in the front (flex center)
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
position_type: PositionType::Absolute,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..Default::default()
},
color: Color::NONE,
..Default::default()
})
.with_children(|parent| {
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
..Default::default()
},
color: Color::rgb(1.0, 0.0, 0.0),
..Default::default()
})
.with_children(|parent| {
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(20.0),
bottom: Val::Px(20.0),
..Default::default()
},
..Default::default()
},
color: Color::rgb(1.0, 0.3, 0.3),
..Default::default()
});
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(40.0),
bottom: Val::Px(40.0),
..Default::default()
},
..Default::default()
},
color: Color::rgb(1.0, 0.5, 0.5),
..Default::default()
});
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(60.0),
bottom: Val::Px(60.0),
..Default::default()
},
..Default::default()
},
color: Color::rgb(1.0, 0.7, 0.7),
..Default::default()
});
// alpha test
parent.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Px(100.0), Val::Px(100.0)),
position_type: PositionType::Absolute,
position: Rect {
left: Val::Px(80.0),
bottom: Val::Px(80.0),
..Default::default()
},
..Default::default()
},
color: Color::rgba(1.0, 0.9, 0.9, 0.4),
..Default::default()
});
});
});
// bevy logo (flex center)
parent
.spawn_bundle(NodeBundle {
style: Style {
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)),
position_type: PositionType::Absolute,
justify_content: JustifyContent::Center,
align_items: AlignItems::FlexEnd,
..Default::default()
},
color: Color::NONE,
..Default::default()
})
.with_children(|parent| {
// bevy logo (image)
parent.spawn_bundle(ImageBundle {
style: Style {
size: Size::new(Val::Px(500.0), Val::Auto),
..Default::default()
},
texture: Some(asset_server.load("branding/bevy_logo_dark_big.png")),
..Default::default()
});
});
});
}
39 changes: 39 additions & 0 deletions pipelined/bevy_ui2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "bevy_ui2"
version = "0.5.0"
edition = "2018"
authors = [
"Bevy Contributors <bevyengine@gmail.com>",
"Carter Anderson <mcanders1@gmail.com>",
]
description = "A custom ECS-driven UI framework built specifically for Bevy Engine"
homepage = "https://bevyengine.org"
repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]

[dependencies]
# bevy
bevy_app = { path = "../../crates/bevy_app", version = "0.5.0" }
bevy_asset = { path = "../../crates/bevy_asset", version = "0.5.0" }
bevy_core = { path = "../../crates/bevy_core", version = "0.5.0" }
bevy_core_pipeline = { path = "../bevy_core_pipeline", version = "0.5.0" }
bevy_derive = { path = "../../crates/bevy_derive", version = "0.5.0" }
bevy_ecs = { path = "../../crates/bevy_ecs", version = "0.5.0" }
bevy_input = { path = "../../crates/bevy_input", version = "0.5.0" }
bevy_log = { path = "../../crates/bevy_log", version = "0.5.0" }
bevy_math = { path = "../../crates/bevy_math", version = "0.5.0" }
bevy_reflect = { path = "../../crates/bevy_reflect", version = "0.5.0", features = ["bevy"] }
bevy_render2 = { path = "../bevy_render2", version = "0.5.0" }
#bevy_text = { path = "../../crates/bevy_text", version = "0.5.0" }
bevy_transform = { path = "../../crates/bevy_transform", version = "0.5.0" }
bevy_window = { path = "../../crates/bevy_window", version = "0.5.0" }
bevy_utils = { path = "../../crates/bevy_utils", version = "0.5.0" }

# other
stretch = "0.3.2"
serde = {version = "1", features = ["derive"]}
smallvec = { version = "1.6", features = ["union", "const_generics"] }
crevice = { path = "../../crates/crevice", version = "0.6.0" }
bytemuck = "1.5"
wgpu = "0.11.0"
46 changes: 46 additions & 0 deletions pipelined/bevy_ui2/src/anchors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#[derive(Debug, Clone)]
pub struct Anchors {
pub left: f32,
pub right: f32,
pub bottom: f32,
pub top: f32,
}

impl Anchors {
pub const BOTTOM_FULL: Anchors = Anchors::new(0.0, 1.0, 0.0, 0.0);
pub const BOTTOM_LEFT: Anchors = Anchors::new(0.0, 0.0, 0.0, 0.0);
pub const BOTTOM_RIGHT: Anchors = Anchors::new(1.0, 1.0, 0.0, 0.0);
pub const CENTER: Anchors = Anchors::new(0.5, 0.5, 0.5, 0.5);
pub const CENTER_BOTTOM: Anchors = Anchors::new(0.5, 0.5, 0.0, 0.0);
pub const CENTER_FULL_HORIZONTAL: Anchors = Anchors::new(0.0, 1.0, 0.5, 0.5);
pub const CENTER_FULL_VERTICAL: Anchors = Anchors::new(0.5, 0.5, 0.0, 1.0);
pub const CENTER_LEFT: Anchors = Anchors::new(0.0, 0.0, 0.5, 0.5);
pub const CENTER_RIGHT: Anchors = Anchors::new(1.0, 1.0, 0.5, 0.5);
pub const CENTER_TOP: Anchors = Anchors::new(0.5, 0.5, 1.0, 1.0);
pub const FULL: Anchors = Anchors::new(0.0, 1.0, 0.0, 1.0);
pub const LEFT_FULL: Anchors = Anchors::new(0.0, 0.0, 0.0, 1.0);
pub const RIGHT_FULL: Anchors = Anchors::new(1.0, 1.0, 0.0, 1.0);
pub const TOP_FULL: Anchors = Anchors::new(0.0, 1.0, 1.0, 1.0);
pub const TOP_LEFT: Anchors = Anchors::new(0.0, 0.0, 1.0, 1.0);
pub const TOP_RIGHT: Anchors = Anchors::new(1.0, 1.0, 1.0, 1.0);

pub const fn new(left: f32, right: f32, bottom: f32, top: f32) -> Self {
Anchors {
left,
right,
bottom,
top,
}
}
}

impl Default for Anchors {
fn default() -> Self {
Anchors {
left: 0.0,
right: 0.0,
bottom: 0.0,
top: 0.0,
}
}
}
Loading

0 comments on commit 6507ba9

Please sign in to comment.