summaryrefslogtreecommitdiff
path: root/source/ui/UI.cs
diff options
context:
space:
mode:
authorhazel <hazel@hazelthats.me>2026-01-26 22:04:39 +0100
committerhazel <hazel@hazelthats.me>2026-01-26 22:04:39 +0100
commit567c422f8cd42eba2437f9a8c2522716a1649be7 (patch)
tree93c5b296f3b7c14b626d0aadf5cad37764c41c74 /source/ui/UI.cs
downloadcelesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.gz
celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.tar.bz2
celesteia-567c422f8cd42eba2437f9a8c2522716a1649be7.zip
celesteia archive, last updated april 9th 2024
Signed-off-by: hazel <hazel@hazelthats.me>
Diffstat (limited to 'source/ui/UI.cs')
-rw-r--r--source/ui/UI.cs81
1 files changed, 81 insertions, 0 deletions
diff --git a/source/ui/UI.cs b/source/ui/UI.cs
new file mode 100644
index 0000000..39f4136
--- /dev/null
+++ b/source/ui/UI.cs
@@ -0,0 +1,81 @@
+using Celesteia.Resources.Management;
+using Celesteia.UI.Properties;
+using Microsoft.Xna.Framework;
+using Microsoft.Xna.Framework.Graphics;
+using MonoGame.Extended.TextureAtlases;
+
+namespace Celesteia {
+ public static class UIReferences {
+ public static GameWindow gameWindow;
+ public static bool GUIEnabled = true;
+ public static int Scaling = 3;
+ }
+
+ public static class TextUtilities {
+ public static void DrawAlignedText(SpriteBatch spriteBatch, Rectangle rect, FontType font, string text, Color color, TextAlignment textAlignment, float targetSize) {
+ // Credit for text alignment: https://stackoverflow.com/a/10263903
+
+ // Measure the text's size from the sprite font.
+ Vector2 size = font.Font.MeasureString(text);
+
+ // Get the origin point at the center.
+ Vector2 origin = 0.5f * size;
+
+ if (textAlignment.HasFlag(TextAlignment.Left))
+ origin.X += rect.Width / 2f - size.X / 2f;
+
+ if (textAlignment.HasFlag(TextAlignment.Right))
+ origin.X -= rect.Width / 2f - size.X / 2f;
+
+ if (textAlignment.HasFlag(TextAlignment.Top))
+ origin.Y += rect.Height / 2f - size.Y / 2f;
+
+ if (textAlignment.HasFlag(TextAlignment.Bottom))
+ origin.Y -= rect.Height / 2f - size.Y / 2f;
+
+ spriteBatch.DrawString(font.Font, text, new Vector2(rect.Center.X, rect.Center.Y), color, 0f, origin, font.Scale(targetSize), SpriteEffects.None, 0f);
+ }
+
+ public static void DrawAlignedText(SpriteBatch spriteBatch, Rectangle rect, TextProperties textProperties) {
+ DrawAlignedText(spriteBatch, rect, textProperties.GetFont(), textProperties.GetText(), textProperties.GetColor(), textProperties.GetAlignment(), textProperties.GetFontSize());
+ }
+ }
+
+ public static class ImageUtilities {
+ private static int xLeft(Rectangle rectangle, int patchSize) => rectangle.X;
+ private static int xMiddle(Rectangle rectangle, int patchSize) => rectangle.X + (patchSize * UIReferences.Scaling);
+ private static int xRight(Rectangle rectangle, int patchSize) => rectangle.X + rectangle.Width - (patchSize * UIReferences.Scaling);
+ private static int yTop(Rectangle rectangle, int patchSize) => rectangle.Y;
+ private static int yMiddle(Rectangle rectangle, int patchSize) => rectangle.Y + (patchSize * UIReferences.Scaling);
+ private static int yBottom(Rectangle rectangle, int patchSize) => rectangle.Y + rectangle.Height - (patchSize * UIReferences.Scaling);
+
+ public static void DrawPatched(SpriteBatch spriteBatch, Rectangle rectangle, TextureAtlas patches, int patchSize, Color color) {
+ int y;
+ int scaled = patchSize * UIReferences.Scaling;
+
+ // Top
+ y = yTop(rectangle, patchSize);
+ {
+ spriteBatch.Draw(patches.GetRegion(0), new Rectangle(xLeft(rectangle, patchSize), y, scaled, scaled), color); // Top left
+ spriteBatch.Draw(patches.GetRegion(1), new Rectangle(xMiddle(rectangle, patchSize), y, rectangle.Width - (2 * scaled), scaled), color); // Top center
+ spriteBatch.Draw(patches.GetRegion(2), new Rectangle(xRight(rectangle, patchSize), y, scaled, scaled), color); // Top right
+ }
+
+ // Center
+ y = yMiddle(rectangle, patchSize);
+ {
+ spriteBatch.Draw(patches.GetRegion(3), new Rectangle(xLeft(rectangle, patchSize), y, scaled, rectangle.Height - (2 * scaled)), color); // Left center
+ spriteBatch.Draw(patches.GetRegion(4), new Rectangle(xMiddle(rectangle, patchSize), y, rectangle.Width - (2 * scaled), rectangle.Height - (2 * scaled)), color); // Center
+ spriteBatch.Draw(patches.GetRegion(5), new Rectangle(xRight(rectangle, patchSize), y, scaled, rectangle.Height - (2 * scaled)), color); // Right center
+ }
+
+ // Bottom
+ y = yBottom(rectangle, patchSize);
+ {
+ spriteBatch.Draw(patches.GetRegion(6), new Rectangle(xLeft(rectangle, patchSize), y, scaled, scaled), color); // Bottom left
+ spriteBatch.Draw(patches.GetRegion(7), new Rectangle(xMiddle(rectangle, patchSize), y, rectangle.Width - (2 * scaled), scaled), color); // Bottom center
+ spriteBatch.Draw(patches.GetRegion(8), new Rectangle(xRight(rectangle, patchSize), y, scaled, scaled), color); // Bottom right
+ }
+ }
+ }
+} \ No newline at end of file