Przeglądaj źródła

Added IsInside() to Rect & IntRect. Closes #128.

Lasse Öörni 12 lat temu
rodzic
commit
3343e52986

+ 2 - 0
Source/Engine/LuaScript/pkgs/Math/Rect.pkg

@@ -22,6 +22,7 @@ class Rect
     Vector2 Size() const;
     Vector2 HalfSize() const;
     bool Equals(const Rect& rhs) const;
+    Intersection IsInside(const Vector2& point) const;
     Vector4 ToVector4() const;
     
     String ToString() const;
@@ -48,6 +49,7 @@ class IntRect
     IntVector2 Size() const;
     int Width() const;
     int Height() const;
+    Intersection IsInside(const IntVector2& point) const;
     
     int left_ @ left;
     int top_ @ top;

+ 19 - 0
Source/Engine/Math/Rect.h

@@ -167,6 +167,15 @@ public:
     /// Test for equality with another rect with epsilon.
     bool Equals(const Rect& rhs) const { return min_.Equals(rhs.min_) && max_.Equals(rhs.max_); }
     
+    /// Test whether a point is inside.
+    Intersection IsInside(const Vector2& point) const
+    {
+        if (point.x_ < min_.x_ || point.y_ < min_.y_ || point.x_ > max_.x_ || point.y_ > max_.y_)
+            return OUTSIDE;
+        else
+            return INSIDE;
+    }
+    
     /// Return float data.
     const void* Data() const { return &min_.x_; }
     /// Return as a vector.
@@ -227,6 +236,16 @@ public:
     int Width() const { return right_ - left_; }
     /// Return height.
     int Height() const { return bottom_ - top_; }
+    
+    /// Test whether a point is inside.
+    Intersection IsInside(const IntVector2& point) const
+    {
+        if (point.x_ < left_ || point.y_ < top_ || point.x_ >= right_ || point.y_ >= bottom_)
+            return OUTSIDE;
+        else
+            return INSIDE;
+    }
+    
     /// Return integer data.
     const int* Data() const { return &left_; }
     /// Return as string.

+ 7 - 5
Source/Engine/Script/MathAPI.cpp

@@ -34,6 +34,11 @@ namespace Urho3D
 
 static void RegisterMathFunctions(asIScriptEngine* engine)
 {
+    engine->RegisterEnum("Intersection");
+    engine->RegisterEnumValue("Intersection", "OUTSIDE", OUTSIDE);
+    engine->RegisterEnumValue("Intersection", "INTERSECTS", INTERSECTS);
+    engine->RegisterEnumValue("Intersection", "INSIDE", INSIDE);
+    
     engine->RegisterGlobalProperty("const float M_INFINITY", (void*)&M_INFINITY);
     engine->RegisterGlobalProperty("const float M_EPSILON", (void*)&M_EPSILON);
     engine->RegisterGlobalProperty("const float M_LARGE_EPSILON", (void*)&M_LARGE_EPSILON);
@@ -111,6 +116,7 @@ static void RegisterIntRect(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("IntRect", asBEHAVE_CONSTRUCT, "void f(const IntRect&in)", asFUNCTION(ConstructIntRectCopy), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("IntRect", asBEHAVE_CONSTRUCT, "void f(int, int, int, int)", asFUNCTION(ConstructIntRectInit), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("IntRect", asBEHAVE_CONSTRUCT, "void f(int[]&)", asFUNCTION(ConstructIntRectArrayInit), asCALL_CDECL_OBJLAST);
+    engine->RegisterObjectMethod("IntRect", "Intersection IsInside(const IntVector2&in) const", asMETHOD(IntRect, IsInside), asCALL_THISCALL);
     engine->RegisterObjectMethod("IntRect", "int[]& get_data() const", asFUNCTION(IntRectData), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("IntRect", "IntRect& opAssign(const IntRect&in)", asMETHOD(IntRect, operator =), asCALL_THISCALL);
     engine->RegisterObjectMethod("IntRect", "bool opEquals(const IntRect&in) const", asMETHOD(IntRect, operator ==), asCALL_THISCALL);
@@ -758,6 +764,7 @@ static void RegisterRect(asIScriptEngine* engine)
     engine->RegisterObjectMethod("Rect", "void Clip(const Rect&in)", asMETHODPR(Rect, Clip, (const Rect&), void), asCALL_THISCALL);
     engine->RegisterObjectMethod("Rect", "void Clear()", asMETHOD(Rect, Clear), asCALL_THISCALL);
     engine->RegisterObjectMethod("Rect", "bool Equals(const Rect&in) const", asMETHOD(Rect, Equals), asCALL_THISCALL);
+    engine->RegisterObjectMethod("Rect", "Intersection IsInside(const Vector2&in) const", asMETHOD(Rect, IsInside), asCALL_THISCALL);
     engine->RegisterObjectMethod("Rect", "Vector4 ToVector4() const", asMETHOD(Rect, ToVector4), asCALL_THISCALL);
     engine->RegisterObjectMethod("Rect", "Vector2 get_center() const", asMETHOD(Rect, Center), asCALL_THISCALL);
     engine->RegisterObjectMethod("Rect", "Vector2 get_size() const", asMETHOD(Rect, Size), asCALL_THISCALL);
@@ -915,11 +922,6 @@ static Vector3 FrustumGetVertex(unsigned index, Frustum* ptr)
 
 static void RegisterVolumes(asIScriptEngine* engine)
 {
-    engine->RegisterEnum("Intersection");
-    engine->RegisterEnumValue("Intersection", "OUTSIDE", OUTSIDE);
-    engine->RegisterEnumValue("Intersection", "INTERSECTS", INTERSECTS);
-    engine->RegisterEnumValue("Intersection", "INSIDE", INSIDE);
-    
     engine->RegisterObjectType("BoundingBox", sizeof(BoundingBox), asOBJ_VALUE | asOBJ_POD | asOBJ_APP_CLASS_CAK);
     engine->RegisterObjectType("Frustum", sizeof(Frustum), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
     engine->RegisterObjectType("Polyhedron", sizeof(Polyhedron), asOBJ_VALUE | asOBJ_APP_CLASS_CDK);