Repository navigation
Expand file tree
/
Copy pathDrawHelper.cs
More file actions
190 lines (174 loc) · 9.48 KB
/
Copy pathDrawHelper.cs
File metadata and controls
190 lines (174 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Pathoschild.Stardew.Common;
using Pathoschild.Stardew.LookupAnything.Components;
using Pathoschild.Stardew.LookupAnything.Framework;
using StardewValley;
namespace Pathoschild.Stardew.LookupAnything
{
/// <summary>Provides utility methods for drawing to the screen.</summary>
internal static class DrawHelper
{
/*********
** Public methods
*********/
/****
** Fonts
****/
/// <summary>Get the dimensions of a space character.</summary>
/// <param name="font">The font to measure.</param>
public static float GetSpaceWidth(SpriteFont font)
{
return CommonHelper.GetSpaceWidth(font);
}
/****
** Drawing
****/
/// <summary>Draw a sprite to the screen.</summary>
/// <param name="spriteBatch">The sprite batch being drawn.</param>
/// <param name="sheet">The sprite sheet containing the sprite.</param>
/// <param name="sprite">The sprite coordinates and dimensions in the sprite sheet.</param>
/// <param name="x">The X-position at which to draw the sprite.</param>
/// <param name="y">The X-position at which to draw the sprite.</param>
/// <param name="color">The color to tint the sprite.</param>
/// <param name="scale">The scale to draw.</param>
public static void DrawSprite(this SpriteBatch spriteBatch, Texture2D sheet, Rectangle sprite, float x, float y, Color? color = null, float scale = 1)
{
spriteBatch.Draw(sheet, new Vector2(x, y), sprite, color ?? Color.White, 0, Vector2.Zero, scale, SpriteEffects.None, 0);
}
/// <summary>Draw a sprite to the screen scaled and centered to fit the given dimensions.</summary>
/// <param name="spriteBatch">The sprite batch being drawn.</param>
/// <param name="sheet">The sprite sheet containing the sprite.</param>
/// <param name="sprite">The sprite coordinates and dimensions in the sprite sheet.</param>
/// <param name="x">The X-position at which to draw the sprite.</param>
/// <param name="y">The X-position at which to draw the sprite.</param>
/// <param name="size">The size to draw.</param>
/// <param name="color">The color to tint the sprite.</param>
public static void DrawSpriteWithin(this SpriteBatch spriteBatch, Texture2D sheet, Rectangle sprite, float x, float y, Vector2 size, Color? color = null)
{
// calculate dimensions
float largestDimension = Math.Max(sprite.Width, sprite.Height);
float scale = size.X / largestDimension;
float leftOffset = Math.Max((size.X - (sprite.Width * scale)) / 2, 0);
float topOffset = Math.Max((size.Y - (sprite.Height * scale)) / 2, 0);
// draw
spriteBatch.DrawSprite(sheet, sprite, x + leftOffset, y + topOffset, color ?? Color.White, scale);
}
/// <summary>Draw an item icon to the screen scaled and centered to fit the given dimensions.</summary>
/// <param name="spriteBatch">The sprite batch being drawn.</param>
/// <param name="item">The item for which to draw an icon.</param>
/// <param name="x">The X-position at which to draw the sprite.</param>
/// <param name="y">The X-position at which to draw the sprite.</param>
/// <param name="size">The size to draw.</param>
/// <param name="color">The color to tint the sprite.</param>
public static void DrawIcon(this SpriteBatch spriteBatch, Item item, float x, float y, Vector2 size, Color? color = null)
{
Tuple<Texture2D, Rectangle> spriteData = GameHelper.GetSprite(item);
if (spriteData != null)
spriteBatch.DrawSpriteWithin(spriteData.Item1, spriteData.Item2, x, y, size, color ?? Color.White);
}
/// <summary>Draw a sprite to the screen.</summary>
/// <param name="batch">The sprite batch.</param>
/// <param name="x">The X-position at which to start the line.</param>
/// <param name="y">The X-position at which to start the line.</param>
/// <param name="size">The line dimensions.</param>
/// <param name="color">The color to tint the sprite.</param>
public static void DrawLine(this SpriteBatch batch, float x, float y, Vector2 size, Color? color = null)
{
batch.Draw(Sprites.Pixel, new Rectangle((int)x, (int)y, (int)size.X, (int)size.Y), color ?? Color.White);
}
/// <summary>Draw a block of text to the screen with the specified wrap width.</summary>
/// <param name="batch">The sprite batch.</param>
/// <param name="font">The sprite font.</param>
/// <param name="text">The block of text to write.</param>
/// <param name="position">The position at which to draw the text.</param>
/// <param name="wrapWidth">The width at which to wrap the text.</param>
/// <param name="color">The text color.</param>
/// <param name="bold">Whether to draw bold text.</param>
/// <param name="scale">The font scale.</param>
/// <returns>Returns the text dimensions.</returns>
public static Vector2 DrawTextBlock(this SpriteBatch batch, SpriteFont font, string text, Vector2 position, float wrapWidth, Color? color = null, bool bold = false, float scale = 1)
{
return batch.DrawTextBlock(font, new IFormattedText[] { new FormattedText(text, color, bold) }, position, wrapWidth, scale);
}
/// <summary>Draw a block of text to the screen with the specified wrap width.</summary>
/// <param name="batch">The sprite batch.</param>
/// <param name="font">The sprite font.</param>
/// <param name="text">The block of text to write.</param>
/// <param name="position">The position at which to draw the text.</param>
/// <param name="wrapWidth">The width at which to wrap the text.</param>
/// <param name="scale">The font scale.</param>
/// <returns>Returns the text dimensions.</returns>
public static Vector2 DrawTextBlock(this SpriteBatch batch, SpriteFont font, IEnumerable<IFormattedText> text, Vector2 position, float wrapWidth, float scale = 1)
{
if (text == null)
return new Vector2(0, 0);
// track draw values
float xOffset = 0;
float yOffset = 0;
float lineHeight = font.MeasureString("ABC").Y * scale;
float spaceWidth = DrawHelper.GetSpaceWidth(font) * scale;
float blockWidth = 0;
float blockHeight = lineHeight;
// draw text snippets
foreach (IFormattedText snippet in text)
{
if (snippet?.Text == null)
continue;
// get word list
List<string> words = new List<string>();
foreach (string word in snippet.Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
{
// split on newlines
string wordPart = word;
int newlineIndex;
while ((newlineIndex = wordPart.IndexOf(Environment.NewLine, StringComparison.InvariantCulture)) >= 0)
{
if (newlineIndex == 0)
{
words.Add(Environment.NewLine);
wordPart = wordPart.Substring(Environment.NewLine.Length);
}
else if (newlineIndex > 0)
{
words.Add(wordPart.Substring(0, newlineIndex));
words.Add(Environment.NewLine);
wordPart = wordPart.Substring(newlineIndex + Environment.NewLine.Length);
}
}
// add remaining word (after newline split)
if (wordPart.Length > 0)
words.Add(wordPart);
}
// draw words to screen
foreach (string word in words)
{
// check wrap width
float wordWidth = font.MeasureString(word).X * scale;
if (word == Environment.NewLine || ((wordWidth + xOffset) > wrapWidth && (int)xOffset != 0))
{
xOffset = 0;
yOffset += lineHeight;
blockHeight += lineHeight;
}
if (word == Environment.NewLine)
continue;
// draw text
Vector2 wordPosition = new Vector2(position.X + xOffset, position.Y + yOffset);
if (snippet.Bold)
Utility.drawBoldText(batch, word, font, wordPosition, snippet.Color ?? Color.Black, scale);
else
batch.DrawString(font, word, wordPosition, snippet.Color ?? Color.Black, 0, Vector2.Zero, scale, SpriteEffects.None, 1);
// update draw values
if (xOffset + wordWidth > blockWidth)
blockWidth = xOffset + wordWidth;
xOffset += wordWidth + spaceWidth;
}
}
// return text position & dimensions
return new Vector2(blockWidth, blockHeight);
}
}
}