textscroller.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // 2018 Ingo Ruhnke <grumbel@gmail.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/textscroller.hpp"
  18. #include <optional>
  19. #include <sexp/value.hpp>
  20. #include "control/input_manager.hpp"
  21. #include "editor/editor.hpp"
  22. #include "supertux/globals.hpp"
  23. #include "supertux/fadetoblack.hpp"
  24. #include "supertux/info_box_line.hpp"
  25. #include "supertux/screen_manager.hpp"
  26. #include "supertux/sector.hpp"
  27. #include "util/log.hpp"
  28. #include "util/reader.hpp"
  29. #include "util/reader_collection.hpp"
  30. #include "util/reader_document.hpp"
  31. #include "util/reader_mapping.hpp"
  32. #include "video/drawing_context.hpp"
  33. #include "video/video_system.hpp"
  34. #include "video/viewport.hpp"
  35. namespace {
  36. const float LEFT_BORDER = 0;
  37. const float DEFAULT_SPEED = 60;
  38. const float SCROLL_JUMP = 60;
  39. } // namespace
  40. TextScroller::TextScroller(const ReaderMapping& mapping) :
  41. LayerObject(mapping),
  42. controller(&InputManager::current()->get_controller()),
  43. m_filename(),
  44. m_finish_script(),
  45. m_lines(),
  46. m_scroll(),
  47. m_default_speed(DEFAULT_SPEED),
  48. m_x_offset(),
  49. m_controllable(true),
  50. m_finished(),
  51. m_fading(),
  52. m_x_anchor(XAnchor::SCROLLER_ANCHOR_CENTER),
  53. m_text_align(TextAlign::SCROLLER_ALIGN_CENTER)
  54. {
  55. mapping.get("file", m_filename);
  56. if (!Editor::is_active())
  57. {
  58. if (m_filename.empty())
  59. throw std::runtime_error("Cannot load text scroller: No file specified!");
  60. parse_file(m_filename);
  61. }
  62. mapping.get("finish-script", m_finish_script, "");
  63. mapping.get("speed", m_default_speed);
  64. mapping.get("x-offset", m_x_offset);
  65. mapping.get("controllable", m_controllable, true);
  66. std::string x_anchor_str;
  67. if (mapping.get("x-anchor", x_anchor_str))
  68. {
  69. if (x_anchor_str == "left")
  70. m_x_anchor = XAnchor::SCROLLER_ANCHOR_LEFT;
  71. else if (x_anchor_str == "right")
  72. m_x_anchor = XAnchor::SCROLLER_ANCHOR_RIGHT;
  73. else
  74. m_x_anchor = XAnchor::SCROLLER_ANCHOR_CENTER;
  75. }
  76. std::string text_align_str;
  77. if (mapping.get("text-align", text_align_str))
  78. {
  79. if (text_align_str == "left")
  80. m_text_align = TextAlign::SCROLLER_ALIGN_LEFT;
  81. else if (text_align_str == "right")
  82. m_text_align = TextAlign::SCROLLER_ALIGN_RIGHT;
  83. else
  84. m_text_align = TextAlign::SCROLLER_ALIGN_CENTER;
  85. }
  86. }
  87. TextScroller::TextScroller(const ReaderObject& root) :
  88. controller(&InputManager::current()->get_controller()),
  89. m_filename(),
  90. m_finish_script(),
  91. m_lines(),
  92. m_scroll(),
  93. m_default_speed(DEFAULT_SPEED),
  94. m_x_offset(),
  95. m_controllable(true),
  96. m_finished(),
  97. m_fading(),
  98. m_x_anchor(XAnchor::SCROLLER_ANCHOR_CENTER),
  99. m_text_align(TextAlign::SCROLLER_ALIGN_CENTER)
  100. {
  101. parse_root(root);
  102. }
  103. void
  104. TextScroller::parse_file(const std::string& filename)
  105. {
  106. register_translation_directory(filename);
  107. auto doc = ReaderDocument::from_file(filename);
  108. auto root = doc.get_root();
  109. parse_root(root);
  110. }
  111. void
  112. TextScroller::parse_root(const ReaderObject& root)
  113. {
  114. if (root.get_name() != "supertux-text")
  115. {
  116. throw std::runtime_error("File isn't a supertux-text file");
  117. }
  118. else
  119. {
  120. auto mapping = root.get_mapping();
  121. int version = 1;
  122. mapping.get("version", version);
  123. if (version == 1)
  124. {
  125. log_info << "[" << mapping.get_doc().get_filename() << "] Text uses old format: version 1" << std::endl;
  126. std::string text;
  127. if (!mapping.get("text", text)) {
  128. throw std::runtime_error("File doesn't contain a text field");
  129. }
  130. // Split text string lines into a vector
  131. m_lines = InfoBoxLine::split(text, static_cast<float>(SCREEN_WIDTH) - 2.0f * LEFT_BORDER);
  132. }
  133. else if (version == 2)
  134. {
  135. std::optional<ReaderCollection> content_collection;
  136. if (!mapping.get("content", content_collection)) {
  137. throw std::runtime_error("File doesn't contain content");
  138. } else {
  139. parse_content(*content_collection);
  140. }
  141. }
  142. }
  143. }
  144. void
  145. TextScroller::parse_content(const ReaderCollection& collection)
  146. {
  147. for (const auto& item : collection.get_objects())
  148. {
  149. if (item.get_name() == "image")
  150. {
  151. std::string image_file = item.get_sexp().as_array()[1].as_string();
  152. m_lines.emplace_back(new InfoBoxLine('!', image_file));
  153. }
  154. else if (item.get_name() == "person")
  155. {
  156. bool simple;
  157. std::string name, info, image_file;
  158. item.get_mapping().get("simple", simple, false);
  159. if (simple) {
  160. if (!item.get_mapping().get("name", name) || !item.get_mapping().get("info", info)) {
  161. throw std::runtime_error("Simple entry requires both name and info specified");
  162. }
  163. if (item.get_mapping().get("image", image_file)) {
  164. log_warning << "[" << collection.get_doc().get_filename() << "] Simple person entry shouldn't specify images" << std::endl;
  165. }
  166. m_lines.emplace_back(new InfoBoxLine(' ', name + " (" + info + ")")); // NOLINT
  167. } else {
  168. if (item.get_mapping().get("name", name)) {
  169. m_lines.emplace_back(new InfoBoxLine('\t', name));
  170. }
  171. if (item.get_mapping().get("image", image_file)) {
  172. m_lines.emplace_back(new InfoBoxLine('!', image_file));
  173. }
  174. if (item.get_mapping().get("info", info)) {
  175. m_lines.emplace_back(new InfoBoxLine(' ', info));
  176. }
  177. }
  178. }
  179. else if (item.get_name() == "blank")
  180. {
  181. // Empty line
  182. m_lines.emplace_back(new InfoBoxLine('\t', ""));
  183. }
  184. else if (item.get_name() == "text")
  185. {
  186. std::string type, string;
  187. if (!item.get_mapping().get("type", type)) {
  188. type = "normal";
  189. }
  190. if (!item.get_mapping().get("string", string)) {
  191. throw std::runtime_error("Text entry requires a string");
  192. }
  193. if (type == "normal")
  194. m_lines.emplace_back(new InfoBoxLine('\t', string));
  195. else if (type == "normal-left")
  196. m_lines.emplace_back(new InfoBoxLine('#', string));
  197. else if (type == "small")
  198. m_lines.emplace_back(new InfoBoxLine(' ', string));
  199. else if (type == "heading")
  200. m_lines.emplace_back(new InfoBoxLine('-', string));
  201. else if (type == "reference")
  202. m_lines.emplace_back(new InfoBoxLine('*', string));
  203. else {
  204. log_warning << "[" << item.get_doc().get_filename() << "] Unknown text type '" << type << "'" << std::endl;
  205. m_lines.emplace_back(new InfoBoxLine('\t', string));
  206. }
  207. }
  208. else
  209. {
  210. log_warning << "[" << item.get_doc().get_filename() << "] Unknown token '" << item.get_name() << "'" << std::endl;
  211. }
  212. }
  213. }
  214. void
  215. TextScroller::draw(DrawingContext& context)
  216. {
  217. context.push_transform();
  218. context.set_translation(Vector(0, 0));
  219. context.transform().scale = 1.f;
  220. const float ctx_w = context.get_width();
  221. const float ctx_h = context.get_height();
  222. float y = floorf(ctx_h - m_scroll);
  223. { // draw text
  224. for (const auto& line : m_lines)
  225. {
  226. if (y + line->get_height() >= 0 && ctx_h - y >= 0) {
  227. line->draw(context, Rectf(LEFT_BORDER, y, ctx_w * (m_x_anchor == XAnchor::SCROLLER_ANCHOR_LEFT ? 0.f :
  228. m_x_anchor == XAnchor::SCROLLER_ANCHOR_RIGHT ? 2.f : 1.f) + m_x_offset, y), LAYER_GUI,
  229. (m_text_align == TextAlign::SCROLLER_ALIGN_LEFT ? line->LineAlignment::LEFT :
  230. m_text_align == TextAlign::SCROLLER_ALIGN_RIGHT ? line->LineAlignment::RIGHT :
  231. line->LineAlignment::CENTER));
  232. }
  233. y += floorf(line->get_height());
  234. }
  235. }
  236. context.pop_transform();
  237. // close when done
  238. if (y < 0)
  239. {
  240. m_finished = true;
  241. set_default_speed(0.f);
  242. }
  243. }
  244. void
  245. TextScroller::update(float dt_sec)
  246. {
  247. float speed = m_default_speed;
  248. if (controller && m_controllable) {
  249. // allow changing speed with up and down keys
  250. if (controller->hold(Control::UP)) {
  251. speed = -m_default_speed * 5;
  252. } else if (controller->hold(Control::DOWN)) {
  253. speed = m_default_speed * 5;
  254. }
  255. // allow jumping ahead with certain keys
  256. if (controller->pressed_any(Control::JUMP, Control::ACTION, Control::MENU_SELECT) &&
  257. !(controller->pressed(Control::UP))) { // prevent skipping if jump with up is enabled
  258. scroll(SCROLL_JUMP);
  259. }
  260. // use start or escape keys to exit
  261. if (controller->pressed_any(Control::START, Control::ESCAPE) &&
  262. !m_fading && m_finish_script.empty()) {
  263. m_fading = true;
  264. ScreenManager::current()->pop_screen(std::make_unique<FadeToBlack>(FadeToBlack::FADEOUT, 0.5f));
  265. return;
  266. }
  267. }
  268. m_scroll += speed * dt_sec;
  269. if (m_scroll < 0)
  270. m_scroll = 0;
  271. if (!m_finish_script.empty())
  272. {
  273. Sector::get().run_script(m_finish_script, "finishscript");
  274. }
  275. else
  276. {
  277. // close when done
  278. if (m_finished && !m_fading)
  279. {
  280. m_fading = true;
  281. ScreenManager::current()->pop_screen(std::unique_ptr<ScreenFade>(new FadeToBlack(FadeToBlack::FADEOUT, 0.25f)));
  282. }
  283. }
  284. }
  285. void
  286. TextScroller::scroll(float offset)
  287. {
  288. m_scroll += offset;
  289. if (m_scroll < 0.0f)
  290. {
  291. m_scroll = 0.0f;
  292. }
  293. }
  294. ObjectSettings
  295. TextScroller::get_settings()
  296. {
  297. ObjectSettings result = GameObject::get_settings();
  298. result.add_file(_("File"), &m_filename, "file");
  299. result.add_script(_("Finish Script"), &m_finish_script, "finish-script");
  300. result.add_float(_("Speed"), &m_default_speed, "speed", DEFAULT_SPEED);
  301. result.add_float(_("X-offset"), &m_x_offset, "x-offset");
  302. result.add_bool(_("Controllable"), &m_controllable, "controllable", true);
  303. result.add_enum(_("Anchor"), reinterpret_cast<int*>(&m_x_anchor),
  304. { _("Left"), _("Center"), _("Right") },
  305. { "left", "center", "right" },
  306. static_cast<int>(XAnchor::SCROLLER_ANCHOR_CENTER), "x-anchor");
  307. result.add_enum(_("Text Alignment"), reinterpret_cast<int*>(&m_text_align),
  308. { _("Left"), _("Center"), _("Right") },
  309. { "left", "center", "right" },
  310. static_cast<int>(TextAlign::SCROLLER_ALIGN_CENTER), "text-align");
  311. result.add_remove();
  312. return result;
  313. }
  314. /* EOF */