Class: Moon::Rect

Inherits:
Object show all
Includes:
Serializable, Serializable::Properties
Defined in:
lib/moon/packages/std/rect.rb

Defined Under Namespace

Modules: Cast

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Rect

Returns a new instance of Rect.


43
44
45
46
47
48
49
# File 'lib/moon/packages/std/rect.rb', line 43

def initialize(*args)
  if args.empty?
    initialize_xywh 0, 0, 0, 0
  else
    initialize_xywh(*args)
  end
end

Class Method Details

.[](*objs) ⇒ Object


173
174
175
176
# File 'lib/moon/packages/std/rect.rb', line 173

def self.[](*objs)
  obj = objs.size == 1 ? objs.first : objs
  new(*extract(obj))
end

.extract(obj) ⇒ Array<Integer>

Extracts Rect related arguments from the given Object (obj)

Parameters:

Returns:


169
170
171
# File 'lib/moon/packages/std/rect.rb', line 169

def self.extract(obj)
  Cast.extract(obj)
end

Instance Method Details

#&(other) ⇒ Object


80
81
82
83
84
# File 'lib/moon/packages/std/rect.rb', line 80

def &(other)
  nx  = [x, other.x].max
  ny  = [y, other.y].max
  Rect.new nx, ny, [x2, other.x2].min - nx, [y2, other.y2].min - ny
end

#clearObject


68
69
70
71
72
73
74
# File 'lib/moon/packages/std/rect.rb', line 68

def clear
  self.x = 0
  self.y = 0
  self.w = 0
  self.h = 0
  self
end

#contains?(*args) ⇒ Boolean

Returns:


63
64
65
66
# File 'lib/moon/packages/std/rect.rb', line 63

def contains?(*args)
  x, y = *Vector2.extract(args.singularize)
  x.between?(self.x, self.x2 - 1) && y.between?(self.y, self.y2 - 1)
end

#empty?Boolean

Returns:


76
77
78
# File 'lib/moon/packages/std/rect.rb', line 76

def empty?
  return w == 0 && h == 0
end

#initialize_xywhObject


42
# File 'lib/moon/packages/std/rect.rb', line 42

alias :initialize_xywh :initialize

#positionObject


149
150
151
# File 'lib/moon/packages/std/rect.rb', line 149

def position
  Vector2.new x, y
end

#position=(other) ⇒ Object


153
154
155
# File 'lib/moon/packages/std/rect.rb', line 153

def position=(other)
  self.x, self.y = *Vector2.extract(other)
end

#resolutionObject


157
158
159
# File 'lib/moon/packages/std/rect.rb', line 157

def resolution
  Vector2.new w, h
end

#resolution=(other) ⇒ Object


161
162
163
# File 'lib/moon/packages/std/rect.rb', line 161

def resolution=(other)
  self.w, self.h = *Vector2.extract(other)
end

#scale(sx, sy) ⇒ Rect

Parameters:

Returns:


119
120
121
122
123
# File 'lib/moon/packages/std/rect.rb', line 119

def scale(*args)
  r = Rect.new x, y, w, h
  r.resolution = r.resolution * Vector2[args.singularize]
  r
end

#set(*args) ⇒ Object


86
87
88
# File 'lib/moon/packages/std/rect.rb', line 86

def set(*args)
  self.x, self.y, self.w, self.h = *Rect.extract(args.singularize)
end

#splitArray[4]<Moon::Rect>

Splits the current rect into 4 sub rects of half size

Returns:


54
55
56
57
58
59
60
61
# File 'lib/moon/packages/std/rect.rb', line 54

def split
  sw = (w / 2.0).round
  sh = (h / 2.0).round
  return Rect.new(x + sw, y, sw, sh),
    Rect.new(x, y, sw, sh),
    Rect.new(x, y + sh, sw, sh),
    Rect.new(x + sw, y + sh, sw, sh)
end

#to_hObject


129
130
131
# File 'lib/moon/packages/std/rect.rb', line 129

def to_h
  { x: x, y: y, w: w, h: h }
end

#to_sObject


125
126
127
# File 'lib/moon/packages/std/rect.rb', line 125

def to_s
  "#{x},#{y},#{w},#{h}"
end

#translate(tx, ty) ⇒ Rect

Creates a new Rect from the current translated by the given position

Parameters:

Returns:


96
97
98
99
100
# File 'lib/moon/packages/std/rect.rb', line 96

def translate(*args)
  r = Rect.new x, y, w, h
  r.position += Vector2[args.singularize]
  r
end

#translatef(tx, ty) ⇒ Rect

Same as translate, however the provided position will be scaled against the Rect's resolution

Parameters:

Returns:


109
110
111
112
113
# File 'lib/moon/packages/std/rect.rb', line 109

def translatef(*args)
  r = Rect.new x, y, w, h
  r.position += r.resolution * Vector2[args.singularize]
  r
end

#x2Object


133
134
135
# File 'lib/moon/packages/std/rect.rb', line 133

def x2
  x + w
end

#x2=(x2) ⇒ Object


137
138
139
# File 'lib/moon/packages/std/rect.rb', line 137

def x2=(x2)
  self.x = x2 - w
end

#y2Object


141
142
143
# File 'lib/moon/packages/std/rect.rb', line 141

def y2
  y + h
end

#y2=(y2) ⇒ Object


145
146
147
# File 'lib/moon/packages/std/rect.rb', line 145

def y2=(y2)
  self.y = y2 - h
end