示例#1
0
        public static void Insert(ICanvas canvas,
                                  IEnumerable <IElement> elements,
                                  bool select,
                                  double offsetX,
                                  double offsetY)
        {
            var    thumbs = elements.Where(x => x is IThumb);
            int    count  = thumbs.Count();
            double minX   = count == 0 ? 0.0 : thumbs.Min(x => x.GetX());
            double minY   = count == 0 ? 0.0 : thumbs.Min(x => x.GetY());
            double dX     = offsetX != 0.0 ? minX - offsetX : 0.0;
            double dY     = offsetY != 0.0 ? minY - offsetY : 0.0;

            foreach (var element in elements)
            {
                canvas.Add(element);

                if (element is IThumb)
                {
                    MoveElement(element, dX, dY);
                }
                else if (element is ILine && (dX != 0.0 || dY != 0.0))
                {
                    MoveLine(element as ILine, dX, dY);
                }

                if (select == true)
                {
                    element.SetSelected(true);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Draws the value bars.
        /// </summary>
        /// <param name="canvas">The canvas.</param>
        /// <param name="points">The points.</param>
        /// <param name="itemSize">The item size.</param>
        /// <param name="origin">The origin.</param>
        /// <param name="headerHeight">The Header height.</param>
        protected void BuildBars(ICanvas canvas, Point[] points, Size itemSize, double origin, double headerHeight)
        {
            var entries = Control.Entries;

            const float MinBarHeight = 4;

            if (points.Length > 0)
            {
                for (int i = 0; i < entries.Count(); i++)
                {
                    var entry = entries.ElementAt(i);
                    var point = points[i];

                    var x      = point.X - (itemSize.Width / 2);
                    var y      = Math.Min(origin, point.Y);
                    var height = Math.Max(MinBarHeight, Math.Abs(origin - point.Y));
                    if (height < MinBarHeight)
                    {
                        height = MinBarHeight;
                        if (y + height > Margin + itemSize.Height)
                        {
                            y = headerHeight + itemSize.Height - height;
                        }
                    }

                    var barRect = Rectangle().Width(itemSize.Width).Height(height).Fill(SolidColorBrush().Color(entry.Color));
                    canvas.Add(x, y, barRect);
                }
            }
        }
示例#3
0
        private static ILine FirstConnection(ICanvas canvas, IElement root, double x, double y, List <Wire> wires, IDiagramCreator creator)
        {
            var    counter = canvas.GetCounter();
            string rootUid = root.GetUid();

            bool startIsIO = StringUtil.StartsWith(rootUid, Constants.TagElementInput) ||
                             StringUtil.StartsWith(rootUid, Constants.TagElementOutput);

            var line = creator.CreateElement(Constants.TagElementWire,
                                             new object[]
            {
                x, y, x, y,
                false, false,
                startIsIO, false,
                counter.Next()
            },
                                             0.0, 0.0, false) as ILine;

            wires.Add(new Wire(line, root, null));

            canvas.Add(line);

            if (line != null || !(line is ILine))
            {
                line.SetTag(root);
            }

            return(line);
        }
示例#4
0
        public static void Main(string[] args)
        {
            UnityContainer container = new UnityContainer();

            container.RegisterType <ICanvas <string>, ConsoleCanvas>();

            ICanvas <string> canvas = container.Resolve <ICanvas <string> >();

            Rect    rect    = new Rect(30, 40, new Point(10, 10));
            Square  square  = new Square(35, new Point(15, 30));
            Ellipse ellipse = new Ellipse(300, 200, new Point(100, 150));
            Circle  circle  = new Circle(300, new Point(1, 1));
            TextBox textBox = new TextBox(200, 100, new Point(5, 5), "sample text");

            canvas
            .Add(rect)
            .Add(square)
            .Add(ellipse)
            .Add(circle)
            .Add(textBox);
            IEnumerable <string> output = canvas.Print().ToArray();

            foreach (string line in output)
            {
                System.Console.WriteLine(line);
            }
            System.Console.Read();
        }
示例#5
0
        private void DrawBorder(ICanvas canvas, Point center, double radius)
        {
            var diameter = radius * 2;

            var circle = Ellipse().Width(diameter).Height(diameter).Stroke(SolidColorBrush().Color(BorderLineColor)).StrokeThickness(BorderLineSize);

            canvas.Add(center.X - radius, center.Y - radius, circle);
        }
示例#6
0
        public static IElement Pin(ICanvas canvas, IPoint point, IDiagramCreator creator, bool snap)
        {
            var thumb = creator.CreateElement(Constants.TagElementPin,
                new object[] { canvas.GetCounter().Next() },
                point.X, point.Y, snap) as IThumb;

            canvas.Add(thumb);

            return thumb;
        }
        public static IElement AndGate(ICanvas canvas, IPoint point, IDiagramCreator creator, bool snap)
        {
            var thumb = creator.CreateElement(Constants.TagElementAndGate,
                                              new object[] { canvas.GetCounter().Next() },
                                              point.X, point.Y, snap) as IThumb;

            canvas.Add(thumb);

            return(thumb);
        }
示例#8
0
        /// <summary>
        /// Draws the given point.
        /// </summary>
        /// <param name="canvas">The canvas.</param>
        /// <param name="point">The point.</param>
        /// <param name="color">The fill color.</param>
        /// <param name="size">The point size.</param>
        /// <param name="mode">The point mode.</param>
        public static void DrawPoint(this ICanvas canvas, Point point, Color color, float size, PointMode mode)
        {
            IShape shape;

            if (mode == PointMode.Square)
            {
                shape = Rectangle();
            }
            else if (mode == PointMode.Circle)
            {
                shape = Ellipse();
            }
            else
            {
                return;
            }

            canvas.Add(point.X - (size / 2), point.Y - (size / 2), shape.Width(size).Height(size).Fill(SolidColorBrush().Color(color)));
        }
示例#9
0
        /// <summary>
        /// Draws the bar background areas.
        /// </summary>
        /// <param name="canvas">The output canvas.</param>
        /// <param name="points">The entry points.</param>
        /// <param name="itemSize">The item size.</param>
        /// <param name="headerHeight">The header height.</param>
        protected void BuildBarAreas(ICanvas canvas, Point[] points, Size itemSize, double headerHeight)
        {
            if (points.Length > 0 && PointAreaAlpha > 0)
            {
                for (int i = 0; i < points.Length; i++)
                {
                    var entry = Entries.ElementAt(i);
                    var point = points[i];

                    var color = entry.Color.WithA((byte)(this.BarAreaAlpha * this.AnimationProgress));
                    var brush = SolidColorBrush().Color(color);

                    var max    = entry.Value > 0 ? headerHeight : headerHeight + itemSize.Height;
                    var height = Math.Abs(max - point.Y);
                    var y      = Math.Min(max, point.Y);

                    var barArea = Rectangle().Width(itemSize.Width).Height(height).Fill(brush);
                    canvas.Add(point.X - (itemSize.Width / 2), y, barArea);
                }
            }
        }
示例#10
0
        public override void BuildContent(ICanvas canvas, int width, int height)
        {
            var total = Entries?.Count() ?? 0;

            if (total > 0)
            {
                var captionHeight = Entries.Max(x =>
                {
                    var result = 0.0;

                    var hasLabel      = !string.IsNullOrEmpty(x.Label);
                    var hasValueLabel = !string.IsNullOrEmpty(x.ValueLabel);
                    if (hasLabel || hasValueLabel)
                    {
                        var hasOffset     = hasLabel && hasValueLabel;
                        var captionMargin = LabelTextSize * 0.60f;
                        var space         = hasOffset ? captionMargin : 0;

                        if (hasLabel)
                        {
                            result += LabelTextSize;
                        }

                        if (hasValueLabel)
                        {
                            result += LabelTextSize;
                        }
                    }

                    return(result);
                });

                var center     = new Point(width / 2, height / 2);
                var radius     = ((Math.Min(width, height) - (2 * Margin)) / 2) - captionHeight;
                var rangeAngle = (float)((Math.PI * 2) / total);
                var startAngle = (float)Math.PI;

                var nextEntry = Entries.First();
                var nextAngle = startAngle;
                var nextPoint = GetPoint(nextEntry.Value * AnimationProgress, center, nextAngle, radius);

                DrawBorder(canvas, center, radius);
#if LATER
                clip.AddCircle(center.X, center.Y, radius);
#endif

                for (int i = 0; i < total; i++)
                {
                    var angle = nextAngle;
                    var entry = nextEntry;
                    var point = nextPoint;

                    var nextIndex = (i + 1) % total;
                    nextAngle = startAngle + (rangeAngle * nextIndex);
                    nextEntry = Entries.ElementAt(nextIndex);
                    nextPoint = GetPoint(nextEntry.Value * AnimationProgress, center, nextAngle, radius);

#if LATER
                    canvas.Save();
                    canvas.ClipPath(clip);
#endif

                    // Border center bars
                    var borderPoint = GetPoint(MaxValue, center, angle, radius);
                    var line        = Line().X1(point.X).Y1(point.Y).X2(borderPoint.X).Y2(borderPoint.Y).Stroke(SolidColorBrush().Color(BorderLineColor)).StrokeThickness(BorderLineSize);
                    canvas.Add(0, 0, line);

                    // Values points and lines
                    //PathEffect = SKPathEffect.CreateDash(new[] { BorderLineSize, BorderLineSize * 2 }, 0),

                    var amount      = Math.Abs(entry.Value - AbsoluteMinimum) / ValueRange;
                    var diameter    = radius * amount * 2;
                    var circleColor = entry.Color.WithA((byte)(entry.Color.A * 0.75f * AnimationProgress));
                    var circle      = Ellipse().Width(diameter).Height(diameter).Stroke(SolidColorBrush().Color(circleColor)).StrokeThickness(BorderLineSize);
                    canvas.Add(center.X - diameter / 2, center.Y - diameter / 2, circle);

                    canvas.DrawGradientLine(center, entry.Color.WithA(0), point, entry.Color.WithA((byte)(entry.Color.A * 0.75f)), LineSize);
                    canvas.DrawGradientLine(point, entry.Color, nextPoint, nextEntry.Color, LineSize);
                    canvas.DrawPoint(point, entry.Color, (float)PointSize, PointMode);
                }

#if LATER
                canvas.Restore();
#endif

#if LATER
                // Labels
                var labelPoint = new Point(0, radius + LabelTextSize + (PointSize / 2));
                var rotation   = SKMatrix.MakeRotation(angle);
                labelPoint = center + rotation.MapPoint(labelPoint);
                var alignment = SKTextAlign.Left;

                if ((Math.Abs(angle - (startAngle + Math.PI)) < Epsilon) || (Math.Abs(angle - Math.PI) < Epsilon))
                {
                    alignment = SKTextAlign.Center;
                }
                else if (angle > (float)(startAngle + Math.PI))
                {
                    alignment = SKTextAlign.Right;
                }

                canvas.DrawCaptionLabels(entry.Label, entry.TextColor, UnicodeMode, UnicodeLanguage, entry.ValueLabel, entry.Color.WithA((byte)(255 * AnimationProgress)), LabelTextSize, labelPoint, alignment, base.Typeface, out var _);
#endif
            }
        }
示例#11
0
        private static ILine FirstConnection(ICanvas canvas, IElement root, double x, double y, List<Wire> wires, IDiagramCreator creator)
        {
            var counter = canvas.GetCounter();
            string rootUid = root.GetUid();

            bool startIsIO = StringUtil.StartsWith(rootUid, Constants.TagElementInput)
                || StringUtil.StartsWith(rootUid, Constants.TagElementOutput);

            var line = creator.CreateElement(Constants.TagElementWire,
                new object[]
                {
                    x, y, x, y,
                    false, false,
                    startIsIO, false,
                    counter.Next()
                },
                0.0, 0.0, false) as ILine;

            wires.Add(new Wire(line, root, null));

            canvas.Add(line);

            if (line != null || !(line is ILine))
                line.SetTag(root);

            return line;
        }
示例#12
0
        public static void BuildCaptionLabels(this ICanvas canvas, string label, Color labelColor, bool labelIsUnicode, char unicodeLang, string value, Color valueColor, double textSize, Point point, TextAlignment horizontalAlignment, SKTypeface typeface, out Rect totalBounds)
        {
            var hasLabel      = !string.IsNullOrEmpty(label);
            var hasValueLabel = !string.IsNullOrEmpty(value);

            totalBounds = new Rect();

            if (hasLabel || hasValueLabel)
            {
                var hasOffset     = hasLabel && hasValueLabel;
                var captionMargin = textSize * 0.60f;
                var space         = hasOffset ? captionMargin : 0;

                if (hasLabel)
                {
                    // TODO: Add support for typeface
                    var labelTextBlock = TextBlock()
                                         .Text(label)
                                         .FontSize(textSize)
                                         .Foreground(SolidColorBrush().Color(labelColor))
                                         .TextAlignment(horizontalAlignment);


                    labelTextBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Size labelSize = labelTextBlock.DesiredSize;

                    //var bounds = new Rect();
                    //paint.MeasureText(text, ref bounds);

                    var y = point.Y - (labelSize.Height / 2) - space;

                    //canvas.DrawText(text, point.X, y, paint);
                    canvas.Add(point.X, y, labelTextBlock);

                    //Rect labelBounds = GetAbsolutePositionRect(point.X, y, labelSize, horizontalAlignment);
                    //totalBounds = labelBounds.Standardized;
                }

                if (hasValueLabel)
                {
                    // TODO: Add support for typeface
                    var valueTextBlock = TextBlock()
                                         .Text(value)
                                         .FontSize(textSize)
                                         .FontWeight(FontWeights.Bold)
                                         .Foreground(SolidColorBrush().Color(valueColor))
                                         .TextAlignment(horizontalAlignment);

                    valueTextBlock.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Size valueSize = valueTextBlock.DesiredSize;

                    //var bounds = new Rect();
                    //paint.MeasureText(text, ref bounds);

                    var y = point.Y - (valueSize.Height / 2) + space;

                    canvas.Add(point.X, y, valueTextBlock);

#if LATER
                    var valueBounds = GetAbsolutePositionRect(point.X, y, bounds, horizontalAlignment);
                    if (totalBounds.IsEmpty)
                    {
                        totalBounds = valueBounds;
                    }
                    else
                    {
                        totalBounds.Union(valueBounds);
                    }
#endif
                }
            }
        }
示例#13
0
        public XBezierEditor(
            INativeConverter nativeConverter, 
            ICanvasFactory canvasFactory,
            IBoundsFactory boundsFactory,
            ICanvas canvas)
        {
            _canvas = canvas;

            Name = "Bézier";
            Key = "B";
            Modifiers = "";

            var moves = _canvas.Moves.Where(_ => _canvas.IsCaptured);
            var drags = Observable.Merge(_canvas.Downs, _canvas.Ups, moves);

            _downs = _canvas.Downs.Where(_ => IsEnabled).Subscribe(p =>
            {
                if (_canvas.IsCaptured)
                {
                    switch (_state)
                    {
                        case State.Start:
                            {
                                //_xb.Point3.X = p.X;
                                //_xb.Point3.Y = p.Y;
                                //_nb.Point3 = _xb.Point3;
                                //_xb.Point2.X = p.X;
                                //_xb.Point2.Y = p.Y;
                                //_nb.Point2 = _xb.Point2;
#if CONNECTORS
                                ConnectPoint3(p);
#endif
                                _nb.Bounds.Update();
                                _canvas.Render(null);
                                _state = State.Point1;
                            }
                            break;
                        case State.Point1:
                            {
                                //_xb.Point1.X = p.X;
                                //_xb.Point1.Y = p.Y;
                                //_nb.Point1 = _xb.Point1;
#if CONNECTORS
                                ConnectPoint1(p);
#endif
                                _nb.Bounds.Update();
                                _canvas.Render(null);
                                _state = State.Point2;
                            }
                            break;
                        case State.Point2:
                            {
                                //_xb.Point2.X = p.X;
                                //_xb.Point2.Y = p.Y;
                                //_nb.Point2 = _xb.Point2;
#if CONNECTORS
                                ConnectPoint2(p);
#endif
                                _nb.Bounds.Hide();
                                _canvas.Render(null);
                                _state = State.None;
                                _canvas.ReleaseCapture();
                            }
                            break;
                    }
                }
                else
                {
                    _xb = canvasFactory.CreateBezier();
                    _xb.Start.X = p.X;
                    _xb.Start.Y = p.Y;
                    _xb.Point1.X = p.X;
                    _xb.Point1.Y = p.Y;
                    _xb.Point2.X = p.X;
                    _xb.Point2.Y = p.Y;
                    _xb.Point3.X = p.X;
                    _xb.Point3.Y = p.Y;
                    _nb = nativeConverter.Convert(_xb);
                    _canvas.History.Snapshot(_canvas);
#if CONNECTORS
                    ConnectStart(p);
#endif
                    _canvas.Add(_nb);
                    _nb.Bounds = boundsFactory.Create(_canvas, _nb);
                    _nb.Bounds.Update();
                    _nb.Bounds.Show();
                    _canvas.Render(null);
                    _canvas.Capture();
                    _state = State.Start;
                }
            });

            _drags = drags.Where(_ => IsEnabled).Subscribe(p =>
            {
                switch (_state)
                {
                    case State.Start:
                        {
                            _xb.Point3.X = p.X;
                            _xb.Point3.Y = p.Y;
                            _nb.Point3 = _xb.Point3;
                            _xb.Point2.X = p.X;
                            _xb.Point2.Y = p.Y;
                            _nb.Point2 = _xb.Point2;
                            _nb.Bounds.Update();
                            _canvas.Render(null);
                        }
                        break;
                    case State.Point1:
                        {
                            _xb.Point1.X = p.X;
                            _xb.Point1.Y = p.Y;
                            _nb.Point1 = _xb.Point1;
                            _nb.Bounds.Update();
                            _canvas.Render(null);
                        }
                        break;
                    case State.Point2:
                        {
                            _xb.Point2.X = p.X;
                            _xb.Point2.Y = p.Y;
                            _nb.Point2 = _xb.Point2;
                            _nb.Bounds.Update();
                            _canvas.Render(null);
                        }
                        break;
                }
            });
        }
示例#14
0
        public XLineEditor(
            INativeConverter nativeConverter, 
            ICanvasFactory canvasFactory,
            IBoundsFactory boundsFactory,
            ICanvas canvas)
        {
            _canvas = canvas;

            Name = "Line";
            Key = "L";
            Modifiers = "";

            var moves = _canvas.Moves.Where(_ => _canvas.IsCaptured);
            var drags = Observable.Merge(_canvas.Downs, _canvas.Ups, moves);

            _downs = _canvas.Downs.Where(_ => IsEnabled).Subscribe(p =>
            {
                if (_canvas.IsCaptured)
                {
                    //_xline.Point2.X = p.X;
                    //_xline.Point2.Y = p.Y;
                    //_nline.Point2 = _xline.Point2;
#if CONNECTORS
                    ConnectPoint2(p);
#endif
                    _nline.Bounds.Hide();
                    _canvas.Render(null);
                    _state = State.None;
                    _canvas.ReleaseCapture();
                }
                else
                {
                    _xline = canvasFactory.CreateLine();
                    _xline.Point1.X = p.X;
                    _xline.Point1.Y = p.Y;
                    _xline.Point2.X = p.X;
                    _xline.Point2.Y = p.Y;
                    _nline = nativeConverter.Convert(_xline);
                    _canvas.History.Snapshot(_canvas);
#if CONNECTORS
                    ConnectPoint1(p);
#endif
                    _canvas.Add(_nline);
                    _nline.Bounds = boundsFactory.Create(_canvas, _nline);
                    _nline.Bounds.Update();
                    _nline.Bounds.Show();
                    _canvas.Capture();
                    _canvas.Render(null);
                    _state = State.End;
                }
            });

            _drags = drags.Where(_ => IsEnabled).Subscribe(p =>
            {
                if (_state == State.End)
                {
                    _xline.Point2.X = p.X;
                    _xline.Point2.Y = p.Y;
                    _nline.Point2 = _xline.Point2;
                    _nline.Bounds.Update();
                    _canvas.Render(null);
                }
            });
        }
示例#15
0
        public XPinEditor(
            INativeConverter nativeConverter,
            ICanvasFactory canvasFactory,
            IBoundsFactory boundsFactory,
            ICanvas canvas)
        {
            _canvas = canvas;

            Name = "Pin";
            Key = "P";
            Modifiers = "";

            var moves = _canvas.Moves.Where(_ => _canvas.IsCaptured);
            var drags = Observable.Merge(_canvas.Downs, _canvas.Ups, moves);

            _downs = _canvas.Downs.Where(_ => IsEnabled).Subscribe(p =>
            {
                _xpin = canvasFactory.CreatePin();
                _xpin.Point.X = p.X;
                _xpin.Point.Y = p.Y;
                _npin = nativeConverter.Convert(_xpin);
                _canvas.History.Snapshot(_canvas);
#if CONNECTORS
                ConnectPoint(p);
#endif
                _canvas.Add(_npin);
                _npin.Bounds = boundsFactory.Create(_canvas, _npin);
                _npin.Bounds.Update();
                _canvas.Render(null);
            });
        }