Пример #1
0
        protected void DrawBorder(MeshGenerationContext mgc, Rect rect, Color color, float alpha)
        {
            if (mgc == null)
            {
                throw new NullReferenceException("The MeshGenerationContext is null");
            }

            color.a = alpha;
            rect.xMin++;
            rect.xMax--;
            rect.yMin++;
            rect.yMax--;
            var width  = rect.xMax - rect.xMin;
            var height = rect.yMax - rect.yMin;

            var topRect    = new Rect(rect.xMin, rect.yMin, width, 1);
            var bottomRect = new Rect(rect.xMin, rect.yMax, width, 1);
            var rightRect  = new Rect(rect.xMax, rect.yMin, 1, height);
            var lefRect    = new Rect(rect.xMin, rect.yMin, 1, height);

            var rectParams = MeshGenerationContextUtils.RectangleParams.MakeSolid(topRect, color, mgc.visualElement.panel.contextType);

            mgc.Rectangle(rectParams);

            rectParams = MeshGenerationContextUtils.RectangleParams.MakeSolid(bottomRect, color, mgc.visualElement.panel.contextType);
            mgc.Rectangle(rectParams);

            rectParams = MeshGenerationContextUtils.RectangleParams.MakeSolid(rightRect, color, mgc.visualElement.panel.contextType);
            mgc.Rectangle(rectParams);

            rectParams = MeshGenerationContextUtils.RectangleParams.MakeSolid(lefRect, color, mgc.visualElement.panel.contextType);
            mgc.Rectangle(rectParams);
        }
Пример #2
0
        void OnGenerateVisualContent(MeshGenerationContext mgc)
        {
            GraphView gView = GetFirstAncestorOfType <GraphView>();

            if (gView == null)
            {
                return;
            }

            VisualElement container = gView.contentViewContainer;

            foreach (Line2 line in lines)
            {
                Vector2 start  = container.ChangeCoordinatesTo(gView, line.start);
                Vector2 end    = container.ChangeCoordinatesTo(gView, line.end);
                float   x      = Math.Min(start.x, end.x);
                float   y      = Math.Min(start.y, end.y);
                float   width  = Math.Max(1, Math.Abs(start.x - end.x));
                float   height = Math.Max(1, Math.Abs(start.y - end.y));

                var rect = new Rect(x, y, width, height);

                mgc.Rectangle(MeshGenerationContextUtils.RectangleParams.MakeSolid(rect, s_SnappingLineColor, ContextType.Editor));
            }
        }
Пример #3
0
        protected void DrawRect(MeshGenerationContext mgc, Rect rect, Color color, float alpha)
        {
            if (mgc == null)
            {
                throw new NullReferenceException("The MeshGenerationContext is null");
            }

            color.a = alpha;

            var rectParams = MeshGenerationContextUtils.RectangleParams.MakeSolid(rect, color, mgc.visualElement.panel.contextType);

            mgc.Rectangle(rectParams);
        }
