예제 #1
0
        public Connection PickConnectionWithText(float x, float y)
        {
            var pickedNode = Graph.PickNode(x, y);

            if (pickedNode == null)
            {
                return(null);
            }

            foreach (var output in pickedNode.Outputs)
            {
                var s     = NvgHelper.MeasureString(output.Text);
                var bound = output.GetBounds();
                var r     = bound.Radius;
                var twor  = 2 * r;

                var outputRect = new Rectangle(bound.X - twor - s.Width, bound.Y - r, s.Width, s.Height);
                if (!outputRect.Pick(x, y))
                {
                    continue;
                }

                return(output);
            }

            return(null);
        }
예제 #2
0
        public static void StartEditing(MainWindow window, Connection output)
        {
            var s          = NvgHelper.MeasureString(output.Text);
            var bound      = output.GetBounds();
            var r          = bound.Radius;
            var twor       = 2 * r;
            var outputRect = new Rectangle(bound.X - twor - s.Width - 3, bound.Y - r - 3, s.Width + 6, s.Height + 6);

            if (TextBox != null)
            {
                Destroy();
            }

            EditingConnection = output;
            TextBox           = new TextBox(window, outputRect)
            {
                Text      = output.Text,
                CursorPos = output.Text.Length
            };
            TextBox.Commit += (sender, args) =>
            {
                var finalText = ((TextBox)sender).Text;
                if (string.IsNullOrWhiteSpace(finalText))
                {
                    finalText = "Empty Dialog Option";
                }

                EditingConnection.Text = finalText;
                Destroy(false);
            };
        }
예제 #3
0
        public static TextBox PickAndCreate(MainWindow window, Graph graph, float x, float y)
        {
            var pickedNode = graph.PickNode(x, y);

            if (TextBox != null && TextBox.BoundingBox.Pick(x, y))
            {
                return(TextBox);
            }

            if (pickedNode == null)
            {
                return(null);
            }

            foreach (var output in pickedNode.Outputs)
            {
                var s     = NvgHelper.MeasureString(output.Text);
                var bound = output.GetBounds();
                var r     = bound.Radius;
                var twor  = 2 * r;

                var outputRect = new Rectangle(bound.X - twor - s.Width, bound.Y - r, s.Width, s.Height);
                if (!outputRect.Pick(x, y) || !output.CanEditName)
                {
                    continue;
                }

                StartEditing(window, output);
                return(TextBox);
            }

            return(null);
        }
예제 #4
0
        public void Render()
        {
            var picked = Pick(_window.MouseScreenSpace.X, _window.MouseScreenSpace.Y);

            var lineHeight = (int)(_window.FontLineHeight * 1.5f);

            NanoVG.nvgFillColor(MainWindow.Nvg, picked ? Color.LightGray.ToNvgColor() : Color.White.ToNvgColor());
            NanoVG.nvgBeginPath(MainWindow.Nvg);
            NanoVG.nvgRect(MainWindow.Nvg, 0, lineHeight * Index, Parent.Width, lineHeight);
            NanoVG.nvgFill(MainWindow.Nvg);

            NanoVG.nvgSave(MainWindow.Nvg);
            NanoVG.nvgFillColor(MainWindow.Nvg, Color.Black.ToNvgColor());
            NanoVG.nvgTranslate(MainWindow.Nvg, 3, lineHeight * Index + 3);
            if (Shortcut == null)
            {
                NvgHelper.RenderString(Text);
            }
            else
            {
                NvgHelper.RenderString($"{Shortcut} {Text}");
                NanoVG.nvgFillColor(MainWindow.Nvg, Color.DarkGray.ToNvgColor());
                NvgHelper.RenderString(Shortcut);
            }

            NanoVG.nvgRestore(MainWindow.Nvg);
        }
