mirror of
https://github.com/eliasstepanik/egui_node_graph.git
synced 2026-01-11 13:58:28 +00:00
Also add NodeData parameter to value_widget
This commit is contained in:
parent
0ab08bd9ca
commit
d6c1b324ee
@ -76,7 +76,8 @@ where
|
|||||||
ValueType = ValueType,
|
ValueType = ValueType,
|
||||||
>,
|
>,
|
||||||
UserResponse: UserResponseTrait,
|
UserResponse: UserResponseTrait,
|
||||||
ValueType: WidgetValueTrait<Response = UserResponse, UserState = UserState>,
|
ValueType:
|
||||||
|
WidgetValueTrait<Response = UserResponse, UserState = UserState, NodeData = NodeData>,
|
||||||
NodeTemplate: NodeTemplateTrait<
|
NodeTemplate: NodeTemplateTrait<
|
||||||
NodeData = NodeData,
|
NodeData = NodeData,
|
||||||
DataType = DataType,
|
DataType = DataType,
|
||||||
@ -391,7 +392,8 @@ where
|
|||||||
ValueType = ValueType,
|
ValueType = ValueType,
|
||||||
>,
|
>,
|
||||||
UserResponse: UserResponseTrait,
|
UserResponse: UserResponseTrait,
|
||||||
ValueType: WidgetValueTrait<Response = UserResponse, UserState = UserState>,
|
ValueType:
|
||||||
|
WidgetValueTrait<Response = UserResponse, UserState = UserState, NodeData = NodeData>,
|
||||||
DataType: DataTypeTrait<UserState>,
|
DataType: DataTypeTrait<UserState>,
|
||||||
{
|
{
|
||||||
pub const MAX_NODE_SIZE: [f32; 2] = [200.0, 200.0];
|
pub const MAX_NODE_SIZE: [f32; 2] = [200.0, 200.0];
|
||||||
@ -469,13 +471,23 @@ where
|
|||||||
if self.graph.connection(param_id).is_some() {
|
if self.graph.connection(param_id).is_some() {
|
||||||
ui.label(param_name);
|
ui.label(param_name);
|
||||||
} else {
|
} else {
|
||||||
responses.extend(
|
// NOTE: We want to pass the `user_data` to
|
||||||
self.graph[param_id]
|
// `value_widget`, but we can't since that would require
|
||||||
.value
|
// borrowing the graph twice. Here, we take the
|
||||||
.value_widget(¶m_name, self.node_id, ui, user_state)
|
// assumption that the value is cheaply replace, and use
|
||||||
.into_iter()
|
// `std::mem::take` to temporarily replace it with a
|
||||||
.map(NodeResponse::User),
|
// dummy value. This requires `ValueType` to implement
|
||||||
|
// Default, but results in a totally safe alternative.
|
||||||
|
let mut value = std::mem::take(&mut self.graph[param_id].value);
|
||||||
|
let node_responses = value.value_widget(
|
||||||
|
¶m_name,
|
||||||
|
self.node_id,
|
||||||
|
ui,
|
||||||
|
user_state,
|
||||||
|
&self.graph[self.node_id].user_data,
|
||||||
);
|
);
|
||||||
|
self.graph[param_id].value = value;
|
||||||
|
responses.extend(node_responses.into_iter().map(NodeResponse::User));
|
||||||
}
|
}
|
||||||
let height_after = ui.min_rect().bottom();
|
let height_after = ui.min_rect().bottom();
|
||||||
input_port_heights.push((height_before + height_after) / 2.0);
|
input_port_heights.push((height_before + height_after) / 2.0);
|
||||||
|
|||||||
@ -3,9 +3,10 @@ use super::*;
|
|||||||
/// This trait must be implemented by the `ValueType` generic parameter of the
|
/// This trait must be implemented by the `ValueType` generic parameter of the
|
||||||
/// [`Graph`]. The trait allows drawing custom inline widgets for the different
|
/// [`Graph`]. The trait allows drawing custom inline widgets for the different
|
||||||
/// types of the node graph.
|
/// types of the node graph.
|
||||||
pub trait WidgetValueTrait {
|
pub trait WidgetValueTrait : Default {
|
||||||
type Response;
|
type Response;
|
||||||
type UserState;
|
type UserState;
|
||||||
|
type NodeData;
|
||||||
/// This method will be called for each input parameter with a widget. The
|
/// This method will be called for each input parameter with a widget. The
|
||||||
/// return value is a vector of custom response objects which can be used
|
/// return value is a vector of custom response objects which can be used
|
||||||
/// to implement handling of side effects. If unsure, the response Vec can
|
/// to implement handling of side effects. If unsure, the response Vec can
|
||||||
@ -16,6 +17,7 @@ pub trait WidgetValueTrait {
|
|||||||
node_id: NodeId,
|
node_id: NodeId,
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
user_state: &mut Self::UserState,
|
user_state: &mut Self::UserState,
|
||||||
|
node_data: &Self::NodeData,
|
||||||
) -> Vec<Self::Response>;
|
) -> Vec<Self::Response>;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,7 +121,11 @@ pub trait NodeTemplateTrait: Clone {
|
|||||||
type UserState;
|
type UserState;
|
||||||
|
|
||||||
/// Returns a descriptive name for the node kind, used in the node finder.
|
/// Returns a descriptive name for the node kind, used in the node finder.
|
||||||
fn node_finder_label(&self, user_state: &mut Self::UserState) -> &str;
|
///
|
||||||
|
/// The return type is Cow<str> to allow returning owned or borrowed values
|
||||||
|
/// more flexibly. Refer to the documentation for `DataTypeTrait::name` for
|
||||||
|
/// more information
|
||||||
|
fn node_finder_label(&self, user_state: &mut Self::UserState) -> std::borrow::Cow<str>;
|
||||||
|
|
||||||
/// Returns a descriptive name for the node kind, used in the graph.
|
/// Returns a descriptive name for the node kind, used in the graph.
|
||||||
fn node_graph_label(&self, user_state: &mut Self::UserState) -> String;
|
fn node_graph_label(&self, user_state: &mut Self::UserState) -> String;
|
||||||
|
|||||||
@ -37,6 +37,14 @@ pub enum MyValueType {
|
|||||||
Scalar { value: f32 },
|
Scalar { value: f32 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for MyValueType {
|
||||||
|
fn default() -> Self {
|
||||||
|
// NOTE: This is just a dummy `Default` implementation. The library
|
||||||
|
// requires it to circumvent some internal borrow checker issues.
|
||||||
|
Self::Scalar { value: 0.0 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl MyValueType {
|
impl MyValueType {
|
||||||
/// Tries to downcast this value type to a vector
|
/// Tries to downcast this value type to a vector
|
||||||
pub fn try_to_vec2(self) -> anyhow::Result<egui::Vec2> {
|
pub fn try_to_vec2(self) -> anyhow::Result<egui::Vec2> {
|
||||||
@ -118,8 +126,8 @@ impl NodeTemplateTrait for MyNodeTemplate {
|
|||||||
type ValueType = MyValueType;
|
type ValueType = MyValueType;
|
||||||
type UserState = MyGraphState;
|
type UserState = MyGraphState;
|
||||||
|
|
||||||
fn node_finder_label(&self, _user_state: &mut Self::UserState) -> &str {
|
fn node_finder_label(&self, _user_state: &mut Self::UserState) -> Cow<'_, str> {
|
||||||
match self {
|
Cow::Borrowed(match self {
|
||||||
MyNodeTemplate::MakeVector => "New vector",
|
MyNodeTemplate::MakeVector => "New vector",
|
||||||
MyNodeTemplate::MakeScalar => "New scalar",
|
MyNodeTemplate::MakeScalar => "New scalar",
|
||||||
MyNodeTemplate::AddScalar => "Scalar add",
|
MyNodeTemplate::AddScalar => "Scalar add",
|
||||||
@ -127,7 +135,7 @@ impl NodeTemplateTrait for MyNodeTemplate {
|
|||||||
MyNodeTemplate::AddVector => "Vector add",
|
MyNodeTemplate::AddVector => "Vector add",
|
||||||
MyNodeTemplate::SubtractVector => "Vector subtract",
|
MyNodeTemplate::SubtractVector => "Vector subtract",
|
||||||
MyNodeTemplate::VectorTimesScalar => "Vector times scalar",
|
MyNodeTemplate::VectorTimesScalar => "Vector times scalar",
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn node_graph_label(&self, user_state: &mut Self::UserState) -> String {
|
fn node_graph_label(&self, user_state: &mut Self::UserState) -> String {
|
||||||
@ -259,12 +267,14 @@ impl NodeTemplateIter for AllMyNodeTemplates {
|
|||||||
impl WidgetValueTrait for MyValueType {
|
impl WidgetValueTrait for MyValueType {
|
||||||
type Response = MyResponse;
|
type Response = MyResponse;
|
||||||
type UserState = MyGraphState;
|
type UserState = MyGraphState;
|
||||||
|
type NodeData = MyNodeData;
|
||||||
fn value_widget(
|
fn value_widget(
|
||||||
&mut self,
|
&mut self,
|
||||||
param_name: &str,
|
param_name: &str,
|
||||||
_node_id: NodeId,
|
_node_id: NodeId,
|
||||||
ui: &mut egui::Ui,
|
ui: &mut egui::Ui,
|
||||||
_user_state: &mut MyGraphState,
|
_user_state: &mut MyGraphState,
|
||||||
|
_node_data: &MyNodeData,
|
||||||
) -> Vec<MyResponse> {
|
) -> Vec<MyResponse> {
|
||||||
// This trait is used to tell the library which UI to display for the
|
// This trait is used to tell the library which UI to display for the
|
||||||
// inline parameter widgets.
|
// inline parameter widgets.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user