Skip to content

Commit

Permalink
[Tizen] Add CornerRadius implementation on BoxView (xamarin#2806)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot authored and StephaneDelcroix committed May 23, 2018
1 parent 4dcfa9e commit 31c432c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 4 deletions.
101 changes: 101 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Native/RoundRectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using ElmSharp;

namespace Xamarin.Forms.Platform.Tizen.Native
{
public class RoundRectangle : Polygon
{
int[] _radius = new int[4];
public RoundRectangle(EvasObject parent) : base(parent)
{
}

public int X { get; set; }
public int Y { get; set; }

public int Width { get; set; }
public int Height { get; set; }

public void SetRadius(int r)
{
SetRadius(r, r, r, r);
}

public void SetRadius(int topLeft, int topRight, int bottomLeft, int bottomRight)
{
_radius[0] = topLeft;
_radius[1] = topRight;
_radius[2] = bottomLeft;
_radius[3] = bottomRight;
}

public void Draw()
{
DrawPoints();
}

public void Draw(Rect bound)
{
X = bound.X;
Y = bound.Y;
Width = bound.Width;
Height = bound.Height;
Draw();
}


protected virtual void DrawPoints()
{
int[] radius = new int[4];
int maxR = Math.Min(Width / 2, Height / 2);
radius[0] = Math.Min(_radius[0], maxR);
radius[1] = Math.Min(_radius[1], maxR);
radius[2] = Math.Min(_radius[2], maxR);
radius[3] = Math.Min(_radius[3], maxR);

ClearPoints();
for (int i = 0; i <= radius[0]; i++)
{
int x = i;
int dx = radius[0] - x;
int y = radius[0] - (int)Math.Sqrt((radius[0] * radius[0]) - (dx * dx));
AddRelativePoint(x, y);
}

AddRelativePoint(Width - radius[1], 0);

for (int i = Width - radius[1]; i <= Width; i++)
{
int x = i;
int dx = radius[1] - (Width - x);
int y = radius[1] - (int)Math.Sqrt((radius[1] * radius[1]) - (dx * dx));
AddRelativePoint(x, y);
}

AddRelativePoint(Width, Height - radius[3]);

for (int i = Width; i >= Width - radius[3]; i--)
{
int x = i;
int dx = radius[3] - (Width - x);
int y = Height - radius[3] + (int)Math.Sqrt((radius[3] * radius[3]) - (dx * dx));
AddRelativePoint(x, y);
}

AddRelativePoint(radius[2], Height);

for (int i = radius[2]; i >= 0; i--)
{
int x = i;
int dx = radius[2] - x;
int y = Height - radius[2] + (int)Math.Sqrt((radius[2] * radius[2]) - (dx * dx));
AddRelativePoint(x, y);
}
}

void AddRelativePoint(int x, int y)
{
AddPoint(X + x, Y + y);
}
}
}
31 changes: 27 additions & 4 deletions Xamarin.Forms.Platform.Tizen/Renderers/BoxViewRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
using System.ComponentModel;
using EColor = ElmSharp.Color;
using ERectangle = ElmSharp.Rectangle;
using Xamarin.Forms.Platform.Tizen.Native;

namespace Xamarin.Forms.Platform.Tizen
{
public class BoxViewRenderer : ViewRenderer<BoxView, ERectangle>
public class BoxViewRenderer : ViewRenderer<BoxView, RoundRectangle>
{
public BoxViewRenderer()
{
RegisterPropertyHandler(nameof(Element.CornerRadius), OnRadiusUpdate);
}

protected override void OnElementChanged(ElementChangedEventArgs<BoxView> e)
{
if (Control == null)
{
SetNativeControl(new ERectangle(Forms.NativeParent));
SetNativeControl(new RoundRectangle(Forms.NativeParent));
}

UpdateColor();

base.OnElementChanged(e);
}

Expand All @@ -38,6 +42,12 @@ protected override void UpdateBackgroundColor(bool initialize)
}
}

protected override void UpdateLayout()
{
base.UpdateLayout();
Control.Draw(Control.Geometry);
}

protected override void UpdateOpacity(bool initialize)
{
if (initialize && Element.Opacity == 1d)
Expand All @@ -46,6 +56,19 @@ protected override void UpdateOpacity(bool initialize)
UpdateColor();
}

void OnRadiusUpdate(bool init)
{
int topLeft = Forms.ConvertToScaledPixel(Element.CornerRadius.TopLeft);
int topRight = Forms.ConvertToScaledPixel(Element.CornerRadius.TopRight);
int bottomLeft = Forms.ConvertToScaledPixel(Element.CornerRadius.BottomLeft);
int bottomRight = Forms.ConvertToScaledPixel(Element.CornerRadius.BottomRight);
Control.SetRadius(topLeft, topRight, bottomLeft, bottomRight);
if (!init)
{
Control.Draw();
}
}

void UpdateColor()
{
if (Element.Color.IsDefault)
Expand Down

0 comments on commit 31c432c

Please sign in to comment.