Skip to content

Commit

Permalink
Fix translation issue with object display names
Browse files Browse the repository at this point in the history
Previously, `ObjectFactory` saved copies of object display names on game startup, which would cause them to not be translatable, unless the game is restarted.
This is now fixed, since a factory function for getting the display name of an object is used instead.

The `m_other_display_names` member variable of `ObjectFactory` was removed as well. Its only use was to save the name of the node marker, since it is a part of the objects in the editor, while not being an actual object, but rather a tool.
It is planned to move the node marker to the toolbar in the future, so this variable would then be redundant.
  • Loading branch information
Vankata453 committed Jul 25, 2023
1 parent da38807 commit 02848c5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 37 deletions.
2 changes: 0 additions & 2 deletions src/object/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ class Path final
easing()
{}

static std::string display_name() { return _("Path Node"); }

Path& get_parent() const { return *parent; }
};

Expand Down
10 changes: 7 additions & 3 deletions src/supertux/game_object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ GameObjectFactory::init_factories()
add_factory<LevelTime>("leveltime");
add_factory<LitObject>("lit-object");
add_factory<MagicBlock>("magicblock");
add_display_name("#node", Path::Node::display_name());
add_factory<ParticleZone>("particle-zone");
add_factory<Platform>("platform");
add_factory<PneumaticPlatform>("pneumatic-platform");
Expand Down Expand Up @@ -309,10 +308,15 @@ GameObjectFactory::init_factories()
add_factory<worldmap::Teleporter>("teleporter", OBJ_PARAM_WORLDMAP);
add_factory<worldmap::SpawnPointObject>("worldmap-spawnpoint", OBJ_PARAM_WORLDMAP);

add_factory("tilemap", TileMap::display_name(), [](const ReaderMapping& reader) {
add_factory("tilemap", {
[](const ReaderMapping& reader) {
auto tileset = TileManager::current()->get_tileset(Level::current()->get_tileset());
return std::make_unique<TileMap>(tileset, reader);
});
},
[]() {
return TileMap::display_name();
}
});
}

std::unique_ptr<GameObject>
Expand Down
21 changes: 7 additions & 14 deletions src/supertux/object_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ ObjectFactory::ObjectFactory() :
m_badguys_names(),
m_badguys_params(),
m_objects_names(),
m_objects_display_names(),
m_objects_params(),
m_other_display_names(),
m_adding_badguys(false)
{
}
Expand All @@ -46,29 +44,24 @@ ObjectFactory::create(const std::string& name, const ReaderMapping& reader) cons
}
else
{
return it->second(reader);
return it->second.create(reader);
}
}

std::string
ObjectFactory::get_display_name(const std::string& name) const
{
auto it = std::find(m_objects_names.begin(), m_objects_names.end(), name);
auto it = factories.find(name);

if (it == m_objects_names.end())
if (it == factories.end())
{
auto it_other_names = m_other_display_names.find(name); // Attempt to find display name in non-factory object names.
if (it_other_names == m_other_display_names.end())
{
std::stringstream msg;
msg << "No display name for object '" << name << "' found.";
throw std::runtime_error(msg.str());
}
return it_other_names->second;
std::stringstream msg;
msg << "No factory for object '" << name << "' found. Unable to get display name.";
throw std::runtime_error(msg.str());
}
else
{
return m_objects_display_names[std::distance(m_objects_names.begin(), it)];
return it->second.get_display_name();
}
}

Expand Down
34 changes: 16 additions & 18 deletions src/supertux/object_factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,23 @@
#include "math/fwd.hpp"
#include "supertux/direction.hpp"

class ReaderMapping;
class GameObject;
class ReaderMapping;

class ObjectFactory
{
private:
typedef std::function<std::unique_ptr<GameObject> (const ReaderMapping&)> FactoryFunction;
typedef std::map<std::string, FactoryFunction> Factories;
struct FactoryFunctions {
std::function<std::unique_ptr<GameObject> (const ReaderMapping&)> create;
std::function<std::string ()> get_display_name;
};
typedef std::map<std::string, FactoryFunctions> Factories;

Factories factories;
std::vector<std::string> m_badguys_names;
std::vector<uint8_t> m_badguys_params;
std::vector<std::string> m_objects_names;
std::vector<std::string> m_objects_display_names;
std::vector<uint8_t> m_objects_params;
std::map<std::string, std::string> m_other_display_names; // Stores display names for non-factory objects.

protected:
bool m_adding_badguys;
Expand Down Expand Up @@ -71,13 +72,7 @@ class ObjectFactory
protected:
ObjectFactory();

void add_display_name(const char* class_name, const std::string& display_name)
{
assert(m_other_display_names.find(class_name) == m_other_display_names.end());
m_other_display_names[class_name] = display_name;
}

void add_factory(const char* name, const std::string& display_name, const FactoryFunction& func, uint8_t obj_params = 0)
void add_factory(const char* name, const FactoryFunctions functions, uint8_t obj_params = 0)
{
assert(factories.find(name) == factories.end());
if (m_adding_badguys)
Expand All @@ -86,17 +81,20 @@ class ObjectFactory
m_badguys_params.push_back(obj_params);
}
m_objects_names.push_back(name);
m_objects_display_names.push_back(display_name);
m_objects_params.push_back(obj_params);
factories[name] = func;
factories[name] = std::move(functions);
}

template<class C>
void add_factory(const char* class_name, uint8_t obj_params = 0, const std::string& display_name = "")
void add_factory(const char* class_name, uint8_t obj_params = 0)
{
add_factory(class_name, display_name.empty() ? C::display_name() : display_name,
[](const ReaderMapping& reader) {
return std::make_unique<C>(reader);
add_factory(class_name, {
[](const ReaderMapping& reader) {
return std::make_unique<C>(reader);
},
[]() {
return C::display_name();
}
}, obj_params);
}
};
Expand Down

0 comments on commit 02848c5

Please sign in to comment.