Skip to content

Commit

Permalink
immplement idrawable per actual drawable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
beto-rodriguez committed Nov 29, 2024
1 parent 7b17052 commit 0f18bf6
Show file tree
Hide file tree
Showing 6 changed files with 496 additions and 185 deletions.
2 changes: 1 addition & 1 deletion src/LiveChartsCore/CoreAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,7 @@ private void UpdateLabel(
SetUpdateMode(label, mode);
}

private void SetUpdateMode(CoreGeometry geometry, UpdateMode mode)
private void SetUpdateMode(IDrawable geometry, UpdateMode mode)
{
switch (mode)
{
Expand Down
173 changes: 0 additions & 173 deletions src/LiveChartsCore/Drawing/Animatable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,45 +23,12 @@
using System;
using System.Collections.Generic;
using LiveChartsCore.Motion;
using LiveChartsCore.Painting;

namespace LiveChartsCore.Drawing;

/// <inheritdoc cref="Animatable" />
public abstract class Animatable
{
private readonly FloatMotionProperty _xProperty;
private readonly FloatMotionProperty _yProperty;
private readonly FloatMotionProperty _rotationProperty;
private readonly PointMotionProperty _transformOriginProperty;
private readonly PointMotionProperty _scaleProperty;
private readonly PointMotionProperty _skewProperty;
private readonly PointMotionProperty _translateProperty;
private readonly FloatMotionProperty _opacityProperty;
private Paint? _stroke;
private Paint? _fill;

/// <summary>
/// Initializes a new instance of the <see cref="Animatable"/> class.
/// </summary>
protected Animatable(bool hasGeometryTransform = false)
{
HasTransform = hasGeometryTransform;
_xProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(X), 0));
_yProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(Y), 0));
_transformOriginProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(TransformOrigin), new LvcPoint(0.5f, 0.5f)));
_translateProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(TranslateTransform), new LvcPoint(0, 0)));
_rotationProperty = RegisterMotionProperty(
new FloatMotionProperty(nameof(RotateTransform), 0));
_scaleProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(ScaleTransform), new LvcPoint(1, 1)));
_skewProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(SkewTransform), new LvcPoint(1, 1)));
_opacityProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(Opacity), 1));
}

/// <summary>
/// Gets or sets a value indicating whether this instance is valid, the instance is valid when all the
/// motion properties in the object finished their animations.
Expand All @@ -78,146 +45,6 @@ protected Animatable(bool hasGeometryTransform = false)
/// </summary>
public bool RemoveOnCompleted { get; set; }

