Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI node graph #565

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions guest/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions guest/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ members = [
"examples/ui/auto_editor",
"examples/ui/button",
"examples/ui/counter",
"examples/ui/nodegraph",
"examples/ui/clock",
"examples/ui/dock_layout",
"examples/ui/editors",
Expand Down
19 changes: 19 additions & 0 deletions guest/rust/examples/ui/nodegraph/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "ambient_example_nodegraph"

edition = "2021"
publish = false
rust-version = { workspace = true }
version = { workspace = true }

[dependencies]
ambient_api = { workspace = true }

[[bin]]
name = "nodegraph_client"
path = "src/client.rs"
required-features = ["client"]

[features]
client = ["ambient_api/client"]
server = ["ambient_api/server"]
7 changes: 7 additions & 0 deletions guest/rust/examples/ui/nodegraph/ambient.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ember]
id = "ambient_example_nodegraph"
name = "Nodegraph"
version = "0.0.1"
repository = "https://github.com/AmbientRun/Ambient/tree/main/guest/rust/examples/ui/nodegraph"
type = "Game"
categories = ["Example"]
23 changes: 23 additions & 0 deletions guest/rust/examples/ui/nodegraph/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use ambient_api::{
components::core::{
layout::{
align_horizontal_center, align_vertical_center, height, space_between_items, width,
},
rect::{
background_color, border_color, border_radius, border_thickness, line_from, line_to,
line_width,
},
transform::translation,
},
prelude::*,
};

#[element_component]
fn App(_hooks: &mut Hooks) -> Element {
Graph::el()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this Graph spawn? Or is it an empty graph?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, how do you interact with the graph and get values out of it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a good idea to e.g. make a calculator using this, so you can see how it works

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Additionally, how do you interact with the graph and get values out of it?

Good question. I haven't got any idea yet on how to store the data structure. Still drafting this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this Graph spawn? Or is it an empty graph?

It's just temporary. Might be good to change it to GraphEditor or something else, and perhaps this whole thing should be put into the editor folder.

}

#[main]
pub fn main() {
App.el().spawn_interactive();
}
1 change: 1 addition & 0 deletions shared_crates/ui/src/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ pub fn Button(
content
}
}

impl Button {
/// Create a new [Button] with the given content and callback.
pub fn new<T: Into<UIElement>>(
Expand Down
103 changes: 103 additions & 0 deletions shared_crates/ui/src/dragarea.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! Defines the [DragArea] element.

use ambient_element::{to_owned, Element, ElementComponent, Hooks};
use ambient_guest_bridge::components::transform::{local_to_parent, local_to_world};
use ambient_guest_bridge::components::{app::cursor_position, transform::translation};
use ambient_guest_bridge::{
components::input::{mouse_over, mouse_pickable_max, mouse_pickable_min},
messages,
};
use ambient_shared_types::CursorIcon;
use glam::Vec3;

#[derive(Debug, Clone)]
/// An area that can be dragged.
pub struct DragArea {
/// The inner element.
pub inner: Element,
}

impl DragArea {
/// Create a new ClickArea.
pub fn new(inner: Element) -> Self {
Self { inner }
}
}
impl ElementComponent for DragArea {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest using the #[element_component] macro

fn render(self: Box<Self>, hooks: &mut Hooks) -> Element {
let Self { inner } = *self;
let id = hooks.use_ref_with(|_| None);
let mouse_over_count = hooks.use_ref_with(|_| 0);
let (moving, set_moving) = hooks.use_state(false);
let (mouse_click_pos, set_mouse_click_pos) = hooks.use_state((0.0, 0.0));
let (click_pos, set_click_pos) = hooks.use_state((0.0, 0.0));
hooks.use_frame({
to_owned![id, mouse_over_count];
move |world| {
if let Some(id) = *id.lock() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest let Some(id) = *id.lock() else { return; } to cut one layer of indentation

if moving {
let mouse_pos = world.resource(cursor_position());
let pos = world.get(id, translation()).unwrap_or(Vec3::ZERO);
world
.set(
id,
translation(),
Vec3 {
x: click_pos.0 + mouse_pos.x - mouse_click_pos.0,
y: click_pos.1 + mouse_pos.y - mouse_click_pos.1,
z: pos.z,
},
)
.unwrap();
}
let next = world.get(id, mouse_over()).unwrap_or(0);
let mut state = mouse_over_count.lock();
// if *state == 0 && next > 0 {
// // println!("mouse enter");
// ambient_guest_bridge::window::set_cursor(world, CursorIcon::Move);
// }
// if *state > 0 && next == 0 {
// // println!("mouse leave");
// ambient_guest_bridge::window::set_cursor(world, CursorIcon::Default);
// }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove lines


if next > 0 {
ambient_guest_bridge::window::set_cursor(world, CursorIcon::Move);
} else {
ambient_guest_bridge::window::set_cursor(world, CursorIcon::Arrow);
}

*state = next;
}
}
});

hooks.use_runtime_message::<messages::WindowMouseInput>({
to_owned![id, mouse_over_count, set_moving];
move |world, event| {
if let Some(id) = *id.lock() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto on let Some

if *mouse_over_count.lock() > 0 {
if event.pressed && event.button == 0 {
set_moving(true);
let mouse_pos = world.resource(cursor_position());
let pos = world.get(id, translation()).unwrap_or(Vec3::ZERO);
set_mouse_click_pos((mouse_pos.x, mouse_pos.y));
set_click_pos((pos.x, pos.y));
} else {
set_moving(false);
}
} else {
set_moving(false);
}
}
}
});

inner
.init(mouse_pickable_min(), Vec3::ZERO)
.init(mouse_pickable_max(), Vec3::ZERO)
.on_spawned(move |_, new_id, _| {
*id.lock() = Some(new_id);
})
}
}
7 changes: 7 additions & 0 deletions shared_crates/ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ use glam::{vec3, Mat4, UVec2, Vec3, Vec4};
pub mod button;
pub mod clickarea;
pub mod default_theme;
pub mod dragarea;
pub mod dropdown;
pub mod editor;
pub mod layout;
pub mod node;
pub mod prelude;
pub mod prompt;
pub mod screens;
Expand Down Expand Up @@ -228,11 +230,16 @@ pub trait UIExt {
fn with_padding_even(self, padding: f32) -> Self;
/// Adds margin to all sides of this element.
fn with_margin_even(self, margin: f32) -> Self;
/// Wraps this element in a [DragArea] element.
fn with_dragarea(self) -> dragarea::DragArea;
}
impl UIExt for Element {
fn with_clickarea(self) -> ClickArea {
ClickArea::new(self)
}
fn with_dragarea(self) -> dragarea::DragArea {
dragarea::DragArea::new(self)
}
fn with_background(self, background: Vec4) -> Self {
with_rect(self).with(background_color(), background)
}
Expand Down
Loading