ProximityGroup: Fix access modifiers, rename private methods for clarity

See #36285 which mistakenly added documentation for the whole C++ API, while
some of it is meant to be and stay private as it's not exposed to scripts.
The access modifiers and method prefix were not used properly.

Cleanup code, and rename wrong `group_name` parameters to `method`, as it's a
method name which is being broadcast.

This is a very old class from pre-open source days, chances are that it was
just forgotten and not meant to be kept as is and undocumented.

(cherry picked from commit 79ba70f7ee)
This commit is contained in:
Rémi Verschelde 2020-12-11 14:26:50 +01:00
parent 70fc0d45f7
commit 1a64f5d2ec
No known key found for this signature in database
GPG key ID: C3336907360768E1
3 changed files with 64 additions and 67 deletions

View file

@ -12,7 +12,7 @@
<method name="broadcast"> <method name="broadcast">
<return type="void"> <return type="void">
</return> </return>
<argument index="0" name="name" type="String"> <argument index="0" name="method" type="String">
</argument> </argument>
<argument index="1" name="parameters" type="Variant"> <argument index="1" name="parameters" type="Variant">
</argument> </argument>
@ -30,7 +30,7 @@
</members> </members>
<signals> <signals>
<signal name="broadcast"> <signal name="broadcast">
<argument index="0" name="group_name" type="String"> <argument index="0" name="method" type="String">
</argument> </argument>
<argument index="1" name="parameters" type="Array"> <argument index="1" name="parameters" type="Array">
</argument> </argument>

View file

