Exemplo n.º 1
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            var canvas = e.Surface.Canvas;

            canvas.Clear();

            int width  = e.Info.Width;
            int height = e.Info.Height;

            SKPaint backPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = SKColors.WhiteSmoke,
            };

            canvas.DrawRect(new SKRect(0, 0, width, height), backPaint);

            canvas.Save();

            canvas.Translate(width / 2, height / 2);
            canvas.Scale(Math.Min(width / 210f, height / 520f));
            SKPoint center = new SKPoint(0, 0);

            var rect = new SKRect(-100, -100, 100, 100);

            // Add a buffer for the rectangle
            rect.Inflate(-10, -10);


            SKPaint GaugePointPaint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = ValueColor.ToSKColor()
            };

            SKPaint HighlightRangePaint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = RangeColor.ToSKColor()
            };


            // Draw the range of values

            var rangeStartAngle = AmountToAngle(HighlightRangeStartValue);
            var rangeEndAngle   = AmountToAngle(HighlightRangeEndValue);
            var angleDistance   = rangeEndAngle - rangeStartAngle;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, rangeStartAngle, angleDistance);
                path.LineTo(center);
                canvas.DrawPath(path, HighlightRangePaint);
            }

            // Draw the main gauge line/arc
            SKPaint GaugeMainLinePaintP1 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Blue,
                StrokeWidth = 10
            };
            var startAngle = 135;
            var sweepAngle = 67.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP1);
            }

            //Sector2
            SKPaint GaugeMainLinePaintP2 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Green,
                StrokeWidth = 10
            };

            var startAngleP2 = 202.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP2, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP2);
            }

            //Sector3
            SKPaint GaugeMainLinePaintP3 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Orange,
                StrokeWidth = 10
            };

            var startAngleP3 = 270f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP3, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP3);
            }

            //Sector 4

            SKPaint GaugeMainLinePaintP4 = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = SKColors.Red,
                StrokeWidth = 10
            };

            var startAngleP4 = 337.5f;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngleP4, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaintP4);
            }

            //Draw Needle
            DrawNeedle(canvas, Value);

            //Draw Screw
            SKPaint NeedleScrewPaint = new SKPaint()
            {
                IsAntialias = true,
                Shader      = SKShader.CreateRadialGradient(center, width / 60, new SKColor[]
                                                            { new SKColor(171, 171, 171), SKColors.White }, new float[] { 0.05f, 0.9f }, SKShaderTileMode.Mirror)
            };

            canvas.DrawCircle(center, width / 60, NeedleScrewPaint);

            SKPaint paint = new SKPaint
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = new SKColor(81, 84, 89).WithAlpha(100),
                StrokeWidth = 1f
            };

            canvas.DrawCircle(center, width / 60, paint);

            // Draw the Units of Measurement Text on the display
            SKPaint textPaint = new SKPaint
            {
                IsAntialias = true,
                Color       = SKColors.Black
            };

            float textWidth = textPaint.MeasureText(UnitsText);

            textPaint.TextSize = 12f;

            SKRect textBounds = SKRect.Empty;

            textPaint.MeasureText(UnitsText, ref textBounds);

            float xText = -1 * textBounds.MidX;
            float yText = 95 - textBounds.Height;

            // And draw the text
            canvas.DrawText(UnitsText, xText, yText, textPaint);

            // Draw the Value on the display
            var   valueText      = Value.ToString("F0"); //You can set F1 or F2 if you need float values
            float valueTextWidth = textPaint.MeasureText(valueText);

            textPaint.TextSize = ValueFontSize;

            textPaint.MeasureText(valueText, ref textBounds);

            xText = -1 * textBounds.MidX;
            yText = 85 - textBounds.Height;

            // And draw the text
            canvas.DrawText(valueText, xText, yText, textPaint);
            canvas.Restore();
        }
