Advertisement
Kernell

Untitled

Sep 14th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.17 KB | None | 0 0
  1. -- Innovation Roleplay Engine
  2. --
  3. -- Author       Kernell
  4. -- Copyright        © 2011 - 2014
  5. -- License      Proprietary Software
  6. -- Version      1.0
  7.  
  8. class: Rectangle
  9. {
  10.     X       = NULL;
  11.     Y       = NULL;
  12.     Width       = NULL;
  13.     Height      = NULL;
  14.    
  15.     Rectangle   = function( this, ... )
  16.         if table.getn( { ... } ) == 2 then
  17.             return this:FromPS( ... );
  18.         elseif table.getn( { ... } ) == 4 then
  19.             return this:FromXYWH( ... );
  20.         end
  21.     end;
  22.    
  23.     FromXYWH    = function( this, X, Y, Width, Height )
  24.         this.X      = X;
  25.         this.Y      = Y;
  26.         this.Width  = Width;
  27.         this.Height = Height;
  28.     end;
  29.  
  30.     FromPS      = function( this, Point, Size )
  31.         this.x      = Point.X;
  32.         this.y      = Point.Y;
  33.         this.width  = Size.Width;
  34.         this.height = Size.Height;
  35.     end;
  36.  
  37.     FromLTRB    = function( this, left, top, right, bottom )
  38.         return Rectangle( left, top, right - left, bottom - top );
  39.     end;
  40.    
  41.     property: Left
  42.     {
  43.         get = function( this )
  44.             return this.X;
  45.         end;
  46.     };
  47.    
  48.     property: Top
  49.     {
  50.         get = function( this )
  51.             return this.Y;
  52.         end;
  53.     };
  54.    
  55.     property: Right
  56.     {
  57.         get = function( this )
  58.             return this.X + this.Width;
  59.         end;
  60.     };
  61.    
  62.     property: Bottom
  63.     {
  64.         get = function( this )
  65.             return this.Y + this.Height;
  66.         end;
  67.     };
  68.    
  69.     property: IsEmpty
  70.     {
  71.         get = function( this )
  72.             return this.Height == 0 and this.Width == 0 and this.X == 0 and this.Y == 0;
  73.         end;
  74.     };
  75.    
  76.     Equals  = function( this, obj )
  77.         if classof( obj ) ~= Rectangle then
  78.             return false;
  79.         end
  80.        
  81.         return obj.X == this.X and obj.Y == this.Y and obj.Width == this.Width and obj.Height == this.Height;
  82.     end;
  83.    
  84.     static
  85.     {
  86.         Ceiling     = function( value )
  87.             return Rectangle( math.ceil( value.X ), math.ceil( value.Y ), math.ceil( value.Width ), math.ceil( value.Height ) );
  88.         end;
  89.        
  90.         Truncate    = function( value )
  91.             return Rectangle( (int)(value.X), (int)(value.Y), (int)(value.Width), (int)(value.Height) );
  92.         end;
  93.        
  94.         Round       = function( value )
  95.             return Rectangle( math.round( value.X ), math.round( value.Y ), math.round( value.Width ), math.round( value.Height ) );
  96.         end;
  97.    
  98.         Union       = function( a, b )
  99.             local x1 = math.min( a.X, b.X );
  100.             local x2 = math.max( a.X + a.Width, b.X + b.Width );
  101.             local y1 = math.min( a.Y, b.Y );
  102.             local y2 = math.max( a.Y + a.Height, b.Y + b.Height );
  103.            
  104.             return Rectangle( x1, y1, x2 - x1, y2 - y1 );
  105.         end;
  106.     };
  107.    
  108.     Contains    = function( this, arg1, arg2 )
  109.         if type( arg1 ) == "number" and type( arg2 ) == "number" then
  110.             return this:Contains_XY( arg1, arg2 );
  111.         elseif classof( arg1 ) == Point then
  112.             return this:Contains_Point( arg1 );
  113.         elseif classof( arg1 ) == Rectangle then
  114.             return this:Contains_Rectangle( arg1 );
  115.         end
  116.        
  117.         return false;
  118.     end;
  119.    
  120.     Contains_XY = function( this, X, Y )
  121.         return this.X <= X and X < this.X + this.Width and this.Y <= Y and Y < this.Y + this.Height;
  122.     end;
  123.    
  124.     Contains_Point  = function( this, pt )
  125.         return this:ContainsXY( pt.X, pt.Y );
  126.     end;
  127.    
  128.     Contains_Rectangle  = function( this, rect )
  129.         return ( this.X <= rect.X ) and ( ( rect.X + rect.Width ) <= ( this.X + this.Width ) ) and ( this.Y <= rect.Y ) and ( ( rect.Y + rect.Height ) <= ( this.Y + this.Height ) );
  130.     end;
  131.    
  132.     Inflate         = function( this, arg1, arg2, arg3 )
  133.         if type( arg1 ) == "number" and type( arg2 ) == "number" then
  134.             this:Inflate_WH( arg1, arg2 );
  135.         elseif classof( arg1 ) == Size then
  136.             this:Inflate_Size( arg1 );
  137.         elseif classof( arg1 ) == Rectangle then
  138.             this:Inflate_Rectangle( arg1, arg2, arg3 );
  139.         end
  140.     end;
  141.    
  142.     Inflate_WH      = function( this, width, height )
  143.         this.X      = this.X - width;
  144.         this.Y      = this.Y - height;
  145.         this.Width  = this.Width + 2 * width;
  146.         this.Height = this.Height + 2 * height;
  147.     end;
  148.    
  149.     Inflate_Size    = function( this, size )
  150.         this:Inflate_WH( size.Width, size.Height );
  151.     end;
  152.    
  153.     Inflate_Rectangle   = function( this, rect, x, y )
  154.         local r = rect;
  155.        
  156.         r:Inflate( x, y );
  157.        
  158.         return r;
  159.     end;
  160.    
  161.     Intersect       = function( this, rect )
  162.         local result = Rectangle.Intersect2( rect, this );
  163.  
  164.         this.X      = result.X;
  165.         this.Y      = result.Y;
  166.         this.Width  = result.Width;
  167.         this.Height = result.Height;
  168.     end;
  169.    
  170.     Intersect2      = function( a, b )
  171.         local x1    = math.max( a.X, b.X );
  172.         local x2    = math.min( a.X + a.Width, b.X + b.Width );
  173.         local y1    = math.max( a.Y, b.Y );
  174.         local y2    = math.min( a.Y + a.Height, b.Y + b.Height );
  175.  
  176.         if x2 >= x1 and y2 >= y1 then
  177.             return Rectangle( x1, y1, x2 - x1, y2 - y1 );
  178.         end
  179.        
  180.         return Rectangle.Empty;
  181.     end;
  182.    
  183.     IntersectsWith  = function( this, rect )
  184.         return ( rect.X < this.X + this.Width ) and ( this.X < ( rect.X + rect.Width ) ) and ( rect.Y < this.Y + this.Height ) and ( this.Y < rect.Y + rect.Height );
  185.     end;
  186.    
  187.     Offset      = function( this, arg1, arg2 )
  188.         if type( arg1 ) == "number" and type( arg2 ) == "number" then
  189.             this:Offset_XY( arg1, arg2 );
  190.         elseif classof( arg1 ) == Point then
  191.             this:Offset_Point( arg1 );
  192.         end
  193.     end;
  194.    
  195.     Offset_Point    = function( this, pos )
  196.         this:Offset( pos.X, pos.Y );
  197.     end;
  198.    
  199.     Offset_XY       = function( this, x, y )
  200.         this.X = this.X + x;
  201.         this.Y = this.Y + y;
  202.     end;
  203.    
  204.     ToString    = function( this )
  205.         return "{X=" + this.X + ",Y=" + this.Y + ",Width=" + this.Width + ",Height=" + this.Height + "}";
  206.     end;
  207. };
  208.  
  209. Rectangle.__eq      = Rectangle.Equals;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement