Пример #1
0
        public static SKImage SensorTitle(List <SensorReading> readings, SKColor readingColor)
        {
            var latestReading = readings.OrderByDescending(x => x.ReadingDateTime).First();

            var fitSize =
                TextHelpers.AutoFitRichStringWidth(
                    $"{latestReading.ReadingValue:0.0} - {latestReading.ReadingDateTime:M/dd HH}",
                    "Arial", 360, 40);

            var titleRichString = new RichString()
                                  .FontFamily("Arial")
                                  .TextColor(SKColors.White)
                                  .FontSize(fitSize)
                                  .TextColor(readingColor)
                                  .Add($"{latestReading.ReadingValue:0.0}")
                                  .TextColor(SKColors.Gray)
                                  .Add(" - ")
                                  .TextColor(SKColors.White)
                                  .Add($"{latestReading.ReadingTag}");

            var titleChartImage   = new SKImageInfo(400, 50);
            var titleChartSurface = SKSurface.Create(titleChartImage);
            var titleChartCanvas  = titleChartSurface.Canvas;

            var titleLeftStart = (400 - titleRichString.MeasuredWidth) / 2;

            titleRichString.Paint(titleChartCanvas, new SKPoint(titleLeftStart, 10));

            titleChartCanvas.Save();

            return(titleChartSurface.Snapshot());
        }
Пример #2
0
        private void OnPainting(object sender, SKPaintSurfaceEventArgs e)
        {
            var surface = e.Surface;
            var canvas  = surface.Canvas;

            canvas.Clear(SKColors.Transparent);
            canvas.Scale(e.Info.Width / (float)Width);

            richString.Paint(canvas);
        }
Пример #3
0
        public static void DrawItem(SKCanvas canvas, Item item, Lang lang, SKRect rect)
        {
            Console.Out.WriteLine($"[{lang}] {item.name.GetText(lang)}".AsSpan());
            DrawItemBackground(canvas, rect);
            SKRect padded      = new(rect.Left + itemPadding, rect.Top + itemPadding, rect.Right - itemPadding, rect.Bottom);
            float  imageHeight = padded.Height * 0.4f;
            float  nameHeight  = padded.Height * 0.2f;
            float  descHeight  = padded.Height * 0.4f;
            SKRect imageRect   = crect(padded.Left, padded.Top, padded.Width, imageHeight);
            SKRect nameRect    = crect(padded.Left + 3, padded.Top + imageHeight, padded.Width - 5, nameHeight);
            SKRect descRect    = crect(padded.Left + 3, padded.Top + imageHeight + nameHeight, padded.Width - 5, descHeight);
            var    scaled      = item.GetScaledBitmap(imageRect);

            if (scaled != null)
            {
                var imageDestRect = CalculateDisplayRect(imageRect, scaled, BitmapAlignment.Start, BitmapAlignment.Center);
                canvas.DrawBitmap(scaled, imageDestRect.Left + 5, imageDestRect.Top);
            }

            #region Draw Name

            RichString name = new RichString()
            {
                MaxWidth     = nameRect.Width,
                DefaultStyle = nameStyle,
            }.Add(item.name.GetText(lang));
            name.MaxLines = 1;
            name.Paint(canvas, new SKPoint(nameRect.Left, nameRect.Top + 4), paintOptions);

            #endregion Draw Name

            RichString desc = new RichString()
            {
                MaxWidth     = nameRect.Width,
                MaxHeight    = null,
                DefaultStyle = descStyle,
            }.Add(item.description.GetText(lang));
            desc.MaxLines = 20;
            desc.Paint(canvas, new SKPoint(descRect.Left, descRect.Top + 1), paintOptions);
        }
