Browse Source

Added Clip, Merge, and contructor from pair of IntVector2 to IntRect.

Rainer Deyke 8 years ago
parent
commit
b8495dbd28
3 changed files with 59 additions and 0 deletions
  1. 8 0
      Source/Urho3D/AngelScript/MathAPI.cpp
  2. 34 0
      Source/Urho3D/Math/Rect.cpp
  3. 17 0
      Source/Urho3D/Math/Rect.h

+ 8 - 0
Source/Urho3D/AngelScript/MathAPI.cpp

@@ -114,6 +114,11 @@ static void ConstructIntRectInit(int left, int top, int right, int bottom, IntRe
     new(ptr) IntRect(left, top, right, bottom);
 }
 
+static void ConstructIntRectMinMax(const IntVector2& min, const IntVector2& max, IntRect* ptr)
+{
+    new(ptr) IntRect(min, max);
+}
+
 static void ConstructIntRectArrayInit(CScriptArray* data, IntRect* ptr)
 {
     new(ptr) IntRect((static_cast<int*>(data->At(0))));
@@ -130,6 +135,7 @@ static void RegisterIntRect(asIScriptEngine* engine)
     engine->RegisterObjectBehaviour("IntRect", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructIntRect), asCALL_CDECL_OBJLAST);
     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(const IntVector2&in, const IntVector2&in)", asFUNCTION(ConstructIntRectMinMax), 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);
@@ -138,6 +144,8 @@ static void RegisterIntRect(asIScriptEngine* engine)
     engine->RegisterObjectMethod("IntRect", "IntVector2 get_size() const", asMETHOD(IntRect, Size), asCALL_THISCALL);
     engine->RegisterObjectMethod("IntRect", "int get_width() const", asMETHOD(IntRect, Width), asCALL_THISCALL);
     engine->RegisterObjectMethod("IntRect", "int get_height() const", asMETHOD(IntRect, Height), asCALL_THISCALL);
+    engine->RegisterObjectMethod("IntRect", "int Merge(const IntRect&in)", asMETHOD(IntRect, Merge), asCALL_THISCALL);
+    engine->RegisterObjectMethod("IntRect", "int Clip(const IntRect&in)", asMETHOD(IntRect, Merge), asCALL_THISCALL);
     engine->RegisterObjectProperty("IntRect", "int left", offsetof(IntRect, left_));
     engine->RegisterObjectProperty("IntRect", "int top", offsetof(IntRect, top_));
     engine->RegisterObjectProperty("IntRect", "int right", offsetof(IntRect, right_));

+ 34 - 0
Source/Urho3D/Math/Rect.cpp

@@ -37,6 +37,40 @@ const Rect Rect::ZERO(0.0f, 0.0f, 0.0f, 0.0f);
 
 const IntRect IntRect::ZERO(0, 0, 0, 0);
 
+void IntRect::Clip(const IntRect& rect)
+{
+    if (rect.left_ > left_)
+        left_ = rect.left_;
+    if (rect.right_ < right_)
+        right_ = rect.right_;
+    if (rect.top_ > top_)
+        top_ = rect.top_;
+    if (rect.bottom_ < bottom_)
+        bottom_ = rect.bottom_;
+
+    if (left_ >= right_ || top_ >= bottom_)
+        *this = IntRect();
+}
+
+void IntRect::Merge(const IntRect& rect)
+{
+    if (Width() <= 0 || Height() <= 0)
+    {
+        *this = rect;
+    }
+    else if (rect.Width() > 0 && rect.Height() > 0)
+    {
+        if (rect.left_ < left_)
+            left_ = rect.left_;
+        if (rect.top_ < top_)
+            top_ = rect.top_;
+        if (rect.right_ > right_)
+            right_ = rect.right_;
+        if (rect.bottom_ > bottom_)
+            bottom_ = rect.bottom_;
+    }
+}
+
 String Rect::ToString() const
 {
     char tempBuffer[CONVERSION_BUFFER_LENGTH];

+ 17 - 0
Source/Urho3D/Math/Rect.h

@@ -216,6 +216,15 @@ public:
     {
     }
 
+    /// Construct from minimum and maximum vectors.
+    IntRect(const IntVector2& min, const IntVector2& max) :
+        left_(min.x_),
+        top_(min.y_),
+        right_(max.x_),
+        bottom_(max.y_)
+    {
+    }
+
     /// Construct from coordinates.
     IntRect(int left, int top, int right, int bottom) :
         left_(left),
@@ -264,6 +273,14 @@ public:
             return INSIDE;
     }
 
+    /// Clip with another rect.  Since IntRect does not have an undefined state
+    /// like Rect, return (0, 0, 0, 0) if the result is empty.
+    void Clip(const IntRect& rect);
+
+    /// Merge a rect.  If this rect was empty, become the other rect.  If the
+    /// other rect is empty, do nothing.
+    void Merge(const IntRect& rect);
+
     /// Return integer data.
     const int* Data() const { return &left_; }