Merge pull request #35 from IsseW/widget-response

Add user response to WidgetValueTrait
This commit is contained in:
setzer22 2022-06-07 21:49:20 +02:00 committed by GitHub
commit 6ebb4995c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 5 deletions

View File

@ -52,7 +52,7 @@ where
ValueType = ValueType,
>,
UserResponse: UserResponseTrait,
ValueType: WidgetValueTrait,
ValueType: WidgetValueTrait<Response = UserResponse>,
NodeTemplate:
NodeTemplateTrait<NodeData = NodeData, DataType = DataType, ValueType = ValueType>,
DataType: DataTypeTrait<UserState>,
@ -298,7 +298,7 @@ where
ValueType = ValueType,
>,
UserResponse: UserResponseTrait,
ValueType: WidgetValueTrait,
ValueType: WidgetValueTrait<Response = UserResponse>,
DataType: DataTypeTrait<UserState>,
{
pub const MAX_NODE_SIZE: [f32; 2] = [200.0, 200.0];
@ -374,7 +374,13 @@ where
if self.graph.connection(param_id).is_some() {
ui.label(param_name);
} else {
self.graph[param_id].value.value_widget(&param_name, ui);
responses.extend(
self.graph[param_id]
.value
.value_widget(&param_name, ui)
.into_iter()
.map(NodeResponse::User),
);
}
let height_after = ui.min_rect().bottom();
input_port_heights.push((height_before + height_after) / 2.0);

View File

@ -4,7 +4,12 @@ use super::*;
/// [`Graph`]. The trait allows drawing custom inline widgets for the different
/// types of the node graph.
pub trait WidgetValueTrait {
fn value_widget(&mut self, param_name: &str, ui: &mut egui::Ui);
type Response;
/// 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
/// to implement handling of side effects. If unsure, the response Vec can
/// be empty.
fn value_widget(&mut self, param_name: &str, ui: &mut egui::Ui) -> Vec<Self::Response>;
}
/// This trait must be implemented by the `DataType` generic parameter of the

View File

@ -250,7 +250,8 @@ impl NodeTemplateIter for AllMyNodeTemplates {
}
impl WidgetValueTrait for MyValueType {
fn value_widget(&mut self, param_name: &str, ui: &mut egui::Ui) {
type Response = MyResponse;
fn value_widget(&mut self, param_name: &str, ui: &mut egui::Ui) -> Vec<MyResponse> {
// This trait is used to tell the library which UI to display for the
// inline parameter widgets.
match self {
@ -270,6 +271,8 @@ impl WidgetValueTrait for MyValueType {
});
}
}
// This allows you to return your responses from the inline widgets.
Vec::new()
}
}