mirror of
https://github.com/NoelFB/blah.git
synced 2024-11-25 16:18:57 +08:00
removed Batch TextAlign in favour of Vec2 Justify
This commit is contained in:
parent
05391f0b3d
commit
78f5fc9388
|
@ -9,39 +9,23 @@
|
|||
|
||||
namespace Blah
|
||||
{
|
||||
// Spritebatcher Color Mode
|
||||
enum class ColorMode
|
||||
{
|
||||
// Draws textures and shapes normally
|
||||
Normal,
|
||||
|
||||
// Ignores the texture color but still uses transparency, essentially
|
||||
// drawing the "shape" of the texture as a solid color
|
||||
Wash
|
||||
};
|
||||
|
||||
enum class TextAlign : char
|
||||
{
|
||||
Center = 0,
|
||||
Left = 1 << 1,
|
||||
Right = 1 << 2,
|
||||
Top = 1 << 3,
|
||||
Bottom = 1 << 4,
|
||||
|
||||
TopLeft = Top | Left,
|
||||
TopRight = Top | Right,
|
||||
BottomLeft = Bottom | Left,
|
||||
BottomRight = Bottom | Right
|
||||
};
|
||||
|
||||
inline TextAlign operator|(TextAlign lhs, TextAlign rhs) { return static_cast<TextAlign>(static_cast<char>(lhs) | static_cast<char>(rhs)); }
|
||||
inline TextAlign operator&(TextAlign lhs, TextAlign rhs) { return static_cast<TextAlign>(static_cast<char>(lhs) & static_cast<char>(rhs)); }
|
||||
|
||||
// A 2D sprite batcher, used for drawing shapes and textures
|
||||
class Batch
|
||||
{
|
||||
public:
|
||||
|
||||
// Spritebatcher Color Mode
|
||||
enum class ColorMode
|
||||
{
|
||||
// Draws textures and shapes normally
|
||||
Normal,
|
||||
|
||||
// Ignores the texture color but still uses transparency, essentially
|
||||
// drawing the "shape" of the texture as a solid color
|
||||
Wash
|
||||
};
|
||||
|
||||
// The name of the default uniforms to set
|
||||
String texture_uniform = "u_texture";
|
||||
String sampler_uniform = "u_texture_sampler";
|
||||
|
@ -177,7 +161,7 @@ namespace Blah
|
|||
void tex(const Subtexture& subtexture, const Rectf& clip, const Vec2f& pos, const Vec2f& origin, const Vec2f& scale, float rotation, Color color);
|
||||
|
||||
void str(const SpriteFont& font, const String& text, const Vec2f& pos, Color color);
|
||||
void str(const SpriteFont& font, const String& text, const Vec2f& pos, TextAlign align, float size, Color color);
|
||||
void str(const SpriteFont& font, const String& text, const Vec2f& pos, const Vec2f& justify, float size, Color color);
|
||||
|
||||
private:
|
||||
|
||||
|
|
|
@ -228,7 +228,7 @@ void Batch::push_color_mode(ColorMode mode)
|
|||
m_tex_wash = (m_color_mode == ColorMode::Wash ? 255 : 0);
|
||||
}
|
||||
|
||||
ColorMode Batch::pop_color_mode()
|
||||
Batch::ColorMode Batch::pop_color_mode()
|
||||
{
|
||||
ColorMode was = m_color_mode;
|
||||
m_color_mode = m_color_mode_stack.pop();
|
||||
|
@ -237,7 +237,7 @@ ColorMode Batch::pop_color_mode()
|
|||
return was;
|
||||
}
|
||||
|
||||
ColorMode Batch::peek_color_mode() const
|
||||
Batch::ColorMode Batch::peek_color_mode() const
|
||||
{
|
||||
return m_color_mode;
|
||||
}
|
||||
|
@ -404,8 +404,22 @@ void Batch::line(const Vec2f& from, const Vec2f& to, float t, Color fromColor, C
|
|||
if (from.x == to.x && from.y == to.y)
|
||||
return;
|
||||
|
||||
Vec2f normal = (to - from).normal();
|
||||
Vec2f perp = Vec2f(normal.y, -normal.x);
|
||||
Vec2f perp;
|
||||
|
||||
if (from.x == to.x)
|
||||
{
|
||||
perp = Vec2f::unit_x;
|
||||
}
|
||||
else if (from.y == to.y)
|
||||
{
|
||||
perp = Vec2f::unit_y;
|
||||
}
|
||||
else
|
||||
{
|
||||
Vec2f normal = (to - from).normal();
|
||||
perp = Vec2f(normal.y, -normal.x);
|
||||
}
|
||||
|
||||
Vec2f pos0 = from + perp * t * 0.5f;
|
||||
Vec2f pos1 = to + perp * t * 0.5f;
|
||||
Vec2f pos2 = to - perp * t * 0.5f;
|
||||
|
@ -938,57 +952,40 @@ void Batch::tex(const Subtexture& sub, const Rectf& clip, const Vec2f& pos, cons
|
|||
|
||||
void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, Color color)
|
||||
{
|
||||
str(font, text, pos, TextAlign::TopLeft, font.size, color);
|
||||
str(font, text, pos, Vec2f::zero, font.size, color);
|
||||
}
|
||||
|
||||
void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, TextAlign align, float size, Color color)
|
||||
void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, const Vec2f& justify, float size, Color color)
|
||||
{
|
||||
push_matrix(
|
||||
Mat3x2f::create_scale(size / font.size) *
|
||||
Mat3x2f::create_translation(pos)
|
||||
);
|
||||
|
||||
Vec2f offset;
|
||||
|
||||
if ((align & TextAlign::Left) == TextAlign::Left)
|
||||
offset.x = 0;
|
||||
else if ((align & TextAlign::Right) == TextAlign::Right)
|
||||
offset.x -= font.width_of_line(text);
|
||||
else
|
||||
offset.x -= font.width_of_line(text) * 0.5f;
|
||||
|
||||
if ((align & TextAlign::Top) == TextAlign::Top)
|
||||
offset.y = font.ascent + font.descent;
|
||||
else if ((align & TextAlign::Bottom) == TextAlign::Bottom)
|
||||
offset.y = font.height() - font.height_of(text);
|
||||
else
|
||||
offset.y = (font.ascent + font.descent + font.height() - font.height_of(text)) * 0.5f;
|
||||
Vec2f offset = Vec2f(0, font.ascent + font.descent);
|
||||
if (justify.x != 0)
|
||||
offset.x -= font.width_of_line(text) * justify.x;
|
||||
if (justify.y != 0)
|
||||
offset.y -= font.height_of(text) * justify.y;
|
||||
|
||||
u32 last = 0;
|
||||
for (int i = 0, l = text.length(); i < l; i++)
|
||||
for (int i = 0, l = text.length(); i < l; i += text.utf8_length(i))
|
||||
{
|
||||
if (text[i] == '\n')
|
||||
u32 next = text.utf8_at(i);
|
||||
|
||||
if (next == '\n')
|
||||
{
|
||||
// increment y
|
||||
offset.x = 0;
|
||||
offset.y += font.line_height();
|
||||
|
||||
// re-align X for this line
|
||||
if ((align & TextAlign::Left) == TextAlign::Left)
|
||||
offset.x = 0;
|
||||
else if ((align & TextAlign::Right) == TextAlign::Right)
|
||||
offset.x = -font.width_of_line(text, i + 1);
|
||||
else
|
||||
offset.x = -font.width_of_line(text, i + 1) * 0.5f;
|
||||
if (justify.x != 0)
|
||||
offset.x -= font.width_of_line(text, i + 1) * justify.x;
|
||||
|
||||
last = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
// get the character
|
||||
u32 next = text.utf8_at(i);
|
||||
const auto& ch = font[next];
|
||||
|
||||
// draw it, if the subtexture exists
|
||||
if (ch.subtexture.texture)
|
||||
{
|
||||
Vec2f at = offset + ch.offset;
|
||||
|
@ -999,14 +996,7 @@ void Batch::str(const SpriteFont& font, const String& text, const Vec2f& pos, Te
|
|||
tex(ch.subtexture, at, color);
|
||||
}
|
||||
|
||||
// move forward
|
||||
offset.x += ch.advance;
|
||||
|
||||
// increment past current character
|
||||
// (minus 1 since the for loop iterator increments as well)
|
||||
i += text.utf8_length(i) - 1;
|
||||
|
||||
// keep last codepoint for next char for kerning
|
||||
last = next;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user