Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiusgreat committed Jan 8, 2025
1 parent 820c876 commit 6ecddac
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 166 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>5.0.0</Version>
<Version>5.5.0</Version>
<Description>Internal package used in Canvas.Views.Web</Description>
<Authors>artemiusgreat</Authors>
<Copyright>indemos.com</Copyright>
Expand Down
135 changes: 49 additions & 86 deletions Canvas.Core/Composers/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Canvas.Core.Composers
{
public interface IComposer
public interface IComposer : IDisposable
{
/// <summary>
/// Name
Expand All @@ -30,12 +29,12 @@ public interface IComposer
/// <summary>
/// Index ticks
/// </summary>
int IndexCount { get; set; }
int Indices { get; set; }

/// <summary>
/// Value ticks
/// </summary>
int ValueCount { get; set; }
int Values { get; set; }

/// <summary>
/// Domain
Expand All @@ -52,11 +51,6 @@ public interface IComposer
/// </summary>
IList<IShape> Items { get; set; }

/// <summary>
/// Views
/// </summary>
IView View { get; set; }

/// <summary>
/// Options
/// </summary>
Expand Down Expand Up @@ -86,45 +80,33 @@ public interface IComposer
/// Mouse wheel event
/// </summary>
/// <param name="e"></param>
void OnWheel(ViewModel e);
DimensionModel OnWheel(ViewModel e);

/// <summary>
/// Horizontal drag and resize event
/// </summary>
/// <param name="e"></param>
void OnMouseMove(ViewModel e);
DimensionModel OnMouseMove(ViewModel e);

/// <summary>
/// Resize event
/// </summary>
/// <param name="e"></param>
/// <param name="orientation"></param>
void OnScale(ViewModel e, int orientation = 0);

/// <summary>
/// Click event in the view area
/// </summary>
/// <param name="e"></param>
void OnMouseDown(ViewModel e);

/// <summary>
/// Mouse leave event
/// </summary>
/// <param name="e"></param>
void OnMouseLeave(ViewModel e);
DimensionModel OnScale(ViewModel e, int orientation = 0);

/// <summary>
/// Update items
/// </summary>
/// <param name="message"></param>
/// <returns></returns>
void Render(DimensionModel message);
ScopeModel Render(DimensionModel message);

/// <summary>
/// Update dimensions
/// </summary>
/// <param name="domain"></param>
void SetDimensions(DimensionModel domain);
DimensionModel SetDimensions(DimensionModel domain);

/// <summary>
/// Convert values to canvas coordinates
Expand Down Expand Up @@ -188,12 +170,12 @@ public partial class Composer : IComposer
/// <summary>
/// Index ticks
/// </summary>
public virtual int IndexCount { get; set; }
public virtual int Indices { get; set; }

/// <summary>
/// Value ticks
/// </summary>
public virtual int ValueCount { get; set; }
public virtual int Values { get; set; }

/// <summary>
/// Domain
Expand All @@ -210,11 +192,6 @@ public partial class Composer : IComposer
/// </summary>
public virtual IList<IShape> Items { get; set; }

/// <summary>
/// Views
/// </summary>
public virtual IView View { get; set; }

/// <summary>
/// Options
/// </summary>
Expand Down Expand Up @@ -247,8 +224,8 @@ public Composer()
{
Size = 0.5;
Space = 0;
ValueCount = 3;
IndexCount = 9;
Values = 3;
Indices = 9;

Dimension = new DimensionModel();
Components = new Dictionary<string, ComponentModel>();
Expand Down Expand Up @@ -304,12 +281,11 @@ public Composer()
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public virtual void Render(DimensionModel domain)
public virtual ScopeModel Render(DimensionModel domain)
{
Engine.Clear();

View.Values = GetValues();
View.Indices = GetIndices();
SetDimensions(domain);

for (var i = domain.MinIndex; i < domain.MaxIndex; i++)
{
Expand All @@ -323,6 +299,12 @@ public virtual void Render(DimensionModel domain)
item.Composer = this;
item.CreateShape(i, null, Items);
}

return new ScopeModel
{
Values = GetValues(),
Indices = GetIndices()
};
}

/// <summary>
Expand Down Expand Up @@ -436,7 +418,7 @@ public virtual IList<int> ZoomIndex(int delta)
}

var increment = Math.Sign(delta);
var isInRange = maxX - minX > IndexCount * increment * 2;
var isInRange = maxX - minX > Indices * increment * 2;

if (isInRange)
{
Expand All @@ -450,7 +432,6 @@ public virtual IList<int> ZoomIndex(int delta)
/// <summary>
/// Index scale
/// </summary>
/// <param name="engine"></param>
/// <param name="delta"></param>
public virtual IList<int> PanIndex(int delta)
{
Expand Down Expand Up @@ -485,7 +466,7 @@ public virtual IList<int> PanIndex(int delta)
/// Mouse wheel event
/// </summary>
/// <param name="e"></param>
public virtual void OnWheel(ViewModel e)
public virtual DimensionModel OnWheel(ViewModel e)
{
var isZoom = e.IsShape;
var domain = Dimension;
Expand All @@ -496,49 +477,51 @@ public virtual void OnWheel(ViewModel e)
case true when e.Data.Y < 0: domain.IndexDomain = isZoom ? ZoomIndex(1) : PanIndex(-1); break;
}

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

/// <summary>
/// Horizontal drag and resize event
/// </summary>
/// <param name="e"></param>
public virtual void OnMouseMove(ViewModel e)
public virtual DimensionModel OnMouseMove(ViewModel e)
{
MoveEvent ??= e;

var domain = Dimension;

if (e.IsMove)
{
var deltaX = MoveEvent?.Data.X - e.Data.X;
var deltaY = MoveEvent?.Data.Y - e.Data.Y;
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;
}

View.Update(domain, Name);
}

MoveEvent = e;

return domain;
}

/// <summary>
/// Resize event
/// </summary>
/// <param name="e"></param>
/// <param name="orientation"></param>
public virtual void OnScale(ViewModel e, int orientation = 0)
public virtual DimensionModel OnScale(ViewModel e, int orientation = 0)
{
ScaleEvent ??= e;

var domain = Dimension;

if (e.IsMove)
{
var deltaX = ScaleEvent?.Data.X - e.Data.X;
var deltaY = ScaleEvent?.Data.Y - e.Data.Y;
var domain = Dimension;

switch (orientation > 0)
{
Expand All @@ -551,36 +534,11 @@ public virtual void OnScale(ViewModel e, int orientation = 0)
case true when deltaY > 0: domain.ValueDomain = ZoomValue(-1); break;
case true when deltaY < 0: domain.ValueDomain = ZoomValue(1); break;
}

View.Update(domain, Name);
}

ScaleEvent = e;
}

/// <summary>
/// Click event in the view area
/// </summary>
/// <param name="e"></param>
public virtual void OnMouseDown(ViewModel e)
{
if (e.IsControl)
{
var domain = Dimension;

domain.ValueDomain = null;

View.Update(domain);
}
}

/// <summary>
/// Mouse leave event
/// </summary>
/// <param name="e"></param>
public virtual void OnMouseLeave(ViewModel e)
{
MoveEvent = null;
return domain;
}

/// <summary>
Expand All @@ -592,7 +550,7 @@ protected virtual IList<MarkerModel> GetIndices()
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);
var step = Math.Round(range / Indices, MidpointRounding.ToZero);
var items = new List<MarkerModel>();

void createItem(double i)
Expand All @@ -610,7 +568,7 @@ void createItem(double i)
}
}

