-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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 UiImage offset property #5629
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
92034d3
Added offset field to UiImage
afonsolage 84e3fb9
Fixed examples
afonsolage 14312a0
Fixed ui image scaling
afonsolage 5681d06
added image button example
afonsolage 57ef2ee
Added examples on cargo and fixed doc comments
afonsolage 7e77da6
Fixed cargo build examples page
afonsolage 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
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,68 @@ | ||||||
//! This example illustrates how to create an imagem button that changes image offset based on its | ||||||
//! interaction state. | ||||||
|
||||||
use bevy::{prelude::*, sprite::Rect, winit::WinitSettings}; | ||||||
|
||||||
fn main() { | ||||||
App::new() | ||||||
// Change image filter to a pixel-art friendly | ||||||
.insert_resource(ImageSettings::default_nearest()) | ||||||
// Match the background color with base image color | ||||||
.insert_resource(ClearColor(Color::rgb(0.475, 0.239, 0.306))) | ||||||
.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(button_system) | ||||||
.run(); | ||||||
} | ||||||
|
||||||
// Image rect in pixels, inside the base image. | ||||||
// Values are using to built a rect with begin (X, Y) and end (X, Y) format | ||||||
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. Users would really benefit from some more advice on how these values were determined. |
||||||
const HOVERED_BUTTON_OFFSET: [f32; 4] = [23.0, 38.0, 36.0, 52.0]; | ||||||
const NORMAL_BUTTON_OFFSET: [f32; 4] = [7.0, 38.0, 20.0, 52.0]; | ||||||
const CLICKED_BUTTON_OFFSET: [f32; 4] = [39.0, 38.0, 52.0, 52.0]; | ||||||
|
||||||
fn button_system( | ||||||
mut interaction_query: Query< | ||||||
(&Interaction, &mut UiImage), | ||||||
(Changed<Interaction>, With<Button>), | ||||||
>, | ||||||
) { | ||||||
for (interaction, mut ui_image) in &mut interaction_query { | ||||||
let offset = match *interaction { | ||||||
Interaction::Hovered => NORMAL_BUTTON_OFFSET, | ||||||
Interaction::None => HOVERED_BUTTON_OFFSET, | ||||||
Interaction::Clicked => CLICKED_BUTTON_OFFSET, | ||||||
}; | ||||||
|
||||||
ui_image.offset = Rect { | ||||||
min: Vec2::new(offset[0], offset[1]), | ||||||
max: Vec2::new(offset[2], offset[3]), | ||||||
}; | ||||||
} | ||||||
} | ||||||
|
||||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { | ||||||
// ui camera | ||||||
commands.spawn_bundle(Camera2dBundle::default()); | ||||||
|
||||||
// image button | ||||||
commands.spawn_bundle(ButtonBundle { | ||||||
style: Style { | ||||||
size: Size::new(Val::Px(150.0), Val::Px(150.0)), | ||||||
// center button | ||||||
margin: UiRect::all(Val::Auto), | ||||||
// horizontally center child text | ||||||
justify_content: JustifyContent::Center, | ||||||
// vertically center child text | ||||||
align_items: AlignItems::Center, | ||||||
..default() | ||||||
}, | ||||||
// Default image has no offset, but that's OK since it'll be update it on button_system | ||||||
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
|
||||||
image: asset_server | ||||||
.load("textures/rpg/ui/generic-rpg-ui-inventario.png") | ||||||
.into(), | ||||||
..default() | ||||||
}); | ||||||
} |
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.