moved utilities for rhythmbullet to separate project for a better custom content loaders for the monogame pipeline as well as for future projects and of course learning how things work.
This commit is contained in:
63
Recrowned-Athenaeum/UI/Modular/Modules/Image.cs
Normal file
63
Recrowned-Athenaeum/UI/Modular/Modules/Image.cs
Normal file
@@ -0,0 +1,63 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.UI.Modular;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Utilities.UI.Modular.Modules
|
||||
{
|
||||
class Image : UIModule
|
||||
{
|
||||
public float rotation = 0f;
|
||||
public Texture2D Texture { get; set; }
|
||||
public float ScaleX
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)bounds.Width / Texture.Width;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bounds.Width = (int)(Texture.Width * value);
|
||||
}
|
||||
}
|
||||
|
||||
public float ScaleY
|
||||
{
|
||||
get
|
||||
{
|
||||
return (float)bounds.Height / Texture.Height;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
bounds.Height = (int)(Texture.Height * value);
|
||||
}
|
||||
}
|
||||
|
||||
public float Scale
|
||||
{
|
||||
set
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
batch.Draw(Texture, bounds, null, color, rotation, origin, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
}
|
||||
}
|
62
Recrowned-Athenaeum/UI/Modular/Modules/Interactive/Button.cs
Normal file
62
Recrowned-Athenaeum/UI/Modular/Modules/Interactive/Button.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
using RhythmBullet.UI.Modular;
|
||||
using RhythmBullet.Utilities.DataTypes;
|
||||
using RhythmBullet.Utilities.Input;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Utilities.UI.Modular.Modules.Interactive
|
||||
{
|
||||
public delegate bool Clicked();
|
||||
|
||||
public class Button : UIModule
|
||||
{
|
||||
private NinePatch background;
|
||||
public event Clicked Listeners;
|
||||
|
||||
public Button(NinePatch background = null)
|
||||
{
|
||||
this.background = background;
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
background?.Draw(batch, bounds);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
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; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,37 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.UI.Modular.Modules;
|
||||
using RhythmBullet.Utilities.DataTypes;
|
||||
using RhythmBullet.Utilities.UI.Modular.Modules.Interactive;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Utilities.UI.Modular.Modules.Interactive
|
||||
{
|
||||
internal class TextButton : Button
|
||||
{
|
||||
private TextLabel label;
|
||||
|
||||
internal TextButton(string text, SpriteFont font, NinePatch background) : base(background)
|
||||
{
|
||||
label = new TextLabel(font, text);
|
||||
label.autoScale = true;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
label.bounds = bounds;
|
||||
label.Update(gameTime);
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
label.Draw(batch);
|
||||
base.Draw(batch);
|
||||
}
|
||||
}
|
||||
}
|
146
Recrowned-Athenaeum/UI/Modular/Modules/TextLabel.cs
Normal file
146
Recrowned-Athenaeum/UI/Modular/Modules/TextLabel.cs
Normal file
@@ -0,0 +1,146 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.Utilities.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.UI.Modular.Modules
|
||||
{
|
||||
public class TextLabel : UIModule
|
||||
{
|
||||
private SpriteFont font;
|
||||
private float scale;
|
||||
private Vector2 position;
|
||||
private string originalText;
|
||||
private string displayedText;
|
||||
private Vector2 modifiedTextSize;
|
||||
public bool autoWrap;
|
||||
public bool autoScale;
|
||||
public bool useEllipses;
|
||||
public string ellipsis = "...";
|
||||
private string ModifiedText
|
||||
{
|
||||
get
|
||||
{
|
||||
return displayedText;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
displayedText = value;
|
||||
modifiedTextSize = font.MeasureString(value);
|
||||
}
|
||||
}
|
||||
public string Text
|
||||
{
|
||||
get
|
||||
{
|
||||
return originalText;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null) value = "...";
|
||||
modifiedTextSize = font.MeasureString(value);
|
||||
originalText = value;
|
||||
displayedText = value;
|
||||
}
|
||||
}
|
||||
|
||||
public TextLabel(SpriteFont font, string text = null)
|
||||
{
|
||||
Text = text;
|
||||
this.font = font;
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
position.X = bounds.X;
|
||||
position.Y = bounds.Y;
|
||||
|
||||
if (useEllipses) AttemptToApplyEllipsis();
|
||||
if (autoWrap) AttemptToWrapText();
|
||||
if (autoScale) AttemptToScaleFont();
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
batch.DrawString(font, Text, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
public void AttemptToApplyEllipsis()
|
||||
{
|
||||
if (modifiedTextSize.X * scale > bounds.Width && ModifiedText.Length > ellipsis.Length + 1)
|
||||
{
|
||||
RemoveLineBreaks();
|
||||
StringBuilder stringBuilder = new StringBuilder(ModifiedText);
|
||||
do
|
||||
{
|
||||
stringBuilder.Remove(stringBuilder.Length, ellipsis.Length - 1);
|
||||
stringBuilder.Insert(stringBuilder.Length, ellipsis);
|
||||
}
|
||||
while (font.MeasureString(stringBuilder).X * scale > bounds.Width);
|
||||
|
||||
ModifiedText = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
public void AttemptToScaleFont()
|
||||
{
|
||||
if (modifiedTextSize.X * scale > bounds.Width || modifiedTextSize.X * scale < bounds.Width - 5)
|
||||
{
|
||||
scale = bounds.Width / modifiedTextSize.X;
|
||||
}
|
||||
|
||||
if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale < bounds.Height - 5)
|
||||
{
|
||||
scale = bounds.Height / modifiedTextSize.Y;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveLineBreaks()
|
||||
{
|
||||
ModifiedText = ModifiedText.Replace("\n", " ");
|
||||
}
|
||||
|
||||
public void ResetToOriginalText()
|
||||
{
|
||||
ModifiedText = originalText;
|
||||
}
|
||||
|
||||
public void AttemptToWrapText(bool unwrap = false)
|
||||
{
|
||||
if (unwrap) RemoveLineBreaks();
|
||||
if (modifiedTextSize.X * scale > bounds.Width)
|
||||
{
|
||||
ModifiedText = ModifiedText.Replace("\n", " ");
|
||||
string[] words = ModifiedText.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;
|
||||
}
|
||||
}
|
||||
ModifiedText = stringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user