mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 14:08:34 +01:00
added text aligning to generic fonts
This commit is contained in:
parent
5404c36f73
commit
d5ce920e52
2 changed files with 46 additions and 1 deletions
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
|
@ -24,6 +25,30 @@ namespace MLEM.Font {
|
|||
|
||||
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
|
||||
|
||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color) {
|
||||
this.DrawString(batch, text, position, align, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0);
|
||||
}
|
||||
|
||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||
this.DrawString(batch, text, position, align, color, rotation, origin, new Vector2(scale), effects, layerDepth);
|
||||
}
|
||||
|
||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, TextAlign align, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||
switch (align) {
|
||||
case TextAlign.Center:
|
||||
case TextAlign.CenterBothAxes:
|
||||
var (w, h) = this.MeasureString(text);
|
||||
position.X -= w / 2;
|
||||
if (align == TextAlign.CenterBothAxes)
|
||||
position.Y -= h / 2;
|
||||
break;
|
||||
case TextAlign.Right:
|
||||
position.X -= this.MeasureString(text).X;
|
||||
break;
|
||||
}
|
||||
this.DrawString(batch, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||
}
|
||||
|
||||
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
|
||||
var total = new StringBuilder();
|
||||
for (var i = 0; i < text.Length; i++) {
|
||||
|
@ -82,4 +107,13 @@ namespace MLEM.Font {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
public enum TextAlign {
|
||||
|
||||
Left,
|
||||
Center,
|
||||
Right,
|
||||
CenterBothAxes
|
||||
|
||||
}
|
||||
}
|
|
@ -51,8 +51,9 @@ namespace Sandbox {
|
|||
};
|
||||
|
||||
var tex = LoadContent<Texture2D>("Textures/Test");
|
||||
var font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
|
||||
this.UiSystem.Style = new UntexturedStyle(this.SpriteBatch) {
|
||||
Font = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")),
|
||||
Font = font,
|
||||
TextScale = 0.1F,
|
||||
PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
|
||||
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4)
|
||||
|
@ -87,6 +88,16 @@ namespace Sandbox {
|
|||
|
||||
var res = this.Content.LoadJson<Test>("Test");
|
||||
Console.WriteLine(res);
|
||||
|
||||
this.OnDraw += (game, time) => {
|
||||
this.SpriteBatch.Begin();
|
||||
font.DrawString(this.SpriteBatch, "Left Aligned\nover multiple lines", new Vector2(640, 0), TextAlign.Left, Color.White);
|
||||
font.DrawString(this.SpriteBatch, "Center Aligned\nover multiple lines", new Vector2(640, 100), TextAlign.Center, Color.White);
|
||||
font.DrawString(this.SpriteBatch, "Right Aligned\nover multiple lines", new Vector2(640, 200), TextAlign.Right, Color.White);
|
||||
font.DrawString(this.SpriteBatch, "Center Aligned on both axes", new Vector2(640, 360), TextAlign.CenterBothAxes, Color.White);
|
||||
this.SpriteBatch.Draw(this.SpriteBatch.GetBlankTexture(), new Rectangle(640 - 4, 360 - 4, 8, 8), Color.Green);
|
||||
this.SpriteBatch.End();
|
||||
};
|
||||
}
|
||||
|
||||
protected override void DoUpdate(GameTime gameTime) {
|
||||
|
|
Loading…
Reference in a new issue