-
-
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
Add window drag move and drag resize without decoration example. #15814
Merged
alice-i-cecile
merged 11 commits into
bevyengine:main
from
shanecelis:window-move-resize-eg
Oct 15, 2024
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
41311e6
feature: Add window move resize example.
shanecelis e39fc92
refactor: Use CompassOctant not ResizeDirection.
shanecelis 8a2eb58
refactor: Use new text span and writer APIs.
shanecelis 5f4f0eb
chore: Removed unused variable.
shanecelis 853dcef
doc: Fix wording.
shanecelis 9e8c891
doc: Better description in cargo.toml.
shanecelis 5f4952a
doc: Build templated pages for examples.
shanecelis c08e217
excise: Remove errant newline.
shanecelis 0c37250
doc: Take into account feedback.
shanecelis d4c91b4
Merge branch 'main' into window-move-resize-eg
alice-i-cecile e78bb49
refactor: Use TextUiWriter.
shanecelis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//! This example illustrates drag move and drag resize without window | ||
//! decorations. | ||
//! | ||
//! When window decorations are not present, the user cannot drag a window by | ||
//! its titlebar to change its position. The `start_drag_move()` function | ||
//! permits a users to drag a window by left clicking anywhere in the window; | ||
//! left click must be pressed and other constraints can be imposed. For | ||
//! instance an application could require a user to hold down alt and left click | ||
//! to drag a window. | ||
//! | ||
//! The `start_drag_resize()` function behaves similarly but permits a window to | ||
//! be resized. | ||
use bevy::{math::CompassOctant, prelude::*}; | ||
|
||
/// Determine what do on left click. | ||
#[derive(Resource, Debug)] | ||
enum LeftClickAction { | ||
/// Do nothing. | ||
Nothing, | ||
/// Move the window on left click. | ||
Move, | ||
/// 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::Move) | ||
.add_systems(Startup, setup) | ||
.add_systems(Update, (handle_input, move_or_resize_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("Move")); | ||
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: TextUiWriter, | ||
) { | ||
use LeftClickAction::*; | ||
if input.just_pressed(KeyCode::KeyA) { | ||
*action = match *action { | ||
Move => Resize, | ||
Resize => Nothing, | ||
Nothing => Move, | ||
}; | ||
*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_or_resize_windows( | ||
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::Move => window.start_drag_move(), | ||
LeftClickAction::Resize => { | ||
let d = DIRECTIONS[dir.0]; | ||
window.start_drag_resize(d); | ||
} | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
😄