Jelajahi Sumber

Just some functions for rectangles.
Added some credits to `dub.json`

electric-gecko 1 tahun lalu
induk
melakukan
a30fe4a9e5
2 mengubah file dengan 76 tambahan dan 1 penghapusan
  1. 1 1
      dub.json
  2. 75 0
      source/raylib/raylib_types.d

+ 1 - 1
dub.json

@@ -18,7 +18,7 @@
                 "lflags-linux" : ["-rpath=$$ORIGIN"]
             }
         ],
-	"copyright": "Copyright (c) Ramon Santamaria (@raysan5), Petro Romanovych (@onroundit), Jan Hoenig (@m3m0ry), Steven Schveighoffer (@schveiguy)",
+	"copyright": "Copyright (c) Ramon Santamaria (@raysan5), Petro Romanovych (@onroundit), Jan Hoenig (@m3m0ry), Steven Schveighoffer (@schveiguy), Liam McGillivray (@LiamM32)",
         "excludedSourceFiles": ["source/rlgl.d", "source/raymath.d", "source/easings.d", "source/raymathext.d", "source/raylib_types.d"],
 	"targetType": "sourceLibrary",
         "subPackages" : [

+ 75 - 0
source/raylib/raylib_types.d

@@ -63,6 +63,51 @@ struct Rectangle
     float height;
     alias w = width;
     alias h = height;
+
+    Vector2 origin() { // Rectangle function exclusive to raylib-d
+        return Vector2(x, y);
+    }
+    alias position = origin;
+    
+    Vector2 dimensions() {
+        return Vector2(width, height);
+    }
+
+    alias topLeft = origin;
+
+    Vector2 topRight() const {
+        return Vector2(x:(x + width), y:y);
+    }
+
+    Vector2 bottomLeft() const {
+        return Vector2(x:x, y:(y + height));
+    }
+
+    Vector2 bottomRight() const {
+        return Vector2(x:(x + width), y:(y + height));
+    }
+
+    Vector2 center() const {
+        return Vector2(x:(x + width/2.0f), y:(y + height/2.0f));
+    }
+    alias centre = center;
+
+    void opOpAssign(string op)(Vector2 offset) if (op == "+" || op == "-") {
+        mixin("this.x ", op, "= offset.x;");
+        mixin("this.y ", op, "= offset.y;");
+    }
+
+    Rectangle opBinary(string op)(Vector2 offset) const if(op=="+" || op=="-") {
+        Rectangle result = this;
+        static if (op=="+") {
+            result.x += offset.x;
+            result.y += offset.y;
+        } else static if (op=="-") {
+            result.x -= offset.x;
+            result.y -= offset.y;
+        }
+        return result;
+    }
 }
 
 enum Colors
@@ -97,3 +142,33 @@ enum Colors
     MAGENTA = Color(255, 0, 255, 255), // Magenta
     RAYWHITE = Color(245, 245, 245, 255), // My own White (raylib logo)
 }
+
+unittest
+{
+    import std.conv;
+    
+    float x = 543.3f;
+    float y = 235.9f;
+    float width = 50.0f;
+    float height = 20.0f;
+    Rectangle rect = Rectangle(x, y, width, height);
+    assert(rect.origin.x == 543.3f, "`rect.origin.x` should be 543.3, not "~to!string(rect.origin.x));
+    assert(rect.origin.y == 235.9f, "`rect.centre.y` should be 235.9, not "~to!string(rect.origin.y));
+    assert(rect.dimensions == Vector2(50.0f, 20.0f));
+    assert(rect.topLeft == Vector2(x:543.3f, y:235.9f), "`rect.topLeft` should be Vector2(543.3, 235.9), not "~to!string(rect.topLeft));
+    assert(rect.topRight.x == 593.3f);
+    assert(rect.bottomLeft == Vector2(543.3f, 255.9f), "`rect.bottomLeft` should be Vector2(543.3, 255.9), not "~to!string(rect.bottomLeft));
+    assert(rect.bottomRight == Vector2(593.3f, 255.9f), "`rect.bottomRight` should be Vector2(593.3, 255.9), not "~to!string(rect.bottomRight));
+    assert(rect.centre == Vector2(568.3f, 245.9f), "`rect.centre` should be Vector2(568.3, 245.9), not "~to!string(rect.centre));
+
+    x += 26.3f;
+    y += 43.2f;
+    assert((rect + Vector2(26.3f, 0.0f)).x == rect.origin.x + 26.3f);
+    rect.x = 10.0f;
+    rect.y = 14.0f;
+    rect += Vector2(19.0f, 7.3f);
+    assert(rect.origin == Vector2(29.0f, 21.3f));
+    rect -= Vector2(4.5f, 2.3f);
+    assert(rect.x == 24.5f, "`rect.x` should be 24.5f. Instead it is "~to!string(rect.x));
+    assert(rect.y == 19.0f, "`rect.y` should be 20.7f. Instead it is "~to!string(rect.y));
+}