@@ -9,7 +9,6 @@ use bevy_http_client::{
9
9
10
10
fn main ( ) {
11
11
App :: new ( )
12
- . insert_resource ( Msaa :: Off )
13
12
. insert_resource ( ClearColor ( Color :: srgb ( 0.4 , 0.4 , 0.4 ) ) )
14
13
. add_plugins ( DefaultPlugins . set ( WindowPlugin {
15
14
primary_window : Some ( Window {
@@ -35,69 +34,75 @@ fn main() {
35
34
#[ derive( Component ) ]
36
35
struct ResponseText ;
37
36
37
+ #[ derive( Component ) ]
38
+ struct ResponseIP ;
39
+
38
40
fn setup ( mut commands : Commands ) {
39
41
// Camera
40
- commands. spawn ( ( Camera2dBundle :: default ( ) , IsDefaultUiCamera ) ) ;
41
-
42
- let text_section = move |color : Srgba , value : & str | {
43
- TextSection :: new (
44
- value,
45
- TextStyle {
46
- font_size : 40.0 ,
47
- color : color. into ( ) ,
48
- ..default ( )
49
- } ,
50
- )
51
- } ;
52
-
42
+ commands. spawn ( ( Camera2d , IsDefaultUiCamera ) ) ;
53
43
commands
54
- . spawn ( NodeBundle {
55
- style : Style {
44
+ . spawn ( (
45
+ Node {
56
46
position_type : PositionType :: Absolute ,
57
47
padding : UiRect :: all ( Val :: Px ( 5.0 ) ) ,
48
+ display : Display :: Grid ,
58
49
..default ( )
59
50
} ,
60
- z_index : ZIndex :: Global ( i32:: MAX ) ,
61
- background_color : Color :: BLACK . with_alpha ( 0.75 ) . into ( ) ,
62
- ..default ( )
63
- } )
64
- . with_children ( |c| {
65
- c. spawn ( (
66
- TextBundle :: from_sections ( [
67
- text_section ( LIME , "Status: " ) ,
68
- text_section ( AQUA , "" ) ,
69
- text_section ( LIME , "\n Ip: " ) ,
70
- text_section ( AQUA , "" ) ,
71
- ] ) ,
72
- ResponseText ,
73
- ) ) ;
51
+ ZIndex ( i32:: MAX ) ,
52
+ BackgroundColor ( Color :: BLACK . with_alpha ( 0.75 ) ) ,
53
+ ) )
54
+ . with_children ( |parent| {
55
+ let text_font = TextFont {
56
+ font_size : 40. ,
57
+ ..default ( )
58
+ } ;
59
+ parent. spawn ( Node :: default ( ) ) . with_children ( |parent| {
60
+ parent. spawn ( (
61
+ Text :: new ( "Status: " ) ,
62
+ TextColor ( LIME . into ( ) ) ,
63
+ text_font. clone ( ) ,
64
+ ) ) ;
65
+ parent. spawn ( (
66
+ Text :: new ( "" ) ,
67
+ TextColor ( AQUA . into ( ) ) ,
68
+ text_font. clone ( ) ,
69
+ ResponseText ,
70
+ ) ) ;
71
+ } ) ;
72
+ parent. spawn ( Node :: default ( ) ) . with_children ( |parent| {
73
+ parent. spawn ( ( Text :: new ( "Ip: " ) , TextColor ( LIME . into ( ) ) , text_font. clone ( ) ) ) ;
74
+ parent. spawn ( (
75
+ Text :: new ( "" ) ,
76
+ TextColor ( AQUA . into ( ) ) ,
77
+ text_font. clone ( ) ,
78
+ ResponseIP ,
79
+ ) ) ;
80
+ } ) ;
74
81
} ) ;
75
82
}
76
83
77
84
fn send_request (
78
85
mut ev_request : EventWriter < HttpRequest > ,
79
- mut query : Query < & mut Text , With < ResponseText > > ,
86
+ mut status_query : Query < & mut Text , ( With < ResponseText > , Without < ResponseIP > ) > ,
87
+ mut ip_query : Query < & mut Text , ( With < ResponseIP > , Without < ResponseText > ) > ,
80
88
) {
81
- let mut text = query. single_mut ( ) ;
82
- text. sections [ 1 ] . value = "Requesting" . to_string ( ) ;
83
- text. sections [ 3 ] . value = "" . to_string ( ) ;
89
+ status_query. single_mut ( ) . 0 = "Requesting " . to_string ( ) ;
90
+ ip_query. single_mut ( ) . 0 = "" . to_string ( ) ;
84
91
let request = HttpClient :: new ( ) . get ( "https://api.ipify.org" ) . build ( ) ;
85
92
ev_request. send ( request) ;
86
93
}
87
94
88
95
fn handle_response (
89
96
mut ev_resp : EventReader < HttpResponse > ,
90
- mut query : Query < & mut Text , With < ResponseText > > ,
97
+ mut status_query : Query < & mut Text , ( With < ResponseText > , Without < ResponseIP > ) > ,
98
+ mut ip_query : Query < & mut Text , ( With < ResponseIP > , Without < ResponseText > ) > ,
91
99
) {
92
100
for response in ev_resp. read ( ) {
93
- let mut text = query. single_mut ( ) ;
94
101
let ip = response. text ( ) . unwrap_or_default ( ) ;
95
-
96
- text. sections [ 1 ] . value = "Got " . to_string ( ) ;
97
- text. sections [ 3 ] . value = ip. to_string ( ) ;
102
+ ip_query. single_mut ( ) . 0 = ip. to_string ( ) ;
103
+ status_query. single_mut ( ) . 0 = "Got " . to_string ( ) ;
98
104
}
99
105
}
100
-
101
106
fn handle_error ( mut ev_error : EventReader < HttpResponseError > ) {
102
107
for error in ev_error. read ( ) {
103
108
println ! ( "Error retrieving IP: {}" , error. err) ;
0 commit comments