Skip to content

Commit a2e1e3f

Browse files
SemphrissSemphris
andauthored
Refactored TextArea (#1)
- Adds a Status object, to have a consistent and reliable way of checking the TextArea's progression - Removes three redundant booleans: - m_started can be checked with the state - m_inside is not needed because the player cannot re-enter the zone without leaving it first (and in any case, if the game bugs and fails to detect Tux leaving the zone, it should work anyways when it re-enters the zone again) - m_finished was redundant with m_started and, like it, was superseded by the status enum - Condenses two timers into one, since only one needs to be running at a time - Improves general code quality (Spacing, variable names, etc.) Co-authored-by: Semphris <[email protected]>
1 parent 4716e51 commit a2e1e3f

File tree

2 files changed

+84
-50
lines changed

2 files changed

+84
-50
lines changed

src/trigger/text_area.cpp

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,16 @@
2626

2727
TextArea::TextArea(const ReaderMapping& mapping) :
2828
TriggerBase(mapping),
29-
m_started(false),
30-
m_inside(false),
3129
m_once(false),
32-
m_finished(false),
3330
m_items(),
3431
m_delay(4.0f),
3532
m_fade_delay(1.0f),
36-
m_text_id(0),
37-
m_update_timer(),
38-
m_fade_timer()
33+
m_current_text(0),
34+
m_status(Status::NOT_STARTED),
35+
m_timer()
3936
{
4037
float w, h;
38+
4139
mapping.get("x", m_col.m_bbox.get_left(), 0.0f);
4240
mapping.get("y", m_col.m_bbox.get_top(), 0.0f);
4341
mapping.get("width", w, 32.0f);
@@ -46,29 +44,27 @@ TextArea::TextArea(const ReaderMapping& mapping) :
4644
mapping.get("delay", m_delay);
4745
mapping.get("once", m_once);
4846
mapping.get("fade-delay", m_fade_delay);
47+
4948
m_col.m_bbox.set_size(w, h);
5049
}
5150

5251
TextArea::TextArea(const Vector& pos) :
53-
m_started(false),
54-
m_inside(false),
5552
m_once(false),
56-
m_finished(false),
5753
m_items(),
5854
m_delay(4.0f),
5955
m_fade_delay(1.0f),
60-
m_text_id(0),
61-
m_update_timer(),
62-
m_fade_timer()
56+
m_current_text(0),
57+
m_status(Status::NOT_STARTED),
58+
m_timer()
6359
{
6460
m_col.m_bbox.set_pos(pos);
65-
m_col.m_bbox.set_size(32,32);
61+
m_col.m_bbox.set_size(32, 32);
6662
}
6763

6864
void
6965
TextArea::draw(DrawingContext& context)
7066
{
71-
if(Editor::is_active())
67+
if (Editor::is_active())
7268
context.color().draw_filled_rect(m_col.m_bbox, Color(1.0f, 1.0f, 1.0f, 0.6f), LAYER_OBJECTS);
7369
}
7470

@@ -77,60 +73,84 @@ TextArea::event(Player& player, EventType type)
7773
{
7874
switch (type)
7975
{
80-
case EVENT_TOUCH:
81-
if (!m_started && !m_fade_timer.started() && m_items.size() > 0 && !m_inside && (!m_once || !m_finished))
82-
{
83-
m_update_timer.start(m_delay + m_fade_delay * 2, true);
84-
m_started = true;
85-
m_text_id = 0;
86-
}
87-
m_inside = true;
88-
break;
89-
case EVENT_LOSETOUCH:
90-
m_inside = false;
91-
break;
92-
default:
93-
break;
76+
case EVENT_TOUCH:
77+
if (m_status == Status::NOT_STARTED)
78+
{
79+
if (m_items.size() < 1)
80+
{
81+
log_warning << "Attempt to run a TextArea with no text, aborting" << std::endl;
82+
return;
83+
}
84+
85+
TextObject& text_object = Sector::get().get_singleton_by_type<TextObject>();
86+
87+
m_current_text = 0;
88+
m_status = Status::FADING_IN;
89+
m_timer.start(m_fade_delay);
90+
text_object.set_text(m_items[m_current_text]);
91+
text_object.fade_in(m_fade_delay);
92+
}
93+
break;
94+
95+
default:
96+
break;
9497
}
9598
}
9699

97100
void
98101
TextArea::update(float dt_sec)
99102
{
100103
TriggerBase::update(dt_sec);
101-
if (m_started)
104+
105+
if (m_timer.check())
102106
{
103107
TextObject& text_object = Sector::get().get_singleton_by_type<TextObject>();
104-
if (m_text_id < m_items.size() && (m_update_timer.check() || m_text_id == 0) && !m_fade_timer.started())
105-
{
106-
m_fade_timer.start(m_delay + m_fade_delay);
107-
text_object.set_text(m_items[m_text_id]);
108-
text_object.fade_in(m_fade_delay);
109-
m_text_id++;
110-
}
111-
else if (m_text_id >= m_items.size())
108+
109+
switch(m_status)
112110
{
113-
m_started = false;
114-
m_update_timer.stop();
115-
m_fade_timer.start(m_delay + m_fade_delay);
116-
m_finished = true;
111+
case Status::FADING_IN:
112+
m_status = Status::WAITING;
113+
m_timer.start(m_delay);
114+
break;
115+
116+
case Status::WAITING:
117+
m_status = Status::FADING_OUT;
118+
m_timer.start(m_fade_delay);
119+
text_object.fade_out(m_fade_delay);
120+
break;
121+
122+
case Status::FADING_OUT:
123+
if (++m_current_text >= m_items.size())
124+
{
125+
m_current_text = 0;
126+
m_status = m_once ? Status::FINISHED : Status::NOT_STARTED;
127+
}
128+
else
129+
{
130+
m_status = Status::FADING_IN;
131+
m_timer.start(m_fade_delay);
132+
text_object.set_text(m_items[m_current_text]);
133+
text_object.fade_in(m_fade_delay);
134+
}
135+
break;
136+
137+
default:
138+
break;
117139
}
118140
}
119-
if (m_fade_timer.check())
120-
{
121-
Sector::get().get_singleton_by_type<TextObject>().fade_out(m_fade_delay);
122-
m_fade_timer.stop();
123-
}
124141
}
125142

126143
ObjectSettings
127144
TextArea::get_settings()
128145
{
129146
ObjectSettings settings = TriggerBase::get_settings();
147+
130148
settings.add_bool(_("Once"), &m_once, "once");
131149
settings.add_float(_("Text change time"), &m_delay, "delay");
132150
settings.add_float(_("Fade time"), &m_fade_delay, "fade-delay");
133151
settings.add_string_array(_("Texts"), "texts", m_items);
152+
134153
return settings;
135154
}
155+
136156
/* EOF */

src/trigger/text_area.hpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@
2222

2323
class TextArea final : public TriggerBase
2424
{
25+
private:
26+
enum class Status
27+
{
28+
NOT_STARTED,
29+
FADING_IN,
30+
WAITING,
31+
FADING_OUT,
32+
FINISHED
33+
};
34+
2535
public:
2636
TextArea(const ReaderMapping& mapping);
2737
TextArea(const Vector& pos);
@@ -34,12 +44,16 @@ class TextArea final : public TriggerBase
3444
virtual std::string get_class() const override { return "text-area"; }
3545
virtual std::string get_display_name() const override { return _("Text Area"); }
3646
virtual bool has_variable_size() const override { return true; }
47+
3748
private:
38-
bool m_started, m_inside, m_once, m_finished;
49+
bool m_once;
3950
std::vector<std::string> m_items;
40-
float m_delay, m_fade_delay;
41-
unsigned int m_text_id;
42-
Timer m_update_timer, m_fade_timer;
51+
float m_delay;
52+
float m_fade_delay;
53+
size_t m_current_text;
54+
Status m_status;
55+
Timer m_timer;
56+
4357
private:
4458
TextArea(const TextArea&) = delete;
4559
TextArea& operator=(const TextArea&) = delete;

0 commit comments

Comments
 (0)