refactored field names, organized classes, text ui module now can scale and wrap.
This commit is contained in:
@@ -16,12 +16,12 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)Bounds.Width / Texture.Width;
|
||||
return (float)bounds.Width / Texture.Width;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
Bounds.Width = (int)(Texture.Width * value);
|
||||
bounds.Width = (int)(Texture.Width * value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,12 +29,12 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)Bounds.Height / Texture.Height;
|
||||
return (float)bounds.Height / Texture.Height;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
Bounds.Height = (int)(Texture.Height * value);
|
||||
bounds.Height = (int)(Texture.Height * value);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,20 +42,20 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules
|
||||
{
|
||||
set
|
||||
{
|
||||
Bounds.Height = (int)(Texture.Height * value);
|
||||
Bounds.Width = (int)(Texture.Width * value);
|
||||
bounds.Height = (int)(Texture.Height * value);
|
||||
bounds.Width = (int)(Texture.Width * value);
|
||||
}
|
||||
}
|
||||
|
||||
public Image(Texture2D texture)
|
||||
{
|
||||
Texture = texture ?? throw new ArgumentException("Image requires a texture.");
|
||||
Bounds = texture.Bounds;
|
||||
bounds = texture.Bounds;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
batch.Draw(Texture, Bounds, Color);
|
||||
batch.Draw(Texture, bounds, color);
|
||||
base.Draw(batch);
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,52 @@
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RhythmBullet.Zer01HD.UI.Modular;
|
||||
using RhythmBullet.Zer01HD.Utilities.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules.Interactive
|
||||
{
|
||||
public delegate bool Clicked();
|
||||
|
||||
public class BasicButton : UIModuleGroup
|
||||
{
|
||||
public event Clicked Listeners;
|
||||
|
||||
public BasicButton()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public sealed override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
if (InputUtilities.MouseWithinBoundries(bounds))
|
||||
{
|
||||
if (InputUtilities.MouseClicked())
|
||||
{
|
||||
OnClick();
|
||||
}
|
||||
Highlighted = true;
|
||||
} else
|
||||
{
|
||||
Highlighted = false;
|
||||
}
|
||||
|
||||
return base.MouseStateChanged(state);
|
||||
}
|
||||
|
||||
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
return base.KeyboardStateChanged(state);
|
||||
}
|
||||
|
||||
protected void OnClick()
|
||||
{
|
||||
Listeners?.Invoke();
|
||||
}
|
||||
|
||||
public bool Highlighted { get; private set; }
|
||||
}
|
||||
}
|
@@ -14,31 +14,99 @@ namespace RhythmBullet.Zer01HD.UI.Modular.Modules
|
||||
private SpriteFont font;
|
||||
private float scale;
|
||||
private Vector2 position;
|
||||
private string text;
|
||||
private Vector2 textSize;
|
||||
public bool autoWrap;
|
||||
public bool autoScale;
|
||||
public string DisplayedText
|
||||
{
|
||||
get
|
||||
{
|
||||
return DisplayedText;
|
||||
return text;
|
||||
}
|
||||
set
|
||||
{
|
||||
Vector2 size = font.MeasureString(value);
|
||||
Bounds.Width = (int) size.X;
|
||||
scale = Bounds.Height / size.Y;
|
||||
textSize = font.MeasureString(value);
|
||||
text = value;
|
||||
}
|
||||
}
|
||||
public Text(string displayedText, SpriteFont font, int height)
|
||||
{
|
||||
Bounds.Height = height;
|
||||
bounds.Height = height;
|
||||
this.font = font;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
position.X = bounds.X;
|
||||
position.Y = bounds.Y;
|
||||
|
||||
if (autoWrap)
|
||||
{
|
||||
AttemptToWrapText();
|
||||
}
|
||||
|
||||
if (autoScale)
|
||||
{
|
||||
AttemptToScaleFont();
|
||||
}
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
position.X = Bounds.X;
|
||||
position.Y = Bounds.Y;
|
||||
batch.DrawString(font, DisplayedText, position, Color);
|
||||
batch.DrawString(font, DisplayedText, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
public void AttemptToScaleFont()
|
||||
{
|
||||
if (textSize.X * scale > bounds.Width || textSize.X * scale < bounds.Width)
|
||||
{
|
||||
scale = bounds.Width / textSize.X;
|
||||
}
|
||||
|
||||
if (textSize.Y * scale > bounds.Height || textSize.Y *scale > bounds.Height)
|
||||
{
|
||||
scale = bounds.Height / textSize.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveLineBreaks()
|
||||
{
|
||||
DisplayedText = DisplayedText.Replace("\n", " ");
|
||||
|
||||
}
|
||||
|
||||
public void AttemptToWrapText(bool unwrap = false)
|
||||
{
|
||||
if (unwrap) RemoveLineBreaks();
|
||||
if (textSize.X * scale > bounds.Width)
|
||||
{
|
||||
text = text.Replace("\n", " ");
|
||||
string[] words = text.Split(' ');
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
float currentScaledLineWidth = 0f;
|
||||
|
||||
for (int w = 0; w < words.Length; w++)
|
||||
{
|
||||
string word = words[w];
|
||||
float scaledWidth = font.MeasureString(word).X * scale;
|
||||
|
||||
if (currentScaledLineWidth + scaledWidth <= bounds.Width)
|
||||
{
|
||||
stringBuilder.Append(word);
|
||||
currentScaledLineWidth += scaledWidth;
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.AppendLine();
|
||||
currentScaledLineWidth = 0;
|
||||
}
|
||||
}
|
||||
DisplayedText = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user