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:
2018-11-29 20:41:06 -06:00
parent b61f390121
commit 698dd9d2f0
26 changed files with 1787 additions and 3 deletions

View 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);
}
}
}

View 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; }
}
}

View File

@@ -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);
}
}
}

View 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();
}
}
}
}

View File

@@ -0,0 +1,74 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RhythmBullet.Utilities.Camera;
using RhythmBullet.Utilities.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RhythmBullet.UI.Modular
{
public class UIModule : IInputListener
{
public Rectangle bounds;
public Vector2 origin;
public UIModuleGroup Parent;
public string Name;
public Color color = Color.White;
public virtual void Update(GameTime gameTime)
{
}
public virtual void Draw(SpriteBatch batch)
{
}
public Rectangle ConvertToParentCoordinates(Rectangle bounds)
{
if (Parent != null)
{
Rectangle parentHitbox = Parent.ConvertToParentCoordinates(bounds);
int tX = bounds.X + parentHitbox.X;
int tY = bounds.Y + parentHitbox.Y;
return new Rectangle(tX, tY, bounds.Width, bounds.Height);
} else
{
return bounds;
}
}
public void RemoveFromParent()
{
if (Parent == null)
{
throw new InvalidOperationException("Parent is null.");
}
Parent.RemoveModule(this);
}
/// <summary>
/// Called whenever the keyboard state is changed.
/// </summary>
/// <param name="state">The current keyboard state.</param>
/// <returns>Returning whether or not to continue to call the next listener.</returns>
public virtual bool KeyboardStateChanged(KeyboardState state)
{
return true;
}
/// <summary>
/// Called whenever the state of the mouse changes. This includes movement.
/// </summary>
/// <param name="state">The current state of the mouse.</param>
/// <returns>Returning whether or not to continue to call the next listener.</returns>
public virtual bool MouseStateChanged(MouseState state)
{
return true;
}
}
}

View File

@@ -0,0 +1,109 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RhythmBullet.Utilities.Camera;
using RhythmBullet.Utilities.Input;
namespace RhythmBullet.UI.Modular
{
public class UIModuleGroup : UIModule
{
List<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer = new RasterizerState();
public Camera2D Camera { get; set; }
public UIModuleGroup(bool crop = false, Camera2D camera = null)
{
Camera = camera;
if (crop)
{
scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle();
}
}
public override void Draw(SpriteBatch batch)
{
if (scissorBounds != null)
{
batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.TransformMatrix);
scissorBounds.Width = bounds.Width;
scissorBounds.Height = bounds.Height;
scissorBounds.X = bounds.X;
scissorBounds.Y = bounds.Y;
Rectangle scissor = scissorBounds;
scissorBounds = batch.GraphicsDevice.ScissorRectangle;
batch.GraphicsDevice.ScissorRectangle = scissor;
}
foreach (UIModule module in modules)
{
int offsetX = module.bounds.X;
int offsetY = module.bounds.Y;
module.bounds.X = bounds.X + offsetX - (int)module.origin.X;
module.bounds.Y = bounds.Y + offsetY - (int)module.origin.Y;
module.Draw(batch);
module.bounds.X = offsetX;
module.bounds.Y = offsetY;
}
if (scissorBounds != null)
{
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, Camera?.TransformMatrix);
}
}
public override void Update(GameTime gameTime)
{
foreach (UIModule module in modules)
{
module.Update(gameTime);
}
}
public void AddModule(params UIModule[] addModules)
{
foreach (UIModule module in addModules)
{
if (modules.Contains(module))
{
throw new InvalidOperationException(module.ToString() + " already exists in " + this.ToString());
}
module.Parent = this;
modules.Add(module);
}
}
public void RemoveModule(UIModule module)
{
module.Parent = null;
modules.Remove(module);
}
public override bool KeyboardStateChanged(KeyboardState state)
{
foreach (UIModule module in modules)
{
module.KeyboardStateChanged(state);
}
return base.KeyboardStateChanged(state);
}
public override bool MouseStateChanged(MouseState state)
{
foreach (UIModule module in modules)
{
module.MouseStateChanged(state);
}
return base.MouseStateChanged(state);
}
}
}