Skip to content

Commit

Permalink
Proper unregister Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus committed Jan 23, 2025
1 parent 107349d commit 73a5610
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [0.4.6] - 2025-01-25
### Changed
- Remove OnViewChangedCallback for better performance

## [0.4.7] - 2025-01-25
### Changed
- Proper unregister Events
36 changes: 29 additions & 7 deletions Editor/Views/Elements/EditableLabelElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ public EditableLabelElement(PropertyField propertyField, System.Action<string> o
this.onValueChange = onValueChange;
this.onEditModeLeft = onEditModeLeft;

void ChangeEvent(ChangeEvent<string> evt) {
this.onValueChange?.Invoke(evt.newValue);
}

// check if the propertyfield value changed
propertyField.RegisterCallback<ChangeEvent<string>>((value) => {
this.onValueChange?.Invoke(value.newValue);
});
propertyField.RegisterCallback<ChangeEvent<string>>(ChangeEvent);
propertyField.AddToClassList(editableLabelUSSClass);

// wait until the visualtree of the property has been built
propertyField.RegisterCallback<GeometryChangedEvent>(InitializeAfterVisualTreeReady);
void DetachFromPanelEvent(DetachFromPanelEvent evt) {
propertyField.UnregisterCallback<ChangeEvent<string>>(ChangeEvent);
propertyField.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);
}
propertyField.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);

// wait until the visualtree of the property has been built
propertyField.RegisterCallback<GeometryChangedEvent>(InitializeAfterVisualTreeReady);
}

/// <summary>
Expand All @@ -58,13 +66,27 @@ private void InitializeAfterVisualTreeReady(GeometryChangedEvent e) {
textField = propertyField[0] as TextField;
textField.RegisterCallback<KeyDownEvent>(OnKeyDown);

inputField = textField[1];
void DetachFromPanelEventTxtFld(DetachFromPanelEvent evt) {
textField.UnregisterCallback<KeyDownEvent>(OnKeyDown);
textField.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEventTxtFld);
}
textField.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEventTxtFld);


inputField = textField[1];

textElement = inputField[0];
editButton = new Image() { image = editIconTexture };

editButton.RegisterCallback<ClickEvent>(OnEditButtonClick);
propertyField.Add(editButton);

void DetachFromPanelEventEditBtn(DetachFromPanelEvent evt) {
editButton.UnregisterCallback<ClickEvent>(OnEditButtonClick);
editButton.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEventEditBtn);
}
editButton.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEventEditBtn);

propertyField.Add(editButton);
isInitialized = true;
EnableInput(false);
propertyField.UnregisterCallback<GeometryChangedEvent>(InitializeAfterVisualTreeReady);
Expand Down
15 changes: 12 additions & 3 deletions Editor/Views/GraphView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ public class GraphView : GraphViewBase.GraphView {
public GraphView(VisualElement parent, VisualElement root, Action<Actions, object> OnAction) {
GraphWindow.OnGlobalKeyDown -= OnKeyDown;
GraphWindow.OnGlobalKeyDown += OnKeyDown;
root.RegisterCallback<MouseDownEvent>((evt) => { OnMouseDown(evt); });

graphViewRoot = parent.Q<VisualElement>(nameof(graphViewRoot));
void MouseDownEvent(MouseDownEvent evt) {
OnMouseDown(evt);
}
root.RegisterCallback<MouseDownEvent>(MouseDownEvent);

void DetachFromPanelEvent(DetachFromPanelEvent evt) {
root.UnregisterCallback<MouseDownEvent>(MouseDownEvent);
root.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);
}
root.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);

graphViewRoot = parent.Q<VisualElement>(nameof(graphViewRoot));
graphViewRoot.Add(this);
this.StretchToParentSize();

Expand Down
39 changes: 29 additions & 10 deletions Editor/Views/NodeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,20 @@ void HandleEmbeddedReferenceCase(VisualElement foldoutContent) {
if (foldout != null) {
// make sure to force the foldoutvalue to stay open
foldout.value = true;
foldout.RegisterValueChangedCallback((evt) => {
if (evt.newValue != true) {
foldout.value = true;
}
});

void RegisterValueChangedCallback(ChangeEvent<bool> evt) {
if (evt.newValue != true) {
foldout.value = true;
}
}
foldout.RegisterValueChangedCallback(RegisterValueChangedCallback);

void DetachFromPanelEvent(DetachFromPanelEvent evt) {
foldout.UnregisterValueChangedCallback(RegisterValueChangedCallback);
foldout.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);
}
foldout.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);