Пример #4
0
        public static void DrawLabel(SKCanvas canvas, MetaType metaType, Lang lang, SKRect rect)
        {
            var lineR = rect;

            lineR.Inflate(-(canvasMaxWidth * 0.8f), -(itemMargin * 2.4f));
            //top
            //canvas.DrawLine(new SKPoint(lineR.Left, lineR.Top), new SKPoint(lineR.Right, lineR.Top), labelLine);
            //bottom
            canvas.DrawLine(new SKPoint(lineR.Left, lineR.Bottom), new SKPoint(lineR.Right, lineR.Bottom), labelLine);

            string     labelName = "- " + GetMetaTypeLabel(lang, metaType) + " -";
            RichString desc      = new RichString()
            {
                DefaultStyle = labelStyle,
                MaxLines     = 1,
            }.Add(labelName);
            const int offsetY = 3;
            int       ori     = canvas.SaveLayer(labelPaint);

            desc.Paint(canvas, new SKPoint(rect.MidX - desc.MeasuredWidth / 2, rect.MidY - desc.MeasuredHeight / 2 - offsetY), paintOptions);
            canvas.RestoreToCount(ori);
        }
Пример #5
0
        void OnRender(SKCanvas canvas)
        {
            canvas.Clear(new SKColor(0xFFFFFFFF));


            float canvasWidth  = canvas.LocalClipBounds.Width;
            float canvasHeight = canvas.LocalClipBounds.Height;
            float?height       = (float)(canvasHeight - margin * 2);
            float?width        = (float)(canvasWidth - margin * 2);

            if (!_useMaxHeight)
            {
                height = null;
            }
            if (!_useMaxWidth)
            {
                width = null;
            }

            using (var gridlinePaint = new SKPaint()
            {
                Color = new SKColor(0x20000000), StrokeWidth = 1
            })
            {
                canvas.DrawLine(new SKPoint(margin, 0), new SKPoint(margin, (float)canvasHeight), gridlinePaint);
                if (width.HasValue)
                {
                    canvas.DrawLine(new SKPoint(margin + width.Value, 0), new SKPoint(margin + width.Value, (float)canvasHeight), gridlinePaint);
                }
                canvas.DrawLine(new SKPoint(0, margin), new SKPoint((float)canvasWidth, margin), gridlinePaint);
                if (height.HasValue)
                {
                    canvas.DrawLine(new SKPoint(0, margin + height.Value), new SKPoint((float)canvasWidth, margin + height.Value), gridlinePaint);
                }
            }

            _richString.MaxWidth  = width;
            _richString.MaxHeight = height;

            var state = $"Measured: {_richString.MeasuredWidth} x {_richString.MeasuredHeight} Lines: {_richString.LineCount} Truncated: {_richString.Truncated} Length: {_richString.MeasuredLength} Revision: {_richString.Revision}";

            canvas.DrawText(state, margin, 20, new SKPaint()
            {
                Typeface    = SKTypeface.FromFamilyName("Arial"),
                TextSize    = 12,
                IsAntialias = true,
            });

            state = $"Hit Test: Over {_htr.OverCodePointIndex} Line {_htr.OverLine}.  Closest: {_htr.ClosestCodePointIndex} Line {_htr.ClosestLine}";
            canvas.DrawText(state, margin, 40, new SKPaint()
            {
                Typeface    = SKTypeface.FromFamilyName("Arial"),
                TextSize    = 12,
                IsAntialias = true,
            });

            var options = new TextPaintOptions()
            {
                SelectionColor = new SKColor(0x60FF0000),
            };

            if (_htr.OverCodePointIndex >= 0)
            {
                options.Selection = new TextRange(_htr.OverCodePointIndex, _htr.OverCodePointIndex + 1);
            }

            _richString.Paint(canvas, new SKPoint(margin, margin), options);

            if (_htr.ClosestCodePointIndex >= 0)
            {
                var ci = _richString.GetCaretInfo(new CaretPosition(_htr.ClosestCodePointIndex, false));
                using (var paint = new SKPaint()
                {
                    Color = new SKColor(0xFF000000),
                    IsStroke = true,
                    IsAntialias = true,
                    StrokeWidth = 1,
                })
                {
                    var rect = ci.CaretRectangle;
                    rect.Offset(margin, margin);
                    canvas.DrawLine(rect.Right, rect.Top, rect.Left, rect.Bottom, paint);
                }
            }
        }