EditorObjects.Visualization.HierarchyNode.SetRelativeX C# (CSharp) Method

SetRelativeX() public method

public SetRelativeX ( ) : void
return void
        public void SetRelativeX()
        {
            if (Parent == null)
            {
                return;
            }

            float parentWidth = ((HierarchyNode)Parent).Width;

            int thisIndexAsChild = Parent.Children.IndexOf(this);

            float runningRelativeX = 0;

            for (int i = 0; i < thisIndexAsChild; i++)
            {
                runningRelativeX += ((HierarchyNode)Parent.Children[i]).Width;
            }

            this.RelativeX = -parentWidth / 2 + runningRelativeX + this.Width / 2.0f;


        }

Usage Example

        public void AutoPosition()
        {
            const float depthSpacing = 4;

            for (int i = 0; i < mNodesAsList.Count; i++)
            {
                HierarchyNode node = mNodesAsList[i];

                node.RelativeY = -depthSpacing;

                IAttachable parent = GetParent(node.ObjectRepresenting);

                if (parent == null)
                {
                    node.AttachTo(null, false);
                    node.Y = 5;
                }
                else
                {
                    HierarchyNode parentNode = GetNodeFromAttachable(parent);
                    node.AttachTo(parentNode, false);
                }
            }

            mUnparentedNodes.Clear();

            // Gotta do this after all attachments have been made
            for (int i = 0; i < mNodesAsList.Count; i++)
            {
                HierarchyNode node = mNodesAsList[i];

                if (node.Parent != null)
                {
                    node.SetRelativeX();
                    node.ForceUpdateDependencies();
                }
                else
                {
                    float xToStartAt = 0;

                    if (mUnparentedNodes.Count != 0)
                    {
                        xToStartAt = mUnparentedNodes.Last.X +
                                     mUnparentedNodes.Last.Width / 2.0f;
                    }

                    node.Y = 5;
                    node.X = xToStartAt + node.Width / 2.0f;

                    mUnparentedNodes.Add(node);
                }
            }

            UpdateSelectionMarker();
        }