Skip to content

Commit 45ec4ad

Browse files
authored
Merge pull request #139 from OggyP/main
Smoothed out Bezier Curve Visuals in Wave Box
2 parents eb5cb35 + 3564fda commit 45ec4ad

5 files changed

Lines changed: 70 additions & 59 deletions

File tree

NickvisionCavalier.GNOME/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public Program(string[] args)
3333
_mainWindow = null;
3434
_mainWindowController = new MainWindowController(args);
3535
_mainWindowController.AppInfo.Changelog =
36-
@"* Updated to GNOME 45 runtime with latest libadwaita design
37-
* Updated to .NET 8.0
36+
@"* The wave box drawing mode now draws smoother bezier curves (Thanks @OggyP)
3837
* Updated translations (Thanks everyone on Weblate!)";
3938
_application.OnActivate += OnActivate;
4039
if (File.Exists(Path.GetFullPath(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)) + "/org.nickvision.cavalier.gresource"))

NickvisionCavalier.Shared/Controllers/MainWindowController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public MainWindowController(string[] args)
6161
AppInfo.EnglishShortName = "Cavalier";
6262
AppInfo.ShortName = _("Cavalier");
6363
AppInfo.Description = _("Visualize audio with CAVA");
64-
AppInfo.Version = "2023.11.0";
64+
AppInfo.Version = "2024.1.0-next";
6565
AppInfo.SourceRepo = new Uri("https://github.com/NickvisionApps/Cavalier");
6666
AppInfo.IssueTracker = new Uri("https://github.com/NickvisionApps/Cavalier/issues/new");
6767
AppInfo.SupportUrl = new Uri("https://github.com/NickvisionApps/Cavalier/discussions");

NickvisionCavalier.Shared/Linux/org.nickvision.cavalier.metainfo.xml.in

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@
4747
</screenshot>
4848
</screenshots>
4949
<releases>
50-
<release date="2023-11-17" version="2023.11.0">
50+
<release version="2024.1.0-next" date="2024-01-01">
5151
<description translatable="no">
52-
<p>- Updated to GNOME 45 runtime with latest libadwaita design</p>
53-
<p>- Updated to .NET 8.0</p>
52+
<p>- The wave box drawing mode now draws smoother bezier curves (Thanks @OggyP)</p>
5453
<p>- Updated translations (Thanks everyone on Weblate!)</p>
5554
</description>
5655
</release>

NickvisionCavalier.Shared/Models/Renderer.cs

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,22 @@ private float GetMirrorHeight(float height)
400400
return height;
401401
}
402402

