IceDragon200

[RUBY]Mixin::RectExpansion

Apr 15th, 2012
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.96 KB | None | 0 0
  1. #==============================================================================#
  2. # ■ Mixin::RectExpansion
  3. #==============================================================================#
  4. # // • Created By    : IceDragon
  5. # // • Modified By   : IceDragon
  6. # // • Data Created  : 12/20/2011
  7. # // • Data Modified : 01/04/2012
  8. # // • Version       : 1.1
  9. #==============================================================================#
  10. # ● Change Log
  11. #     ♣ 12/20/2011 V1.0
  12. #         Added
  13. #           contract!( n )
  14. #           expand!( n )
  15. #           contract( n )
  16. #           expand( n )
  17. #           to_a()
  18. #
  19. #     ♣ 12/28/2011 V1.0
  20. #         Added
  21. #           contract_horz!( n )
  22. #           contract_vert!( n )
  23. #           contract_horz( n )
  24. #           contract_vert( n )
  25. #           expand_horz!( n )
  26. #           expand_vert!( n )
  27. #           expand_horz( n )
  28. #           expand_vert( n )
  29. #
  30. #     ♣ 01/02/2012 V1.0
  31. #         Added
  32. #           Rect.center( parent_rect, child_rect )
  33. #           center( child_rect )
  34. #
  35. #     ♣ 01/02/2012 V1.0
  36. #         Added
  37. #           squeeze_vert( n, invert=false )
  38. #           squeeze_vert!( n, invert=false )
  39. #           squeeze_horz( n, invert=false )
  40. #           squeeze_horz!( n, invert=false )
  41. #           release_vert( n, invert=false )
  42. #           release_vert!( n, invert=false )
  43. #           release_horz( n, invert=false )
  44. #           release_horz!( n, invert=false )
  45. #     ♣ 04/14/2012 V1.0
  46. #         Changed
  47. #           squeeze()
  48. #           release()
  49. #
  50. #==============================================================================#
  51. class Rect
  52.   def self.center( prect, crect )
  53.     Rect.new(
  54.       prect.x+((prect.width-crect.width)/2),
  55.       prect.y+((prect.height-crect.height)/2),
  56.       crect.width, crect.height
  57.     )
  58.   end
  59. end
  60. module Mixin::RectExpansion
  61.   SYM_ARGS = [:x,:y,:width,:height]
  62.   def center( child_rect )
  63.     Rect.center( self, child_rect )
  64.   end    
  65.   def to_a();return self.x, self.y, self.width, self.height;end
  66.   def xto_a(*args,&block)
  67.     (args&SYM_ARGS).collect(&(block_given? ? block : proc{|sym|self.send(sym)}))
  68.   end  
  69.   def xto_h(*args);Hash[xto_a(*args){|sym|[sym,self.send(sym)]}];end
  70.   def cx();x+(width/2);end
  71.   def cy();y+(height/2);end
  72. end
  73. module Mixin::RectExpansion::RectOnly
  74.   def xset(x=nil,y=nil,w=nil,h=nil)
  75.     x,y,w,h = x.get_values(:x,:y,:width,:height) if(x.is_a?(Hash))
  76.     set(x||self.x,y||self.y,w||self.width,h||self.height);self
  77.   end
  78.   # // Destructive
  79.   def contract!(n=0,orn=0)
  80.     self.x      += n   if(orn==0 || orn==1)
  81.     self.y      += n   if(orn==0 || orn==2)
  82.     self.width  -= n*2 if(orn==0 || orn==1)
  83.     self.height -= n*2 if(orn==0 || orn==2)
  84.     self
  85.   end  
  86.   def expand!(n=0)
  87.     contract!( -n )
  88.   end
  89.   def squeeze!(n=0,invert=false,orn=0)
  90.     n = n.round(0)
  91.     unless(invert)
  92.       self.x += n if(orn==0 || orn==1)
  93.       self.y += n if(orn==0 || orn==2)
  94.     end  
  95.     self.width  -= n if(orn==0 || orn==1)
  96.     self.height -= n if(orn==0 || orn==2)
  97.     self
  98.   end
  99.   def release!(n=1,invert=false,orn=0)
  100.     squeeze!(-n,invert,orn)
  101.   end  
  102.   def xpush!(n,orn=0)
  103.     self.x += n if(orn==0 || orn==1)
  104.     self.y += n if(orn==0 || orn==2)
  105.     self
  106.   end  
  107.   def xpull!(n,orn=0)
  108.     xpush(-n,orn)
  109.   end  
  110.   # // . x . Dup
  111.   def contract(n=1,orn=0)
  112.     dup.contract!(n,orn)
  113.   end  
  114.   def expand(n=1,orn=0)
  115.     dup.expand!(n,orn)
  116.   end
  117.   def squeeze(n=1,invert=false,orn=0)
  118.     dup.squeeze!(n,invert,orn)
  119.   end  
  120.   def release(n=1,invert=false,orn=0)
  121.     dup.release!(n,invert,orn)
  122.   end  
  123.   # // 03/05/2012
  124.   def xpush(n,orn=0)  
  125.     dup.xpush!(n,orn)
  126.   end
  127.   def xpull(n,orn=0)  
  128.     dup.xpull!(n,orn)
  129.   end  
  130. end
  131. #=■==========================================================================■=#
  132. #                           // ● End of File ● //                              #
  133. #=■==========================================================================■=#
Advertisement
Add Comment
Please, Sign In to add comment