Skip to content

Commit 62e3c6e

Browse files
authored
Restore bevy_feathers features after merge with upstream (#41)
* Restore the panes in the feathers example * Migrate tool_button to use EntityCursor * Remove unused imports * Fix AlphaPattern with bsn! and simplify handle initialization
1 parent 40e6f12 commit 62e3c6e

File tree

8 files changed

+101
-59
lines changed

8 files changed

+101
-59
lines changed
Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
use bevy_app::Plugin;
2-
use bevy_asset::{Asset, Assets, Handle};
3-
use bevy_ecs::{
4-
component::Component,
5-
lifecycle::Add,
6-
observer::On,
7-
resource::Resource,
8-
system::{Query, Res},
9-
world::FromWorld,
10-
};
1+
use bevy_asset::{Asset, Assets};
2+
use bevy_ecs::{component::Component, lifecycle::HookContext, world::DeferredWorld};
113
use bevy_reflect::TypePath;
124
use bevy_render::render_resource::{AsBindGroup, ShaderRef};
135
use bevy_ui_render::ui_material::{MaterialNode, UiMaterial};
@@ -21,39 +13,28 @@ impl UiMaterial for AlphaPatternMaterial {
2113
}
2214
}
2315

24-
#[derive(Resource)]
25-
pub(crate) struct AlphaPatternResource(pub(crate) Handle<AlphaPatternMaterial>);
26-
27-
impl FromWorld for AlphaPatternResource {
28-
fn from_world(world: &mut bevy_ecs::world::World) -> Self {
29-
let mut ui_materials = world
30-
.get_resource_mut::<Assets<AlphaPatternMaterial>>()
31-
.unwrap();
32-
Self(ui_materials.add(AlphaPatternMaterial::default()))
33-
}
34-
}
35-
3616
/// Marker that tells us we want to fill in the [`MaterialNode`] with the alpha material.
3717
#[derive(Component, Default, Clone)]
18+
#[require(MaterialNode<AlphaPatternMaterial>)]
19+
#[component(on_add = on_add_alpha_pattern)]
3820
pub(crate) struct AlphaPattern;
3921