/// <summary>
/// Gets or sets the opacity.
/// </summary>
public float Opacity
{
get => _opacityProperty.GetMovement(this);
set => _opacityProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.X"/>
public float X
{
get => Parent is null
? _xProperty.GetMovement(this)
: _xProperty.GetMovement(this) + Parent.X;
set => _xProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.Y"/>
public float Y
{
get => Parent is null
? _yProperty.GetMovement(this)
: _yProperty.GetMovement(this) + Parent.Y;
set => _yProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.TransformOrigin"/>
public LvcPoint TransformOrigin
{
get => _transformOriginProperty.GetMovement(this);
set => _transformOriginProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.TranslateTransform"/>
public LvcPoint TranslateTransform
{
get => _translateProperty.GetMovement(this);
set
{
_translateProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.RotateTransform"/>
public float RotateTransform
{
get => _rotationProperty.GetMovement(this);
set
{
_rotationProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.ScaleTransform"/>
public LvcPoint ScaleTransform
{
get => _scaleProperty.GetMovement(this);
set
{
_scaleProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.SkewTransform"/>
public LvcPoint SkewTransform
{
get => _skewProperty.GetMovement(this);
set
{
_skewProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.HasTransform"/>
public bool HasTransform { get; protected set; }

/// <inheritdoc cref="IDrawable.HasTranslate"/>
public bool HasTranslate
{
get
{
var t = TranslateTransform;
return t.X != 0 || t.Y != 0;
}
}

/// <inheritdoc cref="IDrawable.HasScale"/>
public bool HasScale
{
get
{
var s = ScaleTransform;
return s.X != 1 || s.Y != 1;
}
}

/// <inheritdoc cref="IDrawable.HasSkew"/>
public bool HasSkew
{
get
{
var s = SkewTransform;
return s.X != 1 || s.Y != 1;
}
}

/// <inheritdoc cref="IDrawable.HasSkew"/>
public bool HasRotation => Math.Abs(RotateTransform) > 0;

/// <summary>
/// Gets or sets the stroke paint.
/// </summary>
public Paint? Stroke
{
get => _stroke;
set
{
_stroke = value;
if (_stroke is not null) _stroke.IsStroke = true;
}
}

/// <summary>
/// Gets or sets the fill paint.
/// </summary>
public Paint? Fill
{
get => _fill;
set
{
_fill = value;
if (_fill is not null) _fill.IsStroke = false;
}
}

/// <summary>
/// Gets or sets the parent shape, if any the X and Y properties will be relative to the parent.
/// </summary>
Expand Down
178 changes: 173 additions & 5 deletions src/LiveChartsCore/Drawing/CoreGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using LiveChartsCore.Motion;
using LiveChartsCore.Painting;

namespace LiveChartsCore.Drawing;
Expand All @@ -28,13 +30,179 @@ namespace LiveChartsCore.Drawing;
/// <summary>
/// Initializes a new instance of the <see cref="CoreGeometry"/> class.
/// </summary>
public abstract class CoreGeometry(bool hasGeometryTransform = false)
: Animatable(hasGeometryTransform), IDrawable
public abstract class CoreGeometry : Animatable, IDrawable
{
private readonly FloatMotionProperty _xProperty;
private readonly FloatMotionProperty _yProperty;
private readonly FloatMotionProperty _rotationProperty;
private readonly PointMotionProperty _transformOriginProperty;
private readonly PointMotionProperty _scaleProperty;
private readonly PointMotionProperty _skewProperty;
private readonly PointMotionProperty _translateProperty;
private readonly FloatMotionProperty _opacityProperty;
private Paint? _stroke;
private Paint? _fill;

/// <summary>
/// Initializes a new instance of the <see cref="CoreGeometry"/> class.
/// </summary>
protected CoreGeometry()
{
_xProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(X), 0));
_yProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(Y), 0));
_transformOriginProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(TransformOrigin), new LvcPoint(0.5f, 0.5f)));
_translateProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(TranslateTransform), new LvcPoint(0, 0)));
_rotationProperty = RegisterMotionProperty(
new FloatMotionProperty(nameof(RotateTransform), 0));
_scaleProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(ScaleTransform), new LvcPoint(1, 1)));
_skewProperty = RegisterMotionProperty(
new PointMotionProperty(nameof(SkewTransform), new LvcPoint(1, 1)));
_opacityProperty = RegisterMotionProperty(new FloatMotionProperty(nameof(Opacity), 1));
}

/// <summary>
/// Measures the geometry.
/// Gets or sets the opacity.
/// </summary>
/// <param name="drawableTask">The drawable task.</param>
/// <returns>the size of the geometry.</returns>
public float Opacity
{
get => _opacityProperty.GetMovement(this);
set => _opacityProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.X"/>
public float X
{
get => Parent is null
? _xProperty.GetMovement(this)
: _xProperty.GetMovement(this) + Parent.X;
set => _xProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.Y"/>
public float Y
{
get => Parent is null
? _yProperty.GetMovement(this)
: _yProperty.GetMovement(this) + Parent.Y;
set => _yProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.TransformOrigin"/>
public LvcPoint TransformOrigin
{
get => _transformOriginProperty.GetMovement(this);
set => _transformOriginProperty.SetMovement(value, this);
}

/// <inheritdoc cref="IDrawable.TranslateTransform"/>
public LvcPoint TranslateTransform
{
get => _translateProperty.GetMovement(this);
set
{
_translateProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.RotateTransform"/>
public float RotateTransform
{
get => _rotationProperty.GetMovement(this);
set
{
_rotationProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.ScaleTransform"/>
public LvcPoint ScaleTransform
{
get => _scaleProperty.GetMovement(this);
set
{
_scaleProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.SkewTransform"/>
public LvcPoint SkewTransform
{
get => _skewProperty.GetMovement(this);
set
{
_skewProperty.SetMovement(value, this);
HasTransform = true;
}
}

/// <inheritdoc cref="IDrawable.HasTransform"/>
public bool HasTransform { get; protected set; }

/// <inheritdoc cref="IDrawable.HasTranslate"/>
public bool HasTranslate
{
get
{
var t = TranslateTransform;
return t.X != 0 || t.Y != 0;
}
}

/// <inheritdoc cref="IDrawable.HasScale"/>
public bool HasScale
{
get
{
var s = ScaleTransform;
return s.X != 1 || s.Y != 1;
}
}

/// <inheritdoc cref="IDrawable.HasSkew"/>
public bool HasSkew
{
get
{
var s = SkewTransform;
return s.X != 1 || s.Y != 1;
}
}

/// <inheritdoc cref="IDrawable.HasSkew"/>
public bool HasRotation => Math.Abs(RotateTransform) > 0;

/// <summary>
/// Gets or sets the stroke paint.
/// </summary>
public Paint? Stroke
{
get => _stroke;
set
{
_stroke = value;
if (_stroke is not null) _stroke.IsStroke = true;
}
}

/// <summary>
/// Gets or sets the fill paint.
/// </summary>
public Paint? Fill
{
get => _fill;
set
{
_fill = value;
if (_fill is not null) _fill.IsStroke = false;
}
}

/// <inheritdoc cref="IDrawable.Measure(Paint)"/>
public abstract LvcSize Measure(Paint drawableTask);
}
Loading

0 comments on commit 0f18bf6

Please sign in to comment.