@ -32,7 +32,7 @@
#include "core/math/math_funcs.h" #include "core/math/math_funcs.h"
void ProximityGroup::clear_groups() { void ProximityGroup::_clear_groups() {
Map<StringName, uint32_t>::Element *E; Map<StringName, uint32_t>::Element *E;
@ -45,22 +45,22 @@ void ProximityGroup::clear_groups() {
if (E->get() != group_version) { if (E->get() != group_version) {
remove_list[num++] = E->key(); remove_list[num++] = E->key();
}; }
E = E->next(); E = E->next();
}; }
for (int i = 0; i < num; i++) { for (int i = 0; i < num; i++) {
groups.erase(remove_list[i]); groups.erase(remove_list[i]);
}; }
}; }
if (E) { if (E) {
clear_groups(); // call until we go through the whole list _clear_groups(); // call until we go through the whole list
}; }
}; }
void ProximityGroup::update_groups() { void ProximityGroup::_update_groups() {
if (grid_radius == Vector3(0, 0, 0)) if (grid_radius == Vector3(0, 0, 0))
return; return;
@ -71,12 +71,12 @@ void ProximityGroup::update_groups() {
Vector3 vcell = pos / cell_size; Vector3 vcell = pos / cell_size;
int cell[3] = { Math::fast_ftoi(vcell.x), Math::fast_ftoi(vcell.y), Math::fast_ftoi(vcell.z) }; int cell[3] = { Math::fast_ftoi(vcell.x), Math::fast_ftoi(vcell.y), Math::fast_ftoi(vcell.z) };
add_groups(cell, group_name, 0); _add_groups(cell, group_name, 0);
clear_groups(); _clear_groups();
}; }
void ProximityGroup::add_groups(int *p_cell, String p_base, int p_depth) { void ProximityGroup::_add_groups(int *p_cell, String p_base, int p_depth) {
p_base = p_base + "|"; p_base = p_base + "|";
if (grid_radius[p_depth] == 0) { if (grid_radius[p_depth] == 0) {
@ -84,9 +84,9 @@ void ProximityGroup::add_groups(int *p_cell, String p_base, int p_depth) {
if (p_depth == 2) { if (p_depth == 2) {
_new_group(p_base); _new_group(p_base);
} else { } else {
add_groups(p_cell, p_base, p_depth + 1); _add_groups(p_cell, p_base, p_depth + 1);
}; }
}; }
int start = p_cell[p_depth] - grid_radius[p_depth]; int start = p_cell[p_depth] - grid_radius[p_depth];
int end = p_cell[p_depth] + grid_radius[p_depth]; int end = p_cell[p_depth] + grid_radius[p_depth];
@ -97,20 +97,20 @@ void ProximityGroup::add_groups(int *p_cell, String p_base, int p_depth) {
if (p_depth == 2) { if (p_depth == 2) {
_new_group(gname); _new_group(gname);
} else { } else {
add_groups(p_cell, gname, p_depth + 1); _add_groups(p_cell, gname, p_depth + 1);
}; }
}; }
}; }
void ProximityGroup::_new_group(StringName p_name) { void ProximityGroup::_new_group(StringName p_name) {
const Map<StringName, uint32_t>::Element *E = groups.find(p_name); const Map<StringName, uint32_t>::Element *E = groups.find(p_name);
if (!E) { if (!E) {
add_to_group(p_name); add_to_group(p_name);
}; }
groups[p_name] = group_version; groups[p_name] = group_version;
}; }
void ProximityGroup::_notification(int p_what) { void ProximityGroup::_notification(int p_what) {
@ -118,65 +118,65 @@ void ProximityGroup::_notification(int p_what) {
case NOTIFICATION_EXIT_TREE: case NOTIFICATION_EXIT_TREE:
++group_version; ++group_version;
clear_groups(); _clear_groups();
break; break;
case NOTIFICATION_TRANSFORM_CHANGED: case NOTIFICATION_TRANSFORM_CHANGED:
update_groups(); _update_groups();
break; break;
}; }
}; }
void ProximityGroup::broadcast(String p_name, Variant p_params) { void ProximityGroup::broadcast(String p_method, Variant p_parameters) {
Map<StringName, uint32_t>::Element *E; Map<StringName, uint32_t>::Element *E;
E = groups.front(); E = groups.front();
while (E) { while (E) {
get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_name, p_params); get_tree()->call_group_flags(SceneTree::GROUP_CALL_DEFAULT, E->key(), "_proximity_group_broadcast", p_method, p_parameters);
E = E->next(); E = E->next();
}; }
}; }
void ProximityGroup::_proximity_group_broadcast(String p_name, Variant p_params) { void ProximityGroup::_proximity_group_broadcast(String p_method, Variant p_parameters) {
if (dispatch_mode == MODE_PROXY) { if (dispatch_mode == MODE_PROXY) {
get_parent()->call(p_name, p_params); get_parent()->call(p_method, p_parameters);
} else { } else {
emit_signal("broadcast", p_name, p_params); emit_signal("broadcast", p_method, p_parameters);
}; }
}; }
void ProximityGroup::set_group_name(const String &p_group_name) { void ProximityGroup::set_group_name(const String &p_group_name) {
group_name = p_group_name; group_name = p_group_name;
}; }
String ProximityGroup::get_group_name() const { String ProximityGroup::get_group_name() const {
return group_name; return group_name;
}; }
void ProximityGroup::set_dispatch_mode(DispatchMode p_mode) { void ProximityGroup::set_dispatch_mode(DispatchMode p_mode) {
dispatch_mode = p_mode; dispatch_mode = p_mode;
}; }
ProximityGroup::DispatchMode ProximityGroup::get_dispatch_mode() const { ProximityGroup::DispatchMode ProximityGroup::get_dispatch_mode() const {
return dispatch_mode; return dispatch_mode;
}; }
void ProximityGroup::set_grid_radius(const Vector3 &p_radius) { void ProximityGroup::set_grid_radius(const Vector3 &p_radius) {
grid_radius = p_radius; grid_radius = p_radius;
}; }
Vector3 ProximityGroup::get_grid_radius() const { Vector3 ProximityGroup::get_grid_radius() const {
return grid_radius; return grid_radius;
}; }
void ProximityGroup::_bind_methods() { void ProximityGroup::_bind_methods() {
@ -186,18 +186,20 @@ void ProximityGroup::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_dispatch_mode"), &ProximityGroup::get_dispatch_mode); ClassDB::bind_method(D_METHOD("get_dispatch_mode"), &ProximityGroup::get_dispatch_mode);
ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup::set_grid_radius); ClassDB::bind_method(D_METHOD("set_grid_radius", "radius"), &ProximityGroup::set_grid_radius);
ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup::get_grid_radius); ClassDB::bind_method(D_METHOD("get_grid_radius"), &ProximityGroup::get_grid_radius);
ClassDB::bind_method(D_METHOD("broadcast", "name", "parameters"), &ProximityGroup::broadcast);
ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "name", "params"), &ProximityGroup::_proximity_group_broadcast); ClassDB::bind_method(D_METHOD("broadcast", "method", "parameters"), &ProximityGroup::broadcast);
ClassDB::bind_method(D_METHOD("_proximity_group_broadcast", "method", "parameters"), &ProximityGroup::_proximity_group_broadcast);
ADD_PROPERTY(PropertyInfo(Variant::STRING, "group_name"), "set_group_name", "get_group_name"); ADD_PROPERTY(PropertyInfo(Variant::STRING, "group_name"), "set_group_name", "get_group_name");
ADD_PROPERTY(PropertyInfo(Variant::INT, "dispatch_mode", PROPERTY_HINT_ENUM, "Proxy,Signal"), "set_dispatch_mode", "get_dispatch_mode"); ADD_PROPERTY(PropertyInfo(Variant::INT, "dispatch_mode", PROPERTY_HINT_ENUM, "Proxy,Signal"), "set_dispatch_mode", "get_dispatch_mode");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius"); ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "grid_radius"), "set_grid_radius", "get_grid_radius");
ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "group_name"), PropertyInfo(Variant::ARRAY, "parameters"))); ADD_SIGNAL(MethodInfo("broadcast", PropertyInfo(Variant::STRING, "method"), PropertyInfo(Variant::ARRAY, "parameters")));
BIND_ENUM_CONSTANT(MODE_PROXY); BIND_ENUM_CONSTANT(MODE_PROXY);
BIND_ENUM_CONSTANT(MODE_SIGNAL); BIND_ENUM_CONSTANT(MODE_SIGNAL);
}; }
ProximityGroup::ProximityGroup() { ProximityGroup::ProximityGroup() {
@ -207,8 +209,4 @@ ProximityGroup::ProximityGroup() {
cell_size = 1.0; cell_size = 1.0;
grid_radius = Vector3(1, 1, 1); grid_radius = Vector3(1, 1, 1);
set_notify_transform(true); set_notify_transform(true);
}; }
ProximityGroup::~ProximityGroup(){
};

View file

@ -36,7 +36,6 @@
class ProximityGroup : public Spatial { class ProximityGroup : public Spatial {
GDCLASS(ProximityGroup, Spatial); GDCLASS(ProximityGroup, Spatial);
OBJ_CATEGORY("3D");
public: public:
enum DispatchMode { enum DispatchMode {
@ -44,25 +43,25 @@ public:
MODE_SIGNAL, MODE_SIGNAL,
}; };
public: private:
void clear_groups();
void update_groups();
void _notification(int p_what);
DispatchMode dispatch_mode;
Map<StringName, uint32_t> groups; Map<StringName, uint32_t> groups;
String group_name; String group_name;
DispatchMode dispatch_mode;
Vector3 grid_radius;
float cell_size; float cell_size;
Vector3 grid_radius;
uint32_t group_version; uint32_t group_version;
void add_groups(int *p_cell, String p_base, int p_depth); void _clear_groups();
void _update_groups();
void _add_groups(int *p_cell, String p_base, int p_depth);
void _new_group(StringName p_name); void _new_group(StringName p_name);
void _proximity_group_broadcast(String p_name, Variant p_params); void _proximity_group_broadcast(String p_method, Variant p_parameters);
protected:
void _notification(int p_what);
static void _bind_methods(); static void _bind_methods();
@ -76,12 +75,12 @@ public:
void set_grid_radius(const Vector3 &p_radius); void set_grid_radius(const Vector3 &p_radius);
Vector3 get_grid_radius() const; Vector3 get_grid_radius() const;
void broadcast(String p_name, Variant p_params); void broadcast(String p_method, Variant p_parameters);
ProximityGroup(); ProximityGroup();
~ProximityGroup(); ~ProximityGroup() {}
}; };
VARIANT_ENUM_CAST(ProximityGroup::DispatchMode); VARIANT_ENUM_CAST(ProximityGroup::DispatchMode);
#endif #endif // PROXIMITY_GROUP_H