for (var i = 0; i <= Math.Min(IndexCount, range); i++)
for (var i = 0; i <= Math.Min(Indices, range); i++)
{
createItem(center - i * step);
createItem(center + i * step);
Expand All @@ -628,7 +586,7 @@ protected virtual IList<MarkerModel> GetValues()
var maxValue = Dimension.MaxValue;
var range = maxValue - minValue;
var center = minValue + range / 2.0;
var step = range / ValueCount;
var step = range / Values;
var items = new List<MarkerModel>();

void createItem(double i)
Expand All @@ -646,7 +604,7 @@ void createItem(double i)
}
}

for (var i = 0; i <= Math.Min(ValueCount, range); i++)
for (var i = 0; i <= Math.Min(Values, range); i++)
{
createItem(center - i * step);
createItem(center + i * step);
Expand All @@ -660,7 +618,7 @@ void createItem(double i)
/// </summary>
/// <param name="domain"></param>
/// <returns></returns>
public virtual void SetDimensions(DimensionModel domain)
public virtual DimensionModel SetDimensions(DimensionModel domain)
{
var autoMin = 0;
var autoMax = Items.Count;
Expand All @@ -680,16 +638,15 @@ public virtual void SetDimensions(DimensionModel domain)

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

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

return Dimension = response;
}

if (min < 0 && max > 0)
Expand All @@ -698,13 +655,14 @@ public virtual void SetDimensions(DimensionModel domain)

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

return Dimension = response;
}

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

return Dimension = response;
}

/// <summary>
Expand Down Expand Up @@ -732,5 +690,10 @@ protected virtual (double, double, double) GetExtremes(int i, double min, double

return (min, max, average);
}

/// <summary>
/// Dispose
/// </summary>
public void Dispose() => Engine?.Dispose();
}
}
Loading

0 comments on commit 6ecddac

Please sign in to comment.