Skip to content

Commit

Permalink
Merge pull request #10 from fallible-algebra/master
Browse files Browse the repository at this point in the history
Prepare examples for 0.15
  • Loading branch information
foxzool authored Oct 23, 2024
2 parents 4e8bd5e + 162d92b commit 30ed6bc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 46 deletions.
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

0 comments on commit 30ed6bc

Please sign in to comment.