// get the toggle
Toggle toggle = foldout.Q<Toggle>();
if (toggle != null) {
Expand All @@ -226,11 +235,21 @@ void HandleFoldoutState(Foldout newGroup, int index) {
NodeModel.FoldoutState foldOutState = controller.nodeItem.GetOrCreateFoldout(propertyPathHash);
foldOutState.used = true;
newGroup.value = foldOutState.isExpanded;
newGroup.RegisterValueChangedCallback((evt) => {
foldOutState.isExpanded = evt.newValue;
controller.GetSerializedObject().ApplyModifiedPropertiesWithoutUndo();
});
newGroups[index] = newGroup;

void RegisterValueChangedCallback(ChangeEvent<bool> evt) {
foldOutState.isExpanded = evt.newValue;
controller.GetSerializedObject().ApplyModifiedPropertiesWithoutUndo();
}

newGroup.RegisterValueChangedCallback(RegisterValueChangedCallback);

void DetachFromPanelEvent(DetachFromPanelEvent evt) {
newGroup.UnregisterValueChangedCallback(RegisterValueChangedCallback);
newGroup.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);
}
newGroup.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEvent);

newGroups[index] = newGroup;
foldouts.Add(newGroup);
}

Expand Down
32 changes: 28 additions & 4 deletions Editor/Views/Nodes/GroupCommentNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public void Initialize(NodeController nodeController) {
nodeView.RegisterCallback<MouseDownEvent>(OnNodeViewMouseDown);
nodeView.RegisterCallback<MouseUpEvent>(OnMouseUp);

void DetachFromPanelEventNodeView(DetachFromPanelEvent evt) {
nodeView.UnregisterCallback<MouseDownEvent>(OnNodeViewMouseDown);
nodeView.UnregisterCallback<MouseUpEvent>(OnMouseUp);
nodeView.UnregisterCallback<MouseMoveEvent>(OnMouseMove);
nodeView.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEventNodeView);
}
nodeView.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEventNodeView);

// create a custom container
container = new VisualElement();
container.AddToClassList(nameof(GroupCommentNode)+"Container");
Expand All @@ -88,11 +96,27 @@ public void Initialize(NodeController nodeController) {
PropertyField colorfield = nodeView.inspectorContent.Q<PropertyField>(nameof(groupColor));
colorfield.RegisterValueChangeCallback(OnColorChanged);
container.pickingMode = PickingMode.Ignore;

// the element that is initially clicked to "expand" the node
dragElement = new Image() { vectorImage = DragCaretGraphics };

void DetachFromPanelEventColorfield(DetachFromPanelEvent evt) {
colorfield.UnregisterCallback<SerializedPropertyChangeEvent>(OnColorChanged);
colorfield.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEventColorfield);
}
colorfield.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEventColorfield);



// the element that is initially clicked to "expand" the node
dragElement = new Image() { vectorImage = DragCaretGraphics };
dragElement.RegisterCallback<MouseDownEvent>(OnMouseDown);
container.Add(dragElement);


void DetachFromPanelEventDragElement(DetachFromPanelEvent evt) {
dragElement.UnregisterCallback<MouseDownEvent>(OnMouseDown);
dragElement.UnregisterCallback<DetachFromPanelEvent>(DetachFromPanelEventDragElement);
}
dragElement.RegisterCallback<DetachFromPanelEvent>(DetachFromPanelEventDragElement);

container.Add(dragElement);
}

private void OnColorChanged(SerializedPropertyChangeEvent evt) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.gentlymad.newgraph",
"version": "0.4.6",
"version": "0.4.7",
"displayName": "NewGraph",
"description": "A general data oriented node graph solution. This is build upon the idea to visualize complex data structures as graph networks without having to modify already established data classes, except adding [Node], [Port] and [SerializeReference] attributes to call classes that should show in the Graph View.",
"unity": "2022.3",
Expand Down

0 comments on commit 73a5610

Please sign in to comment.