refactoring to comply with c# conventions

This commit is contained in:
2018-11-20 18:56:41 -06:00
parent efbaad9c1b
commit 3347316182
37 changed files with 115 additions and 115 deletions

View File

@@ -0,0 +1,62 @@
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 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, color);
base.Draw(batch);
}
}
}

View File

@@ -0,0 +1,52 @@
using Microsoft.Xna.Framework.Input;
using RhythmBullet.UI.Modular;
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 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; }
}
}

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
{
modifiedTextSize = font.MeasureString(value);
originalText = value;
displayedText = value;
}
}
public TextLabel(string text, SpriteFont font, int height)
{
Text = text;
bounds.Height = height;
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)
{
scale = bounds.Width / modifiedTextSize.X;
}
if (modifiedTextSize.Y * scale > bounds.Height || modifiedTextSize.Y * scale > bounds.Height)
{
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();
}
}
}
}