Skip to content

Commit

Permalink
add unavi-ui update loop
Browse files Browse the repository at this point in the history
  • Loading branch information
kayhhh committed Sep 6, 2024
1 parent f960290 commit e3bd911
Show file tree
Hide file tree
Showing 26 changed files with 9,229 additions and 9,520 deletions.
12 changes: 4 additions & 8 deletions crates/unavi-scripting/src/api/wired/input/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use super::bindings::{
handler::{HostInputHandler, InputEvent},
types::{InputType, Ray},
types::{InputAction, InputData, Ray},
};

pub enum ScriptInputEvent {
Expand Down Expand Up @@ -65,10 +65,7 @@ impl HostInputHandler for StoreState {
Ok(res)
}

fn handle_input(
&mut self,
self_: Resource<InputHandler>,
) -> wasm_bridge::Result<Option<InputEvent>> {
fn next(&mut self, self_: Resource<InputHandler>) -> wasm_bridge::Result<Option<InputEvent>> {
let data = self.table.get(&self_)?;

if let Ok(event) = data.receiver.try_recv() {
Expand All @@ -78,12 +75,11 @@ impl HostInputHandler for StoreState {
orientation,
} => InputEvent {
id: 0,
input: InputType::Ray(Ray {
action: InputAction::Collision,
data: InputData::Ray(Ray {
origin: origin.into(),
orientation: orientation.into(),
}),
order: 0,
distance: 0.0,
},
};

Expand Down
1,133 changes: 551 additions & 582 deletions wasm/example-unavi-layout/src/bindings.rs

Large diffs are not rendered by default.

1,088 changes: 528 additions & 560 deletions wasm/example-unavi-scene/src/bindings.rs

Large diffs are not rendered by default.

1,088 changes: 528 additions & 560 deletions wasm/example-unavi-shapes/src/bindings.rs

Large diffs are not rendered by default.

1,180 changes: 599 additions & 581 deletions wasm/example-unavi-ui/src/bindings.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion wasm/example-unavi-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bindings::{
layout::container::{Alignment, Container},
scene::api::{Root, Scene},
shapes::api::Rectangle,
ui::{button::Button, text::TextBox},
ui::{api::update_ui, button::Button, text::TextBox},
},
wired::{
log::api::{log, LogLevel},
Expand Down Expand Up @@ -83,6 +83,8 @@ impl GuestScript for Script {
}

fn update(&self, delta: f32) {
update_ui(delta);

let time = self.time.get();
self.time.set(time + delta);

Expand Down
1,246 changes: 607 additions & 639 deletions wasm/example-wired-input/src/bindings.rs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions wasm/example-wired-input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod bindings;
mod wired_math_impls;

struct Script {
handler: InputHandler,
input: InputHandler,
material: Material,
}

Expand All @@ -36,16 +36,16 @@ impl GuestScript for Script {
primitive.set_material(Some(&material));
}

let handler = InputHandler::new();
node.set_input_handler(Some(&handler));
let input = InputHandler::new();
node.set_input_handler(Some(&input));

Root::add_scene(&scene);

Script { handler, material }
Script { input, material }
}

fn update(&self, _delta: f32) {
while let Some(event) = self.handler.handle_input() {
while let Some(event) = self.input.next() {
log(LogLevel::Info, &format!("Got input: {:?}", event));

let mut rng = rand::thread_rng();
Expand Down
1,246 changes: 607 additions & 639 deletions wasm/example-wired-physics/src/bindings.rs

Large diffs are not rendered by default.

1,246 changes: 607 additions & 639 deletions wasm/example-wired-player/src/bindings.rs

Large diffs are not rendered by default.

1,221 changes: 595 additions & 626 deletions wasm/example-wired-scene/src/bindings.rs

Large diffs are not rendered by default.

1,201 changes: 585 additions & 616 deletions wasm/test-wired-physics/src/bindings.rs

Large diffs are not rendered by default.

1,201 changes: 585 additions & 616 deletions wasm/test-wired-scene/src/bindings.rs

Large diffs are not rendered by default.

1,035 changes: 502 additions & 533 deletions wasm/unavi-layout/src/bindings.rs

Large diffs are not rendered by default.

1,149 changes: 559 additions & 590 deletions wasm/unavi-scene/src/bindings.rs

Large diffs are not rendered by default.

1,058 changes: 513 additions & 545 deletions wasm/unavi-shapes/src/bindings.rs

Large diffs are not rendered by default.

1,180 changes: 574 additions & 606 deletions wasm/unavi-system/src/bindings.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions wasm/unavi-system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bindings::{
#[allow(warnings)]
mod bindings;
mod clock;
mod wired_input_impls;
mod wired_math_impls;
mod wired_scene_impls;

Expand Down
10 changes: 10 additions & 0 deletions wasm/unavi-system/src/wired_input_impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use crate::bindings::wired::input::types::InputAction;

impl PartialEq for InputAction {
fn eq(&self, other: &Self) -> bool {
matches!(
(self, other),
(Self::Collision, Self::Collision) | (Self::Hover, Self::Hover)
)
}
}
1,239 changes: 631 additions & 608 deletions wasm/unavi-ui/src/bindings.rs

Large diffs are not rendered by default.

74 changes: 68 additions & 6 deletions wasm/unavi-ui/src/button.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,66 @@
use std::{cell::Cell, rc::Rc, sync::atomic::Ordering};

use crate::{
bindings::{
exports::unavi::ui::button::{Guest, GuestButton},
unavi::{layout::container::Container, shapes::api::Cuboid},
wired::input::handler::InputHandler,
wired::input::{handler::InputHandler, types::InputAction},
},
GuestImpl,
GuestImpl, Updatable, ELEMENTS, ELEMENT_ID,
};

impl Guest for GuestImpl {
type Button = Button;
}

pub struct Button {
pub struct Button(Rc<ButtonData>);

impl Drop for Button {
fn drop(&mut self) {
let mut to_remove = Vec::default();

unsafe {
for (i, item) in ELEMENTS.borrow().iter().enumerate() {
if item.id() == self.0.id {
to_remove.push(i);
}
}
}

to_remove.sort();

for i in to_remove {
unsafe {
ELEMENTS.borrow_mut().remove(i);
}
}
}
}

pub struct ButtonData {
id: usize,
input: InputHandler,
root: Container,
hovered: Cell<bool>,
pressed: Cell<bool>,
}

impl Updatable for ButtonData {
fn id(&self) -> usize {
self.id
}

fn update(&self, _delta: f32) {
self.hovered.set(false);
self.pressed.set(false);

while let Some(event) = self.input.next() {
match event.action {
InputAction::Hover => self.hovered.set(true),
InputAction::Collision => self.pressed.set(true),
}
}
}
}

impl GuestButton for Button {
Expand All @@ -26,14 +73,29 @@ impl GuestButton for Button {

root.inner().add_child(&node);

Self { input, root }
let data = Rc::new(ButtonData {
id: ELEMENT_ID.fetch_add(1, Ordering::Relaxed),
input,
root,
hovered: Cell::default(),
pressed: Cell::default(),
});

unsafe {
ELEMENTS.borrow_mut().push(data.clone());
}

Self(data)
}

fn root(&self) -> Container {
self.root.ref_()
self.0.root.ref_()
}

fn hovered(&self) -> bool {
self.0.hovered.get()
}
fn pressed(&self) -> bool {
self.input.handle_input().is_some()
self.0.pressed.get()
}
}
28 changes: 28 additions & 0 deletions wasm/unavi-ui/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
use std::{
cell::{LazyCell, RefCell},
rc::Rc,
sync::atomic::AtomicUsize,
};

use bindings::exports::unavi::ui::api::Guest;

#[allow(warnings)]
mod bindings;

mod button;
mod text;
mod wired_input_impls;

static ELEMENT_ID: AtomicUsize = AtomicUsize::new(0);
static mut ELEMENTS: LazyCell<RefCell<Vec<Rc<dyn Updatable>>>> = LazyCell::new(RefCell::default);

trait Updatable {
fn id(&self) -> usize;
fn update(&self, delta: f32);
}

struct GuestImpl;

impl Guest for GuestImpl {
fn update_ui(delta: f32) {
// WASM is single-threaded, mutable statics are fine.
unsafe {
for item in ELEMENTS.borrow().iter() {
item.update(delta);
}
}
}
}

bindings::export!(GuestImpl with_types_in bindings);
1 change: 1 addition & 0 deletions wasm/unavi-ui/src/wired_input_impls.rs
11 changes: 10 additions & 1 deletion wasm/unavi-ui/wit/world.wit
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@ package unavi:ui;
world guest {
import unavi:shapes/api;

export api;
export button;
export text;
}

world prelude {
import api;
import button;
import text;
}

interface api {
update-ui: func(delta: f32);
}

interface button {
use unavi:layout/container.{container};
use wired:input/handler.{input-handler};
Expand All @@ -23,7 +29,10 @@ interface button {

root: func() -> container;

// Returns `true` if the button is pressed down this frame.
// Returns `true` if the button was hovered over this frame.
hovered: func() -> bool;

// Returns `true` if the button was pressed this frame.
pressed: func() -> bool;
}
}
Expand Down
Loading

0 comments on commit e3bd911

Please sign in to comment.