// Calculate angle to rotate the cake orientation so that it aligns // the selected emission icon on on the top arc of the cake. // Sets the currently selected emission. public float UpdateOrientation(IEmission Selected) { // Reset first (avoid accumulating huge sums in startAngle) //InitializeCake(); bool add = false; float newStartAngleOffset = 0; // Calculate the amount to offset foreach (PieceOfCake Slice in PiecesOfCake) { if (add) { newStartAngleOffset += Slice.ArcAngle; } if (Slice.Emission.Equals(Selected)) { CurrentlySelected = Slice; newStartAngleOffset += (Slice.ArcAngle / 2) - 90; add = true; } } OffsetDifference = newStartAngleOffset - OldOffset; OldOffset = newStartAngleOffset; return(OffsetDifference); }
// Initialises a CakeView based on the given CarbonFootprint // Rotation set to default origin values. private void InitializeCake() { int i = 0; float startAngle = 0; foreach (IEmission Emission in CO2.Emissions) { float sweepAngle = 360f * (float)Emission.KgCO2 / TotalValues; PiecesOfCake[i] = new PieceOfCake(Emission, Center, startAngle, sweepAngle, Radius, IconOffset, IconRadius); startAngle += sweepAngle; i++; } }
public CakeOrientation(CarbonFootprint co2, SKCanvasView canvasView) { this.CanvasView = canvasView; this.Width = (float)canvasView.Width; this.Height = (float)canvasView.Height; this.CurrentlySelected = null; this.Center = new SKPoint(Width / 2, Height / 2); this.Thickness = Width / 10; this.IconOffset = Width / 50; this.IconRadius = Width / 12; this.Radius = Math.Min(Width / 2, Height / 2) - 1.5f * Width / 12; //System.Diagnostics.Debug.WriteLine("Width: " + size.Width); this.TotalValues = 0; this.CO2 = co2; this.PopOver = new PopOver(Radius - Thickness * 1.3f, Width / 5, Width / 14, Width / 18); this.BoldFont = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Fill, TextAlign = SKTextAlign.Center, Typeface = SKTypeface.FromFile("OpenSans-Bold.ttf") }; this.NormalFont = new SKPaint { IsAntialias = true, Style = SKPaintStyle.Fill, TextAlign = SKTextAlign.Center, Typeface = SKTypeface.FromFile("OpenSans-Normal.ttf") }; foreach (IEmission Emission in CO2.Emissions) { TotalValues += (float)Emission.KgCO2; } PiecesOfCake = new PieceOfCake[co2.Emissions.Length]; InitializeCake(); }
public void SetOrientation(float offset) { for (int i = 0; i < PiecesOfCake.Length; i++) { IEmission emission = PiecesOfCake[i].Emission; float startAngle = PiecesOfCake[i].StartAngle += offset; float arcAngle = PiecesOfCake[i].ArcAngle; float iconRadius = IconRadius; float iconOffset = IconOffset; bool isSelected = PiecesOfCake[i].Emission.Equals(CurrentlySelected); // Make icon of selected category bigger if (isSelected) { float factor = 1f; iconRadius = IconRadius * factor; //iconOffset = iconRadius / 2 + IconOffset; } PiecesOfCake[i] = new PieceOfCake(emission, Center, startAngle, arcAngle, Radius, iconOffset, iconRadius); } }
// Check if a touch event occured inside an icon and // returns the corresponding piece of cake public PieceOfCake SelectPieceOfCake(SKTouchEventArgs e) { SKPoint touchLocation = e.Location; PieceOfCake SelectedSlice = null; int i = 0; foreach (PieceOfCake Slice in PiecesOfCake) { float minX = Slice.Icon.Center.X - Slice.Icon.Radius; float maxX = Slice.Icon.Center.X + Slice.Icon.Radius; float minY = Slice.Icon.Center.Y - Slice.Icon.Radius; float maxY = Slice.Icon.Center.Y + Slice.Icon.Radius; minX *= Scale; maxX *= Scale; minY *= Scale; maxY *= Scale; bool insideX = touchLocation.X > minX && touchLocation.X < maxX; bool insideY = touchLocation.Y > minY && touchLocation.Y < maxY; if (insideX && insideY) { SelectedSlice = Slice; } i++; } if (SelectedSlice != null) { CanvasView.InvalidateSurface(); } return(SelectedSlice); }
public void DrawPopover() { // Define paint if (CakeOrientation.CurrentlySelected != null) { SKPaint fillPaint = new SKPaint { Color = SKColors.Transparent, IsAntialias = true }; PopOver popOver = CakeOrientation.PopOver; PieceOfCake Slice = CakeOrientation.CurrentlySelected; SKPath centerCircle = new SKPath(); fillPaint.Color = CakeOrientation.CurrentlySelected.Color; fillPaint.ImageFilter = SKImageFilter.CreateDropShadow(1, 1, 20 * popOver.Factor, 20 * popOver.Factor, SKColors.Black.WithAlpha(80), SKDropShadowImageFilterShadowMode.DrawShadowAndForeground); float radius = popOver.Radius * popOver.Factor; float amountFontSize = popOver.AmountFontSize * popOver.Factor; float unitFontSize = popOver.UnitFontSize * popOver.Factor; float titleFontSize = popOver.TitleFontSize * popOver.Factor; SKColor testColor = SKColors.White; SKColor titleFontColor = testColor; SKColor amountFontColor = testColor; SKColor unitFontColor = testColor; //connectRect.AddRect(rect); centerCircle.AddCircle(CakeOrientation.Center.X, CakeOrientation.Center.Y, radius); // Draw path canvas.DrawPath(centerCircle, fillPaint); // Define paint SKPaint titlePaint = CakeOrientation.NormalFont; titlePaint.Color = titleFontColor; titlePaint.TextSize = titleFontSize; SKPaint amountPaint = CakeOrientation.BoldFont; amountPaint.Color = amountFontColor; amountPaint.TextSize = amountFontSize; SKPaint unitPaint = titlePaint; SKPoint amountTextCenter = CakeOrientation.Center; SKPoint titleTextCenter = CakeOrientation.Center; SKPoint unitTextCenter = CakeOrientation.Center; float amountTextHeight = amountFontSize / 0.75f; float titleTextHeight = titleFontSize / 0.75f; amountTextCenter.Y += amountFontSize / 2 * 0.75f; titleTextCenter.Y -= (amountTextHeight / 2 - titleTextHeight / 2 + 5); unitTextCenter.Y += (amountTextHeight / 2 + 5); // Draw Text canvas.DrawText(((int)Slice.Emission.KgCO2).ToString(), amountTextCenter, amountPaint); canvas.DrawText(CakeUtil.GetTitle(Slice.Emission), titleTextCenter, titlePaint); canvas.DrawText("kg CO2", unitTextCenter, unitPaint); } }