IceDragon200

[RUBY]Surface

Apr 15th, 2012
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.90 KB | None | 0 0
  1. #==============================================================================#
  2. # ■ Surface
  3. #==============================================================================#
  4. # // • Created By    : IceDragon
  5. # // • Modified By   : IceDragon
  6. # // • Data Created  : 12/11/2011
  7. # // • Data Modified : 01/20/2012
  8. # // • Version       : 1.0
  9. #==============================================================================#
  10. # This module contains functions for checking the position, x, y within another
  11. # object's area, this also includes methods for:
  12. #   Rect
  13. #   Vector4 (Abstract of Rect)
  14. #   Array
  15. #
  16. # Conversions
  17. #==============================================================================#
  18. # ● Change Log
  19. #     ♣ 12/11/2011 V1.0
  20. #
  21. # ● Change Log
  22. #     ♣ 01/02/2012 V1.0
  23. #         Added
  24. #           Surface.area_rect( *objs )
  25. #           Vector4 (Class) < Rect
  26. #             v4a_to_rect( v4a )
  27. #     ♣ 01/02/2012 V1.0
  28. #         Added
  29. #           vx=(n)
  30. #           vy=(n)
  31. #           vx2=(n)
  32. #           vy2=(n)
  33. #           set_vx(n)
  34. #           set_vy(n)
  35. #           set_vx2(n)
  36. #           set_vy2(n)
  37. #           set_vxy(x,y)
  38. #           set_vxy2(x,y)
  39. #           offset_vert(n=1.0)
  40. #           offset_horz(n=1.0)
  41. #==============================================================================#
  42. module Mixin::Surface
  43.   def rwidth
  44.     self.width
  45.   end
  46.   def rheight
  47.     self.height
  48.   end  
  49.   def vx
  50.     self.x
  51.   end  
  52.   def vy
  53.     self.y
  54.   end  
  55.   def vx=(n)
  56.     self.x = n
  57.   end
  58.   def vy=(n)
  59.     self.y = n
  60.   end  
  61.   def vwidth()
  62.     self.vx + self.rwidth
  63.   end  
  64.   def vheight()
  65.     self.vy + self.rheight
  66.   end
  67.   def vx2
  68.     vwidth
  69.   end  
  70.   def vy2
  71.     vheight
  72.   end  
  73.   def vx2=( n )
  74.     self.x = n - self.rwidth
  75.   end  
  76.   def vy2=( n )
  77.     self.y = n - self.rheight
  78.   end
  79.   def vset(x=nil,y=nil,x2=nil,y2=nil)
  80.     self.vx = x  || self.vx
  81.     self.vy = y  || self.vy
  82.     self.vx2= x2 || self.vx2
  83.     self.vy2= y2 || self.vy2
  84.     self
  85.   end    
  86.   def to_a()   ;return self.vx, self.vy, self.rwidth, self.rheight;end
  87.   def to_v4a() ;return self.vx, self.vy, self.vwidth, self.vheight;end
  88.   def to_v4()  ;Vector4.new(*to_v4a());end
  89.   def to_rect();Rect.new(*to_a());end
  90.   alias rect to_rect
  91.   # // 02/19/2012 [
  92.   def salign(xn=-1,yn=-1,rx=0,ry=0,rwidth=Graphics.width,rheight=Graphics.height)
  93.     case xn
  94.     when -1; # // Do Nothing
  95.     when 0 ; self.x = rx
  96.     when 1 ; self.x = rx + ((rwidth - self.rwidth) / 2)
  97.     when 2 ; self.vx2 = rwidth
  98.     end
  99.     case yn
  100.     when -1; # // Do Nothing
  101.     when 0 ; self.y = ry
  102.     when 1 ; self.y = ry + ((rheight - self.rheight) / 2)
  103.     when 2 ; self.vy2 = rheight
  104.     end
  105.     self
  106.   end  
  107.   # // 02/19/2012 ]
  108.   def offset_vert!(n=1.0)
  109.     self.vy += (self.rheight * n).to_i
  110.     self
  111.   end  
  112.   def offset_horz!(n=1.0)
  113.     self.vx += (self.rwidth * n).to_i
  114.     self
  115.   end
  116.   def offset_vert(n=1.0)
  117.     dup.offset_vert!(n)
  118.   end  
  119.   def offset_horz(n=1.0)
  120.     dup.offset_horz!(n)
  121.   end
  122.   def area
  123.     rwidth * rheight
  124.   end  
  125.   def perimeter
  126.     (rwidth * 2) + (rheight * 2)
  127.   end
  128.   def space_x # // Clamp Space x offset
  129.     0
  130.   end
  131.   def space_y # // Clamp Space y offset
  132.     0
  133.   end
  134.   def space_width
  135.     0
  136.   end  
  137.   def space_height
  138.     0
  139.   end  
  140.   def clamp_to_space()
  141.     v4 = (viewport || Graphics).rect.to_v4
  142.     cx, cw = v4.x + space_x, v4.vwidth - self.rwidth - space_width
  143.     cy, ch = v4.y + space_y, v4.vheight - self.rheight - space_height
  144.     self.x, self.y = self.x.clamp(cx,cw), self.y.clamp(cy,ch)
  145.   end
  146.   # . x . Only works with Padded stuff
  147.   #def _surface_padding()
  148.   #  (respond_to?(:padding) ? padding : 0)
  149.   #end  
  150.   def _surface_padding()
  151.     standard_padding
  152.   end  
  153.   def adjust_x4contents(x,pad=_surface_padding)
  154.     x + pad
  155.   end  
  156.   def adjust_y4contents(y,pad=_surface_padding)
  157.     y + pad
  158.   end  
  159.   def adjust_w4contents(w,pad=_surface_padding)
  160.     w - (pad * 2)
  161.   end  
  162.   def adjust_h4contents(h,pad=_surface_padding)
  163.     h - (pad * 2)
  164.   end
  165.   def adjust_xywh4contents(x,y,w,h)
  166.     return [adjust_x4contents(x),adjust_y4contents(y),adjust_w4contents(w),adjust_h4contents(h)]
  167.   end
  168.   def adjust_rect4contents(rect)
  169.     Rect.new(*adjust_xywh4contents(*rect.to_a))
  170.   end  
  171.   def adjust_x4window(x,pad=_surface_padding)
  172.     x - pad
  173.   end  
  174.   def adjust_y4window(y,pad=_surface_padding)
  175.     y - pad
  176.   end  
  177.   def adjust_w4window(w,pad=_surface_padding)
  178.     w + (pad * 2)
  179.   end  
  180.   def adjust_h4window(h,pad=_surface_padding)
  181.     h + (pad * 2)
  182.   end
  183.   def adjust_xywh4window(x,y,w,h)
  184.     return [adjust_x4window(x),adjust_y4window(y),adjust_w4window(w),adjust_h4window(h)]
  185.   end
  186.   def adjust_rect4contents(rect)
  187.     Rect.new(*adjust_xywh4window(*rect.to_a))
  188.   end  
  189.   # O_O
  190.   def calc_pressure_horz( n, invert=false )
  191.     return 0 if n < self.x || n > self.vwidth
  192.     n = n - self.x
  193.     n2 = (self.vwidth - self.x)
  194.     n = n2 - n if invert
  195.     n = n / n2.to_f
  196.     return n
  197.   end
  198.   def calc_pressure_vert( n, invert=false )
  199.     return 0 if n < self.y || n > self.vheight
  200.     n = n - self.y
  201.     n2 = (self.vheight - self.y)
  202.     n = n2 - n if invert
  203.     n = n / n2.to_f
  204.     return n
  205.   end
  206.   def in_area?( ax, ay )
  207.     return ax.between?( self.vx, self.vwidth ) &&
  208.       ay.between?( self.vy, self.vheight )
  209.   end
  210.   def intersect?( v4 )  
  211.     return in_area?( v4.x, v4.y ) || in_area?( v4.width, v4.height )
  212.   end  ##
  213. end
  214. class Surface
  215.   include Mixin::RectExpansion
  216.   include Mixin::RectExpansion::RectOnly
  217.   include Mixin::Surface
  218.   def self.area_rect( *objs )
  219.     mx = objs.min { |a, b| a.x <=> b.x }
  220.     my = objs.min { |a, b| a.y <=> b.y }
  221.     mw = objs.max { |a, b| a.vwidth <=> b.vwidth }
  222.     mh = objs.max { |a, b| a.vheight <=> b.vheight }
  223.     return Vector4.v4a_to_rect( [mx.x, my.y, mw.vwidth, mh.vheight] )
  224.   end
  225.   def initialize(x=0,y=0,width=0,height=0)
  226.     self.x,self.y,self.width,self.height = x, y, width, height
  227.   end  
  228.   def set(*args)
  229.     obj,=args
  230.     a = (obj.respond_to?(:to_rect)) ? obj.to_rect.to_a : args
  231.     self.x,self.y,self.width,self.height = a
  232.   end  
  233. end
  234. class Rect
  235.   include Mixin::Surface
  236.   def to_rect ; self.dup ; end
  237.   def empty? ; return self.width == 0 || self.height == 0 ; end unless method_defined? :empty?
  238. end  
  239. class Vector4 < Rect
  240.   def rwidth
  241.     self.width - self.x
  242.   end
  243.   def rheight
  244.     self.height - self.y
  245.   end  
  246.   def vwidth
  247.     self.width
  248.   end  
  249.   def vheight
  250.     self.height
  251.   end  
  252.   def self.v4a_to_rect( v4a )
  253.     return Rect.new( v4a[0], v4a[1], v4a[2]-v4a[0], v4a[3]-v4a[1] )
  254.   end  
  255. end
  256. class Window
  257.   include Mixin::Surface
  258. end  
  259. class Sprite
  260.   include Mixin::Surface
  261. end  
  262. #=■==========================================================================■=#
  263. #                           // ● End of File ● //                              #
  264. #=■==========================================================================■=#
Advertisement
Add Comment
Please, Sign In to add comment