Skip to content

Commit

Permalink
Instantiable Text and TextArray objects (#2722)
Browse files Browse the repository at this point in the history
`TextObject` and `TextArrayObject` can now be instantiated from scripting. This allows for more possibilities in scripting texts.

The former `Text` and `TextArray` instances, which are always added to a `Sector`, have been retained to allow for compatibility. The default `Text` object is used by `TextArea` triggers.

Additionally, a function to get/set the text wrap width of a `Text` or `TextArray` text object is added.
  • Loading branch information
Vankata453 authored Jan 7, 2024
1 parent 240f1b9 commit 50743fb
Show file tree
Hide file tree
Showing 15 changed files with 524 additions and 275 deletions.
17 changes: 1 addition & 16 deletions src/object/text_array_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "control/input_manager.hpp"

TextArrayObject::TextArrayObject(const std::string& name) :
ExposedObject<TextArrayObject, scripting::TextArray>(this),
ExposedObject<TextArrayObject, scripting::TextArrayObject>(this),
m_isDone(false),
m_isAuto(false),
m_keepVisible(false),
Expand All @@ -32,21 +32,6 @@ TextArrayObject::TextArrayObject(const std::string& name) :
m_name = name;
}

TextArrayObject::TextArrayObject(const ReaderMapping& reader) :
GameObject(reader),
ExposedObject<TextArrayObject, scripting::TextArray>(this),
m_isDone(false),
m_isAuto(false),
m_keepVisible(false),
m_fadeTransition(true),
m_fadetime(1.0),
m_texts(),
m_curTextIndex(0),
m_lastTextIndex(0),
m_waiting()
{
}

void
TextArrayObject::clear()
{
Expand Down
10 changes: 3 additions & 7 deletions src/object/text_array_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,26 @@
#include <memory>

#include "squirrel/exposed_object.hpp"
#include "scripting/text_array.hpp"

#include "supertux/game_object.hpp"
#include "supertux/timer.hpp"

#include "scripting/text_array_object.hpp"
#include "supertux/timer.hpp"
#include "object/text_object.hpp"
#include "object/text_array_item.hpp"

typedef size_t ta_index;

/** A text array object intended for narration */
class TextArrayObject final : public GameObject,
public ExposedObject<TextArrayObject, scripting::TextArray>
public ExposedObject<TextArrayObject, scripting::TextArrayObject>
{
public:
TextArrayObject(const std::string& name = "");
TextArrayObject(const ReaderMapping& reader);

~TextArrayObject() override = default;

virtual void draw(DrawingContext& context) override;
virtual void update(float dt_sec) override;

virtual bool is_singleton() const override { return true; }
virtual bool is_saveable() const override { return false; }

static std::string class_name() { return "text-array"; }
Expand Down
5 changes: 3 additions & 2 deletions src/object/text_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

TextObject::TextObject(const std::string& name) :
GameObject(name),
ExposedObject<TextObject, scripting::Text>(this),
ExposedObject<TextObject, scripting::TextObject>(this),
m_font(Resources::normal_font),
m_text(),
m_wrapped_text(),
Expand All @@ -37,6 +37,7 @@ TextObject::TextObject(const std::string& name) :
m_anchor(ANCHOR_MIDDLE),
m_anchor_offset(0, 0),
m_pos(0, 0),
m_wrap_width(500.f),
m_front_fill_color(0.6f, 0.7f, 0.8f, 0.5f),
m_back_fill_color(0.2f, 0.3f, 0.4f, 0.8f),
m_text_color(1.f, 1.f, 1.f, 1.f),
Expand Down Expand Up @@ -95,7 +96,7 @@ TextObject::wrap_text()

do {
std::string overflow;
m_wrapped_text += m_font->wrap_to_width(rest, 500, &overflow);
m_wrapped_text += m_font->wrap_to_width(rest, m_wrap_width, &overflow);
if (!overflow.empty()) {
m_wrapped_text += "\n";
}
Expand Down
14 changes: 8 additions & 6 deletions src/object/text_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
#ifndef HEADER_SUPERTUX_OBJECT_TEXT_OBJECT_HPP
#define HEADER_SUPERTUX_OBJECT_TEXT_OBJECT_HPP

#include "math/anchor_point.hpp"
#include "scripting/text.hpp"
#include "squirrel/exposed_object.hpp"
#include "supertux/game_object.hpp"

#include "math/anchor_point.hpp"
#include "scripting/text_object.hpp"
#include "video/color.hpp"
#include "video/drawing_context.hpp"
#include "video/font_ptr.hpp"
Expand All @@ -30,12 +31,11 @@ class ReaderMapping;

/** A text object intended for scripts that want to tell a story */
class TextObject final : public GameObject,
public ExposedObject<TextObject, scripting::Text>
public ExposedObject<TextObject, scripting::TextObject>
{
static Color default_color;

public:
TextObject(const ReaderMapping& reader);
TextObject(const std::string& name = "");
~TextObject() override;

Expand All @@ -48,10 +48,8 @@ class TextObject final : public GameObject,

virtual void draw(DrawingContext& context) override;
virtual void update(float dt_sec) override;
virtual bool is_singleton() const override { return true; }
virtual bool is_saveable() const override { return false; }


void set_text(const std::string& text);
void set_font(const std::string& name);
void grow_in(float fadetime);
Expand All @@ -70,6 +68,9 @@ class TextObject final : public GameObject,
AnchorPoint get_anchor_point() const { return m_anchor; }
void set_anchor_offset(const Vector& offset) { m_anchor_offset = offset; }

float get_wrap_width() const { return m_wrap_width; }
void set_wrap_width(float width) { m_wrap_width = width; }

void set_pos(const Vector& pos) { m_pos = pos; }
const Vector& get_pos() const { return m_pos; }

Expand All @@ -87,6 +88,7 @@ class TextObject final : public GameObject,
AnchorPoint m_anchor;
Vector m_anchor_offset;
Vector m_pos;
float m_wrap_width;
Color m_front_fill_color;
Color m_back_fill_color;
Color m_text_color;
Expand Down
Loading

0 comments on commit 50743fb

Please sign in to comment.