Ejemplo n.º 1
0
        private static void DrawBackgroundImage(StyleBlock block, Rect position, Color colorTint)
        {
            var backgroundImage = block.GetTexture(StyleCatalogKeyword.backgroundImage, true);

            if (backgroundImage == null)
            {
                return;
            }

            var backgroundPosition = block.GetStruct <StyleBackgroundPosition>(StyleCatalogKeyword.backgroundPosition);
            var backgroundSize     = block.GetRect(StyleCatalogKeyword.backgroundSize, StyleRect.Size(backgroundImage.width, backgroundImage.height));

            StyleRect anchor = StyleRect.Nil;

            if (backgroundPosition.xEdge == StyleCatalogKeyword.left)
            {
                anchor.left = backgroundPosition.xOffset;
            }
            else if (backgroundPosition.yEdge == StyleCatalogKeyword.left)
            {
                anchor.left = backgroundPosition.yOffset;
            }
            if (backgroundPosition.xEdge == StyleCatalogKeyword.right)
            {
                anchor.right = backgroundPosition.xOffset;
            }
            else if (backgroundPosition.yEdge == StyleCatalogKeyword.right)
            {
                anchor.right = backgroundPosition.yOffset;
            }
            if (backgroundPosition.xEdge == StyleCatalogKeyword.top)
            {
                anchor.top = backgroundPosition.xOffset;
            }
            else if (backgroundPosition.yEdge == StyleCatalogKeyword.top)
            {
                anchor.top = backgroundPosition.yOffset;
            }
            if (backgroundPosition.xEdge == StyleCatalogKeyword.bottom)
            {
                anchor.bottom = backgroundPosition.xOffset;
            }
            else if (backgroundPosition.yEdge == StyleCatalogKeyword.bottom)
            {
                anchor.bottom = backgroundPosition.yOffset;
            }

            var bgRect = new Rect(position.xMin, position.yMin, backgroundSize.width, backgroundSize.height);

            if (anchor.left < 1.0f)
            {
                bgRect.xMin = position.xMin + position.width * anchor.left - backgroundSize.width / 2f;
            }
            else if (anchor.left >= 1.0f)
            {
                bgRect.xMin = position.xMin + anchor.left;
            }

            if (anchor.top < 1.0f)
            {
                bgRect.yMin = position.yMin + position.height * anchor.top - backgroundSize.height / 2f;
            }
            else if (anchor.top >= 1.0f)
            {
                bgRect.yMin = position.yMin + anchor.top;
            }

            if (anchor.right == 0f || anchor.right >= 1.0f)
            {
                bgRect.xMin = position.xMax - backgroundSize.width - anchor.right;
            }
            else if (anchor.right < 1.0f)
            {
                bgRect.xMin = position.xMax - position.width * anchor.right - backgroundSize.width / 2f;
            }

            if (anchor.bottom == 0f || anchor.bottom >= 1.0f)
            {
                bgRect.yMin = position.yMax - backgroundSize.height - anchor.bottom;
            }
            if (anchor.bottom < 1.0f)
            {
                bgRect.yMin = position.yMax - position.height * anchor.bottom - backgroundSize.height / 2f;
            }

            bgRect.width  = backgroundSize.width;
            bgRect.height = backgroundSize.height;

            using (new GUI.ColorScope(colorTint))
                GUI.DrawTexture(bgRect, backgroundImage);
        }
Ejemplo n.º 2
0
        internal static void DrawBlock(GUIStyle basis, StyleBlock block, Rect drawRect, GUIContent content, DrawStates states)
        {
            var userRect = drawRect;

            StyleRect offset = block.GetRect(StyleCatalogKeyword.position);

            drawRect.xMin += offset.left;
            drawRect.yMin += offset.top;
            drawRect.yMax += offset.bottom;
            drawRect.xMax += offset.right;

            // Adjust width and height if enforced by style block
            drawRect.width  = basis.fixedWidth == 0f ? drawRect.width : basis.fixedWidth;
            drawRect.height = basis.fixedHeight == 0f ? drawRect.height : basis.fixedHeight;

            Color colorTint = GUI.color;

            if (!GUI.enabled)
            {
                colorTint.a *= block.GetFloat(StyleCatalogKeyword.opacity, 0.5f);
            }
            var border      = new StyleBorder(block);
            var bgColorTint = GUI.backgroundColor * colorTint;

            if (!block.Execute(StyleCatalogKeyword.background, DrawGradient, new GradientParams(drawRect, border.radius, bgColorTint)))
            {
                // Draw background color
                var backgroundColor = block.GetColor(StyleCatalogKeyword.backgroundColor);
                if (backgroundColor.a > 0f)
                {
                    var smoothCorners = !border.all;
                    GUI.DrawTexture(drawRect, EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill, false, 0f, backgroundColor * bgColorTint, Vector4.zero, border.radius, smoothCorners);
                }
            }

            // Draw background image
            if (block.HasValue(StyleCatalogKeyword.backgroundImage))
            {
                DrawBackgroundImage(block, drawRect, bgColorTint);
            }

            if (content != null)
            {
                var guiContentColor = GUI.contentColor;

                // Compute content rect
                Rect contentRect = drawRect;
                if (block.GetKeyword(StyleCatalogKeyword.padding) == StyleValue.Keyword.Auto)
                {
                    GetContentCenteredRect(basis, content, ref contentRect);
                }

                // Draw content (text & image)
                bool  hasImage            = content.image != null;
                float opacity             = hasImage ? block.GetFloat(StyleCatalogKeyword.opacity, 1f) : 1f;
                float contentImageOffsetX = hasImage ? block.GetFloat(StyleCatalogKeyword.contentImageOffsetX) : 0;
                float contentImageOffsetY = hasImage ? block.GetFloat(StyleCatalogKeyword.contentImageOffsetY) : 0;
                basis.Internal_DrawContent(contentRect, content, states.isHover, states.isActive, states.on, states.hasKeyboardFocus,
                                           states.hasTextInput, states.drawSelectionAsComposition, states.cursorFirst, states.cursorLast,
                                           states.cursorColor, states.selectionColor, guiContentColor * opacity,
                                           0, 0, contentImageOffsetY, contentImageOffsetX, false, false);

                // Handle tooltip and hovering region
                if (!String.IsNullOrEmpty(content.tooltip) && contentRect.Contains(Event.current.mousePosition))
                {
                    GUIStyle.SetMouseTooltip(content.tooltip, contentRect);
                }
            }

            // Draw border
            if (border.any)
            {
                GUI.DrawTexture(drawRect, EditorGUIUtility.whiteTexture, ScaleMode.StretchToFill, true, 0f, border.borderLeftColor * colorTint,
                                border.borderTopColor * colorTint, border.borderRightColor * colorTint, border.borderBottomColor * colorTint, border.widths, border.radius);
            }

            if (block.GetKeyword(k_EnableHovering) == StyleValue.Keyword.True)
            {
                var currentView = GUIView.current;

                if (currentView != null)
                {
                    currentView.MarkHotRegion(GUIClip.UnclipToWindow(userRect));
                }
            }
        }