403+
/// <summary>
404+
/// Flips a coordinate value to the other side of the screen and ensure it doesn't exceed the maximum value
405+
/// </summary>
406+
/// <param name="enabled">Flip coordinate enabled</param>
407+
/// <param name="screenDimension">Dimension of screen in axis to be flipped</param>
408+
/// <param name="coordinate">Coordinate in axis to be flipped</param>
409+
/// <returns>New coordinate in axis</returns>
410+
private float FlipCoord(bool enabled, float screenDimension, float coordinate)
411+
{
412+
var max = Math.Max(0, Math.Min(coordinate, screenDimension));
413+
return enabled ? screenDimension - max : max;
414+
// Note: By camping these values, it ensures the resulting bezier curve when smoothed
415+
// never goes below 0 however, it does mean that it is not as smooth, as some values
416+
// may be smoothed to be negative based on the expected gradient
417+
}
418+
403419
/// <summary>
404420
/// Draw picture, Wave mode, Box variant
405421
/// </summary>
@@ -417,81 +433,78 @@ private float GetMirrorHeight(float height)
417433
{
418434
var step = (direction < DrawingDirection.LeftRight ? width : height) / (sample.Length - 1);
419435
var path = new SKPath();
436+
var flipImage = false;
437+
var pointsArray = new (float x, float y)[sample.Length];
438+
var gradientsList = new float[sample.Length];
420439
switch (direction)
421440
{
422-
case DrawingDirection.TopBottom:
423-
path.MoveTo(x, y + height * sample[0] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
424-
for (var i = 0; i < sample.Length - 1; i++)
441+
case DrawingDirection.TopBottom or DrawingDirection.BottomTop:
442+
flipImage = direction == DrawingDirection.TopBottom;
443+
// Create a list of point of where the the curve must pass through
444+
for (var i = 0; i < sample.Length; i++)
425445
{
426-
path.CubicTo(
427-
x + step * (i + 0.5f),
428-
y + height * sample[i] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
429-
x + step * (i + 0.5f),
430-
y + height * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
431-
x + step * (i + 1),
432-
y + height * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
446+
pointsArray[i] = (step * i, height * (1 - sample[i]));
433447
}
434-
if (Configuration.Current.Filling)
448+
// Calculate gradient between the two neighbouring points for every point
449+
for (var i = 0; i < pointsArray.Length; i++)
435450
{
436-
path.LineTo(x + width, y);
437-
path.LineTo(x, y);
438-
path.Close();
451+
// Determine the previous and next point
452+
// If there isn't one, use the current point
453+
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
454+
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
455+
var gradient = nextPoint.y - previousPoint.y;
456+
// If using the current point (when at the edges)
457+
// then the run in rise/run = 1, otherwise a two step run exists
458+
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
439459
}
440-
break;
441-
case DrawingDirection.BottomTop:
442-
path.MoveTo(x, y + height * (1 - sample[0]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
443-
for (var i = 0; i < sample.Length - 1; i++)
460+
var yOffset = y + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
461+
path.MoveTo(x + pointsArray[0].x, yOffset + FlipCoord(flipImage, height, pointsArray[0].y));
462+
for (var i = 0; i < pointsArray.Length - 1; i++)
444463
{
445464
path.CubicTo(
446-
x + step * (i + 0.5f),
447-
y + height * (1 - sample[i]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
448-
x + step * (i + 0.5f),
449-
y + height * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
450-
x + step * (i + 1),
451-
y + height * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2));
465+
x + pointsArray[i].x + step * 0.5f,
466+
yOffset + FlipCoord(flipImage, height, pointsArray[i].y + gradientsList[i] * 0.5f),
467+
x + pointsArray[i + 1].x + step * -0.5f,
468+
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y + gradientsList[i + 1] * -0.5f),
469+
x + pointsArray[i + 1].x,
470+
yOffset + FlipCoord(flipImage, height, pointsArray[i + 1].y));
452471
}
453472
if (Configuration.Current.Filling)
454473
{
455-
path.LineTo(x + width, y + height);
456-
path.LineTo(x, y + height);
474+
path.LineTo(x + width, y + FlipCoord(flipImage, height, height));
475+
path.LineTo(x, y + FlipCoord(flipImage, height, height));
457476
path.Close();
458477
}
459478
break;
460-
case DrawingDirection.LeftRight:
461-
path.MoveTo(x + width * sample[0] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2), y);
462-
for (var i = 0; i < sample.Length - 1; i++)
479+
case DrawingDirection.LeftRight or DrawingDirection.RightLeft:
480+
flipImage = direction == DrawingDirection.RightLeft;
481+
for (var i = 0; i < sample.Length; i++)
463482
{
464-
path.CubicTo(
465-
x + width * sample[i] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
466-
y + step * (i + 0.5f),
467-
x + width * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
468-
y + step * (i + 0.5f),
469-
x + width * sample[i + 1] - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
470-
y + step * (i + 1));
483+
pointsArray[i] = (width * sample[i], step * i);
471484
}
472-
if (Configuration.Current.Filling)
485+
for (var i = 0; i < pointsArray.Length; i++)
473486
{
474-
path.LineTo(x, y + height);
475-
path.LineTo(x, y);
476-
path.Close();
487+
var previousPoint = pointsArray[Math.Max(i - 1, 0)];
488+
var nextPoint = pointsArray[Math.Min(i + 1, pointsArray.Length - 1)];
489+
var gradient = nextPoint.x - previousPoint.x;
490+
gradientsList[i] = i == 0 || i == pointsArray.Length - 1 ? gradient : gradient / 2;
477491
}
478-
break;
479-
case DrawingDirection.RightLeft:
480-
path.MoveTo(x + width * (1 - sample[0]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2), y);
481-
for (var i = 0; i < sample.Length - 1; i++)
492+
var xOffset = x - (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2);
493+
path.MoveTo(xOffset + FlipCoord(flipImage, width, pointsArray[0].x), y + pointsArray[0].y);
494+
for (var i = 0; i < pointsArray.Length - 1; i++)
482495
{
483496
path.CubicTo(
484-
x + width * (1 - sample[i]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
485-
y + step * (i + 0.5f),
486-
x + width * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
487-
y + step * (i + 0.5f),
488-
x + width * (1 - sample[i + 1]) + (Configuration.Current.Filling ? 0 : Configuration.Current.LinesThickness / 2),
489-
y + step * (i + 1));
497+
xOffset + FlipCoord(flipImage, width, pointsArray[i].x + gradientsList[i] * 0.5f),
498+
y + pointsArray[i].y + step * 0.5f,
499+
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x + gradientsList[i + 1] * -0.5f),
500+
y + pointsArray[i + 1].y + step * -0.5f,
501+
xOffset + FlipCoord(flipImage, width, pointsArray[i + 1].x),
502+
y + pointsArray[i + 1].y);
490503
}
491504
if (Configuration.Current.Filling)
492505
{
493-
path.LineTo(x + width, y + height);
494-
path.LineTo(x + width, y);
506+
path.LineTo(x + FlipCoord(flipImage, width, 0), y + height);
507+
path.LineTo(x + FlipCoord(flipImage, width, 0), y);
495508
path.Close();
496509
}
497510
break;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how can you help the proje
3838

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

4343
# Code of Conduct
4444

0 commit comments

Comments
 (0)