text_area.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SuperTux
  2. // Copyright (C) 2021 mrkubax10 <mrkubax10@onet.pl>
  3. // 2021 A. Semphris <semphris@protonmail.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 "trigger/text_area.hpp"
  18. #include "editor/editor.hpp"
  19. #include "math/anchor_point.hpp"
  20. #include "object/player.hpp"
  21. #include "object/text_object.hpp"
  22. #include "supertux/sector.hpp"
  23. #include "util/reader_mapping.hpp"
  24. #include "video/drawing_context.hpp"
  25. #include "video/layer.hpp"
  26. TextArea::TextArea(const ReaderMapping& mapping) :
  27. Trigger(mapping),
  28. m_once(false),
  29. m_items(),
  30. m_delay(4.0f),
  31. m_fade_delay(1.0f),
  32. m_current_text(0),
  33. m_status(Status::NOT_STARTED),
  34. m_timer(),
  35. m_anchor(AnchorPoint::ANCHOR_MIDDLE),
  36. m_anchor_offset(0, 0)
  37. {
  38. mapping.get("strings", m_items);
  39. mapping.get("delay", m_delay);
  40. mapping.get("once", m_once);
  41. mapping.get("fade-delay", m_fade_delay);
  42. mapping.get("anchor-offset-x", m_anchor_offset.x);
  43. mapping.get("anchor-offset-y", m_anchor_offset.y);
  44. std::string anchor;
  45. if (mapping.get("anchor-point", anchor))
  46. m_anchor = string_to_anchor_point(anchor);
  47. }
  48. void
  49. TextArea::draw(DrawingContext& context)
  50. {
  51. if (Editor::is_active())
  52. context.color().draw_filled_rect(m_col.m_bbox, Color(1.0f, 1.0f, 1.0f, 0.6f), LAYER_OBJECTS);
  53. }
  54. void
  55. TextArea::event(Player& player, EventType type)
  56. {
  57. switch (type)
  58. {
  59. case EVENT_TOUCH:
  60. if (m_status == Status::NOT_STARTED)
  61. {
  62. if (m_items.size() < 1)
  63. {
  64. log_warning << "Attempt to run a TextArea with no text, aborting" << std::endl;
  65. return;
  66. }
  67. TextObject& text_object = Sector::get().get_text_object();
  68. m_current_text = 0;
  69. m_status = Status::FADING_IN;
  70. m_timer.start(m_fade_delay);
  71. text_object.set_anchor_point(m_anchor);
  72. text_object.set_anchor_offset(m_anchor_offset);
  73. text_object.set_text(m_items[m_current_text]);
  74. text_object.fade_in(m_fade_delay);
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. void
  82. TextArea::update(float dt_sec)
  83. {
  84. Trigger::update(dt_sec);
  85. if (m_timer.check())
  86. {
  87. TextObject& text_object = Sector::get().get_text_object();
  88. switch(m_status)
  89. {
  90. case Status::FADING_IN:
  91. m_status = Status::WAITING;
  92. m_timer.start(m_delay);
  93. break;
  94. case Status::WAITING:
  95. m_status = Status::FADING_OUT;
  96. m_timer.start(m_fade_delay);
  97. text_object.fade_out(m_fade_delay);
  98. break;
  99. case Status::FADING_OUT:
  100. if (++m_current_text >= m_items.size())
  101. {
  102. m_current_text = 0;
  103. m_status = m_once ? Status::FINISHED : Status::NOT_STARTED;
  104. }
  105. else
  106. {
  107. m_status = Status::FADING_IN;
  108. m_timer.start(m_fade_delay);
  109. text_object.set_anchor_point(m_anchor);
  110. text_object.set_anchor_offset(m_anchor_offset);
  111. text_object.set_text(m_items[m_current_text]);
  112. text_object.fade_in(m_fade_delay);
  113. }
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. }
  120. ObjectSettings
  121. TextArea::get_settings()
  122. {
  123. ObjectSettings settings = Trigger::get_settings();
  124. settings.add_bool(_("Once"), &m_once, "once");
  125. settings.add_float(_("Text change time"), &m_delay, "delay");
  126. settings.add_float(_("Fade time"), &m_fade_delay, "fade-delay");
  127. settings.add_enum(_("Anchor"), reinterpret_cast<int*>(&m_anchor),
  128. get_anchor_names(), g_anchor_keys,
  129. static_cast<int>(AnchorPoint::ANCHOR_MIDDLE),
  130. "anchor-point");
  131. settings.add_float(_("Anchor offset X"), &m_anchor_offset.x, "anchor-offset-x");
  132. settings.add_float(_("Anchor offset Y"), &m_anchor_offset.y, "anchor-offset-y");
  133. settings.add_string_array(_("Texts"), "texts", m_items);
  134. return settings;
  135. }
  136. /* EOF */