예제 #5
0
        public void RenderNode(Node node)
        {
            if (!ScreenContains(node))
            {
                return;
            }
            const int   borderRadius   = 6;
            const int   panelInset     = 2;
            const float halfPanelInset = panelInset / 2f;

            var headerHeight = (int)(_window.FontLineHeight * 1.2f);

            NanoVG.nvgSave(MainWindow.Nvg);

            if (_window.Selection.SelectedNodes.Contains(node))
            {
                NanoVG.nvgFillColor(MainWindow.Nvg, Color.Black.ToNvgColor());
                NanoVG.nvgBeginPath(MainWindow.Nvg);
                NanoVG.nvgRoundedRect(MainWindow.Nvg, node.X - panelInset - 2, node.Y - 2, node.Width + 2 * (2 + panelInset), node.Height + 4, borderRadius + 2);
                NanoVG.nvgFill(MainWindow.Nvg);
            }

            NanoVG.nvgFillColor(MainWindow.Nvg, _colorMap.ContainsKey(node.NodeInfo) ? _colorMap[node.NodeInfo] : NanoVG.nvgRGBA(0, 0, 0, 255));

            NanoVG.nvgBeginPath(MainWindow.Nvg);
            NanoVG.nvgRoundedRect(MainWindow.Nvg, node.X - panelInset, node.Y, node.Width + 2 * panelInset, node.Height, borderRadius);
            NanoVG.nvgFill(MainWindow.Nvg);

            NanoVG.nvgFillColor(MainWindow.Nvg, Color.DarkSlateGray.ToNvgColor());
            NanoVG.nvgBeginPath(MainWindow.Nvg);
            NanoVG.nvgRoundedRect(MainWindow.Nvg, node.X, node.Y + headerHeight + panelInset,
                                  node.Width, node.Height - headerHeight - 2 * panelInset,
                                  borderRadius - halfPanelInset);
            NanoVG.nvgFill(MainWindow.Nvg);

            NanoVG.nvgFillColor(MainWindow.Nvg, Color.White.ToNvgColor());

            NanoVG.nvgSave(MainWindow.Nvg);
            var headerOffset = (headerHeight + panelInset) / 2f - NvgHelper.MeasureString(node.Name).Height / 2;

            NanoVG.nvgTranslate(MainWindow.Nvg, (int)(node.X + 2 * panelInset), (int)(node.Y + headerOffset));
            NvgHelper.RenderString(node.Name);
            NanoVG.nvgRestore(MainWindow.Nvg);

            if (node.Input != null)
            {
                RenderConnector(node.Input);
            }

            foreach (var nodeOutput in node.Outputs)
            {
                RenderConnector(nodeOutput);
            }
            NanoVG.nvgRestore(MainWindow.Nvg);
        }
예제 #6
0
        public int GetNodeWidth(Node node)
        {
            var       width       = 120;
            const int textPadding = 40;

            width = (int)Math.Max(NvgHelper.MeasureString(node.Name).Width + textPadding, width);

            if (node.Input != null)
            {
                width = (int)Math.Max(NvgHelper.MeasureString(node.Input.Text).Width + textPadding, width);
            }

            foreach (var connection in node.Outputs)
            {
                width = (int)Math.Max(NvgHelper.MeasureString(connection.Text).Width + textPadding, width);
            }

            width = (int)(Math.Ceiling(width / gridPitch) * gridPitch);

            return(width);
        }
