Skip to content

Commit

Permalink
add windows example
Browse files Browse the repository at this point in the history
  • Loading branch information
foxzool committed Jul 6, 2024
1 parent 3639f23 commit 705bd7c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 9 deletions.
24 changes: 23 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,26 @@ serde_json = "1.0"
doctest = false

[dev-dependencies]
bevy = { version = "0.14.0", default-features = false, features = ["multi_threaded"] }
bevy = { version = "0.14.0", default-features = false, features = [
"animation",
"bevy_asset",
"bevy_gilrs",
"bevy_scene",
"bevy_winit",
"bevy_core_pipeline",
"bevy_pbr",
"bevy_gltf",
"bevy_render",
"bevy_sprite",
"bevy_text",
"bevy_ui",
"multi_threaded",
"png",
"hdr",
"x11",
"bevy_gizmos",
"tonemapping_luts",
"default_font",
"webgl2",
"sysinfo_plugin",
] }
105 changes: 105 additions & 0 deletions examples/window.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
use bevy::{
color::palettes::css::{AQUA, LIME},
prelude::*,
time::common_conditions::on_timer,
};
use bevy_http_client::{
HttpClient, HttpClientPlugin, HttpRequest, HttpResponse, HttpResponseError,
};

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 {
title: "Wasm http request".to_string(),
// Bind to canvas included in `index.html`
canvas: Some("#bevy".to_owned()),
// Tells wasm not to override default event handling, like F5 and Ctrl+R
prevent_default_event_handling: false,
..default()
}),
..default()
}))
.add_plugins(HttpClientPlugin)
.add_systems(Startup, setup)
.add_systems(
Update,
send_request.run_if(on_timer(std::time::Duration::from_secs(2))),
)
.add_systems(Update, (handle_response, handle_error))
.run();
}

#[derive(Component)]
struct ResponseText;

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(NodeBundle {
style: Style {
position_type: PositionType::Absolute,
padding: UiRect::all(Val::Px(5.0)),
..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,
));
});
}

fn send_request(
mut ev_request: EventWriter<HttpRequest>,
mut query: Query<&mut Text, With<ResponseText>>,
) {
let mut text = query.single_mut();
text.sections[1].value = "Requesting".to_string();
text.sections[3].value = "".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>>,
) {
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();
}
}

fn handle_error(mut ev_error: EventReader<HttpResponseError>) {
for error in ev_error.read() {
println!("Error retrieving IP: {}", error.err);
}
}
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#![doc = include_str!("../README.md")]


use bevy_ecs::world::CommandQueue;
use bevy_app::{App, Plugin, Update};
use bevy_derive::Deref;
use bevy_ecs::prelude::*;
use bevy_ecs::{prelude::*, world::CommandQueue};
use bevy_hierarchy::DespawnRecursiveExt;
use bevy_tasks::IoTaskPool;
use crossbeam_channel::Receiver;
Expand Down
8 changes: 3 additions & 5 deletions src/typed.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use bevy_ecs::world::CommandQueue;
use bevy_tasks::IoTaskPool;
use std::marker::PhantomData;
use bevy_app::{App, PreUpdate};
use bevy_derive::Deref;
use bevy_ecs::prelude::*;
use bevy_ecs::system::Commands;
use bevy_ecs::{prelude::*, system::Commands, world::CommandQueue};
use bevy_hierarchy::DespawnRecursiveExt;
use bevy_tasks::IoTaskPool;
use ehttp::{Request, Response};
use serde::Deserialize;
use std::marker::PhantomData;

use crate::{HttpClientSetting, RequestTask};

Expand Down

0 comments on commit 705bd7c

Please sign in to comment.