Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reference counter for PathObjects #1931

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions src/badguy/ghoul.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,6 @@ Ghoul::finish_construction()
}
}

void
Ghoul::editor_delete()
{
auto path_obj = get_path_gameobject();
if(path_obj != nullptr)
{
path_obj->editor_delete();
}
GameObject::editor_delete();
}

void
Ghoul::activate()
{
Expand Down
1 change: 0 additions & 1 deletion src/badguy/ghoul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Ghoul final : public BadGuy,
bool is_flammable() const override;

void finish_construction() override;
void editor_delete() override;

void activate() override;
void deactivate() override;
Expand Down
11 changes: 0 additions & 11 deletions src/badguy/willowisp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,6 @@ WillOWisp::after_editor_set()
m_sprite->set_color(m_color);
}

void
WillOWisp::editor_delete()
{
auto path_obj = get_path_gameobject();
if(path_obj != nullptr)
{
path_obj->editor_delete();
}
GameObject::editor_delete();
}

void
WillOWisp::active_update(float dt_sec)
{
Expand Down
1 change: 0 additions & 1 deletion src/badguy/willowisp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class WillOWisp final :

virtual void finish_construction() override;
virtual void after_editor_set() override;
virtual void editor_delete() override;

virtual void activate() override;
virtual void deactivate() override;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/menu_paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ auto on_select = [](std::string path, PathObject& target, std::string path_ref)
auto* into = Editor::current()->get_sector()->get_object_by_name<PathGameObject>(path_ref);
if (from && into) {
from->copy_into(*into);
MenuManager::instance().pop_menu();
} else {
log_warning << "Could not copy path, misses " << (from ? "" : "'from'")
<< (into ? "" : "'into'") << std::endl;
Expand All @@ -40,6 +41,7 @@ auto on_select = [](std::string path, PathObject& target, std::string path_ref)
});
dialog->add_button(_("Bind"), [path, &target] {
target.editor_set_path_by_ref(path);
MenuManager::instance().pop_menu();
});
dialog->add_cancel_button(_("Cancel"));
dialog->set_text("Do you wish to clone the path to edit it separately,\nor do you want to bind both paths together\nso that any edit on one edits the other?");
Expand Down
15 changes: 0 additions & 15 deletions src/object/coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,6 @@ Coin::editor_update()
}
}

void
Coin::editor_delete()
{
// Removed since paths can be shared by multiple objects
// TODO: Handle reference counting for paths
#if 0
auto path_obj = get_path_gameobject();
if(path_obj != nullptr)
{
path_obj->editor_delete();
}
#endif
GameObject::editor_delete();
}

void
Coin::collect()
{
Expand Down
1 change: 0 additions & 1 deletion src/object/coin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ friend class HeavyCoin;
virtual ObjectSettings get_settings() override;
virtual void after_editor_set() override;
virtual void editor_update() override;
virtual void editor_delete() override;

virtual void move_to(const Vector& pos) override;

Expand Down
45 changes: 44 additions & 1 deletion src/object/path_gameobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@

#include <boost/optional.hpp>

#include "editor/node_marker.hpp"
#include "gui/menu_manager.hpp"
#include "object/path.hpp"
#include "object/path_object.hpp"
#include "sprite/sprite.hpp"
#include "sprite/sprite_manager.hpp"
#include "supertux/debug.hpp"
#include "supertux/sector.hpp"
#include "util/log.hpp"
#include "util/reader_mapping.hpp"
#include "util/unique_name.hpp"
Expand Down Expand Up @@ -102,7 +106,7 @@ PathGameObject::~PathGameObject()
void
PathGameObject::update(float dt_sec)
{
// nothing to do
check_references();
}

void
Expand Down Expand Up @@ -175,6 +179,12 @@ PathGameObject::get_settings()
return result;
}

void
PathGameObject::editor_update()
{
check_references();
}

void
PathGameObject::editor_select()
{
Expand All @@ -187,10 +197,43 @@ PathGameObject::editor_deselect()
log_fatal << "PathGameObject::deselected" << std::endl;
}

void
PathGameObject::remove_me()
{
if (Sector::current())
{
auto handles = Sector::get().get_objects_by_type<NodeMarker>();

for (auto& handle : handles)
handle.remove_me(); // Removing a node handle also removes its bezier handles
}

GameObject::remove_me();
}

void
PathGameObject::copy_into(PathGameObject& other)
{
other.get_path().m_nodes = get_path().m_nodes;
}

void
PathGameObject::check_references()
{
if (!Sector::current())
return;

// Object settings menu might hold references to paths
if (MenuManager::instance().is_active())
return;

const auto& path_objects = Sector::get().get_objects_by_type<PathObject>();

for (const auto& path_obj : path_objects)
if (path_obj.get_path_gameobject() == this)
return;

remove_me();
}

/* EOF */
7 changes: 7 additions & 0 deletions src/object/path_gameobject.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,22 @@ class PathGameObject : public GameObject
return "images/engine/editor/path.png";
}

virtual void editor_update() override;
virtual void editor_select() override;
virtual void editor_deselect() override;

virtual void remove_me() override;

virtual ObjectSettings get_settings() override;

Path& get_path() { return *m_path; }

void copy_into(PathGameObject& other);

private:
/** Removes the object if the path is not referenced anywhere */
void check_references();

private:
std::unique_ptr<Path> m_path;
PathStyle m_style;
Expand Down
11 changes: 0 additions & 11 deletions src/object/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,6 @@ Platform::editor_update()
set_pos(get_path()->get_nodes()[m_starting_node].position);
}

void
Platform::editor_delete()
{
auto path_obj = get_path_gameobject();
if(path_obj != nullptr)
{
path_obj->editor_delete();
}
GameObject::editor_delete();
}

void
Platform::goto_node(int node_no)
{
Expand Down
1 change: 0 additions & 1 deletion src/object/platform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class Platform : public MovingSprite,
virtual std::string get_display_name() const override { return _("Platform"); }

virtual void editor_update() override;
virtual void editor_delete() override;

const Vector& get_speed() const { return m_speed; }

Expand Down
14 changes: 0 additions & 14 deletions src/object/tilemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,6 @@ TileMap::editor_update()
}
}

void
TileMap::editor_delete()
{
// Paths may be used by multiple objects
#if 0
auto path_obj = get_path_gameobject();
if(path_obj != nullptr)
{
path_obj->editor_delete();
}
#endif
GameObject::editor_delete();
}

void
TileMap::draw(DrawingContext& context)
{
Expand Down
1 change: 0 additions & 1 deletion src/object/tilemap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class TileMap final :

virtual ObjectSettings get_settings() override;
virtual void after_editor_set() override;
virtual void editor_delete() override;

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