Skip to content

Commit

Permalink
View as composer
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Oct 9, 2024
1 parent d727bce commit 7e40290
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 121 deletions.
2 changes: 1 addition & 1 deletion Canvas.Core/Canvas.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>3.3.0</Version>
<Version>4.0.0</Version>
<Description>Internal package used in Canvas.Views.Web</Description>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
Expand Down
131 changes: 51 additions & 80 deletions Canvas.Core/Composers/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public interface IComposer
/// <summary>
/// Domain
/// </summary>
DomainModel Domain { get; set; }
DimensionModel Dimension { get; set; }

/// <summary>
/// Engine
Expand Down Expand Up @@ -80,7 +80,7 @@ public interface IComposer
/// <summary>
/// Domain update event
/// </summary>
Action<DomainModel, string> OnRender { get; set; }
Action<DimensionModel> OnAction { get; set; }

/// <summary>
/// Mouse wheel event
Expand Down Expand Up @@ -114,25 +114,17 @@ public interface IComposer
void OnMouseLeave(ViewModel e);

/// <summary>
/// Create
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
Task Create<T>() where T : IEngine, new();

/// <summary>
/// Update
/// Update items
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
Task Update(DomainModel? message = null, string source = null);
/// <returns></returns>
void Render(DimensionModel message);

/// <summary>
/// Update items
/// Update dimensions
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
void Render(DomainModel message);
/// <param name="domain"></param>
void SetDimensions(DimensionModel domain);

/// <summary>
/// Convert values to canvas coordinates
Expand Down Expand Up @@ -206,7 +198,7 @@ public partial class Composer : IComposer
/// <summary>
/// Domain
/// </summary>
public virtual DomainModel Domain { get; set; }
public virtual DimensionModel Dimension { get; set; }

/// <summary>
/// Engine
Expand Down Expand Up @@ -246,7 +238,7 @@ public partial class Composer : IComposer
/// <summary>
/// Domain update event
/// </summary>
public virtual Action<DomainModel, string> OnRender { get; set; }
public virtual Action<DimensionModel> OnAction { get; set; }

/// <summary>
/// Constructor
Expand All @@ -258,15 +250,15 @@ public Composer()
ValueCount = 3;
IndexCount = 9;

Domain = new DomainModel();
Dimension = new DimensionModel();
Components = new Dictionary<string, ComponentModel>();
Items = [];

ShowBoard = o => $"{o:0.00}";
ShowIndex = o => $"{o:0.00}";
ShowValue = o => $"{o:0.00}";

OnRender = (message, source) => { };
OnAction = o => { };

Components[nameof(ComponentEnum.Shape)] = new ComponentModel
{
Expand Down Expand Up @@ -307,32 +299,12 @@ public Composer()
};
}

/// <summary>
/// Create
/// </summary>
/// <param name="message"></param>
/// <param name="source"></param>
/// <returns></returns>
public virtual Task Create<T>() where T : IEngine, new() => View.Create<T>(() => this);

/// <summary>
/// Update
/// </summary>
/// <param name="domain"></param>
/// <param name="source"></param>
/// <returns></returns>
public virtual Task Update(DomainModel? domain = null, string source = null)
{
OnRender(Domain = ComposeDomain(domain ?? Domain), source);
return View.Update(Domain);
}

