2024-10-15 23:38:35 +00:00
|
|
|
//! 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((
|
Merge Style properties into Node. Use ComputedNode for computed properties. (#15975)
# Objective
Continue improving the user experience of our UI Node API in the
direction specified by [Bevy's Next Generation Scene / UI
System](https://github.com/bevyengine/bevy/discussions/14437)
## Solution
As specified in the document above, merge `Style` fields into `Node`,
and move "computed Node fields" into `ComputedNode` (I chose this name
over something like `ComputedNodeLayout` because it currently contains
more than just layout info. If we want to break this up / rename these
concepts, lets do that in a separate PR). `Style` has been removed.
This accomplishes a number of goals:
## Ergonomics wins
Specifying both `Node` and `Style` is now no longer required for
non-default styles
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
## Conceptual clarity
`Style` was never a comprehensive "style sheet". It only defined "core"
style properties that all `Nodes` shared. Any "styled property" that
couldn't fit that mold had to be in a separate component. A "real" style
system would style properties _across_ components (`Node`, `Button`,
etc). We have plans to build a true style system (see the doc linked
above).
By moving the `Style` fields to `Node`, we fully embrace `Node` as the
driving concept and remove the "style system" confusion.
## Next Steps
* Consider identifying and splitting out "style properties that aren't
core to Node". This should not happen for Bevy 0.15.
---
## Migration Guide
Move any fields set on `Style` into `Node` and replace all `Style`
component usage with `Node`.
Before:
```rust
commands.spawn((
Node::default(),
Style {
width: Val::Px(100.),
..default()
},
));
```
After:
```rust
commands.spawn(Node {
width: Val::Px(100.),
..default()
});
```
For any usage of the "computed node properties" that used to live on
`Node`, use `ComputedNode` instead:
Before:
```rust
fn system(nodes: Query<&Node>) {
for node in &nodes {
let computed_size = node.size();
}
}
```
After:
```rust
fn system(computed_nodes: Query<&ComputedNode>) {
for computed_node in &computed_nodes {
let computed_size = computed_node.size();
}
}
```
2024-10-18 22:25:33 +00:00
|
|
|
Node {
|
Migrate UI bundles to required components (#15898)
# Objective
- Migrate UI bundles to required components, fixes #15889
## Solution
- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`
## Testing
CI.
## Migration Guide
- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
commands
- .spawn(NodeBundle {
- style: Style {
+ .spawn((
+ Node::default(),
+ Style {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
- ..default()
- })
+ ))
```
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
.spawn((
- ButtonBundle {
- style: Style {
- width: Val::Px(w),
- height: Val::Px(h),
- // horizontally center child text
- justify_content: JustifyContent::Center,
- // vertically center child text
- align_items: AlignItems::Center,
- margin: UiRect::all(Val::Px(20.0)),
- ..default()
- },
- image: image.clone().into(),
+ Button,
+ Style {
+ width: Val::Px(w),
+ height: Val::Px(h),
+ // horizontally center child text
+ justify_content: JustifyContent::Center,
+ // vertically center child text
+ align_items: AlignItems::Center,
+ margin: UiRect::all(Val::Px(20.0)),
..default()
},
+ UiImage::from(image.clone()),
ImageScaleMode::Sliced(slicer.clone()),
))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
- commands.spawn(ImageBundle {
- image: UiImage {
+ commands.spawn((
+ UiImage {
texture: metering_mask,
..default()
},
- style: Style {
+ Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
- ..default()
- });
+ ));
```
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
padding: UiRect::all(Val::Px(5.0)),
|
2024-10-15 23:38:35 +00:00
|
|
|
..default()
|
|
|
|
},
|
Migrate UI bundles to required components (#15898)
# Objective
- Migrate UI bundles to required components, fixes #15889
## Solution
- deprecate `NodeBundle` in favor of `Node`
- deprecate `ImageBundle` in favor of `UiImage`
- deprecate `ButtonBundle` in favor of `Button`
## Testing
CI.
## Migration Guide
- Replace all uses of `NodeBundle` with `Node`. e.g.
```diff
commands
- .spawn(NodeBundle {
- style: Style {
+ .spawn((
+ Node::default(),
+ Style {
width: Val::Percent(100.),
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
..default()
},
- ..default()
- })
+ ))
```
- Replace all uses of `ButtonBundle` with `Button`. e.g.
```diff
.spawn((
- ButtonBundle {
- style: Style {
- width: Val::Px(w),
- height: Val::Px(h),
- // horizontally center child text
- justify_content: JustifyContent::Center,
- // vertically center child text
- align_items: AlignItems::Center,
- margin: UiRect::all(Val::Px(20.0)),
- ..default()
- },
- image: image.clone().into(),
+ Button,
+ Style {
+ width: Val::Px(w),
+ height: Val::Px(h),
+ // horizontally center child text
+ justify_content: JustifyContent::Center,
+ // vertically center child text
+ align_items: AlignItems::Center,
+ margin: UiRect::all(Val::Px(20.0)),
..default()
},
+ UiImage::from(image.clone()),
ImageScaleMode::Sliced(slicer.clone()),
))
```
- Replace all uses of `ImageBundle` with `UiImage`. e.g.
```diff
- commands.spawn(ImageBundle {
- image: UiImage {
+ commands.spawn((
+ UiImage {
texture: metering_mask,
..default()
},
- style: Style {
+ Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
..default()
},
- ..default()
- });
+ ));
```
---------
Co-authored-by: Carter Anderson <mcanders1@gmail.com>
2024-10-17 21:11:02 +00:00
|
|
|
BackgroundColor(Color::BLACK.with_alpha(0.75)),
|
2024-10-15 23:38:35 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|