Skip to content

Commit

Permalink
Merge pull request #139 from OggyP/main
Browse files Browse the repository at this point in the history
Smoothed out Bezier Curve Visuals in Wave Box
  • Loading branch information
nlogozzo authored Jan 4, 2024
2 parents eb5cb35 + 3564fda commit 45ec4ad
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 59 deletions.
3 changes: 1 addition & 2 deletions NickvisionCavalier.GNOME/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public Program(string[] args)
_mainWindow = null;
_mainWindowController = new MainWindowController(args);
_mainWindowController.AppInfo.Changelog =
@"* Updated to GNOME 45 runtime with latest libadwaita design
* Updated to .NET 8.0
@"* The wave box drawing mode now draws smoother bezier curves (Thanks @OggyP)
* Updated translations (Thanks everyone on Weblate!)";
_application.OnActivate += OnActivate;
if (File.Exists(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "/org.nickvision.cavalier.gresource"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public MainWindowController(string[] args)
AppInfo.EnglishShortName = "Cavalier";
AppInfo.ShortName = _("Cavalier");
AppInfo.Description = _("Visualize audio with CAVA");
AppInfo.Version = "2023.11.0";
AppInfo.Version = "2024.1.0-next";
AppInfo.SourceRepo = new Uri("https://github.com/NickvisionApps/Cavalier");
AppInfo.IssueTracker = new Uri("https://github.com/NickvisionApps/Cavalier/issues/new");
AppInfo.SupportUrl = new Uri("https://github.com/NickvisionApps/Cavalier/discussions");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,9 @@
</screenshot>
</screenshots>
<releases>
<release date="2023-11-17" version="2023.11.0">
<release version="2024.1.0-next" date="2024-01-01">
<description translatable="no">
<p>- Updated to GNOME 45 runtime with latest libadwaita design</p>
<p>- Updated to .NET 8.0</p>
<p>- The wave box drawing mode now draws smoother bezier curves (Thanks @OggyP)</p>
<p>- Updated translations (Thanks everyone on Weblate!)</p>
</description>
</release>
Expand Down
117 changes: 65 additions & 52 deletions NickvisionCavalier.Shared/Models/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,22 @@ private float GetMirrorHeight(float height)
return height;
}

/// <summary>
/// Flips a coordinate value to the other side of the screen and ensure it doesn't exceed the maximum value
/// </summary>
/// <param name="enabled">Flip coordinate enabled</param>
/// <param name="screenDimension">Dimension of screen in axis to be flipped</param>
/// <param name="coordinate">Coordinate in axis to be flipped</param>
/// <returns>New coordinate in axis</returns>
private float FlipCoord(bool enabled, float screenDimension, float coordinate)
{
var max = Math.Max(0, Math.Min(coordinate, screenDimension));
return enabled ? screenDimension - max : max;
// Note: By camping these values, it ensures the resulting bezier curve when smoothed
// never goes below 0 however, it does mean that it is not as smooth, as some values
// may be smoothed to be negative based on the expected gradient
}

/// <summary>
/// Draw picture, Wave mode, Box variant
/// </summary>
Expand All @@ -417,81 +433,78 @@ private float GetMirrorHeight(float height)
{
var step = (direction < DrawingDirection.LeftRight ? width : height) / (sample.Length - 1);
var path = new SKPath();
var flipImage = false;
var pointsArray = new (float x, float y)[sample.Length];
var gradientsList = new float[sample.Length];
switch (direction)
{
case DrawingDirection.TopBottom:
path.MoveTo(x, y + height * sample[0] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
for (var i = 0; i < sample.Length - 1; i++)
case DrawingDirection.TopBottom or DrawingDirection.BottomTop:
flipImage = direction == DrawingDirection.TopBottom;
// Create a list of point of where the the curve must pass through
for (var i = 0; i < sample.Length; i++)
{
path.CubicTo(
x + step * (i + 0.5f),
y + height * sample[i] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
x + step * (i + 0.5f),
y + height * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
x + step * (i + 1),
y + height * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
pointsArray[i] = (step * i, height * (1 - sample[i]));
}
if (Configuration.Current.Filling)
// Calculate gradient between the two neighbouring points for every point
for (var i = 0; i < pointsArray.Length; i++)
{
path.LineTo(x + width, y);
path.LineTo(x, y);
path.Close();
// Determine the previous and next point
// If there isn't one, use the current point
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
var gradient = nextPoint.y - previousPoint.y;
// If using the current point (when at the edges)
// then the run in rise/run = 1, otherwise a two step run exists
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
}
break;
case DrawingDirection.BottomTop:
path.MoveTo(x, y + height * (1 - sample[0]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
for (var i = 0; i < sample.Length - 1; i++)
var yOffset = y + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
path.MoveTo(x + pointsArray[0].x, yOffset + FlipCoord(flipImage, height, pointsArray[0].y));
for (var i = 0; i < pointsArray.Length - 1; i++)
{
path.CubicTo(
x + step * (i + 0.5f),
y + height * (1 - sample[i]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
x + step * (i + 0.5f),
y + height * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
x + step * (i + 1),
y + height * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
x + pointsArray[i].x + step * 0.5f,
yOffset + FlipCoord(flipImage, height, pointsArray[i].y + gradientsList[i] * 0.5f),
x + pointsArray[i + 1].x + step * -0.5f,
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y + gradientsList[i + 1] * -0.5f),
x + pointsArray[i + 1].x,
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y));
}
if (Configuration.Current.Filling)
{
path.LineTo(x + width, y + height);
path.LineTo(x, y + height);
path.LineTo(x + width, y + FlipCoord(flipImage, height, height));
path.LineTo(x, y + FlipCoord(flipImage, height, height));
path.Close();
}
break;
case DrawingDirection.LeftRight:
path.MoveTo(x + width * sample[0] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2), y);
for (var i = 0; i < sample.Length - 1; i++)
case DrawingDirection.LeftRight or DrawingDirection.RightLeft:
flipImage = direction == DrawingDirection.RightLeft;
for (var i = 0; i < sample.Length; i++)
{
path.CubicTo(
x + width * sample[i] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 0.5f),
x + width * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 0.5f),
x + width * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 1));
pointsArray[i] = (width * sample[i], step * i);
}
if (Configuration.Current.Filling)
for (var i = 0; i < pointsArray.Length; i++)
{
path.LineTo(x, y + height);
path.LineTo(x, y);
path.Close();
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
var gradient = nextPoint.x - previousPoint.x;
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
}
break;
case DrawingDirection.RightLeft:
path.MoveTo(x + width * (1 - sample[0]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2), y);
for (var i = 0; i < sample.Length - 1; i++)
var xOffset = x - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
path.MoveTo(xOffset + FlipCoord(flipImage, width, pointsArray[0].x), y + pointsArray[0].y);
for (var i = 0; i < pointsArray.Length - 1; i++)
{
path.CubicTo(
x + width * (1 - sample[i]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 0.5f),
x + width * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 0.5f),
x + width * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
y + step * (i + 1));
xOffset + FlipCoord(flipImage, width, pointsArray[i].x + gradientsList[i] * 0.5f),
y + pointsArray[i].y + step * 0.5f,
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x + gradientsList[i + 1] * -0.5f),
y + pointsArray[i + 1].y + step * -0.5f,
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x),
y + pointsArray[i + 1].y);
}
if (Configuration.Current.Filling)
{
path.LineTo(x + width, y + height);
path.LineTo(x + width, y);
path.LineTo(x + FlipCoord(flipImage, width, 0), y + height);
path.LineTo(x + FlipCoord(flipImage, width, 0), y);
path.Close();
}
break;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how can you help the proje

# Dependencies
- [.NET 8](https://dotnet.microsoft.com/en-us/)
- [CAVA](https://github.com/karlstav/cava/) >= 0.9.0
- [CAVA](https://github.com/karlstav/cava/) >= 0.9.1

# Code of Conduct

Expand Down

0 comments on commit 45ec4ad

Please sign in to comment.