summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/input/input_event.cpp35
-rw-r--r--core/math/delaunay_2d.h127
-rw-r--r--core/object/message_queue.cpp6
-rw-r--r--core/object/object.cpp5
-rw-r--r--core/object/object.h30
-rw-r--r--core/os/keyboard.cpp10
-rw-r--r--core/string/string_name.cpp8
-rw-r--r--core/string/string_name.h2
8 files changed, 114 insertions, 109 deletions
diff --git a/core/input/input_event.cpp b/core/input/input_event.cpp
index 9d5d84a508..46f07fe041 100644
--- a/core/input/input_event.cpp
+++ b/core/input/input_event.cpp
@@ -33,6 +33,7 @@
#include "core/input/input_map.h"
#include "core/input/shortcut.h"
#include "core/os/keyboard.h"
+#include "core/os/os.h"
const int InputEvent::DEVICE_ID_EMULATION = -1;
const int InputEvent::DEVICE_ID_INTERNAL = -2;
@@ -145,13 +146,13 @@ int64_t InputEventFromWindow::get_window_id() const {
void InputEventWithModifiers::set_command_or_control_autoremap(bool p_enabled) {
command_or_control_autoremap = p_enabled;
if (command_or_control_autoremap) {
-#ifdef MACOS_ENABLED
- ctrl_pressed = false;
- meta_pressed = true;
-#else
- ctrl_pressed = true;
- meta_pressed = false;
-#endif
+ if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
+ ctrl_pressed = false;
+ meta_pressed = true;
+ } else {
+ ctrl_pressed = true;
+ meta_pressed = false;
+ }
} else {
ctrl_pressed = false;
meta_pressed = false;
@@ -164,11 +165,11 @@ bool InputEventWithModifiers::is_command_or_control_autoremap() const {
}
bool InputEventWithModifiers::is_command_or_control_pressed() const {
-#ifdef MACOS_ENABLED
- return meta_pressed;
-#else
- return ctrl_pressed;
-#endif
+ if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
+ return meta_pressed;
+ } else {
+ return ctrl_pressed;
+ }
}
void InputEventWithModifiers::set_shift_pressed(bool p_enabled) {
@@ -231,11 +232,11 @@ BitField<KeyModifierMask> InputEventWithModifiers::get_modifiers_mask() const {
mask.set_flag(KeyModifierMask::META);
}
if (is_command_or_control_autoremap()) {
-#ifdef MACOS_ENABLED
- mask.set_flag(KeyModifierMask::META);
-#else
- mask.set_flag(KeyModifierMask::CTRL);
-#endif
+ if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
+ mask.set_flag(KeyModifierMask::META);
+ } else {
+ mask.set_flag(KeyModifierMask::CTRL);
+ }
}
return mask;
}
diff --git a/core/math/delaunay_2d.h b/core/math/delaunay_2d.h
index 8d602da241..cf7ba8d48e 100644
--- a/core/math/delaunay_2d.h
+++ b/core/math/delaunay_2d.h
@@ -38,7 +38,8 @@ class Delaunay2D {
public:
struct Triangle {
int points[3];
- bool bad = false;
+ Vector2 circum_center;
+ real_t circum_radius_squared;
Triangle() {}
Triangle(int p_a, int p_b, int p_c) {
points[0] = p_a;
@@ -48,117 +49,109 @@ public:
};
struct Edge {
- int edge[2];
+ int points[2];
bool bad = false;
Edge() {}
Edge(int p_a, int p_b) {
- edge[0] = p_a;
- edge[1] = p_b;
+ // Store indices in a sorted manner to avoid having to check both orientations later.
+ if (p_a > p_b) {
+ points[0] = p_b;
+ points[1] = p_a;
+ } else {
+ points[0] = p_a;
+ points[1] = p_b;
+ }
}
};
- static bool circum_circle_contains(const Vector<Vector2> &p_vertices, const Triangle &p_triangle, int p_vertex) {
- Vector2 p1 = p_vertices[p_triangle.points[0]];
- Vector2 p2 = p_vertices[p_triangle.points[1]];
- Vector2 p3 = p_vertices[p_triangle.points[2]];
+ static Triangle create_triangle(const Vector<Vector2> &p_vertices, const int &p_a, const int &p_b, const int &p_c) {
+ Triangle triangle = Triangle(p_a, p_b, p_c);
- real_t ab = p1.x * p1.x + p1.y * p1.y;
- real_t cd = p2.x * p2.x + p2.y * p2.y;
- real_t ef = p3.x * p3.x + p3.y * p3.y;
+ // Get the values of the circumcircle and store them inside the triangle object.
+ Vector2 a = p_vertices[p_b] - p_vertices[p_a];
+ Vector2 b = p_vertices[p_c] - p_vertices[p_a];
- Vector2 circum(
- (ab * (p3.y - p2.y) + cd * (p1.y - p3.y) + ef * (p2.y - p1.y)) / (p1.x * (p3.y - p2.y) + p2.x * (p1.y - p3.y) + p3.x * (p2.y - p1.y)),
- (ab * (p3.x - p2.x) + cd * (p1.x - p3.x) + ef * (p2.x - p1.x)) / (p1.y * (p3.x - p2.x) + p2.y * (p1.x - p3.x) + p3.y * (p2.x - p1.x)));
+ Vector2 O = (b * a.length_squared() - a * b.length_squared()).orthogonal() / (a.cross(b) * 2.0f);
- circum *= 0.5;
- float r = p1.distance_squared_to(circum);
- float d = p_vertices[p_vertex].distance_squared_to(circum);
- return d <= r;
- }
+ triangle.circum_radius_squared = O.length_squared();
+ triangle.circum_center = O + p_vertices[p_a];
- static bool edge_compare(const Vector<Vector2> &p_vertices, const Edge &p_a, const Edge &p_b) {
- if (p_vertices[p_a.edge[0]].is_equal_approx(p_vertices[p_b.edge[0]]) && p_vertices[p_a.edge[1]].is_equal_approx(p_vertices[p_b.edge[1]])) {
- return true;
- }
-
- if (p_vertices[p_a.edge[0]].is_equal_approx(p_vertices[p_b.edge[1]]) && p_vertices[p_a.edge[1]].is_equal_approx(p_vertices[p_b.edge[0]])) {
- return true;
- }
-
- return false;
+ return triangle;
}
static Vector<Triangle> triangulate(const Vector<Vector2> &p_points) {
Vector<Vector2> points = p_points;
Vector<Triangle> triangles;
- Rect2 rect;
- for (int i = 0; i < p_points.size(); i++) {
- if (i == 0) {
- rect.position = p_points[i];
- } else {
- rect.expand_to(p_points[i]);
- }
+ int point_count = p_points.size();
+ if (point_count <= 2) {
+ return triangles;
}
- float delta_max = MAX(rect.size.width, rect.size.height);
+ // Get a bounding rectangle.
+ Rect2 rect = Rect2(p_points[0], Size2());
+ for (int i = 1; i < point_count; i++) {
+ rect.expand_to(p_points[i]);
+ }
+
+ real_t delta_max = MAX(rect.size.width, rect.size.height);
Vector2 center = rect.get_center();
- points.push_back(Vector2(center.x - 20 * delta_max, center.y - delta_max));
- points.push_back(Vector2(center.x, center.y + 20 * delta_max));
- points.push_back(Vector2(center.x + 20 * delta_max, center.y - delta_max));
+ // Construct a bounding triangle around the rectangle.
+ points.push_back(Vector2(center.x - delta_max * 16, center.y - delta_max));
+ points.push_back(Vector2(center.x, center.y + delta_max * 16));
+ points.push_back(Vector2(center.x + delta_max * 16, center.y - delta_max));
- triangles.push_back(Triangle(p_points.size() + 0, p_points.size() + 1, p_points.size() + 2));
+ Triangle bounding_triangle = create_triangle(points, point_count + 0, point_count + 1, point_count + 2);
+ triangles.push_back(bounding_triangle);
- for (int i = 0; i < p_points.size(); i++) {
+ for (int i = 0; i < point_count; i++) {
Vector<Edge> polygon;
- for (int j = 0; j < triangles.size(); j++) {
- if (circum_circle_contains(points, triangles[j], i)) {
- triangles.write[j].bad = true;
+ // Save the edges of the triangles whose circumcircles contain the i-th vertex. Delete the triangles themselves.
+ for (int j = triangles.size() - 1; j >= 0; j--) {
+ if (points[i].distance_squared_to(triangles[j].circum_center) < triangles[j].circum_radius_squared) {
polygon.push_back(Edge(triangles[j].points[0], triangles[j].points[1]));
polygon.push_back(Edge(triangles[j].points[1], triangles[j].points[2]));
polygon.push_back(Edge(triangles[j].points[2], triangles[j].points[0]));
- }
- }
- for (int j = 0; j < triangles.size(); j++) {
- if (triangles[j].bad) {
triangles.remove_at(j);
- j--;
}
}
+ // Create a triangle for every unique edge.
for (int j = 0; j < polygon.size(); j++) {
+ if (polygon[j].bad) {
+ continue;
+ }
+
for (int k = j + 1; k < polygon.size(); k++) {
- if (edge_compare(points, polygon[j], polygon[k])) {
+ // Compare the edges.
+ if (polygon[k].points[0] == polygon[j].points[0] && polygon[k].points[1] == polygon[j].points[1]) {
polygon.write[j].bad = true;
polygon.write[k].bad = true;
+
+ break; // Since no more than two triangles can share an edge, no more than two edges can share vertices.
}
}
- }
- for (int j = 0; j < polygon.size(); j++) {
- if (polygon[j].bad) {
- continue;
+ // Create triangles out of good edges.
+ if (!polygon[j].bad) {
+ triangles.push_back(create_triangle(points, polygon[j].points[0], polygon[j].points[1], i));
}
- triangles.push_back(Triangle(polygon[j].edge[0], polygon[j].edge[1], i));
}
}
- for (int i = 0; i < triangles.size(); i++) {
- bool invalid = false;
- for (int j = 0; j < 3; j++) {
- if (triangles[i].points[j] >= p_points.size()) {
- invalid = true;
- break;
- }
- }
- if (invalid) {
- triangles.remove_at(i);
- i--;
+ // Filter out the triangles containing vertices of the bounding triangle.
+ int preserved_count = 0;
+ Triangle *triangles_ptrw = triangles.ptrw();
+ for (int i = 0; i < triangles.size() - 1; i++) {
+ if (!(triangles[i].points[0] >= point_count || triangles[i].points[1] >= point_count || triangles[i].points[2] >= point_count)) {
+ triangles_ptrw[preserved_count] = triangles[i];
+ preserved_count++;
}
}
+ triangles.resize(preserved_count);
return triangles;
}
diff --git a/core/object/message_queue.cpp b/core/object/message_queue.cpp
index decf030e27..1542decc6f 100644
--- a/core/object/message_queue.cpp
+++ b/core/object/message_queue.cpp
@@ -55,7 +55,7 @@ Error MessageQueue::push_set(ObjectID p_id, const StringName &p_prop, const Vari
if (ObjectDB::get_instance(p_id)) {
type = ObjectDB::get_instance(p_id)->get_class();
}
- ERR_PRINT("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
+ ERR_PRINT("Failed set: " + type + ":" + p_prop + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing \"memory/limits/message_queue/max_size_kb\" in project settings.");
statistics();
return ERR_OUT_OF_MEMORY;
}
@@ -82,7 +82,7 @@ Error MessageQueue::push_notification(ObjectID p_id, int p_notification) {
uint8_t room_needed = sizeof(Message);
if ((buffer_end + room_needed) >= buffer_size) {
- ERR_PRINT("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
+ ERR_PRINT("Failed notification: " + itos(p_notification) + " target ID: " + itos(p_id) + ". Message queue out of memory. Try increasing \"memory/limits/message_queue/max_size_kb\" in project settings.");
statistics();
return ERR_OUT_OF_MEMORY;
}
@@ -117,7 +117,7 @@ Error MessageQueue::push_callablep(const Callable &p_callable, const Variant **p
int room_needed = sizeof(Message) + sizeof(Variant) * p_argcount;
if ((buffer_end + room_needed) >= buffer_size) {
- ERR_PRINT("Failed method: " + p_callable + ". Message queue out of memory. Try increasing 'memory/limits/message_queue/max_size_kb' in project settings.");
+ ERR_PRINT("Failed method: " + p_callable + ". Message queue out of memory. Try increasing \"memory/limits/message_queue/max_size_kb\" in project settings.");
statistics();
return ERR_OUT_OF_MEMORY;
}
diff --git a/core/object/object.cpp b/core/object/object.cpp
index c324eab9bb..39cae7c5bd 100644
--- a/core/object/object.cpp
+++ b/core/object/object.cpp
@@ -195,14 +195,15 @@ bool Object::_predelete() {
_predelete_ok = 1;
notification(NOTIFICATION_PREDELETE, true);
if (_predelete_ok) {
- _class_ptr = nullptr; //must restore so destructors can access class ptr correctly
+ _class_name_ptr = nullptr; // Must restore, so constructors/destructors have proper class name access at each stage.
}
return _predelete_ok;
}
void Object::_postinitialize() {
- _class_ptr = _get_class_namev();
+ _class_name_ptr = _get_class_namev(); // Set the direct pointer, which is much faster to obtain, but can only happen after postinitialize.
_initialize_classv();
+ _class_name_ptr = nullptr; // May have been called from a constructor.
notification(NOTIFICATION_POSTINITIALIZE);
}
diff --git a/core/object/object.h b/core/object/object.h
index 5ec69a371b..4226b5e67b 100644
--- a/core/object/object.h
+++ b/core/object/object.h
@@ -376,7 +376,6 @@ private:
#define GDCLASS(m_class, m_inherits) \
private: \
void operator=(const m_class &p_rval) {} \
- mutable StringName _class_name; \
friend class ::ClassDB; \
\
public: \
@@ -388,13 +387,11 @@ public:
return String(#m_class); \
} \
virtual const StringName *_get_class_namev() const override { \
- if (_get_extension()) { \
- return &_get_extension()->class_name; \
- } \
- if (!_class_name) { \
- _class_name = get_class_static(); \
+ static StringName _class_name_static; \
+ if (unlikely(!_class_name_static)) { \
+ StringName::assign_static_unique_class_name(&_class_name_static, #m_class); \
} \
- return &_class_name; \
+ return &_class_name_static; \
} \
static _FORCE_INLINE_ void *get_class_ptr_static() { \
static int ptr; \
@@ -614,8 +611,7 @@ private:
Variant script; // Reference does not exist yet, store it in a Variant.
HashMap<StringName, Variant> metadata;
HashMap<StringName, Variant *> metadata_properties;
- mutable StringName _class_name;
- mutable const StringName *_class_ptr = nullptr;
+ mutable const StringName *_class_name_ptr = nullptr;
void _add_user_signal(const String &p_name, const Array &p_args = Array());
bool _has_user_signal(const StringName &p_name) const;
@@ -714,10 +710,11 @@ protected:
Variant _call_deferred_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error);
virtual const StringName *_get_class_namev() const {
- if (!_class_name) {
- _class_name = get_class_static();
+ static StringName _class_name_static;
+ if (unlikely(!_class_name_static)) {
+ StringName::assign_static_unique_class_name(&_class_name_static, "Object");
}
- return &_class_name;
+ return &_class_name_static;
}
Vector<StringName> _get_meta_list_bind() const;
@@ -788,13 +785,16 @@ public:
_FORCE_INLINE_ const StringName &get_class_name() const {
if (_extension) {
+ // Can't put inside the unlikely as constructor can run it
return _extension->class_name;
}
- if (!_class_ptr) {
+
+ if (unlikely(!_class_name_ptr)) {
+ // While class is initializing / deinitializing, constructors and destructurs
+ // need access to the proper class at the proper stage.
return *_get_class_namev();
- } else {
- return *_class_ptr;
}
+ return *_class_name_ptr;
}
/* IAPI */
diff --git a/core/os/keyboard.cpp b/core/os/keyboard.cpp
index 25a4b320cd..1e32e6e096 100644
--- a/core/os/keyboard.cpp
+++ b/core/os/keyboard.cpp
@@ -367,11 +367,11 @@ String keycode_get_string(Key p_code) {
codestr += "+";
}
if ((p_code & KeyModifierMask::CMD_OR_CTRL) != Key::NONE) {
-#ifdef MACOS_ENABLED
- codestr += find_keycode_name(Key::META);
-#else
- codestr += find_keycode_name(Key::CTRL);
-#endif
+ if (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) {
+ codestr += find_keycode_name(Key::META);
+ } else {
+ codestr += find_keycode_name(Key::CTRL);
+ }
codestr += "+";
}
if ((p_code & KeyModifierMask::CTRL) != Key::NONE) {
diff --git a/core/string/string_name.cpp b/core/string/string_name.cpp
index df9b6b3f1a..6099fea13f 100644
--- a/core/string/string_name.cpp
+++ b/core/string/string_name.cpp
@@ -201,6 +201,14 @@ StringName::StringName(const StringName &p_name) {
}
}
+void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
+ mutex.lock();
+ if (*ptr == StringName()) {
+ *ptr = StringName(p_name, true);
+ }
+ mutex.unlock();
+}
+
StringName::StringName(const char *p_name, bool p_static) {
_data = nullptr;
diff --git a/core/string/string_name.h b/core/string/string_name.h
index 177e82896d..07abc781a2 100644
--- a/core/string/string_name.h
+++ b/core/string/string_name.h
@@ -177,6 +177,8 @@ public:
StringName(const String &p_name, bool p_static = false);
StringName(const StaticCString &p_static_string, bool p_static = false);
StringName() {}
+
+ static void assign_static_unique_class_name(StringName *ptr, const char *p_name);
_FORCE_INLINE_ ~StringName() {
if (likely(configured) && _data) { //only free if configured
unref();