refactored field names, organized classes, text ui module now can scale and wrap.

This commit is contained in:
Harrison Deng 2018-11-20 18:12:02 -06:00
parent 1edebdd223
commit 88baab37db
10 changed files with 117 additions and 45 deletions

View File

@ -61,7 +61,7 @@
<Compile Include="Zer01HD\Utilities\Input\InputUtilities.cs" /> <Compile Include="Zer01HD\Utilities\Input\InputUtilities.cs" />
<Compile Include="Zer01HD\Utilities\Persistence\Preferences.cs" /> <Compile Include="Zer01HD\Utilities\Persistence\Preferences.cs" />
<Compile Include="Zer01HD\Utilities\ScreenSystem\ScreenManager.cs" /> <Compile Include="Zer01HD\Utilities\ScreenSystem\ScreenManager.cs" />
<Compile Include="Zer01HD\Utilities\UI\Interactive\BasicButton.cs" /> <Compile Include="Zer01HD\Utilities\UI\Modular\Modules\Interactive\BasicButton.cs" />
<Compile Include="Zer01HD\Utilities\ScreenSystem\ITransition.cs" /> <Compile Include="Zer01HD\Utilities\ScreenSystem\ITransition.cs" />
<Compile Include="Zer01HD\Utilities\ScreenSystem\LoadingScreen.cs" /> <Compile Include="Zer01HD\Utilities\ScreenSystem\LoadingScreen.cs" />
<Compile Include="Zer01HD\Screens\MainMenu\MainScreen.cs" /> <Compile Include="Zer01HD\Screens\MainMenu\MainScreen.cs" />

View File

@ -46,14 +46,14 @@ namespace RhythmBullet.Zer01HD.Audio.Visualizer
{ {
for (int i = 0; i < BAR_COUNT; i++) for (int i = 0; i < BAR_COUNT; i++)
{ {
bar.X = (i * (bar.Width + spaceBetweenBars)) + Bounds.X; bar.X = (i * (bar.Width + spaceBetweenBars)) + bounds.X;
bar.Y = Bounds.Y; bar.Y = bounds.Y;
bar.Height = barValue[i]; bar.Height = barValue[i];
batch.Draw(barTexture, bar, Color); batch.Draw(barTexture, bar, color);
bar.Height = -barValue[BAR_COUNT - i - 1]; bar.Height = -barValue[BAR_COUNT - i - 1];
batch.Draw(barTexture, bar, Color); batch.Draw(barTexture, bar, color);
} }
base.Draw(batch); base.Draw(batch);
} }

View File

@ -18,8 +18,8 @@ namespace RhythmBullet.Zer01HD.Screens.MainMenu
public override void ApplySize(int width, int height) public override void ApplySize(int width, int height)
{ {
title.Scale = (width - 40) / title.Texture.Width; title.Scale = (width - 40) / title.Texture.Width;
title.Bounds.X = (int)((width - title.Bounds.Width) / 2f); title.bounds.X = (int)((width - title.bounds.Width) / 2f);
title.Bounds.Y = (int)((height - title.Bounds.Height) / 2f); title.bounds.Y = (int)((height - title.bounds.Height) / 2f);
base.ApplySize(width, height); base.ApplySize(width, height);
} }

View File

@ -42,7 +42,7 @@ namespace RhythmBullet.Zer01HD.UI.Book
if (targetPage != null) if (targetPage != null)
{ {
Vector2 position; Vector2 position;
Rectangle targetBounds = targetPage.Bounds; Rectangle targetBounds = targetPage.bounds;
position.X = targetBounds.X + (targetBounds.Width * 0.5f); position.X = targetBounds.X + (targetBounds.Width * 0.5f);
position.Y = targetBounds.Y + (targetBounds.Height * 0.5f); position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
camera.LinearInterpolationToPosition(0.4f, position, (float)gameTime.ElapsedGameTime.TotalSeconds); camera.LinearInterpolationToPosition(0.4f, position, (float)gameTime.ElapsedGameTime.TotalSeconds);
@ -85,7 +85,7 @@ namespace RhythmBullet.Zer01HD.UI.Book
public void GoToPage(Page page) public void GoToPage(Page page)
{ {
Rectangle targetBounds = page.Bounds; Rectangle targetBounds = page.bounds;
camera.Position.X = targetBounds.X + (targetBounds.Width * 0.5f); camera.Position.X = targetBounds.X + (targetBounds.Width * 0.5f);
camera.Position.Y = targetBounds.Y + (targetBounds.Height * 0.5f); camera.Position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
} }

View File

@ -25,10 +25,10 @@ namespace RhythmBullet.Zer01HD.UI.Book
public virtual void ApplySize(int width, int height) public virtual void ApplySize(int width, int height)
{ {
Bounds.X = pageX * width; bounds.X = pageX * width;
Bounds.Y = pageY * height; bounds.Y = pageY * height;
Bounds.Width = width; bounds.Width = width;
Bounds.Height = height; bounds.Height = height;
NeedsSizeUpdate = false; NeedsSizeUpdate = false;
} }
} }

View File

