refactored field names, organized classes, text ui module now can scale and wrap.
This commit is contained in:
parent
1edebdd223
commit
88baab37db
@ -61,7 +61,7 @@
|
||||
<Compile Include="Zer01HD\Utilities\Input\InputUtilities.cs" />
|
||||
<Compile Include="Zer01HD\Utilities\Persistence\Preferences.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\LoadingScreen.cs" />
|
||||
<Compile Include="Zer01HD\Screens\MainMenu\MainScreen.cs" />
|
||||
|
@ -46,14 +46,14 @@ namespace RhythmBullet.Zer01HD.Audio.Visualizer
|
||||
{
|
||||
for (int i = 0; i < BAR_COUNT; i++)
|
||||
{
|
||||
bar.X = (i * (bar.Width + spaceBetweenBars)) + Bounds.X;
|
||||
bar.Y = Bounds.Y;
|
||||
bar.X = (i * (bar.Width + spaceBetweenBars)) + bounds.X;
|
||||
bar.Y = bounds.Y;
|
||||
|
||||
bar.Height = barValue[i];
|
||||
batch.Draw(barTexture, bar, Color);
|
||||
batch.Draw(barTexture, bar, color);
|
||||
|
||||
bar.Height = -barValue[BAR_COUNT - i - 1];
|
||||
batch.Draw(barTexture, bar, Color);
|
||||
batch.Draw(barTexture, bar, color);
|
||||
}
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
@ -18,8 +18,8 @@ namespace RhythmBullet.Zer01HD.Screens.MainMenu
|
||||
public override void ApplySize(int width, int height)
|
||||
{
|
||||
title.Scale = (width - 40) / title.Texture.Width;
|
||||
title.Bounds.X = (int)((width - title.Bounds.Width) / 2f);
|
||||
title.Bounds.Y = (int)((height - title.Bounds.Height) / 2f);
|
||||
title.bounds.X = (int)((width - title.bounds.Width) / 2f);
|
||||
title.bounds.Y = (int)((height - title.bounds.Height) / 2f);
|
||||
base.ApplySize(width, height);
|
||||
}
|
||||
|
||||
|
@ -42,7 +42,7 @@ namespace RhythmBullet.Zer01HD.UI.Book
|
||||
if (targetPage != null)
|
||||
{
|
||||
Vector2 position;
|
||||
Rectangle targetBounds = targetPage.Bounds;
|
||||
Rectangle targetBounds = targetPage.bounds;
|
||||
position.X = targetBounds.X + (targetBounds.Width * 0.5f);
|
||||
position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
|
||||
camera.LinearInterpolationToPosition(0.4f, position, (float)gameTime.ElapsedGameTime.TotalSeconds);
|
||||
@ -85,7 +85,7 @@ namespace RhythmBullet.Zer01HD.UI.Book
|
||||
|
||||
public void GoToPage(Page page)
|
||||
{
|
||||
Rectangle targetBounds = page.Bounds;
|
||||
Rectangle targetBounds = page.bounds;
|
||||
camera.Position.X = targetBounds.X + (targetBounds.Width * 0.5f);
|
||||
camera.Position.Y = targetBounds.Y + (targetBounds.Height * 0.5f);
|
||||
}
|
||||
|
@ -25,10 +25,10 @@ namespace RhythmBullet.Zer01HD.UI.Book
|
||||
|
||||
public virtual void ApplySize(int width, int height)
|
||||
{
|
||||
Bounds.X = pageX * width;
|
||||
Bounds.Y = pageY * height;
|
||||
Bounds.Width = width;
|
||||
Bounds.Height = height;
|
||||
bounds.X = pageX * width;
|
||||
bounds.Y = pageY * height;
|
||||
bounds.Width = width;
|
||||
bounds.Height = height;
|
||||
NeedsSizeUpdate = false;
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive
|
||||
namespace RhythmBullet.Zer01HD.Utilities.UI.Modular.Modules.Interactive
|
||||
{
|
||||
public delegate bool Clicked();
|
||||
|
||||
@ -20,11 +20,9 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public sealed override bool MouseStateChanged(MouseState state)
|
||||
{
|
||||
if (InputUtilities.MouseWithinBoundries(Bounds))
|
||||
if (InputUtilities.MouseWithinBoundries(bounds))
|
||||
{
|
||||
if (InputUtilities.MouseClicked())
|
||||
{
|
||||
@ -39,6 +37,11 @@ namespace RhythmBullet.Zer01HD.Utilities.UI.Interactive
|
||||
return base.MouseStateChanged(state);
|
||||
}
|
||||
|
||||
public sealed override bool KeyboardStateChanged(KeyboardState state)
|
||||
{
|
||||
return base.KeyboardStateChanged(state);
|
||||
}
|
||||
|
||||
protected void OnClick()
|
||||
{
|
||||
Listeners?.Invoke();
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,10 +13,11 @@ namespace RhythmBullet.Zer01HD.UI.Modular
|
||||
{
|
||||
public class UIModule : IInputListener
|
||||
{
|
||||
public Rectangle Bounds;
|
||||
public Rectangle bounds;
|
||||
public Vector2 origin;
|
||||
public UIModuleGroup Parent;
|
||||
public string Name;
|
||||
public Color Color = Color.White;
|
||||
public Color color = Color.White;
|
||||
|
||||
public virtual void Update(GameTime gameTime)
|
||||
{
|
||||
|
@ -34,23 +34,23 @@ namespace RhythmBullet.Zer01HD.UI.Modular
|
||||
{
|
||||
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;
|
||||
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;
|
||||
module.Bounds.Y = Bounds.Y + offsetY;
|
||||
int offsetX = module.bounds.X;
|
||||
int offsetY = module.bounds.Y;
|
||||
module.bounds.X = bounds.X + offsetX;
|
||||
module.bounds.Y = bounds.Y + offsetY;
|
||||
module.Draw(batch);
|
||||
module.Bounds.X = offsetX;
|
||||
module.Bounds.Y = offsetY;
|
||||
module.bounds.X = offsetX;
|
||||
module.bounds.Y = offsetY;
|
||||
}
|
||||
|
||||
if (scissorBounds != null)
|
||||
|
Loading…
Reference in New Issue
Block a user