Skip to content

Commit

Permalink
Merge pull request #3 from mintlu8/async
Browse files Browse the repository at this point in the history
Added support for async systems, replacing event handlers and mutation.
  • Loading branch information
mintlu8 committed Jan 24, 2024
2 parents 16cbe33 + a4befd6 commit 0821795
Show file tree
Hide file tree
Showing 141 changed files with 6,960 additions and 4,502 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ Cargo.lock

flamegraph.svg

.vscode
.vscode

.idea
15 changes: 0 additions & 15 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[workspace]
members = [
"aoui",
"shapes",
# "shapes",
"matui"
]
resolver="2"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ which is a combination of `Commands`, `AssetServer` and

### child

`child:` is a special field that can be repeated, it accepts an `Entity`,
an iterator of `Entity` or `&Entity` and inserts it/them as a child/children.
`child:` is a special field that can be repeated, it accepts an `Entity`, `Option<Entity>`
or an iterator of `Entity`/`&Entity`,
and inserts it/them as a child/children.

```rust
frame! (commands {
Expand Down
2 changes: 2 additions & 0 deletions aoui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ xi-unicode = "^0.3"
thiserror = "^1"
once_cell = "^1.19"
atomic = "^0.6"
futures = "^0.3.30"
parking_lot = "^0.12"


[features]
Expand Down
5 changes: 3 additions & 2 deletions aoui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ which is a combination of `Commands`, `AssetServer` and

### child

`child:` is a special field that can be repeated, it accepts an `Entity`,
an iterator of `Entity` or `&Entity` and inserts it/them as a child/children.
`child:` is a special field that can be repeated, it accepts an `Entity`, `Option<Entity>`
or an iterator of `Entity`/`&Entity`,
and inserts it/them as a child/children.

```rust
frame! (commands {
Expand Down
2 changes: 1 addition & 1 deletion aoui/examples/anchor_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use std::f32::consts::PI;

use bevy_aoui::{*, DimensionMut, dsl::AouiCommands};
use bevy_aoui::{*, DimensionMut, util::AouiCommands};
use bevy::prelude::*;
use bevy_egui::{EguiContexts, egui::{self, Slider, ComboBox, Ui}, EguiPlugin};

Expand Down
2 changes: 1 addition & 1 deletion aoui/examples/aspect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple test case for percentage size.
use bevy::prelude::*;
use bevy_aoui::{AouiPlugin, dsl::AouiCommands};
use bevy_aoui::{AouiPlugin, util::AouiCommands};

pub fn main() {
App::new()
Expand Down
11 changes: 6 additions & 5 deletions aoui/examples/atlas.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::asset::AssetLoader;
use bevy::{prelude::*, diagnostic::FrameTimeDiagnosticsPlugin};
use bevy_aoui::WorldExtension;
use bevy_aoui::util::WorldExtension;
use bevy_aoui::AouiPlugin;
use bevy_aoui::dsl::AouiCommands;
use bevy_aoui::util::AouiCommands;

pub fn main() {
App::new()
Expand Down Expand Up @@ -72,9 +72,10 @@ pub fn init(mut commands: AouiCommands) {
anchor: TopRight,
text: "FPS: 0.00",
color: color!(gold),
extra: fps_signal(|fps: f32, text: &mut Text| {
format_widget!(text, "FPS: {:.2}", fps);
})
system: |fps: Fps, text: Ac<Text>| {
let fps = fps.get().await;
text.set(move |text| format_widget!(text, "FPS: {:.2}", fps)).await?;
}
});

vstack!(commands {
Expand Down
2 changes: 1 addition & 1 deletion aoui/examples/bisection.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A simple test case for percentage size.
use bevy::prelude::*;
use bevy_aoui::{AouiPlugin, dsl::AouiCommands};
use bevy_aoui::{AouiPlugin, util::AouiCommands};

pub fn main() {
App::new()
Expand Down
73 changes: 45 additions & 28 deletions aoui/examples/buttons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#![recursion_limit = "256"]
use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
use bevy::prelude::*;
use bevy_aoui::sync::Signal;
use bevy_aoui::AouiPlugin;
use bevy_aoui::WorldExtension;
use bevy_aoui::dsl::AouiCommands;
use bevy_aoui::signals::Invoke;
use bevy_aoui::signals::ReceiveInvoke;
use bevy_aoui::util::Object;
use bevy_aoui::util::WorldExtension;
use bevy_aoui::util::AouiCommands;

pub fn main() {
App::new()
Expand All @@ -26,15 +26,11 @@ pub fn main() {
}

#[derive(Debug, Component)]
pub struct Listen(Invoke<Listen>);

impl ReceiveInvoke for Listen {
type Type = ();
}
pub struct Listen(Signal<Object>);

pub fn recv(query: Query<&Listen>) {
for item in query.iter() {
if item.0.poll_any() {
if item.0.try_read().is_some() {
println!("Signal is received!");
}
}
Expand All @@ -48,14 +44,14 @@ pub fn init(mut commands: AouiCommands) {
anchor: TopRight,
text: "FPS: 0.00",
color: color!(gold),
extra: fps_signal(|fps: f32, text: &mut Text| {
format_widget!(text, "FPS: {:.2}", fps);
})
system: |fps: Fps, text: Ac<Text>| {
let fps = fps.get().await;
text.set(move |text| format_widget!(text, "FPS: {:.2}", fps)).await?;
}
});


let (send1, recv1) = commands.signal();
let (send2, recv2) = commands.signal();
let (send1, recv1) = signal();
let (send2, recv2) = signal();

vstack!(commands {
offset: [0, 100],
Expand Down Expand Up @@ -108,24 +104,37 @@ pub fn init(mut commands: AouiCommands) {
offset: [300, 120],
color: color!(gold),
text: "<= true!",
extra: recv1.recv0(|x: bool, text: &mut Text| format_widget!(text, "<= {}!", x))
signal: receiver::<ToggleChange>(recv1),
system: |toggle: SigRecv<ToggleChange>, text: Aeq<&mut Text>| {
let checked = toggle.recv().await;
text.set(move |mut text| format_widget!(text.as_mut(), "<= {}!", checked)).await;
}
});

text! (commands {
offset: [300, 80],
color: color!(gold),
text: "<= false!",
extra: recv2.recv0(|x: bool, text: &mut Text| format_widget!(text, "<= {}!", x))
signal: receiver::<ToggleChange>(recv2),
system: |x: SigRecv<ToggleChange>, text: Aeq<&mut Text>| {
let checked = x.recv().await;
text.set(move |mut text| format_widget!(text.as_mut(), "<= {}!", checked)).await;
}
});

let ctx = radio_button_group::<[_; 4]>("Fire");
let sig = ctx[0].recv();
let elements = ["Fire", "Water", "Earth", "Wind"];

text! (commands {
offset: [300, -100],
color: color!(gold),
text: "<= This reflects the value of the radio button.",
extra: sig.recv0(|x: &str, text: &mut Text| format_widget!(text, "<= has value {}!", x))
text: "<= Click the radio button and this will change!",
signal: receiver::<FormatTextStatic>(sig),
system: |x: SigRecv<FormatTextStatic>, text: Ac<Text>| {
let checked = x.recv().await;
text.set(move |text| format_widget!(text, "<= {}!", checked)).await?;
}
});

vstack!(commands {
Expand Down Expand Up @@ -153,22 +162,29 @@ pub fn init(mut commands: AouiCommands) {
},
},
});

let (send, recv, recv2) = commands.signal();

commands.spawn_bundle(Listen(recv2.invoke()));
let (send, recv, recv2) = signal::<Object, _>();

commands.spawn_bundle(Listen(Signal::from_typed(recv2)));

text! (commands {
offset: [300, 0],
color: color!(gold),
text: "<= Click this button.",
extra: recv.recv0(|text: &mut Text| format_widget!(text, "You clicked it!"))
signal: receiver::<Invocation>(recv),
system: |sig: SigRecv<Invocation>, text: Aeq<&mut Text>| {
sig.recv().await;
text
.set(|text| format_widget!(text, "You clicked it!"))
.await;
}
});

button! (commands {
dimension: size2!(12 em, 2 em),
font_size: em(2),
cursor: CursorIcon::Hand,
on_click: send,
child: rectangle!{
dimension: size2!(100%, 100%),
color: color!(blue500),
Expand Down Expand Up @@ -197,8 +213,9 @@ pub fn init(mut commands: AouiCommands) {
extra: DisplayIf(EventFlags::LeftPressed),
z: 0.1
},
extra: Handlers::<EvLeftClick>::oneshot(&mut commands, ||println!("Clicked"))
.and(send),
extra: Handlers::<EvHover>::oneshot(&mut commands, ||println!("Hovering")),
system: |sender: SigSend<ButtonClick>| {
sender.recv().await;
println!("Clicked")
}
});
}
8 changes: 5 additions & 3 deletions aoui/examples/chain.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Showcases a simple skeletal system by chaining rectangles.

use bevy_aoui::{*, bundles::*, dsl::AouiCommands};
use bevy_aoui::{*, bundles::*, util::AouiCommands};
use bevy::prelude::*;
use bevy_egui::{EguiContexts, egui::{Slider, self}};
pub fn main() {
Expand Down Expand Up @@ -34,10 +34,12 @@ pub fn init(mut commands: AouiCommands) {
anchor: TopRight,
text: "FPS: 0.00",
color: color!(gold),
extra: fps_signal(|fps: f32, text: &mut Text| {
format_widget!(text, "FPS: {:.2}", fps);
extra: async_systems!(|fps: Fps, text: Ac<Text>| {
let fps = fps.get().await;
text.set(move |text| format_widget!(text, "FPS: {:.2}", fps)).await?;
})
});

use rand::prelude::*;
let mut rng = rand::thread_rng();
let mut last = commands.spawn_bundle((AouiSpriteBundle {
Expand Down
9 changes: 5 additions & 4 deletions aoui/examples/clipping.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::{prelude::*, diagnostic::FrameTimeDiagnosticsPlugin};
use bevy_aoui::{AouiPlugin, widgets::richtext::RichTextBuilder, dsl::AouiCommands};
use bevy_aoui::{AouiPlugin, widgets::richtext::RichTextBuilder, util::AouiCommands};

pub fn main() {
App::new()
Expand All @@ -25,9 +25,10 @@ pub fn init(mut commands: AouiCommands) {
anchor: TopRight,
text: "FPS: 0.00",
color: color!(gold),
extra: fps_signal(|fps: f32, text: &mut Text| {
format_widget!(text, "FPS: {:.2}", fps);
})
system: |fps: Fps, text: Ac<Text>| {
let fps = fps.get().await;
text.set(move |text| format_widget!(text, "FPS: {:.2}", fps)).await?;
}
});

let (target_in, target_out) = commands.render_target([800, 800]);
Expand Down
28 changes: 19 additions & 9 deletions aoui/examples/counter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This is in fact a show case for `Mutation` and not how you typically implement a counter.

use bevy::{prelude::*, diagnostic::FrameTimeDiagnosticsPlugin};
use bevy_aoui::{AouiPlugin, widgets::button::Payload, WorldExtension, dsl::AouiCommands};
use bevy_aoui::{signal_ids, util::{WorldExtension, AouiCommands}, widgets::button::Payload, AouiPlugin};

pub fn main() {
App::new()
Expand Down Expand Up @@ -29,30 +29,40 @@ pub fn init(mut commands: AouiCommands) {
anchor: TopRight,
text: "FPS: 0.00",
color: color!(gold),
extra: fps_signal(|fps: f32, text: &mut Text| {
format_widget!(text, "FPS: {:.2}", fps);
})
system: |fps: Fps, text: Ac<Text>| {
let fps = fps.get().await;
text.set(move |text| format_widget!(text, "FPS: {:.2}", fps)).await?;
}
});

let (sender, receiver) = commands.signal();
let (send, recv, recv2) = signal();

signal_ids!(SigI32: i32);

hstack!(commands {
margin: size2!(0.5 em, 0.5 em),
font_size: em(2),
child: text! {
text: "0",
extra: receiver.recv0(|x: i32, text: &mut Text| format_widget!(text, "{}", x))
signal: receiver::<SigI32>(recv),
system: |x: SigRecv<SigI32>, text: Ac<Text>| {
let val = x.recv().await;
text.set(move |text|format_widget!(text, "{}", val)).await?;
}
},
child: button! {
event: EventFlags::LeftClick,
dimension: size2!(1 em, 1 em),
payload: 1i32,
on_click: Handlers::new(
Mutation::dynamic::<i32, _, _>(|payload: i32, x: &mut Payload| x.mut_dyn(|_: &i32| payload + 1))
).and(sender.type_erase()),
on_click: send.type_erase(),
child: rectangle! {
dimension: Size2::FULL,
color: color!(red),
},
signal: receiver::<SigI32>(recv2),
system: |x: SigRecv<SigI32>, payload: Ac<Payload>| {
x.recv().await;
payload.set(|payload| payload.mut_dyn(|x: &i32| dbg!(*x + 1))).await?;
}
}
});
Expand Down
2 changes: 1 addition & 1 deletion aoui/examples/description.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// A simple example of rendering description using the paragraph layout.

use bevy::prelude::*;
use bevy_aoui::{AouiPlugin, dsl::AouiCommands};
use bevy_aoui::{AouiPlugin, util::AouiCommands};

pub fn main() {
App::new()
Expand Down
Loading

0 comments on commit 0821795

Please sign in to comment.