Hlavní menu, synchronizace jmen hráčů. Client hru spustí až ve chvíli kdy dostane správný packet. Oprava bugu v se scalováním UIElementu

This commit is contained in:
Perry 2026-03-11 22:35:30 +01:00
parent e6128dc9f5
commit 7656707177
19 changed files with 315 additions and 53 deletions

View file

@ -7,11 +7,28 @@ namespace FNAF_Clone.GUI;
public class TextUIElement : UIElement {
public SpriteFont Font { get; set; }
public string Text{ get; set; } = "";
public Alignment CurrentAlignment { get; set; }
public string Text{
get;
set{
field = value;
if(AutoBounds)
Bounds = (Bounds.Item1, Bounds.Item1 + new Point((int)Measure().X, (int)Measure().Y));
}
} = "";
public Color Color{ get; set; } = Color.White;
public bool AutoBounds{ get; protected set; } = false;
private Vector2 origin;
private const float UNIVERSAL_TEXT_SCALE_MULTIPLIER = 0.3f;
public TextUIElement(Point corner1, SpriteFont font, Alignment alignment = Alignment.LEFT) : base(corner1, corner1) {
public TextUIElement(Point corner1, SpriteFont font, Alignment alignment = Alignment.LEFT, bool autoBounds = true) : base(corner1, corner1) {
Font = font;
AutoBounds = autoBounds;
Align(alignment);
}
public TextUIElement(Point corner1, Point corner2, SpriteFont font, Alignment alignment = Alignment.LEFT) : base(corner1, corner2) {
Font = font;
Align(alignment);
}
@ -19,10 +36,11 @@ public class TextUIElement : UIElement {
public override void Draw(SpriteBatch spriteBatch) {
base.Draw(spriteBatch);
align();
spriteBatch.DrawString(Font, Text, screenSpaceBounds.Item1.ToVector2(), Color, 0, origin, ScaleMultiplier, SpriteEffects.None, 0);
spriteBatch.DrawString(Font, Text, screenSpaceBounds.Item1.ToVector2(), Color, 0, origin, pixelScaleMultiplier * UNIVERSAL_TEXT_SCALE_MULTIPLIER, SpriteEffects.None, 0);
}
public void Align(Alignment alignment) {
CurrentAlignment = alignment;
switch (alignment){
case Alignment.LEFT:
AlignLeft();
@ -35,15 +53,24 @@ public class TextUIElement : UIElement {
break;
}
}
protected override void UpdateBounds() {
Point inSpaceOrigin = Bounds.Item1 + origin.ToPoint();
_bounds = ((Bounds.Item1 - inSpaceOrigin).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin, (Bounds.Item2 - Bounds.Item1).MultiplyByScalar(ScaleMultiplier) + inSpaceOrigin);
screenSpaceBounds = (Bounds.Item1.MultiplyByScalar(pixelScaleMultiplier), Bounds.Item2.MultiplyByScalar(pixelScaleMultiplier));
Align(CurrentAlignment);
}
public Vector2 Measure() => Font.MeasureString(Text) * UNIVERSAL_TEXT_SCALE_MULTIPLIER * ScaleMultiplier;
private void AlignLeft() {
align = () => origin = Vector2.Zero;
}
private void AlignRight() {
align = () => origin = Font.MeasureString(Text);
align = () => origin = Font.MeasureString(Text) * ScaleMultiplier;
}
private void AlignCenter() {
align = () => origin = Font.MeasureString(Text) / 2;
align = () => origin = new(Font.MeasureString(Text).X * ScaleMultiplier / 2, 0);
}
private Action align;