Textbox.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "Textbox.h"
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include "utility/misc.h"
  5. #include "input/Input.h"
  6. using WalrusRPG::MAGIC_TOKEN;
  7. using WalrusRPG::COMMAND_LEGNTH;
  8. using WalrusRPG::Textbox;
  9. using WalrusRPG::TextboxState;
  10. using WalrusRPG::TextboxChar;
  11. using WalrusRPG::Graphics::Font;
  12. using WalrusRPG::Graphics::CharacterParameters;
  13. using WalrusRPG::Graphics::Pixel;
  14. using WalrusRPG::Input::Key;
  15. namespace
  16. {
  17. signed strlen_tokens(const char *str)
  18. {
  19. signed len = 0;
  20. for (; str[len]; ++len)
  21. {
  22. if (str[len] == MAGIC_TOKEN)
  23. len += COMMAND_LEGNTH;
  24. }
  25. return len;
  26. }
  27. }
  28. Textbox::Textbox(Font fnt)
  29. : fnt(fnt), buffer(0), buffer_index(-1), global_string_offset(0),
  30. current_color(0, 0, 0), letter_wait(0), letter_wait_cooldown(10),
  31. dimensions(40, 4, 200, 32), state(Waiting)
  32. {
  33. }
  34. Textbox::~Textbox()
  35. {
  36. }
  37. void Textbox::set_text(char *new_text)
  38. {
  39. letter_wait = 0;
  40. letter_wait_cooldown = 10;
  41. buffer_index = -1;
  42. global_string_offset = 0;
  43. nb_line_to_update = 0;
  44. for (unsigned i = 0; i < nb_lines; ++i)
  45. {
  46. line_nb_characters[i] = 0;
  47. line_widths[i] = 0;
  48. }
  49. buffer.clear();
  50. for (signed i = 0; i < strlen_tokens(new_text); ++i)
  51. {
  52. TextboxChar t;
  53. if (new_text[i] == MAGIC_TOKEN)
  54. {
  55. t.c = MAGIC_TOKEN;
  56. t.routine = new_text[i + 1];
  57. t.arg1 = new_text[i + 2];
  58. t.arg2 = new_text[i + 3];
  59. t.arg3 = new_text[i + 4];
  60. i += COMMAND_LEGNTH;
  61. }
  62. else
  63. {
  64. t.c = new_text[i];
  65. t.routine = 0;
  66. t.arg1 = 0;
  67. t.arg2 = 0;
  68. t.arg3 = 0;
  69. }
  70. buffer.push_back(t);
  71. }
  72. state = Updating;
  73. }
  74. void Textbox::add_letter(unsigned nb_letters)
  75. {
  76. if (state != Updating)
  77. return;
  78. if (buffer.size() <= 0)
  79. return;
  80. for (unsigned i = 0;
  81. (i < nb_letters) &&
  82. (buffer_index < 0 || buffer_index < static_cast<signed>(buffer.size()) - 1);
  83. ++i)
  84. {
  85. ++buffer_index;
  86. if (buffer[buffer_index].c == MAGIC_TOKEN)
  87. {
  88. switch (buffer[buffer_index].routine)
  89. {
  90. // wait a bit
  91. case 0x81:
  92. letter_wait = buffer[buffer_index].arg1;
  93. break;
  94. }
  95. line_nb_characters[nb_line_to_update]++;
  96. }
  97. else
  98. {
  99. CharacterParameters &p =
  100. fnt.chars[static_cast<signed>(buffer[buffer_index].c)];
  101. TextboxChar &t = buffer[buffer_index];
  102. if (t.c == '\n')
  103. {
  104. if (nb_line_to_update + 1 >= nb_lines)
  105. {
  106. line_nb_characters[nb_line_to_update]++;
  107. // --buffer_index;
  108. state = Full;
  109. return;
  110. }
  111. nb_line_to_update++;
  112. }
  113. if (line_widths[nb_line_to_update] + p.dimensions.width + 1 >
  114. dimensions.width)
  115. {
  116. if (nb_line_to_update + 1 >= nb_lines)
  117. {
  118. --buffer_index;
  119. state = Full;
  120. return;
  121. }
  122. nb_line_to_update++;
  123. }
  124. if (t.c == ' ')
  125. line_widths[nb_line_to_update] += fnt.space_width;
  126. else
  127. line_widths[nb_line_to_update] += p.dimensions.width + 1;
  128. line_nb_characters[nb_line_to_update]++;
  129. }
  130. }
  131. if (buffer_index >= static_cast<signed>(buffer.size() - 1))
  132. {
  133. state = Done;
  134. }
  135. letter_wait = letter_wait_cooldown;
  136. }
  137. void Textbox::update(unsigned dt)
  138. {
  139. switch (state)
  140. {
  141. case Waiting:
  142. return;
  143. break;
  144. case Updating:
  145. if ((buffer_index >= 0) &&
  146. (buffer_index >= static_cast<signed>(buffer.size())))
  147. return;
  148. letter_wait -= dt;
  149. if (letter_wait <= 0)
  150. {
  151. unsigned add = (-letter_wait) / letter_wait_cooldown + 1;
  152. add_letter(add);
  153. }
  154. break;
  155. case Full:
  156. if (key_pressed(Key::K_A))
  157. {
  158. for (unsigned i = 0; i < nb_lines - 1; ++i)
  159. global_string_offset += line_nb_characters[i];
  160. line_widths[0] = line_widths[nb_lines - 1];
  161. line_nb_characters[0] = line_nb_characters[nb_lines - 1];
  162. for (unsigned i = 1; i < nb_lines; ++i)
  163. {
  164. line_widths[i] = 0;
  165. line_nb_characters[i] = 0;
  166. }
  167. nb_line_to_update = 1;
  168. state = Updating;
  169. }
  170. default:
  171. break;
  172. }
  173. }
  174. void Textbox::render(unsigned dt)
  175. {
  176. UNUSED(dt);
  177. if (buffer_index < 0)
  178. return;
  179. current_color = 0xFFFF;
  180. put_rectangle(dimensions, Graphics::Black);
  181. // signed cur_x_max = cur_x + dimensions.width;
  182. unsigned global_index = global_string_offset;
  183. for (unsigned l = 0; l < nb_lines; l++)
  184. {
  185. unsigned cur_x = dimensions.x;
  186. unsigned cur_y = dimensions.y + l * fnt.baseline;
  187. for (unsigned line_index = 0; line_index < line_nb_characters[l]; ++line_index)
  188. {
  189. TextboxChar b = buffer[global_index + line_index];
  190. char c = b.c;
  191. if (c == MAGIC_TOKEN)
  192. {
  193. switch (b.routine)
  194. {
  195. // Change current color
  196. case 0x01:
  197. current_color = ((b.arg1 << 8) + b.arg2);
  198. break;
  199. }
  200. continue;
  201. }
  202. fnt.draw(cur_x, cur_y, c, current_color);
  203. if (c == '\n')
  204. continue;
  205. else if (c == ' ')
  206. cur_x += fnt.space_width;
  207. else
  208. cur_x += fnt.chars[static_cast<signed>(c)].dimensions.width + 1;
  209. }
  210. global_index += line_nb_characters[l];
  211. }
  212. if (state == Full)
  213. {
  214. put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
  215. dimensions.y + static_cast<signed>(dimensions.height), 3, 3},
  216. Graphics::Red);
  217. }
  218. if (state == Done)
  219. {
  220. put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
  221. dimensions.y + static_cast<signed>(dimensions.height), 3, 3},
  222. Graphics::Blue);
  223. }
  224. }