diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index 92020285d..1d3b833cf 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -717,20 +717,26 @@ void Graphics::print(const std::vector &str, const Matrix4 { checkSetDefaultFont(); - DisplayState &state = states.back(); + if (states.back().font.get() != nullptr) + print(str, states.back().font.get(), m); +} - if (state.font.get() != nullptr) - state.font->print(this, str, m, state.color); +void Graphics::print(const std::vector &str, Font *font, const Matrix4 &m) +{ + font->print(this, str, m, states.back().color); } void Graphics::printf(const std::vector &str, float wrap, Font::AlignMode align, const Matrix4 &m) { checkSetDefaultFont(); - DisplayState &state = states.back(); + if (states.back().font.get() != nullptr) + printf(str, states.back().font.get(), wrap, align, m); +} - if (state.font.get() != nullptr) - state.font->printf(this, str, wrap, align, m, state.color); +void Graphics::printf(const std::vector &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m) +{ + font->printf(this, str, wrap, align, m, states.back().color); } /** diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index 9c3acef05..bc6c5b3b4 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -593,11 +593,13 @@ public: * Draws text at the specified coordinates **/ void print(const std::vector &str, const Matrix4 &m); + void print(const std::vector &str, Font *font, const Matrix4 &m); /** * Draws formatted text on screen at the specified coordinates. **/ void printf(const std::vector &str, float wrap, Font::AlignMode align, const Matrix4 &m); + void printf(const std::vector &str, Font *font, float wrap, Font::AlignMode align, const Matrix4 &m); /** * Draws a point at (x,y). diff --git a/src/modules/graphics/wrap_Graphics.cpp b/src/modules/graphics/wrap_Graphics.cpp index 1b24cd57a..f6e0a0413 100644 --- a/src/modules/graphics/wrap_Graphics.cpp +++ b/src/modules/graphics/wrap_Graphics.cpp @@ -2028,10 +2028,22 @@ int w_print(lua_State *L) std::vector str; luax_checkcoloredstring(L, 1, str); - luax_checkstandardtransform(L, 2, [&](const Matrix4 &m) + if (luax_istype(L, 2, Font::type)) { - luax_catchexcept(L, [&](){ instance()->print(str, m); }); - }); + Font *font = luax_checkfont(L, 2); + + luax_checkstandardtransform(L, 3, [&](const Matrix4 &m) + { + luax_catchexcept(L, [&](){ instance()->print(str, font, m); }); + }); + } + else + { + luax_checkstandardtransform(L, 2, [&](const Matrix4 &m) + { + luax_catchexcept(L, [&](){ instance()->print(str, m); }); + }); + } return 0; } @@ -2041,29 +2053,38 @@ int w_printf(lua_State *L) std::vector str; luax_checkcoloredstring(L, 1, str); + Font *font = nullptr; + int startidx = 2; + + if (luax_istype(L, startidx, Font::type)) + { + font = luax_checkfont(L, startidx); + startidx++; + } + Font::AlignMode align = Font::ALIGN_LEFT; Matrix4 m; - int formatidx = 4; + int formatidx = startidx + 2; - if (luax_istype(L, 2, math::Transform::type)) + if (luax_istype(L, startidx, math::Transform::type)) { - math::Transform *tf = luax_totype(L, 2); + math::Transform *tf = luax_totype(L, startidx); m = tf->getMatrix(); - formatidx = 3; + formatidx = startidx + 1; } else { - float x = (float)luaL_checknumber(L, 2); - float y = (float)luaL_checknumber(L, 3); + float x = (float)luaL_checknumber(L, startidx + 0); + float y = (float)luaL_checknumber(L, startidx + 1); - float angle = (float) luaL_optnumber(L, 6, 0.0f); - float sx = (float) luaL_optnumber(L, 7, 1.0f); - float sy = (float) luaL_optnumber(L, 8, sx); - float ox = (float) luaL_optnumber(L, 9, 0.0f); - float oy = (float) luaL_optnumber(L, 10, 0.0f); - float kx = (float) luaL_optnumber(L, 11, 0.0f); - float ky = (float) luaL_optnumber(L, 12, 0.0f); + float angle = (float) luaL_optnumber(L, startidx + 4, 0.0f); + float sx = (float) luaL_optnumber(L, startidx + 5, 1.0f); + float sy = (float) luaL_optnumber(L, startidx + 6, sx); + float ox = (float) luaL_optnumber(L, startidx + 7, 0.0f); + float oy = (float) luaL_optnumber(L, startidx + 8, 0.0f); + float kx = (float) luaL_optnumber(L, startidx + 9, 0.0f); + float ky = (float) luaL_optnumber(L, startidx + 10, 0.0f); m = Matrix4(x, y, angle, sx, sy, ox, oy, kx, ky); } @@ -2074,7 +2095,11 @@ int w_printf(lua_State *L) if (astr != nullptr && !Font::getConstant(astr, align)) return luaL_error(L, "Incorrect alignment: %s", astr); - luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); }); + if (font != nullptr) + luax_catchexcept(L, [&](){ instance()->printf(str, font, wrap, align, m); }); + else + luax_catchexcept(L, [&](){ instance()->printf(str, wrap, align, m); }); + return 0; }