wrap_TextBatch.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "wrap_TextBatch.h"
  21. #include "wrap_Font.h"
  22. #include "math/wrap_Transform.h"
  23. namespace love
  24. {
  25. namespace graphics
  26. {
  27. TextBatch *luax_checktextbatch(lua_State *L, int idx)
  28. {
  29. return luax_checktype<TextBatch>(L, idx);
  30. }
  31. int w_TextBatch_set(lua_State *L)
  32. {
  33. TextBatch *t = luax_checktextbatch(L, 1);
  34. std::vector<Font::ColoredString> newtext;
  35. luax_checkcoloredstring(L, 2, newtext);
  36. luax_catchexcept(L, [&](){ t->set(newtext); });
  37. return 0;
  38. }
  39. int w_TextBatch_setf(lua_State *L)
  40. {
  41. TextBatch *t = luax_checktextbatch(L, 1);
  42. float wraplimit = (float) luaL_checknumber(L, 3);
  43. Font::AlignMode align;
  44. const char *alignstr = luaL_checkstring(L, 4);
  45. if (!Font::getConstant(alignstr, align))
  46. return luax_enumerror(L, "align mode", Font::getConstants(align), alignstr);
  47. std::vector<Font::ColoredString> newtext;
  48. luax_checkcoloredstring(L, 2, newtext);
  49. luax_catchexcept(L, [&](){ t->set(newtext, wraplimit, align); });
  50. return 0;
  51. }
  52. int w_TextBatch_add(lua_State *L)
  53. {
  54. TextBatch *t = luax_checktextbatch(L, 1);
  55. int index = 0;
  56. std::vector<Font::ColoredString> text;
  57. luax_checkcoloredstring(L, 2, text);
  58. if (luax_istype(L, 3, math::Transform::type))
  59. {
  60. math::Transform *tf = luax_totype<math::Transform>(L, 3);
  61. luax_catchexcept(L, [&](){ index = t->add(text, tf->getMatrix()); });
  62. }
  63. else
  64. {
  65. float x = (float) luaL_optnumber(L, 3, 0.0);
  66. float y = (float) luaL_optnumber(L, 4, 0.0);
  67. float a = (float) luaL_optnumber(L, 5, 0.0);
  68. float sx = (float) luaL_optnumber(L, 6, 1.0);
  69. float sy = (float) luaL_optnumber(L, 7, sx);
  70. float ox = (float) luaL_optnumber(L, 8, 0.0);
  71. float oy = (float) luaL_optnumber(L, 9, 0.0);
  72. float kx = (float) luaL_optnumber(L, 10, 0.0);
  73. float ky = (float) luaL_optnumber(L, 11, 0.0);
  74. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  75. luax_catchexcept(L, [&](){ index = t->add(text, m); });
  76. }
  77. lua_pushnumber(L, index + 1);
  78. return 1;
  79. }
  80. int w_TextBatch_addf(lua_State *L)
  81. {
  82. TextBatch *t = luax_checktextbatch(L, 1);
  83. int index = 0;
  84. std::vector<Font::ColoredString> text;
  85. luax_checkcoloredstring(L, 2, text);
  86. float wrap = (float) luaL_checknumber(L, 3);
  87. Font::AlignMode align = Font::ALIGN_MAX_ENUM;
  88. const char *alignstr = luaL_checkstring(L, 4);
  89. if (!Font::getConstant(alignstr, align))
  90. return luax_enumerror(L, "align mode", Font::getConstants(align), alignstr);
  91. if (luax_istype(L, 5, math::Transform::type))
  92. {
  93. math::Transform *tf = luax_totype<math::Transform>(L, 5);
  94. luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, tf->getMatrix()); });
  95. }
  96. else
  97. {
  98. float x = (float) luaL_optnumber(L, 5, 0.0);
  99. float y = (float) luaL_optnumber(L, 6, 0.0);
  100. float a = (float) luaL_optnumber(L, 7, 0.0);
  101. float sx = (float) luaL_optnumber(L, 8, 1.0);
  102. float sy = (float) luaL_optnumber(L, 9, sx);
  103. float ox = (float) luaL_optnumber(L, 10, 0.0);
  104. float oy = (float) luaL_optnumber(L, 11, 0.0);
  105. float kx = (float) luaL_optnumber(L, 12, 0.0);
  106. float ky = (float) luaL_optnumber(L, 13, 0.0);
  107. Matrix4 m(x, y, a, sx, sy, ox, oy, kx, ky);
  108. luax_catchexcept(L, [&](){ index = t->addf(text, wrap, align, m); });
  109. }
  110. lua_pushnumber(L, index + 1);
  111. return 1;
  112. }
  113. int w_TextBatch_clear(lua_State *L)
  114. {
  115. TextBatch *t = luax_checktextbatch(L, 1);
  116. luax_catchexcept(L, [&](){ t->clear(); });
  117. return 0;
  118. }
  119. int w_TextBatch_setFont(lua_State *L)
  120. {
  121. TextBatch *t = luax_checktextbatch(L, 1);
  122. Font *f = luax_checktype<Font>(L, 2);
  123. luax_catchexcept(L, [&](){ t->setFont(f); });
  124. return 0;
  125. }
  126. int w_TextBatch_getFont(lua_State *L)
  127. {
  128. TextBatch *t = luax_checktextbatch(L, 1);
  129. Font *f = t->getFont();
  130. luax_pushtype(L, f);
  131. return 1;
  132. }
  133. int w_TextBatch_getWidth(lua_State *L)
  134. {
  135. TextBatch *t = luax_checktextbatch(L, 1);
  136. int index = (int) luaL_optinteger(L, 2, 0) - 1;
  137. lua_pushnumber(L, t->getWidth(index));
  138. return 1;
  139. }
  140. int w_TextBatch_getHeight(lua_State *L)
  141. {
  142. TextBatch *t = luax_checktextbatch(L, 1);
  143. int index = (int) luaL_optinteger(L, 2, 0) - 1;
  144. lua_pushnumber(L, t->getHeight(index));
  145. return 1;
  146. }
  147. int w_TextBatch_getDimensions(lua_State *L)
  148. {
  149. TextBatch *t = luax_checktextbatch(L, 1);
  150. int index = (int) luaL_optinteger(L, 2, 0) - 1;
  151. lua_pushnumber(L, t->getWidth(index));
  152. lua_pushnumber(L, t->getHeight(index));
  153. return 2;
  154. }
  155. static const luaL_Reg w_TextBatch_functions[] =
  156. {
  157. { "set", w_TextBatch_set },
  158. { "setf", w_TextBatch_setf },
  159. { "add", w_TextBatch_add },
  160. { "addf", w_TextBatch_addf },
  161. { "clear", w_TextBatch_clear },
  162. { "setFont", w_TextBatch_setFont },
  163. { "getFont", w_TextBatch_getFont },
  164. { "getWidth", w_TextBatch_getWidth },
  165. { "getHeight", w_TextBatch_getHeight },
  166. { "getDimensions", w_TextBatch_getDimensions },
  167. { 0, 0 }
  168. };
  169. extern "C" int luaopen_textbatch(lua_State *L)
  170. {
  171. return luax_register_type(L, &TextBatch::type, w_TextBatch_functions, nullptr);
  172. }
  173. } // graphics
  174. } // love