-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[Merged by Bors] - Relative cursor position #7199
Changes from all commits
71056a3
d607e6b
83f2417
6da0e41
3500039
d823b1b
f68eb33
6ed303e
23a22ae
a27f8b1
8513317
a81cb5a
0921e75
42699d1
ae57369
3ddd0c0
2b64fa6
9713cc8
32cde9a
b52f950
6a00611
b5d915e
2462874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//! Showcases the `RelativeCursorPosition` component, used to check the position of the cursor relative to a UI node. | ||
|
||
use bevy::{prelude::*, ui::RelativeCursorPosition, winit::WinitSettings}; | ||
|
||
fn main() { | ||
App::new() | ||
.add_plugins(DefaultPlugins) | ||
// Only run the app when there is user input. This will significantly reduce CPU/GPU use. | ||
.insert_resource(WinitSettings::desktop_app()) | ||
.add_startup_system(setup) | ||
.add_system(relative_cursor_position_system) | ||
.run(); | ||
} | ||
|
||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||
commands.spawn(Camera2dBundle::default()); | ||
|
||
commands | ||
.spawn(NodeBundle { | ||
style: Style { | ||
size: Size::new(Val::Percent(100.0), Val::Percent(100.0)), | ||
align_items: AlignItems::Center, | ||
justify_content: JustifyContent::Center, | ||
flex_direction: FlexDirection::Column, | ||
..default() | ||
}, | ||
..default() | ||
}) | ||
.with_children(|parent| { | ||
parent | ||
.spawn(NodeBundle { | ||
style: Style { | ||
size: Size::new(Val::Px(250.0), Val::Px(250.0)), | ||
margin: UiRect::new(Val::Px(0.), Val::Px(0.), Val::Px(0.), Val::Px(15.)), | ||
..default() | ||
}, | ||
background_color: Color::rgb(235., 35., 12.).into(), | ||
..default() | ||
}) | ||
.insert(RelativeCursorPosition::default()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should all node-bundles with some form of interaction have this component as default? I think it would make sense on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think that it's necessary. If someone needs this functionality they can always insert the component themselves, but I imagine most people won't use it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, having thought about it a bit more, it only makes sense to add this to "default" bundles when we are building some more functionality on top of this and that is a requirement. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, we should only add components to bundles when they have a built-in use. |
||
|
||
parent.spawn(TextBundle { | ||
text: Text::from_section( | ||
"(0.0, 0.0)", | ||
TextStyle { | ||
font: asset_server.load("fonts/FiraSans-Bold.ttf"), | ||
font_size: 40.0, | ||
color: Color::rgb(0.9, 0.9, 0.9), | ||
}, | ||
), | ||
..default() | ||
}); | ||
}); | ||
} | ||
|
||
/// This systems polls the relative cursor position and displays its value in a text component. | ||
fn relative_cursor_position_system( | ||
Pietrek14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
relative_cursor_position_query: Query<&RelativeCursorPosition>, | ||
mut output_query: Query<&mut Text>, | ||
) { | ||
let relative_cursor_position = relative_cursor_position_query.single(); | ||
|
||
let mut output = output_query.single_mut(); | ||
|
||
output.sections[0].value = | ||
if let Some(relative_cursor_position) = relative_cursor_position.normalized { | ||
format!( | ||
"({:.1}, {:.1})", | ||
relative_cursor_position.x, relative_cursor_position.y | ||
) | ||
} else { | ||
"unknown".to_string() | ||
}; | ||
|
||
output.sections[0].style.color = if relative_cursor_position.mouse_over() { | ||
Color::rgb(0.1, 0.9, 0.1) | ||
} else { | ||
Color::rgb(0.9, 0.1, 0.1) | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this, we would have all the required building-blocks aside from rendering to produce the press-ripples sometime down the line: https://material.angular.io/components/ripple/overview