diff --git a/Cargo.toml b/Cargo.toml index 9f07a4e..292e305 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,4 +27,26 @@ serde_json = "1.0" doctest = false [dev-dependencies] -bevy = { version = "0.14.0", default-features = false, features = ["multi_threaded"] } \ No newline at end of file +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", +] } \ No newline at end of file diff --git a/examples/window.rs b/examples/window.rs new file mode 100644 index 0000000..4951783 --- /dev/null +++ b/examples/window.rs @@ -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, + mut query: Query<&mut Text, With>, +) { + 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, + mut query: Query<&mut Text, With>, +) { + 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) { + for error in ev_error.read() { + println!("Error retrieving IP: {}", error.err); + } +} diff --git a/src/lib.rs b/src/lib.rs index 1e55ff4..f24fe87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/typed.rs b/src/typed.rs index 02eae78..f100c34 100644 --- a/src/typed.rs +++ b/src/typed.rs @@ -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};