-
Notifications
You must be signed in to change notification settings - Fork 126
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
base: main
Are you sure you want to change the base?
UI node graph #565
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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"] |
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"] |
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() | ||
} | ||
|
||
#[main] | ||
pub fn main() { | ||
App.el().spawn_interactive(); | ||
} |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest using the |
||
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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggest |
||
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); | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto on |
||
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); | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question. I haven't got any idea yet on how to store the data structure. Still drafting this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just temporary. Might be good to change it to
GraphEditor
or something else, and perhaps this whole thing should be put into theeditor
folder.