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

Add TileProperties, generalize EditorPropertyResource #23864

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 31 additions & 0 deletions editor/plugins/tile_set_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3424,6 +3424,13 @@ bool TilesetEditorContext::_set(const StringName &p_name, const Variant &p_value
}
}
return false;
} else if (name == "tileset_meta") {
tileset->set_meta_properties(p_value);
return true;
} else if (name.left(8) == "tileset_") {
bool v = false;
tileset->set(name.right(8), p_value, &v);
return v;
}

tileset_editor->err_dialog->set_text(TTR("This property can't be changed."));
Expand Down Expand Up @@ -3494,6 +3501,11 @@ bool TilesetEditorContext::_get(const StringName &p_name, Variant &r_ret) const
} else if (name == "tileset_script") {
r_ret = tileset->get_script();
v = true;
} else if (name == "tileset_meta") {
r_ret = tileset->get_meta_properties();
v = true;
} else if (name.left(8) == "tileset_") {
r_ret = tileset->get(name.substr(8, name.length()), &v);
}
return v;
}
Expand All @@ -3505,6 +3517,23 @@ void TilesetEditorContext::_get_property_list(List<PropertyInfo> *p_list) const
p_list->push_back(PropertyInfo(Variant::VECTOR2, "options_step"));
p_list->push_back(PropertyInfo(Variant::VECTOR2, "options_separation"));
}
if (tileset.is_valid()) {
p_list->push_back(PropertyInfo(Variant::NIL, "TileSet", PROPERTY_HINT_NONE, "tileset_", PROPERTY_USAGE_GROUP));
p_list->push_back(PropertyInfo(Variant::OBJECT, "tileset_script", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
p_list->push_back(PropertyInfo(Variant::DICTIONARY, "tileset_meta_properties", PROPERTY_HINT_NONE, String()));
List<PropertyInfo> tp_list;
tileset->get_property_list(&tp_list);
if (tp_list.size()) {
for (List<PropertyInfo>::Element *E = tp_list.front(); E; E = E->next()) {
if ((E->get().usage & PROPERTY_USAGE_SCRIPT_VARIABLE)) {
PropertyInfo &pi = E->get();
pi.name = "tileset_" + pi.name;
p_list->push_back(pi);
}
}
}
}

if (tileset_editor->get_current_tile() >= 0 && !tileset.is_null()) {
int id = tileset_editor->get_current_tile();
p_list->push_back(PropertyInfo(Variant::NIL, "Selected Tile", PROPERTY_HINT_NONE, "tile_", PROPERTY_USAGE_GROUP));
Expand All @@ -3527,6 +3556,8 @@ void TilesetEditorContext::_get_property_list(List<PropertyInfo> *p_list) const
p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_shape_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
p_list->push_back(PropertyInfo(Variant::VECTOR2, "tile_shape_transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
p_list->push_back(PropertyInfo(Variant::INT, "tile_z_index", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1"));
p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_properties", PROPERTY_HINT_RESOURCE_TYPE, "TileProperties", PROPERTY_USAGE_DEFAULT, "TileProperties"));
p_list->push_back(PropertyInfo(Variant::OBJECT, "tile_properties_script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT, "Script"));
}
if (tileset_editor->edit_mode == TileSetEditor::EDITMODE_COLLISION && tileset_editor->edited_collision_shape.is_valid()) {
p_list->push_back(PropertyInfo(Variant::OBJECT, "selected_collision", PROPERTY_HINT_RESOURCE_TYPE, tileset_editor->edited_collision_shape->get_class()));
Expand Down
1 change: 1 addition & 0 deletions scene/register_scene_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,7 @@ void register_scene_types() {
ClassDB::register_class<PinJoint2D>();
ClassDB::register_class<GrooveJoint2D>();
ClassDB::register_class<DampedSpringJoint2D>();
ClassDB::register_class<TileProperties>();
ClassDB::register_class<TileSet>();
ClassDB::register_class<TileMap>();
ClassDB::register_class<ParallaxBackground>();
Expand Down
81 changes: 81 additions & 0 deletions scene/resources/tile_set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
#include "core/engine.h"
#include "core/math/geometry_2d.h"

void TileProperties::_bind_methods() {}

TileProperties::TileProperties() {}

bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
String n = p_name;
int slash = n.find("/");
Expand Down Expand Up @@ -203,6 +207,14 @@ bool TileSet::_set(const StringName &p_name, const Variant &p_value) {
tile_set_navigation_polygon_offset(id, p_value);
} else if (what == "z_index") {
tile_set_z_index(id, p_value);
} else if (what == "properties") {
tile_set_properties(id, p_value);
} else if (what == "properties_script") {
Ref<TileProperties> properties = tile_get_properties(id);
if (properties.is_valid()) {
Ref<Script> script = p_value;
properties->set_script(script.ptr());
}
} else {
return false;
}
Expand Down Expand Up @@ -318,6 +330,13 @@ bool TileSet::_get(const StringName &p_name, Variant &r_ret) const {
r_ret = tile_get_navigation_polygon_offset(id);
} else if (what == "z_index") {
r_ret = tile_get_z_index(id);
} else if (what == "properties") {
r_ret = tile_get_properties(id);
} else if (what == "properties_script") {
RES properties = tile_get_properties(id);
if (properties.is_valid()) {
r_ret = properties->get_script();
}
} else {
return false;
}
Expand Down Expand Up @@ -367,17 +386,48 @@ void TileSet::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::FLOAT, pre + "shape_one_way_margin", PROPERTY_HINT_RANGE, "0,128,0.01", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::ARRAY, pre + "shapes", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::INT, pre + "z_index", PROPERTY_HINT_RANGE, itos(RS::CANVAS_ITEM_Z_MIN) + "," + itos(RS::CANVAS_ITEM_Z_MAX) + ",1", PROPERTY_USAGE_NOEDITOR));
p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "properties", PROPERTY_HINT_RESOURCE_TYPE, "TileProperties"));
p_list->push_back(PropertyInfo(Variant::OBJECT, pre + "properties_script", PROPERTY_HINT_RESOURCE_TYPE, "Script"));
}
}

void TileSet::create_tile(int p_id) {
ERR_FAIL_COND(tile_map.has(p_id));
tile_map[p_id] = TileData();
tile_map[p_id].autotile_data = AutotileData();
if (tile_properties_script.is_valid()) {
tile_map[p_id].properties = Ref<TileProperties>(memnew(TileProperties));
tile_map[p_id].properties->set_script(tile_properties_script.ptr());
}
_change_notify("");
emit_changed();
}

void TileSet::set_tile_properties_script(const Ref<Script> &p_script) {
if (tile_properties_script == p_script)
return;
if (tile_properties_script.is_null()) {
for (Map<int, TileSet::TileData>::Element *E = tile_map.front(); E; E = E->next()) {
Ref<TileProperties> tp = memnew(TileProperties());
tp->set_script(p_script.ptr());
E->value().properties = tp;
}
} else {
for (Map<int, TileSet::TileData>::Element *E = tile_map.front(); E; E = E->next()) {
Ref<TileProperties> p = E->value().properties;
ERR_CONTINUE(p.is_valid());
Ref<Script> s = p->get_script();
if (s != p_script)
E->value().properties->set_script(p_script.ptr());
}
}
tile_properties_script = p_script;
}

Ref<Script> TileSet::get_tile_properties_script() const {
return tile_properties_script;
}

void TileSet::autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode) {
ERR_FAIL_COND(!tile_map.has(p_id));
tile_map[p_id].autotile_data.bitmask_mode = p_mode;
Expand Down Expand Up @@ -907,6 +957,17 @@ Vector2 TileSet::tile_get_occluder_offset(int p_id) const {
return tile_map[p_id].occluder_offset;
}

Ref<TileProperties> TileSet::tile_get_properties(int p_id) const {
ERR_FAIL_COND_V(!tile_map.has(p_id), 0);
return tile_map[p_id].properties;
}

void TileSet::tile_set_properties(int p_id, const Ref<TileProperties> &p_properties) {
ERR_FAIL_COND(!tile_map.has(p_id));
tile_map[p_id].properties = p_properties;
emit_changed();
}

void TileSet::tile_set_shapes(int p_id, const Vector<ShapeData> &p_shapes) {
ERR_FAIL_COND(!tile_map.has(p_id));
tile_map[p_id].shapes_data = p_shapes;
Expand Down Expand Up @@ -1049,6 +1110,14 @@ void TileSet::_decompose_convex_shape(Ref<Shape2D> p_shape) {
}
}

void TileSet::set_meta_properties(Dictionary p_dict) {
meta_properties = p_dict;
}

Dictionary TileSet::get_meta_properties() const {
return meta_properties;
}

void TileSet::get_tile_list(List<int> *p_tiles) const {
for (Map<int, TileData>::Element *E = tile_map.front(); E; E = E->next()) {
p_tiles->push_back(E->key());
Expand Down Expand Up @@ -1164,12 +1233,24 @@ void TileSet::_bind_methods() {
ClassDB::bind_method(D_METHOD("tile_get_occluder_offset", "id"), &TileSet::tile_get_occluder_offset);
ClassDB::bind_method(D_METHOD("tile_set_z_index", "id", "z_index"), &TileSet::tile_set_z_index);
ClassDB::bind_method(D_METHOD("tile_get_z_index", "id"), &TileSet::tile_get_z_index);
ClassDB::bind_method(D_METHOD("tile_set_properties", "id", "properties"), &TileSet::tile_set_properties);
ClassDB::bind_method(D_METHOD("tile_get_properties", "id"), &TileSet::tile_get_properties);

ClassDB::bind_method(D_METHOD("remove_tile", "id"), &TileSet::remove_tile);
ClassDB::bind_method(D_METHOD("clear"), &TileSet::clear);
ClassDB::bind_method(D_METHOD("get_last_unused_tile_id"), &TileSet::get_last_unused_tile_id);
ClassDB::bind_method(D_METHOD("find_tile_by_name", "name"), &TileSet::find_tile_by_name);
ClassDB::bind_method(D_METHOD("get_tiles_ids"), &TileSet::_get_tiles_ids);
ClassDB::bind_method(D_METHOD("set_tile_properties_script", "script"), &TileSet::set_tile_properties_script);
ClassDB::bind_method(D_METHOD("get_tile_properties_script"), &TileSet::get_tile_properties_script);

ClassDB::bind_method(D_METHOD("set_meta_properties", "dict"), &TileSet::set_meta_properties);
ClassDB::bind_method(D_METHOD("get_meta_properties"), &TileSet::get_meta_properties);

ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "meta_properties", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT, "Script"), "set_meta_properties", "get_meta_properties");

ADD_GROUP("Tiles", "tiles_");
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "tiles_default_script", PROPERTY_HINT_RESOURCE_TYPE, "Script", PROPERTY_USAGE_DEFAULT, "Script"), "set_tile_properties_script", "get_tile_properties_script");

BIND_VMETHOD(MethodInfo(Variant::BOOL, "_is_tile_bound", PropertyInfo(Variant::INT, "drawn_id"), PropertyInfo(Variant::INT, "neighbor_id")));
BIND_VMETHOD(MethodInfo(Variant::VECTOR2, "_forward_subtile_selection", PropertyInfo(Variant::INT, "autotile_id"), PropertyInfo(Variant::INT, "bitmask"), PropertyInfo(Variant::OBJECT, "tilemap", PROPERTY_HINT_NONE, "TileMap"), PropertyInfo(Variant::VECTOR2, "tile_location")));
Expand Down
32 changes: 32 additions & 0 deletions scene/resources/tile_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
#include "scene/resources/shape_2d.h"
#include "scene/resources/texture.h"

class TileSet;

class TileProperties : public Resource {
GDCLASS(TileProperties, Resource);

friend class TileSet;

private:
int id;
Ref<TileSet> tileset;

protected:
static void _bind_methods();

public:
TileProperties();
};

class TileSet : public Resource {
GDCLASS(TileSet, Resource);

Expand Down Expand Up @@ -120,16 +138,21 @@ class TileSet : public Resource {
Color modulate = Color(1, 1, 1);
AutotileData autotile_data;
int z_index = 0;
Ref<TileProperties> properties;

explicit TileData() {}
};

Map<int, TileData> tile_map;

Ref<Script> tile_properties_script;
Dictionary meta_properties;

protected:
bool _set(const StringName &p_name, const Variant &p_value);
bool _get(const StringName &p_name, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
void _get_tile_property_list(int p_id, List<PropertyInfo> *p_list) const;
void _tile_set_shapes(int p_id, const Array &p_shapes);
Array _tile_get_shapes(int p_id) const;
Array _get_tiles_ids() const;
Expand All @@ -140,6 +163,12 @@ class TileSet : public Resource {
public:
void create_tile(int p_id);

void set_meta_properties(Dictionary p_dict);
Dictionary get_meta_properties() const;

void set_tile_properties_script(const Ref<Script> &p_script);
Ref<Script> get_tile_properties_script() const;

void autotile_set_bitmask_mode(int p_id, BitmaskMode p_mode);
BitmaskMode autotile_get_bitmask_mode(int p_id) const;

Expand Down Expand Up @@ -236,6 +265,9 @@ class TileSet : public Resource {
void tile_set_z_index(int p_id, int p_z_index);
int tile_get_z_index(int p_id) const;

void tile_set_properties(int p_id, const Ref<TileProperties> &p_meta);
Ref<TileProperties> tile_get_properties(int p_id) const;

void remove_tile(int p_id);

bool has_tile(int p_id) const;
Expand Down