Skip to content

Commit

Permalink
Replace VisualElement for IInteractable
Browse files Browse the repository at this point in the history
Visual elements will be obsolete, insteadd they are replaced by the Visual class, both classes implement IInteractable as a temporal solution for the migration to the new way of buiilding custom visuuals in the library.
  • Loading branch information
beto-rodriguez committed Dec 14, 2024
1 parent 52df6e1 commit 1abd04d
Show file tree
Hide file tree
Showing 32 changed files with 261 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/LiveChartsCore/Chart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,9 @@ protected internal virtual void InvokePointerDown(LvcPoint point, bool isSeconda

// fire the visual elements event.
var hitElements =
_everMeasuredElements.OfType<CoreVisualElement>()
.Cast<CoreVisualElement>()
.SelectMany(x => x.IsHitBy(this, point));
_everMeasuredElements
.OfType<IInternalInteractable>()
.Where(x => x.GetHitBox().Contains(point));

foreach (var ve in hitElements)
ve.InvokePointerDown(new VisualElementEventArgs(this, ve, point));
Expand Down
22 changes: 18 additions & 4 deletions src/LiveChartsCore/Drawing/LvcRectangle.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.Drawing;

namespace LiveChartsCore.Drawing;

/// <summary>
Expand Down Expand Up @@ -59,12 +61,12 @@ private LvcRectangle(bool empty)
/// <summary>
/// Gets the X location coordinate.
/// </summary>
public float X => Location.X;
public readonly float X => Location.X;

/// <summary>
/// Gets the Y location coordinate.
/// </summary>
public float Y => Location.Y;
public readonly float Y => Location.Y;

/// <summary>
/// Gets or sets the size.
Expand All @@ -74,18 +76,30 @@ private LvcRectangle(bool empty)
/// <summary>
/// Gets the width.
/// </summary>
public float Width => Size.Width;
public readonly float Width => Size.Width;

/// <summary>
/// Gets the height.
/// </summary>
public float Height => Size.Height;
public readonly float Height => Size.Height;

/// <summary>
/// Gets or sets whether the instance is empty.
/// </summary>
private bool IsEmpty { get; set; }

/// <summary>
/// Determines whether the given point is contained in the rectangle.
/// </summary>
/// <param name="point">The point.</param>
/// <returns>True if contained, otherwise false.</returns>
public readonly bool Contains(LvcPoint point)
{
return
point.X >= Location.X && point.X <= Location.X + Size.Width &&
point.Y >= Location.Y && point.Y <= Location.Y + Size.Height;
}

/// <summary>
/// Determines whether the instance is equals to the given instance.
/// </summary>
Expand Down
6 changes: 2 additions & 4 deletions src/LiveChartsCore/Kernel/Events/VisualElementEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
// SOFTWARE.

using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.VisualElements;

namespace LiveChartsCore.Kernel.Events;
Expand All @@ -37,10 +36,9 @@ namespace LiveChartsCore.Kernel.Events;
/// <param name="visualElement">The visual element.</param>
public class VisualElementEventArgs(
Chart chart,
CoreVisualElement visualElement,
IInteractable visualElement,
LvcPoint pointerLocation)
{

/// <summary>
/// Gets the chart.
/// </summary>
Expand All @@ -54,5 +52,5 @@ public class VisualElementEventArgs(
/// <summary>
/// Gets the visual elements found.
/// </summary>
public CoreVisualElement VisualElement { get; } = visualElement;
public IInteractable VisualElement { get; } = visualElement;
}
2 changes: 1 addition & 1 deletion src/LiveChartsCore/Kernel/Events/VisualElementHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ namespace LiveChartsCore.Kernel.Events;
/// <param name="visual">The sender visual.</param>
/// <param name="visualElementsArgs">The visual elements arguments.</param>
public delegate void VisualElementHandler(
CoreVisualElement visual, VisualElementEventArgs visualElementsArgs);
IInteractable visual, VisualElementEventArgs visualElementsArgs);
17 changes: 8 additions & 9 deletions src/LiveChartsCore/Kernel/Events/VisualElementsEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using System.Collections.Generic;
using System.Linq;
using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel.Sketches;
using LiveChartsCore.VisualElements;

namespace LiveChartsCore.Kernel.Events;
Expand All @@ -40,10 +39,10 @@ namespace LiveChartsCore.Kernel.Events;
/// <param name="visualElements">The visual elements.</param>
public class VisualElementsEventArgs(
Chart chart,
IEnumerable<CoreVisualElement> visualElements,
IEnumerable<IInteractable> visualElements,
LvcPoint pointerLocation)
{
private CoreVisualElement? _closer;
private IInteractable? _closer;

/// <summary>
/// Gets the chart.
Expand All @@ -58,24 +57,24 @@ public class VisualElementsEventArgs(
/// <summary>
/// Gets the closest visual element to the pointer position.
/// </summary>
public CoreVisualElement? ClosestToPointerVisualElement => _closer ??= FindClosest();
public IInteractable? ClosestToPointerVisualElement => _closer ??= FindClosest();

/// <summary>
/// Gets all the visual elements that were found.
/// </summary>
public IEnumerable<CoreVisualElement> VisualElements { get; } = visualElements;
public IEnumerable<IInteractable> VisualElements { get; } = visualElements;

private CoreVisualElement? FindClosest()
private IInteractable? FindClosest()
{
return VisualElements.Select(visual =>
{
var size = visual.Measure(Chart);
var hitbox = visual.GetHitBox();

return new
{
distance = Math.Sqrt(
Math.Pow(PointerLocation.X - (visual.X + size.Width * 0.5), 2) +
Math.Pow(PointerLocation.Y - (visual.Y + size.Height * 0.5), 2)),
Math.Pow(PointerLocation.X - (hitbox.X + hitbox.Width * 0.5), 2) +
Math.Pow(PointerLocation.Y - (hitbox.Y + hitbox.Height * 0.5), 2)),
visual
};
})
Expand Down
2 changes: 1 addition & 1 deletion src/LiveChartsCore/Kernel/Sketches/IChartView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,5 @@ public interface IChartView
/// </summary>
/// <param name="visualElements">The visual elements.</param>
/// <param name="pointer">The pointer location.</param>
void OnVisualElementPointerDown(IEnumerable<CoreVisualElement> visualElements, LvcPoint pointer);
void OnVisualElementPointerDown(IEnumerable<IInteractable> visualElements, LvcPoint pointer);
}
36 changes: 26 additions & 10 deletions src/LiveChartsCore/VisualElements/CoreVisualElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace LiveChartsCore.VisualElements;
/// <summary>
/// Defines the base visual element class, inheriting from this class makes it easy to implement a visual element.
/// </summary>
public abstract class CoreVisualElement : ChartElement, INotifyPropertyChanged
public abstract class CoreVisualElement : ChartElement, INotifyPropertyChanged, IInternalInteractable
{
internal double _x;
internal double _y;
Expand All @@ -44,6 +44,7 @@ public abstract class CoreVisualElement : ChartElement, INotifyPropertyChanged
private int _scalesYAt;
private MeasureUnit _locationUnit = MeasureUnit.Pixels;
private ClipMode _clippingMode = ClipMode.XY;
private Chart? _chart;

/// <summary>
/// Gets the primary scaler.
Expand Down Expand Up @@ -104,14 +105,14 @@ public abstract class CoreVisualElement : ChartElement, INotifyPropertyChanged
/// </summary>
public ClipMode ClippingMode { get => _clippingMode; set => SetProperty(ref _clippingMode, value); }

/// <summary>
/// Called when the pointer goes down on the visual.
/// </summary>
/// <inheritdoc cref="IInteractable.PointerDown"/>
public event VisualElementHandler? PointerDown;

/// <inheritdoc cref="ChartElement.Invalidate(Chart)"/>
public override void Invalidate(Chart chart)
{
_chart = chart;

if (chart is CartesianChartEngine cc)
{
var primaryAxis = cc.YAxes[ScalesYAt];
Expand Down Expand Up @@ -207,12 +208,17 @@ protected internal virtual IEnumerable<CoreVisualElement> IsHitBy(Chart chart, L
}
}

/// <summary>
/// Called when the pointer goes down on the visual.
/// </summary>
/// <param name="args">The event arguments.</param>
protected internal void InvokePointerDown(VisualElementEventArgs args) =>
PointerDown?.Invoke(this, args);
/// <inheritdoc cref="IInteractable.GetHitBox"/>
public LvcRectangle GetHitBox()
{
if (_chart is null) return new();

var location = GetActualCoordinate();
var translatedLocation = new LvcPoint(location.X + _translate.X, location.Y + Translate.Y);
var size = Measure(_chart);

return new(translatedLocation, size);
}

/// <summary>
/// Gets the geometries to draw.
Expand All @@ -236,8 +242,18 @@ protected virtual void ApplyTheme<T>()
_isInternalSet = false;
}

/// <inheritdoc cref="ChartElement.RemoveFromUI(Chart)"/>
public override void RemoveFromUI(Chart chart)
{
base.RemoveFromUI(chart);
_chart = null;
}

internal virtual void AlignToTopLeftCorner()
{
// just a workaround to align labels as the rest of the geometries.
}

void IInternalInteractable.InvokePointerDown(VisualElementEventArgs args) =>
PointerDown?.Invoke(this, args);
}
43 changes: 43 additions & 0 deletions src/LiveChartsCore/VisualElements/IInteractable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using LiveChartsCore.Drawing;
using LiveChartsCore.Kernel.Events;

namespace LiveChartsCore.VisualElements;

/// <summary>
/// Defines an object that the user can interact with.
/// </summary>
public interface IInteractable
{
/// <summary>
/// Called when the pointer goes down on the visual.
/// </summary>
event VisualElementHandler? PointerDown;

/// <summary>
/// Gets the hit box, an area where the user can interact with the object.
/// </summary>
/// <returns>The hitbox rectangle.</returns>
LvcRectangle GetHitBox();
}
30 changes: 30 additions & 0 deletions src/LiveChartsCore/VisualElements/IInternalInteractable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// The MIT License(MIT)
//
// Copyright(c) 2021 Alberto Rodriguez Orozco & LiveCharts Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using LiveChartsCore.Kernel.Events;

namespace LiveChartsCore.VisualElements;

internal interface IInternalInteractable : IInteractable
{
void InvokePointerDown(VisualElementEventArgs args);
}
Loading

0 comments on commit 1abd04d

Please sign in to comment.