// 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); } }
// 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 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)); } } }