示例#1
0
        private bool TryGetElementIdAtPosition(ref Vector3 worldPosition, out Guid elementId)
        {
            var   hitResults   = GetAdornerVisualsAtPosition(ref worldPosition) as List <UIRenderFeature.HitTestResult>;
            float smallestArea = float.MaxValue;

            elementId = default;
            for (int i = 0; i < hitResults.Count; i++)
            {
                UIRenderFeature.HitTestResult hr = hitResults[i];
                if (hr.Element.DependencyProperties.TryGetValue(AssociatedElementIdPropertyKey, out var thisElementId))
                {
                    var editor    = Controller.Editor;
                    var partId    = new AbsoluteId(editor.Asset.Id, thisElementId);
                    var element   = editor.FindPartViewModel(partId);
                    var uielement = (element as UIElementViewModel)?.AssetSideUIElement as UIElement;

                    if (uielement?.IsVisibleInTree ?? false)
                    {
                        float mysize = uielement.Width * uielement.Height;
                        if (mysize < smallestArea)
                        {
                            smallestArea = mysize;
                            elementId    = thisElementId;
                        }
                    }
                }
            }
            return(smallestArea < float.MaxValue);
        }
示例#2
0
        private bool TryGetElementIdAtPosition(ref Vector3 worldPosition, out Guid elementId)
        {
            var   hitResults   = GetAdornerVisualsAtPosition(ref worldPosition) as List <UIRenderFeature.HitTestResult>;
            float smallestArea = float.MaxValue;

            elementId = default;
            var uiComponent = Controller.GetEntityByName(UIEditorController.AdornerEntityName).Get <UIComponent>();

            for (int i = 0; i < hitResults.Count; i++)
            {
                UIRenderFeature.HitTestResult hr = hitResults[i];
                if (hr.Element.DependencyProperties.TryGetValue(AssociatedElementIdPropertyKey, out var thisElementId))
                {
                    var editor    = Controller.Editor;
                    var partId    = new AbsoluteId(editor.Asset.Id, thisElementId);
                    var element   = editor.FindPartViewModel(partId);
                    var uielement = (element as UIElementViewModel)?.AssetSideUIElement as UIElement;

                    if (uielement?.IsVisibleInTree ?? false)
                    {
                        // make sure we get a size that isn't NaN
                        float     mysize;
                        UIElement lookat = uielement;
                        do
                        {
                            mysize = lookat.Width * lookat.Height;
                            if (float.IsNaN(mysize) == false)
                            {
                                break;
                            }
                            if (uielement is TextBlock tb)
                            {
                                mysize = tb.CalculateTextSize().Area();
                                break;
                            }
                            if (lookat.Parent == null)
                            {
                                mysize = uiComponent.Resolution.X * uiComponent.Resolution.Y;
                                break;
                            }
                            lookat = lookat.Parent;
                        } while (true);
                        // ok, use this size info to select the smallest element
                        if (mysize < smallestArea)
                        {
                            smallestArea = mysize;
                            elementId    = thisElementId;
                        }
                    }
                }
            }
            return(smallestArea < float.MaxValue);
        }