refactor: rewrite Models.CommitGraph.PathHelper (#528)

This commit is contained in:
leo 2024-09-29 18:16:15 +08:00
parent fe015f9bfd
commit 4b5d65cdb7
No known key found for this signature in database

View file

@ -20,7 +20,6 @@ namespace SourceGit.Models
public bool IsMerged; public bool IsMerged;
public double LastX; public double LastX;
public double LastY; public double LastY;
public double EndY;
public Path Path; public Path Path;
public PathHelper(string next, bool isMerged, int color, Point start) public PathHelper(string next, bool isMerged, int color, Point start)
@ -29,7 +28,6 @@ namespace SourceGit.Models
IsMerged = isMerged; IsMerged = isMerged;
LastX = start.X; LastX = start.X;
LastY = start.Y; LastY = start.Y;
EndY = LastY;
Path = new Path(); Path = new Path();
Path.Color = color; Path.Color = color;
@ -42,7 +40,6 @@ namespace SourceGit.Models
IsMerged = isMerged; IsMerged = isMerged;
LastX = to.X; LastX = to.X;
LastY = to.Y; LastY = to.Y;
EndY = LastY;
Path = new Path(); Path = new Path();
Path.Color = color; Path.Color = color;
@ -50,43 +47,87 @@ namespace SourceGit.Models
Path.Points.Add(to); Path.Points.Add(to);
} }
public void Add(double x, double y, double halfHeight, bool isEnd = false) /// <summary>
/// A path that just passed this row.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="halfHeight"></param>
public void Pass(double x, double y, double halfHeight)
{ {
if (x > LastX) if (x > LastX)
{ {
Add(new Point(LastX, LastY)); Add(LastX, LastY);
Add(new Point(x, y - halfHeight)); Add(x, y - halfHeight);
if (isEnd)
Add(new Point(x, y));
} }
else if (x < LastX) else if (x < LastX)
{ {
var testY = LastY + halfHeight; Add(LastX, y - halfHeight);
if (y > testY)
Add(new Point(LastX, testY));
if (!isEnd)
y += halfHeight; y += halfHeight;
Add(x, y);
Add(new Point(x, y));
}
else if (isEnd)
{
Add(new Point(x, y));
} }
LastX = x; LastX = x;
LastY = y; LastY = y;
} }
private void Add(Point p) /// <summary>
/// A path that has commit in this row but not ended
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="halfHeight"></param>
public void Goto(double x, double y, double halfHeight)
{ {
if (EndY < p.Y) if (x > LastX)
{ {
Path.Points.Add(p); Add(LastX, LastY);
EndY = p.Y; Add(x, y - halfHeight);
}
else if (x < LastX)
{
Add(LastX, y - halfHeight);
Add(x, y);
}
LastX = x;
LastY = y;
}
/// <summary>
/// A path that has commit in this row and end.
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="halfHeight"></param>
public void End(double x, double y, double halfHeight)
{
if (x > LastX)
{
Add(LastX, LastY);
Add(x, y - halfHeight);
}
else if (x < LastX)
{
Add(LastX, y - halfHeight);
}
Add(x, y);
LastX = x;
LastY = y;
}
private void Add(double x, double y)
{
if (_endY < y)
{
Path.Points.Add(new Point(x, y));
_endY = y;
} }
} }
private double _endY = 0;
} }
public class Link public class Link
@ -173,17 +214,17 @@ namespace SourceGit.Models
if (commit.Parents.Count > 0) if (commit.Parents.Count > 0)
{ {
major.Next = commit.Parents[0]; major.Next = commit.Parents[0];
major.Add(offsetX, offsetY, HALF_HEIGHT); major.Goto(offsetX, offsetY, HALF_HEIGHT);
} }
else else
{ {
major.Add(offsetX, offsetY, HALF_HEIGHT, true); major.End(offsetX, offsetY, HALF_HEIGHT);
ended.Add(l); ended.Add(l);
} }
} }
else else
{ {
l.Add(major.LastX, offsetY, HALF_HEIGHT, true); l.End(major.LastX, offsetY, HALF_HEIGHT);
ended.Add(l); ended.Add(l);
} }
@ -193,7 +234,7 @@ namespace SourceGit.Models
else else
{ {
offsetX += UNIT_WIDTH; offsetX += UNIT_WIDTH;
l.Add(offsetX, offsetY, HALF_HEIGHT); l.Pass(offsetX, offsetY, HALF_HEIGHT);
} }
} }
@ -278,7 +319,7 @@ namespace SourceGit.Models
if (path.Path.Points.Count == 1 && Math.Abs(path.Path.Points[0].Y - endY) < 0.0001) if (path.Path.Points.Count == 1 && Math.Abs(path.Path.Points[0].Y - endY) < 0.0001)
continue; continue;
path.Add((i + 0.5) * UNIT_WIDTH + H_MARGIN, endY + HALF_HEIGHT, HALF_HEIGHT, true); path.End((i + 0.5) * UNIT_WIDTH + H_MARGIN, endY + HALF_HEIGHT, HALF_HEIGHT);
} }
unsolved.Clear(); unsolved.Clear();