Exemplo n.º 2
0
        // manage active particles, spawn new particles if it's time to do so
        public virtual void Update(double elapsed)
        {
            // only update if enabled
            if (Enabled)
            {
                // temp variables to save typing
                bool outOfBounds;
                int  parX, parY;
                for (int i = 0; i < m_ActiveParticles.Count; i++)
                {
                    var particle = m_ActiveParticles[i];

                    // when particle leaves emitter bounds, mark inactive
                    parX        = (int)Math.Round(particle.Position.X);
                    parY        = (int)Math.Round(particle.Position.Y);
                    outOfBounds =
                        parX > EmitterBoundsRect.Right ||
                        parX + TextureRect.Width <EmitterBoundsRect.Left ||
                                                  parY> EmitterBoundsRect.Bottom ||
                        parY + TextureRect.Height < EmitterBoundsRect.Top;
                    if (outOfBounds)
                    {
                        particle.IsActive = false;
                    }

                    // process active particles, cleanup inactive particles
                    if (particle.IsActive)
                    {
                        // allow active modifiers to update particle
                        foreach (var modifier in Modifiers)
                        {
                            if (modifier.Enabled)
                            {
                                // tell the modifier to update this particle
                                modifier.Update(particle, elapsed);
                            }
                        }
                        // tell particle to update itself
                        particle.Update(elapsed);
                        m_ActiveParticles[i] = particle;
                    }
                    else
                    {
                        // move particle to inactive list for later reuse
                        m_InactiveParticles.Add(particle);
                        m_ActiveParticles.RemoveAt(i--);
                    }
                }

                // try to generate ParticlesPerUpdate new particles
                for (long i = 0; Active && i < ParticlesPerUpdate; i++)
                {
                    if (m_InactiveParticles.Count > 0)
                    {
                        // reset particle and add it to our pool of active particles
                        var particle = m_InactiveParticles[0];
                        particle.Position = EmitterRV2.RandomValue();
                        particle.Velocity = RangeVelocity == null ? Vector2.Zero : RangeVelocity.RandomValue();
                        particle.Color    = RangeColor.RandomValue();
                        particle.IsActive = true;
                        particle.LifeTime = ParticleLifetime;
                        particle.Scale    = 1.0f;
                        particle.Age      = 0.0f;
                        m_InactiveParticles.RemoveAt(0);
                        m_ActiveParticles.Add(particle);
                    }

                    else
                    {
                        // no more particles in our inactive pool
                        break;
                    }
                }
            }
        }
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            base.OnPaintSurface(e);

            var canvas = e.Surface.Canvas;

            SKPaint backPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = Color.WhiteSmoke.ToSKColor(),
            };

            int width  = e.Info.Width;
            int height = e.Info.Height;

            canvas.DrawRect(new SKRect(0, 0, width, height), backPaint);

            canvas.Save();

            canvas.Translate(width / 2, height / 2);
            canvas.Scale(Math.Min(width / 210f, height / 520f));

            SKPaint GaugeMainLinePaint = new SKPaint
            {
                Style       = SKPaintStyle.Stroke,
                Color       = GaugeLineColor.ToSKColor(),
                StrokeWidth = 10
            };

            SKPaint GaugePointPaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = ValueColor.ToSKColor()
            };

            SKPaint HighlightRangePaint = new SKPaint
            {
                Style = SKPaintStyle.Fill,
                Color = RangeColor.ToSKColor()
            };

            SKPoint center = new SKPoint(0, 0);

            var rect = new SKRect(-100, -100, 100, 100);

            // Add a buffer for the rectangle
            rect.Inflate(-10, -10);

            // Draw the range of values
            if (RangeIsVisible)
            {
                var rangeStartAngle = AmountToAngle(HighlightRangeStartValue);
                var rangeEndAngle   = AmountToAngle(HighlightRangeEndValue);
                var angleDistance   = rangeEndAngle - rangeStartAngle;

                using (SKPath path = new SKPath())
                {
                    path.AddArc(rect, rangeStartAngle, angleDistance);
                    path.LineTo(center);
                    canvas.DrawPath(path, HighlightRangePaint);
                }
            }

            // Draw the main gauge line/arc
            var startAngle = 135;
            var sweepAngle = 270;

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, startAngle, sweepAngle);
                canvas.DrawPath(path, GaugeMainLinePaint);
            }

            // Draw the current value on the arc
            var valueAsAngle = AmountToAngle(Value);

            using (SKPath path = new SKPath())
            {
                path.AddArc(rect, valueAsAngle, 2);
                canvas.DrawCircle(path.Bounds.Left + path.Bounds.Width / 2,
                                  path.Bounds.Top + path.Bounds.Height / 2,
                                  10f,
                                  GaugePointPaint);
            }

            // Draw the Units of Measurement Text on the display
            SKPaint textPaint = new SKPaint
            {
                Color = SKColors.Black
            };

            float textWidth = textPaint.MeasureText(UnitsText);

            textPaint.TextSize = 12f;

            SKRect textBounds = SKRect.Empty;

            textPaint.MeasureText(UnitsText, ref textBounds);

            float xText = -1 * textBounds.MidX;
            float yText = 95 - textBounds.Height;

            // And draw the text
            canvas.DrawText(UnitsText, xText, yText, textPaint);

            // Draw the Value on the display
            var   valueText      = Value.ToString("F1");
            float valueTextWidth = textPaint.MeasureText(valueText);

            textPaint.TextSize = 35f;

            textPaint.MeasureText(valueText, ref textBounds);

            xText = -1 * textBounds.MidX;
            yText = 85 - textBounds.Height;

            // And draw the text
            canvas.DrawText(valueText, xText, yText, textPaint);

            canvas.Restore();
        }