Skip to content

Prepare examples for 0.15 #10

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
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ keywords = ["bevy", "http", "plugin", "wasm"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy_app = "0.14.0"
bevy_derive = "0.14.0"
bevy_hierarchy = "0.14.0"
bevy_ecs = { version = "0.14.0", features = ["multi_threaded"] }
bevy_tasks = "0.14.0"
bevy_app = "0.15.0-rc.1"
bevy_derive = "0.15.0-rc.1"
bevy_hierarchy = "0.15.0-rc.1"
bevy_ecs = { version = "0.15.0-rc.1", features = ["multi_threaded"] }
bevy_tasks = "0.15.0-rc.1"

crossbeam-channel = "0.5.11"
ehttp = { version = "0.5.0", features = ["native-async", "json"] }
Expand All @@ -27,7 +27,7 @@ serde_json = "1.0"
doctest = false

[dev-dependencies]
bevy = { version = "0.14.0", default-features = false, features = [
bevy = { version = "0.15.0-rc.1", default-features = false, features = [
"animation",
"bevy_asset",
"bevy_gilrs",
Expand Down
85 changes: 45 additions & 40 deletions examples/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use bevy_http_client::{

fn main() {
App::new()
.insert_resource(Msaa::Off)
.insert_resource(ClearColor(Color::srgb(0.4, 0.4, 0.4)))
.add_plugins(DefaultPlugins.set(WindowPlugin {
primary_window: Some(Window {
Expand All @@ -35,69 +34,75 @@ fn main() {
#[derive(Component)]
struct ResponseText;

#[derive(Component)]
struct ResponseIP;

fn setup(mut commands: Commands) {
// Camera
commands.spawn((Camera2dBundle::default(), IsDefaultUiCamera));

let text_section = move |color: Srgba, value: &str| {
TextSection::new(
value,
TextStyle {
font_size: 40.0,
color: color.into(),
..default()
},
)
};

commands.spawn((Camera2d, IsDefaultUiCamera));
commands
.spawn(NodeBundle {
style: Style {
.spawn((
Node {
position_type: PositionType::Absolute,
padding: UiRect::all(Val::Px(5.0)),
display: Display::Grid,
..default()
},
z_index: ZIndex::Global(i32::MAX),
background_color: Color::BLACK.with_alpha(0.75).into(),
..default()
})
.with_children(|c| {
c.spawn((
TextBundle::from_sections([
text_section(LIME, "Status: "),
text_section(AQUA, ""),
text_section(LIME, "\nIp: "),
text_section(AQUA, ""),
]),
ResponseText,
));
ZIndex(i32::MAX),
BackgroundColor(Color::BLACK.with_alpha(0.75)),
))
.with_children(|parent| {
let text_font = TextFont {
font_size: 40.,
..default()
};
parent.spawn(Node::default()).with_children(|parent| {
parent.spawn((
Text::new("Status: "),
TextColor(LIME.into()),
text_font.clone(),
));
parent.spawn((
Text::new(""),
TextColor(AQUA.into()),
text_font.clone(),
ResponseText,
));
});
parent.spawn(Node::default()).with_children(|parent| {
parent.spawn((Text::new("Ip: "), TextColor(LIME.into()), text_font.clone()));
parent.spawn((
Text::new(""),
TextColor(AQUA.into()),
text_font.clone(),
ResponseIP,
));
});
});
}

fn send_request(
mut ev_request: EventWriter<HttpRequest>,
mut query: Query<&mut Text, With<ResponseText>>,
mut status_query: Query<&mut Text, (With<ResponseText>, Without<ResponseIP>)>,
mut ip_query: Query<&mut Text, (With<ResponseIP>, Without<ResponseText>)>,
) {
let mut text = query.single_mut();
text.sections[1].value = "Requesting".to_string();
text.sections[3].value = "".to_string();
status_query.single_mut().0 = "Requesting ".to_string();
ip_query.single_mut().0 = "".to_string();
let request = HttpClient::new().get("https://api.ipify.org").build();
ev_request.send(request);
}

fn handle_response(
mut ev_resp: EventReader<HttpResponse>,
mut query: Query<&mut Text, With<ResponseText>>,
mut status_query: Query<&mut Text, (With<ResponseText>, Without<ResponseIP>)>,
mut ip_query: Query<&mut Text, (With<ResponseIP>, Without<ResponseText>)>,
) {
for response in ev_resp.read() {
let mut text = query.single_mut();
let ip = response.text().unwrap_or_default();

text.sections[1].value = "Got ".to_string();
text.sections[3].value = ip.to_string();
ip_query.single_mut().0 = ip.to_string();
status_query.single_mut().0 = "Got ".to_string();
}
}

fn handle_error(mut ev_error: EventReader<HttpResponseError>) {
for error in ev_error.read() {
println!("Error retrieving IP: {}", error.err);
Expand Down
Loading