4022
/// Observer to fill in the material handle (since we don't have access to the materials asset
4123
/// in the template)
42-
fn on_add_alpha_pattern(
43-
ev: On<Add, AlphaPattern>,
44-
mut q_material_node: Query<&mut MaterialNode<AlphaPatternMaterial>>,
45-
r_material: Res<AlphaPatternResource>,
46-
) {
47-
if let Ok(mut material) = q_material_node.get_mut(ev.target()) {
48-
material.0 = r_material.0.clone();
49-
}
50-
}
51-
52-
/// Plugin which registers the systems for updating the button styles.
53-
pub struct AlphaPatternPlugin;
54-
55-
impl Plugin for AlphaPatternPlugin {
56-
fn build(&self, app: &mut bevy_app::App) {
57-
app.add_observer(on_add_alpha_pattern);
24+
fn on_add_alpha_pattern(mut world: DeferredWorld, context: HookContext) {
25+
let mut materials = world.resource_mut::<Assets<AlphaPatternMaterial>>();
26+
27+
let handle = if materials.is_empty() {
28+
materials.add(AlphaPatternMaterial::default())
29+
} else {
30+
let id = materials.iter().next().unwrap().0;
31+
materials.get_strong_handle(id).unwrap()
32+
};
33+
34+
if let Some(mut material) = world
35+
.entity_mut(context.entity)
36+
.get_mut::<MaterialNode<AlphaPatternMaterial>>()
37+
{
38+
material.0 = handle;
5839
}
5940
}

crates/bevy_feathers/src/controls/button.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{
2121
tokens,
2222
};
2323
use bevy_input_focus::tab_navigation::TabIndex;
24-
use bevy_winit::cursor::CursorIcon;
2524

2625
/// Color variants for buttons. This also functions as a component used by the dynamic styling
2726
/// system to identify which entities are buttons.
@@ -100,8 +99,7 @@ pub fn tool_button(props: ButtonProps) -> impl Scene {
10099
template_value(props.variant)
101100
template_value(props.corners.to_border_radius(3.0))
102101
Hovered
103-
// TODO: port CursonIcon to GetTemplate
104-
// CursorIcon::System(bevy_window::SystemCursorIcon::Pointer)
102+
EntityCursor::System(bevy_window::SystemCursorIcon::Pointer)
105103
TabIndex(0)
106104
ThemeBackgroundColor(tokens::BUTTON_BG)
107105
ThemeFontColor(tokens::BUTTON_TEXT)

crates/bevy_feathers/src/controls/color_swatch.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
use bevy_asset::Handle;
21
use bevy_color::Alpha;
32
use bevy_ecs::component::Component;
3+
use bevy_scene2::{bsn, Scene};
44
use bevy_ui::{BackgroundColor, BorderRadius, Node, PositionType, Val};
5-
use bevy_ui_render::ui_material::MaterialNode;
65

7-
use crate::{
8-
alpha_pattern::{AlphaPattern, AlphaPatternMaterial},
9-
constants::size,
10-
palette,
11-
};
12-
use bevy_scene2::{bsn, Scene};
6+
use crate::{alpha_pattern::AlphaPattern, constants::size, palette};
137

148
/// Marker identifying a color swatch.
159
#[derive(Component, Default, Clone)]
@@ -33,7 +27,6 @@ pub fn color_swatch() -> impl Scene {
3327
}
3428
ColorSwatch
3529
AlphaPattern
36-
MaterialNode::<AlphaPatternMaterial>(Handle::default())
3730
BorderRadius::all(Val::Px(5.0))
3831
[
3932
Node {

crates/bevy_feathers/src/controls/mod.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ pub use radio::{radio, RadioPlugin};
1515
pub use slider::{slider, SliderPlugin, SliderProps};
1616
pub use toggle_switch::{toggle_switch, ToggleSwitchPlugin, ToggleSwitchProps};
1717

18-
use crate::alpha_pattern::AlphaPatternPlugin;
19-
2018
/// Plugin which registers all `bevy_feathers` controls.
2119
pub struct ControlsPlugin;
2220

2321
impl Plugin for ControlsPlugin {
2422
fn build(&self, app: &mut bevy_app::App) {
2523
app.add_plugins((
26-
AlphaPatternPlugin,
2724
ButtonPlugin,
2825
CheckboxPlugin,
2926
RadioPlugin,

crates/bevy_feathers/src/controls/toggle_switch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use accesskit::Role;
22
use bevy_a11y::AccessibilityNode;
33
use bevy_app::{Plugin, PreUpdate};
4-
use bevy_core_widgets::{Callback, CallbackTemplate, CoreCheckbox, ValueChange};
4+
use bevy_core_widgets::{CallbackTemplate, CoreCheckbox, ValueChange};
55
use bevy_ecs::{
66
component::Component,
77
entity::Entity,

crates/bevy_feathers/src/cursor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use bevy_ecs::{
99
resource::Resource,
1010
schedule::IntoScheduleConfigs,
1111
system::{Commands, Query, Res},
12-
template::GetTemplate,
1312
VariantDefaults,
1413
};
1514
use bevy_picking::{hover::HoverMap, pointer::PointerId, PickingSystems};

crates/bevy_feathers/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use bevy_text::{TextColor, TextFont};
2525
use bevy_ui_render::UiMaterialPlugin;
2626

2727
use crate::{
28-
alpha_pattern::{AlphaPatternMaterial, AlphaPatternResource},
28+
alpha_pattern::AlphaPatternMaterial,
2929
controls::ControlsPlugin,
3030
cursor::{CursorIconPlugin, DefaultCursor, EntityCursor},
3131
theme::{ThemedText, UiTheme},
@@ -78,7 +78,5 @@ impl Plugin for FeathersPlugin {
7878
.add_observer(theme::on_changed_border)
7979
.add_observer(theme::on_changed_font_color)
8080
.add_observer(font_styles::on_changed_font);
81-
82-
app.init_resource::<AlphaPatternResource>();
8381
}
8482
}

examples/ui/feathers.rs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy::{
1212
subpane_header,
1313
},
1414
controls::{
15-
button, checkbox, color_swatch, radio, slider, toggle_switch, ButtonProps,
15+
button, checkbox, color_swatch, radio, slider, toggle_switch, tool_button, ButtonProps,
1616
ButtonVariant, CheckboxProps, SliderProps, ToggleSwitchProps,
1717
},
1818
dark_theme::create_dark_theme,
@@ -222,6 +222,82 @@ fn demo_root() -> impl Scene {
222222
SliderPrecision(2)
223223
),
224224
color_swatch(),
225+
],
226+
Node {
227+
display: Display::Flex,
228+
flex_direction: FlexDirection::Column,
229+
align_items: AlignItems::Stretch,
230+
justify_content: JustifyContent::Start,
231+
padding: UiRect::all(Val::Px(8.0)),
232+
row_gap: Val::Px(8.0),
233+
width: Val::Percent(30.),
234+
min_width: Val::Px(200.),
235+
} [
236+
(
237+
:subpane [
238+
:subpane_header [
239+
(Text("Left") ThemedText),
240+
(Text("Center") ThemedText),
241+
(Text("Right") ThemedText)
242+
],
243+
:subpane_body [
244+
(Text("Body") ThemedText),
245+
],
246+
]
247+
),
248+
(
249+
:pane [
250+
:pane_header [
251+
:tool_button(ButtonProps {
252+
variant: ButtonVariant::Selected,
253+
..default()
254+
}) [
255+
(Text("\u{0398}") ThemedText)
256+
],
257+
:pane_header_divider,
258+
:tool_button(ButtonProps{
259+
variant: ButtonVariant::Plain,
260+
..default()
261+
}) [
262+
(Text("\u{00BC}") ThemedText)
263+
],
264+
:tool_button(ButtonProps{
265+
variant: ButtonVariant::Plain,
266+
..default()
267+
}) [
268+
(Text("\u{00BD}") ThemedText)
269+
],
270+
:tool_button(ButtonProps{
271+
variant: ButtonVariant::Plain,
272+
..default()
273+
}) [
274+
(Text("\u{00BE}") ThemedText)
275+
],
276+
:pane_header_divider,
277+
:tool_button(ButtonProps{
278+
variant: ButtonVariant::Plain,
279+
..default()
280+
}) [
281+
(Text("\u{20AC}") ThemedText)
282+
],
283+
:flex_spacer,
284+
:tool_button(ButtonProps{
285+
variant: ButtonVariant::Plain,
286+
..default()
287+
}) [
288+
(Text("\u{00D7}") ThemedText)
289+
],
290+
],
291+
(
292+
:pane_body [
293+
(Text("Some") ThemedText),
294+
(Text("Content") ThemedText),
295+
(Text("Here") ThemedText),
296+
]
297+
BackgroundColor(palettes::tailwind::EMERALD_800)
298+
),
299+
]
300+
)
225301
]
226302
]
227303
}

0 commit comments

Comments
 (0)