Пример #4
0
            internal void DrawWithTextSelectionAndCursor(MeshGenerationContext mgc, string newText)
            {
                var playmodeTintColor = panel.contextType == ContextType.Editor
                    ? UIElementsUtility.editorPlayModeTintColor
                    : Color.white;

                var keyboardTextEditor = editorEventHandler as KeyboardTextEditorEventHandler;

                if (keyboardTextEditor == null)
                {
                    return;
                }

                keyboardTextEditor.PreDrawCursor(newText);

                int  cursorIndex   = editorEngine.cursorIndex;
                int  selectIndex   = editorEngine.selectIndex;
                Rect localPosition = editorEngine.localPosition;
                var  scrollOffset  = editorEngine.scrollOffset;

                float textScaling = TextHandle.ComputeTextScaling(worldTransform, GUIUtility.pixelsPerPoint);

                var textParams = MeshGenerationContextUtils.TextParams.MakeStyleBased(this, text);

                textParams.text          = " ";
                textParams.wordWrapWidth = 0.0f;
                textParams.wordWrap      = false;

                float lineHeight = m_TextHandle.ComputeTextHeight(textParams, textScaling);

                float wordWrapWidth = 0.0f;

                // Make sure to take into account the word wrap style...
                if (editorEngine.multiline && (resolvedStyle.whiteSpace == WhiteSpace.Normal))
                {
                    wordWrapWidth = contentRect.width;

                    // Since the wrapping is enabled, there is no need to offset the text... It will always fit the space on screen !
                    scrollOffset = Vector2.zero;
                }

                GUIUtility.compositionCursorPos = editorEngine.graphicalCursorPos - scrollOffset +
                                                  new Vector2(localPosition.x, localPosition.y + lineHeight);

                Color drawCursorColor = cursorColor;

                int selectionEndIndex = string.IsNullOrEmpty(GUIUtility.compositionString)
                    ? selectIndex
                    : cursorIndex + GUIUtility.compositionString.Length;

                CursorPositionStylePainterParameters cursorParams;

                // Draw highlighted section, if any
                if ((cursorIndex != selectionEndIndex) && !isDragging)
                {
                    int min = cursorIndex < selectionEndIndex ? cursorIndex : selectionEndIndex;
                    int max = cursorIndex > selectionEndIndex ? cursorIndex : selectionEndIndex;

                    cursorParams               = CursorPositionStylePainterParameters.GetDefault(this, text);
                    cursorParams.text          = editorEngine.text;
                    cursorParams.wordWrapWidth = wordWrapWidth;
                    cursorParams.cursorIndex   = min;

                    Vector2 minPos = m_TextHandle.GetCursorPosition(cursorParams, textScaling);

                    cursorParams.cursorIndex = max;
                    Vector2 maxPos = m_TextHandle.GetCursorPosition(cursorParams, textScaling);

                    minPos -= scrollOffset;
                    maxPos -= scrollOffset;

                    if (Mathf.Approximately(minPos.y, maxPos.y))
                    {
                        mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams()
                        {
                            rect              = new Rect(minPos.x, minPos.y, maxPos.x - minPos.x, lineHeight),
                            color             = selectionColor,
                            playmodeTintColor = playmodeTintColor
                        });
                    }
                    else
                    {
                        // Draw first line
                        mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams()
                        {
                            rect              = new Rect(minPos.x, minPos.y, contentRect.xMax - minPos.x, lineHeight),
                            color             = selectionColor,
                            playmodeTintColor = playmodeTintColor
                        });

                        var inbetweenHeight = (maxPos.y - minPos.y) - lineHeight;
                        if (inbetweenHeight > 0f)
                        {
                            // Draw all lines in-between
                            mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams()
                            {
                                rect              = new Rect(contentRect.xMin, minPos.y + lineHeight, contentRect.width, inbetweenHeight),
                                color             = selectionColor,
                                playmodeTintColor = playmodeTintColor
                            });
                        }

                        // Draw last line if not empty
                        if (maxPos.x != contentRect.x)
                        {
                            mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams()
                            {
                                rect              = new Rect(contentRect.xMin, maxPos.y, maxPos.x, lineHeight),
                                color             = selectionColor,
                                playmodeTintColor = playmodeTintColor
                            });
                        }
                    }
                }

                // Draw the text with the scroll offset
                if (!string.IsNullOrEmpty(editorEngine.text) && contentRect.width > 0.0f && contentRect.height > 0.0f)
                {
                    textParams      = MeshGenerationContextUtils.TextParams.MakeStyleBased(this, text);
                    textParams.rect = new Rect(contentRect.x - scrollOffset.x, contentRect.y - scrollOffset.y, contentRect.width + scrollOffset.x, contentRect.height + scrollOffset.y);
                    textParams.text = editorEngine.text;

                    mgc.Text(textParams, m_TextHandle);
                }

                // Draw the cursor
                if (!isReadOnly && !isDragging)
                {
                    if (cursorIndex == selectionEndIndex && computedStyle.unityFont.value != null)
                    {
                        cursorParams               = CursorPositionStylePainterParameters.GetDefault(this, text);
                        cursorParams.text          = editorEngine.text;
                        cursorParams.wordWrapWidth = wordWrapWidth;
                        cursorParams.cursorIndex   = cursorIndex;

                        Vector2 cursorPosition = m_TextHandle.GetCursorPosition(cursorParams, textScaling);
                        cursorPosition -= scrollOffset;
                        mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams
                        {
                            rect              = new Rect(cursorPosition.x, cursorPosition.y, 1f, lineHeight),
                            color             = drawCursorColor,
                            playmodeTintColor = playmodeTintColor
                        });
                    }

                    // Draw alternate cursor, if any
                    if (editorEngine.altCursorPosition != -1)
                    {
                        cursorParams               = CursorPositionStylePainterParameters.GetDefault(this, text);
                        cursorParams.text          = editorEngine.text.Substring(0, editorEngine.altCursorPosition);
                        cursorParams.wordWrapWidth = wordWrapWidth;
                        cursorParams.cursorIndex   = editorEngine.altCursorPosition;

                        Vector2 altCursorPosition = m_TextHandle.GetCursorPosition(cursorParams, textScaling);
                        altCursorPosition -= scrollOffset;
                        mgc.Rectangle(new MeshGenerationContextUtils.RectangleParams
                        {
                            rect              = new Rect(altCursorPosition.x, altCursorPosition.y, 1f, lineHeight),
                            color             = drawCursorColor,
                            playmodeTintColor = playmodeTintColor
                        });
                    }
                }

                keyboardTextEditor.PostDrawCursor();
            }
 public static void SolidRectangle(MeshGenerationContext mgc, Rect rectParams, Color color, ContextType context)
 {
     mgc.Rectangle(MeshGenerationContextUtils.RectangleParams.MakeSolid(rectParams, color, context));
 }