예제 #7
0
        private void RenderConnector(Connection connection)
        {
            NanoVG.nvgSave(MainWindow.Nvg);

            var pickedForDeletion =
                _window.Selection.HoveringConnection == connection && _window.Keyboard[Key.ShiftLeft];
            var       bound          = connection.GetBounds();
            var       r              = bound.Radius;
            var       twor           = 2 * r;
            var       halfr          = r / 2;
            const int cxnBorderWidth = 2;

            NanoVG.nvgFillColor(MainWindow.Nvg, Color.White.ToNvgColor());

            NanoVG.nvgSave(MainWindow.Nvg);
            if (connection != TextBoxHandler.EditingConnection)
            {
                switch (connection.Side)
                {
                case NodeSide.Input:
                    NanoVG.nvgTranslate(MainWindow.Nvg, bound.X + twor, bound.Y - r);
                    NvgHelper.RenderString(connection.Text);
                    break;

                case NodeSide.Output:
                    var s = connection.Text;
                    NanoVG.nvgTranslate(MainWindow.Nvg, bound.X - twor - NvgHelper.MeasureString(s).Width, bound.Y - r);
                    NvgHelper.RenderString(s);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
            else
            {
                TextBoxHandler.TextBox.RenderBackground();
                TextBoxHandler.TextBox.RenderForeground();
            }

            NanoVG.nvgRestore(MainWindow.Nvg);

            NanoVG.nvgFillColor(MainWindow.Nvg, Color.DarkSlateGray.ToNvgColor());

            NanoVG.nvgBeginPath(MainWindow.Nvg);
            NanoVG.nvgCircle(MainWindow.Nvg, bound.X, bound.Y, r + cxnBorderWidth);
            NanoVG.nvgFill(MainWindow.Nvg);

            NanoVG.nvgFillColor(MainWindow.Nvg, connection.Side == NodeSide.Input ? Color.DeepSkyBlue.ToNvgColor() : Color.LimeGreen.ToNvgColor());
            NanoVG.nvgBeginPath(MainWindow.Nvg);
            NanoVG.nvgCircle(MainWindow.Nvg, bound.X, bound.Y, r);
            NanoVG.nvgFill(MainWindow.Nvg);

            NanoVG.nvgBeginPath(MainWindow.Nvg);
            if (_window.Selection.HoveringConnection != null && _window.Selection.DraggingConnection == connection &&
                _window.Selection.HoveringConnection.Side != _window.Selection.DraggingConnection.Side)
            {
                NanoVG.nvgFillColor(MainWindow.Nvg, Color.SlateGray.ToNvgColor());
            }
            else if (connection.ConnectedNode != null)
            {
                NanoVG.nvgFillColor(MainWindow.Nvg, Color.DarkSlateGray.ToNvgColor());
            }

            NanoVG.nvgCircle(MainWindow.Nvg, bound.X, bound.Y, halfr);
            NanoVG.nvgFill(MainWindow.Nvg);

            if (pickedForDeletion)
            {
                NanoVG.nvgFillColor(MainWindow.Nvg, Color.Red.ToNvgColor());

                NanoVG.nvgBeginPath(MainWindow.Nvg);
                NanoVG.nvgCircle(MainWindow.Nvg, bound.X, bound.Y, halfr);
                NanoVG.nvgFill(MainWindow.Nvg);
            }

            NanoVG.nvgRestore(MainWindow.Nvg);
        }
예제 #8
0
        private void Render(object sender, FrameEventArgs e)
        {
            // Reset the view
            GL.Clear(ClearBufferMask.ColorBufferBit |
                     ClearBufferMask.DepthBufferBit |
                     ClearBufferMask.StencilBufferBit);

            //GL.PushMatrix();
            NanoVG.nvgBeginFrame(Nvg, Width, Height, 1);
            NanoVG.nvgSave(Nvg);
            NanoVG.nvgScale(Nvg, Zoom, Zoom);

            //_grid.Draw();

            NanoVG.nvgTranslate(Nvg, _nodeRenderer.gridOffset.X, _nodeRenderer.gridOffset.Y);
            NanoVG.nvgStrokeWidth(Nvg, 2);

            NanoVG.nvgStrokeColor(Nvg, NanoVG.nvgRGBA(0, 0, 0, 255));

            NanoVG.nvgBeginPath(Nvg);
            NanoVG.nvgMoveTo(Nvg, -_nodeRenderer.gridPitch, 0);
            NanoVG.nvgLineTo(Nvg, _nodeRenderer.gridPitch, 0);
            NanoVG.nvgStroke(Nvg);

            NanoVG.nvgBeginPath(Nvg);
            NanoVG.nvgMoveTo(Nvg, 0, -_nodeRenderer.gridPitch);
            NanoVG.nvgLineTo(Nvg, 0, _nodeRenderer.gridPitch);
            NanoVG.nvgStroke(Nvg);

            const int cxnLineWidth = 3;

            NanoVG.nvgStrokeWidth(Nvg, cxnLineWidth);
            if (Selection.DraggingConnection != null)
            {
                var end = _mouseCanvasSpace;

                if (Selection.CreatingConnectedNode)
                {
                    end = new Vector2(_contextMenu.X, _contextMenu.Y);
                }

                var picked = Selection.HoveringConnection;
                if (picked != null && picked.Side != Selection.DraggingConnection.Side)
                {
                    var b = picked.GetBounds();
                    end = new Vector2(b.X, b.Y);
                }

                _nodeRenderer.RenderConnection(Selection.DraggingConnection, end);
            }

            foreach (var node in Graph)
            {
                _nodeRenderer.RenderConnections(node);
            }

            foreach (var node in Graph)
            {
                _nodeRenderer.RenderNode(node);
            }

            if (Selection.SelectionRectangle != null && Selection.SelectionRectangle.Width != 0 && Selection.SelectionRectangle.Height != 0)
            {
                NanoVG.nvgFillColor(Nvg, _selectionRectangleColor.ToNvgColor());
                NanoVG.nvgBeginPath(Nvg);
                var r = Selection.SelectionRectangle;
                var x = Math.Min(r.X, r.X + r.Width);
                var y = Math.Min(r.Y, r.Y + r.Height);
                var w = Math.Abs(r.Width);
                var h = Math.Abs(r.Height);
                NanoVG.nvgRect(Nvg, x, y, w, h);
                NanoVG.nvgFill(Nvg);
            }
            NanoVG.nvgRestore(Nvg);

            _contextMenu.Render();

            GL.Color4(0, 0, 0, 1f);
            if (Keyboard[Key.D] && Focused && TextBoxHandler.TextBox == null)
            {
                // Static diagnostic header
                NanoVG.nvgSave(Nvg);
                NanoVG.nvgFillColor(Nvg, Color.Black.ToNvgColor());
                NvgHelper.RenderString($"{Zoom}x Zoom");
                NanoVG.nvgTranslate(Nvg, 0, FontLineHeight);
                NvgHelper.RenderString($"{Graph.Count} Nodes");

                // Sparklines
                //                GL.Translate(5, (int)(Height - Font.Common.LineHeight * 1.4f * 2), 0);
                //                _fpsSparkline.Render(Color.Blue, Color.LimeGreen);
                //                GL.Translate(0, (int)(Font.Common.LineHeight * 1.4f), 0);
                //                _renderTimeSparkline.Render(Color.Blue, Color.LimeGreen);
                NanoVG.nvgRestore(Nvg);
            }

            NanoVG.nvgEndFrame(Nvg);
            // Swap the graphics buffer
            SwapBuffers();
        }