-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add window drag move and drag resize without decoration example. #15814
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
Changes from 8 commits
41311e6
e39fc92
8a2eb58
5f4f0eb
853dcef
9e8c891
5f4952a
c08e217
0c37250
d4c91b4
e78bb49
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,140 @@ | ||||||
| //! This example illustrates drag move and drag resize without window | ||||||
| //! decorations. | ||||||
| //! | ||||||
| //! When window decorations are not present, the user cannot drag the window. | ||||||
| //! The `start_drag_move()` function will permit the application to make the | ||||||
| //! window draggable. It does require that the left mouse button was pressed | ||||||
|
Contributor
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. is being pressed sounds better to me. Currently the tenses are conflicting. |
||||||
| //! when it is called. | ||||||
|
Contributor
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. When (inconsistent capitalization with other comments)
Contributor
Author
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 apologize. I don't quite follow this suggestion. I have rewritten that paragraph. Please see if your comment still applies. |
||||||
| use bevy::{math::CompassOctant, prelude::*}; | ||||||
|
|
||||||
| /// Determine what do on left click. | ||||||
| #[derive(Resource, Debug)] | ||||||
| enum LeftClickAction { | ||||||
| /// Do nothing. | ||||||
| Nothing, | ||||||
| /// Drag the window on left click. | ||||||
| Drag, | ||||||
| /// Resize the window on left click. | ||||||
| Resize, | ||||||
| } | ||||||
|
|
||||||
| /// What direction index should the window resize toward. | ||||||
| #[derive(Resource)] | ||||||
| struct ResizeDir(usize); | ||||||
|
|
||||||
| /// Directions that the drag resizes the window toward. | ||||||
| const DIRECTIONS: [CompassOctant; 8] = [ | ||||||
| CompassOctant::North, | ||||||
| CompassOctant::NorthEast, | ||||||
| CompassOctant::East, | ||||||
| CompassOctant::SouthEast, | ||||||
| CompassOctant::South, | ||||||
| CompassOctant::SouthWest, | ||||||
| CompassOctant::West, | ||||||
| CompassOctant::NorthWest, | ||||||
| ]; | ||||||
|
|
||||||
| fn main() { | ||||||
| App::new() | ||||||
| .add_plugins(DefaultPlugins.set(WindowPlugin { | ||||||
| primary_window: Some(Window { | ||||||
| decorations: false, | ||||||
| ..default() | ||||||
| }), | ||||||
| ..default() | ||||||
| })) | ||||||
| .insert_resource(ResizeDir(7)) | ||||||
| .insert_resource(LeftClickAction::Drag) | ||||||
| .add_systems(Startup, setup) | ||||||
| .add_systems(Update, (handle_input, move_windows)) | ||||||
| .run(); | ||||||
| } | ||||||
|
|
||||||
| fn setup(mut commands: Commands) { | ||||||
| // Camera | ||||||
| commands.spawn(Camera3d::default()); | ||||||
|
|
||||||
| // UI | ||||||
| commands | ||||||
| .spawn(( | ||||||
| NodeBundle { | ||||||
| style: Style { | ||||||
| position_type: PositionType::Absolute, | ||||||
| padding: UiRect::all(Val::Px(5.0)), | ||||||
| ..default() | ||||||
| }, | ||||||
| background_color: Color::BLACK.with_alpha(0.75).into(), | ||||||
| ..default() | ||||||
| }, | ||||||
| GlobalZIndex(i32::MAX), | ||||||
| )) | ||||||
| .with_children(|p| { | ||||||
| p.spawn(Text::default()).with_children(|p| { | ||||||
| p.spawn(TextSpan::new( | ||||||
| "Demonstrate drag move and drag resize without window decorations.\n\n", | ||||||
| )); | ||||||
| p.spawn(TextSpan::new("Controls:\n")); | ||||||
| p.spawn(TextSpan::new("A - change left click action [")); | ||||||
| p.spawn(TextSpan::new("Drag")); | ||||||
| p.spawn(TextSpan::new("]\n")); | ||||||
| p.spawn(TextSpan::new("S / D - change resize direction [")); | ||||||
| p.spawn(TextSpan::new("NorthWest")); | ||||||
| p.spawn(TextSpan::new("]\n")); | ||||||
| }); | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| fn handle_input( | ||||||
| input: Res<ButtonInput<KeyCode>>, | ||||||
| mut action: ResMut<LeftClickAction>, | ||||||
| mut dir: ResMut<ResizeDir>, | ||||||
| example_text: Query<Entity, With<Text>>, | ||||||
| mut writer: UiTextWriter, | ||||||
| ) { | ||||||
| use LeftClickAction::*; | ||||||
| if input.just_pressed(KeyCode::KeyA) { | ||||||
| *action = match *action { | ||||||
| Drag => Resize, | ||||||
| Resize => Nothing, | ||||||
| Nothing => Drag, | ||||||
| }; | ||||||
| *writer.text(example_text.single(), 4) = format!("{:?}", *action); | ||||||
| } | ||||||
|
|
||||||
| if input.just_pressed(KeyCode::KeyS) { | ||||||
| dir.0 = dir | ||||||
| .0 | ||||||
| .checked_sub(1) | ||||||
| .unwrap_or(DIRECTIONS.len().saturating_sub(1)); | ||||||
| *writer.text(example_text.single(), 7) = format!("{:?}", DIRECTIONS[dir.0]); | ||||||
| } | ||||||
|
|
||||||
| if input.just_pressed(KeyCode::KeyD) { | ||||||
| dir.0 = (dir.0 + 1) % DIRECTIONS.len(); | ||||||
| *writer.text(example_text.single(), 7) = format!("{:?}", DIRECTIONS[dir.0]); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn move_windows( | ||||||
|
Contributor
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.
Suggested change
|
||||||
| mut windows: Query<&mut Window>, | ||||||
| action: Res<LeftClickAction>, | ||||||
| input: Res<ButtonInput<MouseButton>>, | ||||||
| dir: Res<ResizeDir>, | ||||||
| ) { | ||||||
| // Both `start_drag_move()` and `start_drag_resize()` must be called after a | ||||||
| // left mouse button press as done here. | ||||||
| // | ||||||
| // winit 0.30.5 may panic when initiated without a left mouse button press. | ||||||
| if input.just_pressed(MouseButton::Left) { | ||||||
| for mut window in windows.iter_mut() { | ||||||
| match *action { | ||||||
| LeftClickAction::Nothing => (), | ||||||
| LeftClickAction::Drag => window.start_drag_move(), | ||||||
|
Contributor
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.
Suggested change
this would be more consistent with |
||||||
| LeftClickAction::Resize => { | ||||||
| let d = DIRECTIONS[dir.0]; | ||||||
| window.start_drag_resize(d); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
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.
I like this idea of using the existing
CompassOctant😄