public void ProcessEvent(AnchorField anchorField) { var e = Event.current; bool contains = false; if (e.type == EventType.ContextClick) { Debug.Log(e.mousePosition); } foreach (var anchor in anchorField.anchors) { if (anchor.visibility == Visibility.Visible && anchor.rect.Contains(e.mousePosition)) { editorEvents.mouseOverAnchor = anchor; editorEvents.isMouseOverAnchorFrame = true; if (e.type == EventType.MouseDown && e.button == 0) { editorEvents.isMouseClickOnAnchor = true; } contains = true; } } //clean anchor field if the old anchor was in this anchorField if (!contains) { if (anchorField.anchors.Contains(editorEvents.mouseOverAnchor)) { editorEvents.mouseOverAnchor = null; } } }
AnchorField CreateAnchorField() { AnchorField newAnchorField = new AnchorField(); newAnchorField.Initialize(this); return(newAnchorField); }
public void RenderAnchorField(AnchorField anchorField, ref Rect renderRect) { int index = 0; int anchorDefaultPadding = 18; renderRect.y += anchorField.offset; foreach (var anchor in anchorField.anchors) { //render anchor if visible and linkable if (anchor.visibility == Visibility.Visible && anchor.isLinkable) { RenderAnchor(anchorField, anchor, ref renderRect); index++; } //if anchor is not gone, increment the padding for next anchor if (anchor.visibility != Visibility.Gone) { renderRect.y += anchorField.padding + anchorDefaultPadding; } } }
//the anchor passed to ths function must be in the `anchors` list void RenderAnchor(AnchorField anchorField, Anchor anchor, ref Rect renderRect) { //visual parameters for anchors: Vector2 anchorSize = new Vector2(13, 13); Vector2 margin = new Vector2(0, 2); if (anchor.forcedY != -1) { renderRect.yMin = anchor.forcedY; } anchor.rect = new Rect(renderRect.min + margin, anchorSize); //anchor name: string anchorName = anchorField.name; if (!String.IsNullOrEmpty(anchor.name)) { anchorName = anchor.name; } //anchor color: if (anchor.color != new Color(0, 0, 0, 0)) { GUI.color = anchor.color; } else { GUI.color = ColorTheme.GetAnchorColor(anchor.colorSchemeName); } //highlight mode to GUI color: if (graphRef.editorEvents.isDraggingLink || graphRef.editorEvents.isDraggingNewLink) { if (anchor.highlighMode != AnchorHighlight.None) { if (anchor.isLinkable) { GUI.color = highlightModeToColor[anchor.highlighMode]; } else { GUI.color = ColorTheme.disabledAnchorColor; } } } //Draw the anchor: GUI.DrawTexture(anchor.rect, anchorTexture, ScaleMode.ScaleToFit); //Draw the anchor name if not null if (!string.IsNullOrEmpty(anchorName)) { Rect anchorNameRect = anchor.rect; Vector2 textSize = GUI.skin.label.CalcSize(new GUIContent(anchorName)); if (anchorField.anchorType == AnchorType.Input) { anchorNameRect.position += new Vector2(-6, -2); } else { anchorNameRect.position += new Vector2(-textSize.x + 4, -2); } anchorNameRect.size = textSize + new Vector2(15, 4); //add the anchorLabel size GUI.depth = 10; GUI.Label(anchorNameRect, anchorName, (anchorField.anchorType == AnchorType.Input) ? inputAnchorLabelStyle : outputAnchorLabelStyle); } //error display (required unlinked anchors) if (anchor.visibility == Visibility.Visible && anchorField.required && !anchorField.multiple && anchor.anchorType == AnchorType.Input && anchor.linkCount == 0) { Rect errorIconRect = new Rect(anchor.rect); errorIconRect.size = Vector2.one * 17; errorIconRect.position += new Vector2(-6, -10); GUI.color = Color.red; GUI.DrawTexture(errorIconRect, errorIcon); GUI.color = Color.white; } //debug: if (anchor.debug) { GUIContent debugContent = new GUIContent("c:" + anchor.linkCount + "|i:" + anchor.fieldIndex); Rect debugRect = anchor.rect; debugRect.xMax += 50; if (anchor.anchorType == AnchorType.Output) { debugRect.x -= EditorStyles.label.CalcSize(debugContent).x; } EditorGUI.DrawRect(debugRect, Color.white * .8f); GUI.Label(debugRect, debugContent); } }
void LoadFieldAttributes() { //get input variables System.Reflection.FieldInfo[] fInfos = GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly); List <string> actualFields = new List <string>(); anchorFieldInfoMap.Clear(); foreach (var field in fInfos) { // reading field informations. actualFields.Add(field.Name); anchorFieldInfoMap[field.Name] = field; if (!anchorFieldDictionary.ContainsKey(field.Name)) { anchorFieldDictionary[field.Name] = CreateAnchorField(); } AnchorField anchorField = anchorFieldDictionary[field.Name]; //detect multi-anchor by checking for PWArray<T> type if (field.FieldType.IsGenericType) { if (field.FieldType.GetGenericTypeDefinition() == typeof(PWArray <>)) { anchorField.multiple = true; } } System.Object[] attrs = field.GetCustomAttributes(true); foreach (var attr in attrs) { InputAttribute inputAttr = attr as InputAttribute; OutputAttribute outputAttr = attr as OutputAttribute; ColorAttribute colorAttr = attr as ColorAttribute; OffsetAttribute offsetAttr = attr as OffsetAttribute; VisibilityAttribute visibilityAttr = attr as VisibilityAttribute; NotRequiredAttribute notRequiredAttr = attr as NotRequiredAttribute; if (inputAttr != null) { anchorField.anchorType = AnchorType.Input; if (inputAttr.name != null) { anchorField.name = inputAttr.name; } } if (outputAttr != null) { anchorField.anchorType = AnchorType.Output; if (outputAttr.name != null) { anchorField.name = outputAttr.name; } } if (colorAttr != null) { anchorField.color = new SerializableColor(colorAttr.color); } if (offsetAttr != null) { anchorField.offset = offsetAttr.offset; anchorField.padding = offsetAttr.padding; } if (notRequiredAttr != null) { anchorField.required = false; } if (visibilityAttr != null) { anchorField.defaultVisibility = visibilityAttr.visibility; } } if (anchorField.anchorType == AnchorType.None) //field does not have a PW attribute { anchorFieldDictionary.Remove(field.Name); } else { //create anchor in this anchorField if there is not existing one if (anchorField.anchors.Count == 0) { anchorField.CreateNewAnchor(); } anchorField.colorSchemeName = ColorTheme.GetAnchorColorSchemeName(field.FieldType); anchorField.fieldName = field.Name; anchorField.fieldType = (SerializableType)field.FieldType; anchorField.fieldValue = field.GetValue(this); anchorField.nodeRef = this; } } //remove inhexistants field dictionary entries (for renamed variables): var toRemoveKeys = new List <string>(); foreach (var kp in anchorFieldDictionary) { if (!actualFields.Contains(kp.Key)) { toRemoveKeys.Add(kp.Key); } } foreach (var toRemoveKey in toRemoveKeys) { anchorFieldDictionary.Remove(toRemoveKey); } }