Skip to content

Commit

Permalink
switching to arch hold on
Browse files Browse the repository at this point in the history
  • Loading branch information
phasephasephase committed Jan 27, 2025
1 parent b98f004 commit 6515fe5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/AdvancedWindow/MyWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public override void Initialize()
new Label
{
Text = "The quick brown fox jumps over the lazy dog",
Position = new Vector2(10, 10),
Position = new Vector2(0, 10),
Anchor = Anchor.TopRight,
FontName = "Segoe UI",
FontSize = 24,
Expand Down
2 changes: 1 addition & 1 deletion src/Jiayi.UI/Render/Graphics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public Vector2 MeasureText(string text, string fontFamily, float size, int weigh
{
var maxSizeVec = maxSize ?? new Vector2(float.MaxValue, float.MaxValue);
var layout = FontCache.GetLayout(text, fontFamily, size, weight, italic, maxSizeVec);
return new Vector2(layout.Metrics.Width, layout.Metrics.Height);
return new Vector2(layout.Metrics.WidthIncludingTrailingWhitespace, layout.Metrics.Height);
}

// dispose pattern
Expand Down
40 changes: 11 additions & 29 deletions src/Jiayi.UI/Widget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,20 @@ public virtual Vector2 GetAbsolutePosition()
var absolutePosition = parentPosition + Position;

// calculate anchor
return Anchor switch
switch (Anchor)
{
Anchor.TopLeft =>
case Anchor.TopLeft:
// normal coordinates
absolutePosition,
Anchor.TopCenter =>
return absolutePosition;
case Anchor.TopCenter:
// x is centered
absolutePosition + new Vector2((Parent!.Size.X - Size.X) / 2, 0),
Anchor.TopRight =>
// x is flipped; positive x is left
// the x value in absolutePosition can be made negative to make this easier
new Vector2(-absolutePosition.X + Parent!.Size.X - Size.X, absolutePosition.Y),
Anchor.MiddleLeft =>
// y is centered
absolutePosition + new Vector2(0, (Parent!.Size.Y - Size.Y) / 2),
Anchor.MiddleCenter =>
// both x and y are centered
absolutePosition + (Parent!.Size - Size) / 2,
Anchor.MiddleRight =>
// x is flipped; positive x is left, y is centered
new Vector2(-absolutePosition.X + Parent!.Size.X - Size.X, absolutePosition.Y + (Parent!.Size.Y - Size.Y) / 2),
Anchor.BottomLeft =>
// y is flipped; positive y is up
new Vector2(absolutePosition.X, -absolutePosition.Y + Parent!.Size.Y - Size.Y),
Anchor.BottomCenter =>
// x is centered, y is flipped; positive y is up
new Vector2(absolutePosition.X + (Parent!.Size.X - Size.X) / 2, -absolutePosition.Y + Parent!.Size.Y - Size.Y),
Anchor.BottomRight =>
// x is flipped; positive x is left, y is flipped; positive y is up
-absolutePosition + Parent!.Size - Size,
_ => throw new ArgumentOutOfRangeException()
};
return absolutePosition + new Vector2((Parent!.Size.X - Size.X) / 2, 0);
case Anchor.TopRight:
// x is right-aligned
return absolutePosition + new Vector2(Parent!.Size.X - Size.X, 0);
default:
throw new ArgumentOutOfRangeException();
}
}

public virtual void Render(Graphics g)
Expand Down
3 changes: 3 additions & 0 deletions src/Jiayi.UI/Widgets/Label.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ public override void Render(Graphics g)
}

g.DrawText(Text, FontName, GetAbsolutePosition(), TextColor, FontSize, FontWeight, Italic, Size);

// debug
g.DrawRect(GetAbsolutePosition(), Size, Color.Red, 5);
}
}

0 comments on commit 6515fe5

Please sign in to comment.