@ -16,12 +16,12 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules
{ {
get get
{ {
return (float)Bounds.Width / Texture.Width; return (float)bounds.Width / Texture.Width;
} }
set 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 get
{ {
return (float)Bounds.Height / Texture.Height; return (float)bounds.Height / Texture.Height;
} }
set 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 set
{ {
Bounds.Height = (int)(Texture.Height * value); bounds.Height = (int)(Texture.Height * value);
Bounds.Width = (int)(Texture.Width * value); bounds.Width = (int)(Texture.Width * value);
} }
} }
public Image(Texture2D texture) public Image(Texture2D texture)
{ {
Texture = texture ?? throw new ArgumentException("Image requires a texture."); Texture = texture ?? throw new ArgumentException("Image requires a texture.");
Bounds = texture.Bounds; bounds = texture.Bounds;
} }
public override void Draw(SpriteBatch batch) public override void Draw(SpriteBatch batch)
{ {
batch.Draw(Texture, Bounds, Color); batch.Draw(Texture, bounds, color);
base.Draw(batch); base.Draw(batch);
} }
} }

View File

@ -7,7 +7,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules.Interactive
{ {
public delegate bool Clicked(); public delegate bool Clicked();
@ -20,11 +20,9 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive
} }
public sealed override bool MouseStateChanged(MouseState state) public sealed override bool MouseStateChanged(MouseState state)
{ {
if (InputUtilities.MouseWithinBoundries(Bounds)) if (InputUtilities.MouseWithinBoundries(bounds))
{ {
if (InputUtilities.MouseClicked()) if (InputUtilities.MouseClicked())
{ {
@ -39,6 +37,11 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive
return base.MouseStateChanged(state); return base.MouseStateChanged(state);
} }
public sealed override bool KeyboardStateChanged(KeyboardState state)
{
return base.KeyboardStateChanged(state);
}
protected void OnClick() protected void OnClick()
{ {
Listeners?.Invoke(); Listeners?.Invoke();

View File

@ -14,31 +14,99 @@ namespace RhythmBullet.Zer01HD.UI.Modular.Modules
private SpriteFont font; private SpriteFont font;
private float scale; private float scale;
private Vector2 position; private Vector2 position;
private string text;
private Vector2 textSize;
public bool autoWrap;
public bool autoScale;
public string DisplayedText public string DisplayedText
{ {
get get
{ {
return DisplayedText; return text;
} }
set set
{ {
Vector2 size = font.MeasureString(value); textSize = font.MeasureString(value);
Bounds.Width = (int) size.X; text = value;
scale = Bounds.Height / size.Y;
} }
} }
public Text(string displayedText, SpriteFont font, int height) public Text(string displayedText, SpriteFont font, int height)
{ {
Bounds.Height = height; bounds.Height = height;
this.font = font; 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) public override void Draw(SpriteBatch batch)
{ {
position.X = Bounds.X; batch.DrawString(font, DisplayedText, position, color, 0f, origin, scale, SpriteEffects.None, 0f);
position.Y = Bounds.Y;
batch.DrawString(font, DisplayedText, position, Color);
base.Draw(batch); 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();
}
}
} }
} }

View File

@ -13,10 +13,11 @@ namespace RhythmBullet.Zer01HD.UI.Modular
{ {
public class UIModule : IInputListener public class UIModule : IInputListener
{ {
public Rectangle Bounds; public Rectangle bounds;
public Vector2 origin;
public UIModuleGroup Parent; public UIModuleGroup Parent;
public string Name; public string Name;
public Color Color = Color.White; public Color color = Color.White;
public virtual void Update(GameTime gameTime) public virtual void Update(GameTime gameTime)
{ {

View File

@ -34,23 +34,23 @@ namespace RhythmBullet.Zer01HD.UI.Modular
{ {
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.TransformMatrix); batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, Camera?.TransformMatrix);
scissorBounds.Width = Bounds.Width; scissorBounds.Width = bounds.Width;
scissorBounds.Height = Bounds.Height; scissorBounds.Height = bounds.Height;
scissorBounds.X = Bounds.X; scissorBounds.X = bounds.X;
scissorBounds.Y = Bounds.Y; scissorBounds.Y = bounds.Y;
Rectangle scissor = scissorBounds; Rectangle scissor = scissorBounds;
scissorBounds = batch.GraphicsDevice.ScissorRectangle; scissorBounds = batch.GraphicsDevice.ScissorRectangle;
batch.GraphicsDevice.ScissorRectangle = scissor; batch.GraphicsDevice.ScissorRectangle = scissor;
} }
foreach (UIModule module in modules) foreach (UIModule module in modules)
{ {
int offsetX = module.Bounds.X; int offsetX = module.bounds.X;
int offsetY = module.Bounds.Y; int offsetY = module.bounds.Y;
module.Bounds.X = Bounds.X + offsetX; module.bounds.X = bounds.X + offsetX;
module.Bounds.Y = Bounds.Y + offsetY; module.bounds.Y = bounds.Y + offsetY;
module.Draw(batch); module.Draw(batch);
module.Bounds.X = offsetX; module.bounds.X = offsetX;
module.Bounds.Y = offsetY; module.bounds.Y = offsetY;
} }
if (scissorBounds != null) if (scissorBounds != null)