Skip to content

Commit

Permalink
Fixed ui/interacting.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ab0v3g4me committed Jul 15, 2020
1 parent cc3c1b7 commit bfa894e
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 deletions.
2 changes: 1 addition & 1 deletion book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
* [Orthographic Camera](./sprites/orthographic_camera.md)
* [User Interface](./ui/introduction.md)
* [Creating a simple button](./ui/simple_button.md)
* [Interacting with the UI](./ui/interaction.md)
* [Interacting with the UI](./ui/interacting.md)
* [Interacting through a state](./ui/state_interaction.md)
* [Testing](./testing.md)
* [Test Examples](./testing/test_examples.md)
Expand Down
105 changes: 91 additions & 14 deletions book/src/ui/interacting.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Let's start of with some boilerplate code:
pub struct SimpleButtonSystem;
impl System for SimpleButtonSystem {
impl<'s> System<'s> for SimpleButtonSystem {
type SystemData = ();
fn run(&mut self, data: Self::SystemData) {
Expand All @@ -36,16 +36,16 @@ Adding it up, it should look like this:

```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::ecs::System;
# use amethyst::shrev::ReaderId;
# use amethyst::ecs::{System, Read};
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::UiEvent;
pub struct SimpleButtonSystem {
reader_id: ReaderId<UiEvent>,
}
impl<'s> System<'s> for SimpleButtonSystem {
type SystemData = Write<'s, EventChannel<UiEvent>>;
type SystemData = Read<'s, EventChannel<UiEvent>>;
fn run(&mut self, events: Self::SystemData) {
Expand All @@ -56,6 +56,22 @@ impl<'s> System<'s> for SimpleButtonSystem {
We also need a constructor for our system:

```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::ecs::{System, Read};
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::UiEvent;
#
# pub struct SimpleButtonSystem {
# reader_id: ReaderId<UiEvent>,
# }
#
# impl<'s> System<'s> for SimpleButtonSystem {
# type SystemData = Read<'s, EventChannel<UiEvent>>;
#
# fn run(&mut self, events: Self::SystemData) {
#
# }
# }
impl SimpleButtonSystem {
pub fn new(reader_id: ReaderId<UiEvent>) -> Self {
Self {
Expand All @@ -69,15 +85,33 @@ To add the system to our game data we actually need a `SystemDesc` implementatio

```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::ecs::{ System, World, };
# use amethyst::ecs::{System, World, Read, Write, SystemData};
# use amethyst::core::SystemDesc;
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::UiEvent;
# pub struct SimpleButtonSystem {
# reader_id: ReaderId<UiEvent>,
# }
#
# impl<'s> System<'s> for SimpleButtonSystem {
# type SystemData = Read<'s, EventChannel<UiEvent>>;
#
# fn run(&mut self, events: Self::SystemData) {
#
# }
# }
# impl SimpleButtonSystem {
# pub fn new(reader_id: ReaderId<UiEvent>) -> Self {
# Self {
# reader_id,
# }
# }
# }
pub struct SimpleButtonSystemDesc;
impl<'a, 'b> SystemDesc<'a, 'b, SimpleButtonSystem> for SimpleButtonSystemDesc {
fn build(self, world: &mut World) -> SimpleButtonSystem {
let mut event_channel = Write<EventChannel<UiEvent>>::fetch(world);
let mut event_channel = <Write<EventChannel<UiEvent>>>::fetch(world);
let reader_id = event_channel.register_reader();
SimpleButtonSystem::new(reader_id)
Expand All @@ -89,11 +123,24 @@ Now that this is done we can start reading our events!
In our systems `run` method we are going to loop through all the events:

```rust,edition2018,no_run,noplaypen
# extern crate amethyst;
# use amethyst::ecs::{System, World, Read};
# use amethyst::core::SystemDesc;
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::UiEvent;
# pub struct SimpleButtonSystem {
# reader_id: ReaderId<UiEvent>,
# }
#
# impl<'s> System<'s> for SimpleButtonSystem {
# type SystemData = Read<'s, EventChannel<UiEvent>>;
#
fn run(&mut self, events: Self::SystemData) {
for event in events.read(&mut self.reader_id) {
info!("{:?}", event);
println!("{:?}", event);
}
}
# }
```

Let's try and change the text color when the button receives a hovered event!
Expand All @@ -102,18 +149,47 @@ Firstly we need to fetch two more components that
we used for our entity - `UiTransform` and `UiText`.

```rust,edition2018,no_run,noplaypen
type SystemData: (
Write<'s, EventChannel<UiEvent>>,
ReadStorage<'s, UiTransfrom>,
WriteStorage<'s, UiText>,
);
# extern crate amethyst;
# use amethyst::ecs::{System, World, Read, ReadStorage, WriteStorage};
# use amethyst::core::SystemDesc;
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::{UiTransform, UiText, UiEvent};
# pub struct SimpleButtonSystem {
# reader_id: ReaderId<UiEvent>,
# }
#
# impl<'s> System<'s> for SimpleButtonSystem {
type SystemData = Read<'s, EventChannel<UiEvent>>;
#
# fn run(&mut self, events: Self::SystemData) {
# for event in events.read(&mut self.reader_id) {
# println!("{:?}", event);
# }
# }
# }
```

Usage of `WriteStorage<'s, UiText>` is needed since we will be changing
the color that is the property of the `UiText` component.

```rust,edition2018,no_run,noplaypen
fn run(&mut self, (events, transforms, texts): Self::SystemData) {
# extern crate amethyst;
# use amethyst::ecs::{System, World, Read, ReadStorage, WriteStorage};
# use amethyst::core::SystemDesc;
# use amethyst::shrev::{EventChannel, ReaderId};
# use amethyst::ui::{UiTransform, UiText, UiEvent, UiEventType};
# pub struct SimpleButtonSystem {
# reader_id: ReaderId<UiEvent>,
# }
#
# impl<'s> System<'s> for SimpleButtonSystem {
# type SystemData = (
# Read<'s, EventChannel<UiEvent>>,
# ReadStorage<'s, UiTransform>,
# WriteStorage<'s, UiText>,
# );
#
fn run(&mut self, (events, transforms, mut texts): Self::SystemData) {
for event in events.read(&mut self.reader_id) {
let button_text = texts.get_mut(event.target).unwrap();
Expand All @@ -128,6 +204,7 @@ fn run(&mut self, (events, transforms, texts): Self::SystemData) {
}
}
}
# }
```

The `HoverStart` and `HoverStop` are emitted once, upon the cursor
Expand Down
1 change: 0 additions & 1 deletion book/src/ui/interaction.md

This file was deleted.

0 comments on commit bfa894e

Please sign in to comment.