internal static void GetStyleRectOffset(StyleBlock block, string propertyKey, RectOffset src)
 {
     src.left   = block.GetInt(propertyKey + "-left", src.left);
     src.right  = block.GetInt(propertyKey + "-right", src.right);
     src.top    = block.GetInt(propertyKey + "-top", src.top);
     src.bottom = block.GetInt(propertyKey + "-bottom", src.bottom);
 }
            // borderWidths The width of the borders(left, top, right and bottom). If Vector4.zero, the full texture is drawn.
            // borderRadiuses The radiuses for rounded corners (top-left, top-right, bottom-right and bottom-left). If Vector4.zero, corners will not be rounded.
            public StyleBorder(StyleBlock block)
            {
                if (block.HasValue(StyleCatalogKeyword.border))
                {
                    var defaultColor = block.GetColor(StyleCatalogKeyword.borderColor);
                    var borderWidth  = block.GetFloat(StyleCatalogKeyword.borderWidth);

                    widths = new Vector4(
                        block.GetFloat(StyleCatalogKeyword.borderLeftWidth, borderWidth),
                        block.GetFloat(StyleCatalogKeyword.borderTopWidth, borderWidth),
                        block.GetFloat(StyleCatalogKeyword.borderRightWidth, borderWidth),
                        block.GetFloat(StyleCatalogKeyword.borderBottomWidth, borderWidth));
                    borderLeftColor   = block.GetColor(StyleCatalogKeyword.borderLeftColor, defaultColor);
                    borderTopColor    = block.GetColor(StyleCatalogKeyword.borderTopColor, defaultColor);
                    borderRightColor  = block.GetColor(StyleCatalogKeyword.borderRightColor, defaultColor);
                    borderBottomColor = block.GetColor(StyleCatalogKeyword.borderBottomColor, defaultColor);
                }
                else
                {
                    widths          = Vector4.zero;
                    borderLeftColor = borderTopColor = borderRightColor = borderBottomColor = Color.cyan;
                }

                var defaultRadius = block.GetFloat(StyleCatalogKeyword.borderRadius);

                radius = new Vector4(
                    block.GetFloat(k_BorderTopLeftRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderTopRightRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderBottomRightRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderBottomLeftRadiusKey, defaultRadius));
            }
 internal static T ParseEnum <T>(StyleBlock block, int key, T defaultValue)
 {
     try
     {
         if (block.HasValue(key, StyleValue.Type.Text))
         {
             return((T)Enum.Parse(typeof(T), block.GetText(key), true));
         }
         return(defaultValue);
     }
     catch
     {
         return(defaultValue);
     }
 }
        private static bool DrawGradient(StyleBlock block, string funcName, List <StyleSheetResolver.Value[]> args, GradientParams gp)
        {
            if (funcName != "linear-gradient")
            {
                return(false);
            }

            var gradientTexture = GenerateGradient(gp.rect, args);

            if (gradientTexture == null)
            {
                return(false);
            }
            GUI.DrawTexture(gp.rect, gradientTexture, ScaleMode.ScaleAndCrop, true, 0f, gp.colorTint, Vector4.zero, gp.radius);
            return(true);
        }
        internal static void PopulateStyleState(StyleBlock styleBlock, GUIStyleState state, GUIStyleState defaultState)
        {
            var background = styleBlock.GetResource <Texture2D>(StyleCatalogKeyword.backgroundImage);

            if (background != null)
            {
                state.background = background;
            }
            var scaledBackground = styleBlock.GetResource <Texture2D>(StyleCatalogKeyword.scaledBackgroundImage);

            if (scaledBackground != null)
            {
                state.scaledBackgrounds = new[] { scaledBackground };
            }
            state.textColor = styleBlock.GetColor(StyleCatalogKeyword.color, (state.background != null || defaultState == null) ? state.textColor : defaultState.textColor);
        }
            // borderWidths The width of the borders(left, top, right and bottom). If Vector4.zero, the full texture is drawn.
            // borderRadiuses The radiuses for rounded corners (top-left, top-right, bottom-right and bottom-left). If Vector4.zero, corners will not be rounded.
            public StyleBorder(StyleBlock block)
            {
                color = block.GetColor(StyleCatalogKeyword.borderColor);

                var defaultRadius = block.GetFloat(StyleCatalogKeyword.borderRadius);
                var borderWidth   = block.GetFloat(StyleCatalogKeyword.borderWidth);

                widths = new Vector4(
                    block.GetFloat(StyleCatalogKeyword.borderLeftWidth, borderWidth),
                    block.GetFloat(StyleCatalogKeyword.borderTopWidth, borderWidth),
                    block.GetFloat(StyleCatalogKeyword.borderRightWidth, borderWidth),
                    block.GetFloat(StyleCatalogKeyword.borderBottomWidth, borderWidth));
                radius = new Vector4(
                    block.GetFloat(k_BorderTopLeftRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderTopRightRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderBottomRightRadiusKey, defaultRadius),
                    block.GetFloat(k_BorderBottomLeftRadiusKey, defaultRadius));
            }
Beispiel #7
0
            // borderWidths The width of the borders(left, top, right and bottom). If Vector4.zero, the full texture is drawn.
            // borderRadiuses The radiuses for rounded corners (top-left, top-right, bottom-right and bottom-left). If Vector4.zero, corners will not be rounded.
            public StyleBorder(StyleBlock block)
            {
                if (block.HasValue(StyleCatalogKeyword.border))
                {
                    var defaultColor = block.GetColor(StyleCatalogKeyword.borderColor);

                    var borderWidths = block.GetRect(StyleCatalogKeyword.borderWidth);
                    widths            = new Vector4(borderWidths.left, borderWidths.top, borderWidths.right, borderWidths.bottom);
                    borderLeftColor   = block.GetColor(StyleCatalogKeyword.borderLeftColor, defaultColor);
                    borderTopColor    = block.GetColor(StyleCatalogKeyword.borderTopColor, defaultColor);
                    borderRightColor  = block.GetColor(StyleCatalogKeyword.borderRightColor, defaultColor);
                    borderBottomColor = block.GetColor(StyleCatalogKeyword.borderBottomColor, defaultColor);
                }
                else
                {
                    widths          = Vector4.zero;
                    borderLeftColor = borderTopColor = borderRightColor = borderBottomColor = Color.cyan;
                }

                var borderRadius = block.GetRect(StyleCatalogKeyword.borderRadius);

                radius = new Vector4(borderRadius.left, borderRadius.top, borderRadius.right, borderRadius.bottom);
            }
        internal static bool DrawStyle(GUIStyle gs, Rect position, GUIContent content, DrawStates states)
        {
            if (gs == GUIStyle.none || String.IsNullOrEmpty(gs.name) || gs.normal.background != null)
            {
                return(false);
            }

            if (!GUIClip.visibleRect.Overlaps(position))
            {
                return(true);
            }

            var        styleName = GUIStyleExtensions.StyleNameToBlockName(gs.name, false);
            StyleBlock block     = FindBlock(styleName.GetHashCode(), states);

            if (!block.IsValid())
            {
                return(false);
            }

            DrawBlock(gs, block, position, content, states);

            return(true);
        }
Beispiel #9
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);
        }
Beispiel #10
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));
                }
            }
        }