/// <summary>
/// Update items
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public virtual void Render(DomainModel domain)
public virtual void Render(DimensionModel domain)
{
Engine.Clear();

Expand All @@ -359,11 +331,11 @@ public virtual void Render(DomainModel domain)
/// <param name="item"></param>
public virtual DataModel GetItemPosition(DataModel item)
{
var valueRange = Domain.MaxValue - Domain.MinValue;
var minX = Domain.MinIndex;
var maxX = Domain.MaxIndex;
var minY = Domain.MinValue - valueRange * Space;
var maxY = Domain.MaxValue + valueRange * Space;
var valueRange = Dimension.MaxValue - Dimension.MinValue;
var minX = Dimension.MinIndex;
var maxX = Dimension.MaxIndex;
var minY = Dimension.MinValue - valueRange * Space;
var maxY = Dimension.MaxValue + valueRange * Space;

// Convert to device pixels

Expand Down Expand Up @@ -395,11 +367,11 @@ public virtual DataModel GetItemPosition(double index, double value)
/// <param name="item"></param>
public virtual DataModel GetItemValue(DataModel item)
{
var valueRange = Domain.MaxValue - Domain.MinValue;
var minX = Domain.MinIndex;
var maxX = Domain.MaxIndex;
var minY = Domain.MinValue - valueRange * Space;
var maxY = Domain.MaxValue + valueRange * Space;
var valueRange = Dimension.MaxValue - Dimension.MinValue;
var minX = Dimension.MinIndex;
var maxX = Dimension.MaxIndex;
var minY = Dimension.MinValue - valueRange * Space;
var maxY = Dimension.MaxValue + valueRange * Space;

// Convert to values

Expand All @@ -420,8 +392,8 @@ public virtual DataModel GetItemValue(DataModel item)
/// <param name="delta"></param>
public virtual IList<double> ZoomValue(int delta)
{
var minY = Domain.MinValue;
var maxY = Domain.MaxValue;
var minY = Dimension.MinValue;
var maxY = Dimension.MaxValue;
var domain = new List<double> { minY, maxY };

if (Equals(maxY, minY))
Expand Down Expand Up @@ -454,8 +426,8 @@ public virtual IList<double> ZoomValue(int delta)
/// <param name="delta"></param>
public virtual IList<int> ZoomIndex(int delta)
{
var minX = Domain.MinIndex;
var maxX = Domain.MaxIndex;
var minX = Dimension.MinIndex;
var maxX = Dimension.MaxIndex;
var domain = new[] { minX, maxX };

if (Equals(minX, maxX))
Expand All @@ -482,8 +454,8 @@ public virtual IList<int> ZoomIndex(int delta)
/// <param name="delta"></param>
public virtual IList<int> PanIndex(int delta)
{
var minX = Domain.MinIndex;
var maxX = Domain.MaxIndex;
var minX = Dimension.MinIndex;
var maxX = Dimension.MaxIndex;
var domain = new List<int> { minX, maxX };

if (Equals(minX, maxX))
Expand Down Expand Up @@ -516,15 +488,15 @@ public virtual IList<int> PanIndex(int delta)
public virtual void OnWheel(ViewModel e)
{
var isZoom = e.IsShape;
var domain = Domain;
var domain = Dimension;

switch (true)
{
case true when e.Data.Y > 0: domain.IndexDomain = isZoom ? ZoomIndex(-1) : PanIndex(1); break;
case true when e.Data.Y < 0: domain.IndexDomain = isZoom ? ZoomIndex(1) : PanIndex(-1); break;
}

Update(domain, Name);
View.Update(domain, Name);
}

/// <summary>
Expand All @@ -539,15 +511,15 @@ public virtual void OnMouseMove(ViewModel e)
{
var deltaX = MoveEvent?.Data.X - e.Data.X;
var deltaY = MoveEvent?.Data.Y - e.Data.Y;
var domain = Domain;
var domain = Dimension;

switch (true)
{
case true when deltaX > 0: domain.IndexDomain = PanIndex(1); break;
case true when deltaX < 0: domain.IndexDomain = PanIndex(-1); break;
}

Update(domain, Name);
View.Update(domain, Name);
}

MoveEvent = e;
Expand All @@ -566,8 +538,7 @@ public virtual void OnScale(ViewModel e, int orientation = 0)
{
var deltaX = ScaleEvent?.Data.X - e.Data.X;
var deltaY = ScaleEvent?.Data.Y - e.Data.Y;
var domain = Domain;
var source = Name;
var domain = Dimension;

switch (orientation > 0)
{
Expand All @@ -577,11 +548,11 @@ public virtual void OnScale(ViewModel e, int orientation = 0)

switch (orientation < 0)
{
case true when deltaY > 0: domain.ValueDomain = ZoomValue(-1); source = null; break;
case true when deltaY < 0: domain.ValueDomain = ZoomValue(1); source = null; break;
case true when deltaY > 0: domain.ValueDomain = ZoomValue(-1); break;
case true when deltaY < 0: domain.ValueDomain = ZoomValue(1); break;
}

Update(domain, source);
View.Update(domain, Name);
}

ScaleEvent = e;
Expand All @@ -595,11 +566,11 @@ public virtual void OnMouseDown(ViewModel e)
{
if (e.IsControl)
{
var domain = Domain;
var domain = Dimension;

domain.ValueDomain = null;

Update(domain);
View.Update(domain);
}
}

Expand All @@ -617,8 +588,8 @@ public virtual void OnMouseLeave(ViewModel e)
/// </summary>
protected virtual IList<MarkerModel> GetIndices()
{
var minIndex = Domain.MinIndex;
var maxIndex = Domain.MaxIndex;
var minIndex = Dimension.MinIndex;
var maxIndex = Dimension.MaxIndex;
var range = 0.0 + maxIndex - minIndex;
var center = Math.Round(minIndex + range / 2.0, MidpointRounding.ToEven);
var step = Math.Round(range / IndexCount, MidpointRounding.ToZero);
Expand Down Expand Up @@ -653,8 +624,8 @@ void createItem(double i)
/// </summary>
protected virtual IList<MarkerModel> GetValues()
{
var minValue = Domain.MinValue;
var maxValue = Domain.MaxValue;
var minValue = Dimension.MinValue;
var maxValue = Dimension.MaxValue;
var range = maxValue - minValue;
var center = minValue + range / 2.0;
var step = range / ValueCount;
Expand Down Expand Up @@ -689,7 +660,7 @@ void createItem(double i)
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
protected virtual DomainModel ComposeDomain(DomainModel domain)
public virtual void SetDimensions(DimensionModel domain)
{
var autoMin = 0;
var autoMax = Items.Count;
Expand All @@ -709,15 +680,16 @@ protected virtual DomainModel ComposeDomain(DomainModel domain)

if (min > max)
{
return response;
Dimension = response;
return;
}

if (Equals(min, max))
{
response.AutoValueDomain[0] = Math.Min(0, min);
response.AutoValueDomain[1] = Math.Max(0, max);

return response;
Dimension = response;
return;
}

if (min < 0 && max > 0)
Expand All @@ -726,14 +698,13 @@ protected virtual DomainModel ComposeDomain(DomainModel domain)

response.AutoValueDomain[0] = -extreme;
response.AutoValueDomain[1] = extreme;

return response;
Dimension = response;
return;
}

response.AutoValueDomain[0] = min;
response.AutoValueDomain[1] = max;

return response;
Dimension = response;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Canvas.Core/Composers/GroupComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class GroupComposer : Composer
/// <param name="engine"></param>
/// <param name="domain"></param>
/// <returns></returns>
public override void Render(DomainModel domain)
public override void Render(DimensionModel domain)
{
Engine.Clear();

Expand Down
Loading

0 comments on commit 7e40290

Please sign in to comment.