Skip to content

Commit

Permalink
added label coloring support
Browse files Browse the repository at this point in the history
fixed connection policy
  • Loading branch information
Doppelkeks committed Jan 9, 2025
1 parent a84eb26 commit cac5f4a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 23 deletions.
22 changes: 15 additions & 7 deletions Attributes/NodeAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using UnityEngine;

namespace NewGraph {
Expand All @@ -17,11 +17,16 @@ public class NodeAttribute : Attribute {
/// Color of the node as a hex string, like #FFFFFFFF. Be aware: The last two characters are for alpha values!
/// </summary>
public Color color = default;

/// <summary>
/// Color of the node label as a hex string, like #FFFFFFFF. Be aware: The last two characters are for alpha values!
/// </summary>
public Color labelColor = default;

/// <summary>
/// A custom name for the node
/// </summary>
public string nodeName = null;
/// <summary>
/// A custom name for the node
/// </summary>
public string nodeName = null;

/// <summary>
/// The maximum amount of allowed connections to the input port of this node.
Expand All @@ -48,11 +53,14 @@ public class NodeAttribute : Attribute {
/// <param name="inputPortCapacity">The maximum amount of allowed connections to the input port of this node.</param>
/// <param name="nodeName">A custom name for the node.</param>
/// <param name="createInputPort">The maximum amount of allowed connections to the input port of this node.</param>
public NodeAttribute(string color = null, string categories = "", string inputPortName = null, Capacity inputPortCapacity = Capacity.Multiple, string nodeName = null, bool createInputPort = true) {
public NodeAttribute(string color = null, string categories = "", string inputPortName = null, Capacity inputPortCapacity = Capacity.Multiple, string nodeName = null, bool createInputPort = true, string labelColor = null) {
if (color != null) {
ColorUtility.TryParseHtmlString(color, out this.color);
}
if (nodeName != null) {
if (labelColor != null) {
ColorUtility.TryParseHtmlString(color, out this.labelColor);
}
if (nodeName != null) {
this.nodeName = nodeName;
}
this.inputPortName = inputPortName;
Expand Down
6 changes: 3 additions & 3 deletions Attributes/PortBaseAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;

namespace NewGraph {
Expand All @@ -16,8 +16,8 @@ public class PortBaseAttribute : Attribute {

private static Dictionary<ConnectionPolicy, Func<Type, Type, bool>> connectionPolicybehaviors = new Dictionary<ConnectionPolicy, Func<Type, Type, bool>>() {
{ ConnectionPolicy.Identical, (input, output) => input == output },
{ ConnectionPolicy.IdenticalOrSubclass, (input, output) => input == output || input.IsSubclassOf(output) },
};
{ ConnectionPolicy.IdenticalOrSubclass, (input, output) => input == output || input.IsSubclassOf(output) || output.IsAssignableFrom(input) },
};

/// <summary>
/// Port Base attribute. This needs to be added to every SerializeReference field that should show up in the graph as a assignable node.
Expand Down
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Added user specific flag and logic to auto expand all foldouts in the side inspector

## [0.4.4] - 2024-03-27
## [0.4.4] - 2025-01-09
### Added
- Added event callback when nodes were pasted
- Added event callback when nodes were pasted

## [0.4.5] - 2025-01-09
### Added
- Added ability to customize label colors (thanks for the pr @LuizMoratelli)
### Fixed
- Fixed port connection policies so extended classes can also be used for port connections (thanks for the pr @emrys90)
2 changes: 1 addition & 1 deletion Editor/Controllers/NodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public NodeController(NodeModel nodeItem, GraphController graphController) {
this.nodeDataProperty = nodeItem.GetSpecificSerializedProperty();
this.propertyBag = PropertyBag.GetCachedOrCreate(nodeItem.nodeAttribute, nodeItem.nodeType, nodeDataProperty);
this.serializedObject = graphController.graphData.SerializedGraphData;
this.nodeView = new NodeView(this, nodeItem.nodeAttribute.color);
this.nodeView = new NodeView(this, nodeItem.nodeAttribute.color, nodeItem.nodeAttribute.labelColor);

string name = nodeItem.GetName();
if (string.IsNullOrEmpty(name)) {
Expand Down
4 changes: 2 additions & 2 deletions Editor/Views/NodeEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using OdinSerializer.Utilities;
using OdinSerializer.Utilities;
using System;
using System.Collections.Generic;
using UnityEditor;
Expand Down Expand Up @@ -85,5 +85,5 @@ public static NodeEditor CreateEditor(Type nodeType) {
public virtual bool ShouldSetBackgroundColor() {
return true;
}
}
}
}
29 changes: 21 additions & 8 deletions Editor/Views/NodeView.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using UnityEditor;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine;
using UnityEngine.UIElements;
Expand All @@ -15,18 +15,20 @@ public class NodeView : BaseNode {
private ReactiveSettings reactiveSettings;
public List<EditableLabelElement> editableLabels = new List<EditableLabelElement>();
public Color nodeColor;
public bool shouldSetBackgroundColor = true;
private bool hasInspectorProperty = false;
public Color labelColor;
public bool shouldSetBackgroundColor = true;
private bool hasInspectorProperty = false;

public NodeController controller;
public PortView inputPort = null;
public List<PortView> outputPorts = new List<PortView>();
public List<PortListView> portLists = new List<PortListView>();
public List<Foldout> foldouts = new List<Foldout>();

public NodeView(NodeController controller, Color nodeColor) {
public NodeView(NodeController controller, Color nodeColor, Color labelColor) {
this.controller = controller;
this.nodeColor = nodeColor;
this.labelColor = labelColor;
}

private void ColorizeBackground() {
Expand All @@ -36,8 +38,18 @@ private void ColorizeBackground() {
style.backgroundColor = Settings.defaultNodeColor;
}
}

public void InitializeView() {
private void ColorizeLabels() {
if (labelColor != default) {
foreach (VisualElement field in controller.nodeView.ExtensionContainer.Children()) {
Label label = field.Q<Label>(className: "unity-base-field__label");
if (label != null) {
label.style.color = labelColor;
}
}
}
}

public void InitializeView() {

editableLabels.Clear();

Expand All @@ -46,9 +58,10 @@ public void InitializeView() {

Vector2 position = controller.GetStartPosition();
SetPosition(position);

if (!controller.nodeItem.isUtilityNode) {
if (!controller.nodeItem.isUtilityNode) {
ColorizeBackground();
ColorizeLabels();

inspectorContent = new VisualElement();
controller.DoForNameProperty(CreateLabelUI);
Expand Down

0 comments on commit cac5f4a

Please sign in to comment.