Class: Applitools::Region

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/applitools/core/region.rb

Direct Known Subclasses

FloatingRegion

Constant Summary collapse

EMPTY =
Region.new(0, 0, 0, 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left, top, width, height) ⇒ Region

Returns a new instance of Region.



18
19
20
21
22
23
24
# File 'lib/applitools/core/region.rb', line 18

def initialize(left, top, width, height)
  @left = left.round
  @top = top.round
  @width = width.round
  @height = height.round
  @padding = Applitools::PaddingBounds::ZERO_PADDING
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



5
6
7
# File 'lib/applitools/core/region.rb', line 5

def height
  @height
end

#leftObject Also known as: x

Returns the value of attribute left.



5
6
7
# File 'lib/applitools/core/region.rb', line 5

def left
  @left
end

#topObject Also known as: y

Returns the value of attribute top.



5
6
7
# File 'lib/applitools/core/region.rb', line 5

def top
  @top
end

#widthObject

Returns the value of attribute width.



5
6
7
# File 'lib/applitools/core/region.rb', line 5

def width
  @width
end

Class Method Details

.from_location_size(location, size) ⇒ Object



13
14
15
# File 'lib/applitools/core/region.rb', line 13

def from_location_size(location, size)
  new location.x, location.y, size.width, size.height
end

.sub_regions_with_fixed_size(container_region, sub_region) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/applitools/core/region.rb', line 142

def sub_regions_with_fixed_size(container_region, sub_region)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  sub_region_width = sub_region.width
  sub_region_height = sub_region.height

  # Normalizing.
  sub_region_width = container_region.width if sub_region_width > container_region.width
  sub_region_height = container_region.height if sub_region_height > container_region.height

  if sub_region_width == container_region.width && sub_region_height == container_region.height
    return Enumerator(1) do |y|
      y << sub_region
    end
  end

  current_top = container_region.top
  bottom = container_region.top + container_region.height - 1
  right = container_region.left + container_region.width - 1
  Enumerator.new do |y|
    while current_top <= bottom
      current_top = (bottom - sub_region_height) + 1 if current_top + sub_region_height > bottom
      current_left = container_region.left
      while current_left <= right
        current_left = (rught - sub_region_width) + 1 if current_left + sub_region_width > right
        y << new(current_left, current_top, sub_region_width, sub_region_height)
        current_left += sub_region_width
      end
      current_top += sub_region_height
    end
  end
end

.sub_regions_with_varying_size(container_region, sub_region) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/applitools/core/region.rb', line 179

def sub_regions_with_varying_size(container_region, sub_region)
  Applitools::ArgumentGuard.not_nil container_region, 'container_region'
  Applitools::ArgumentGuard.not_nil sub_region, 'sub_region'

  Applitools::ArgumentGuard.greater_than_zero(sub_region.width, 'sub_region.width')
  Applitools::ArgumentGuard.greater_than_zero(sub_region.height, 'sub_region.height')

  current_top = container_region.top
  bottom = container_region.top + container_region.height
  right = container_region.left + container_region.width

  Enumerator.new do |y|
    while current_top < bottom
      current_bottom = current_top + sub_region.height
      current_bottom = bottom if current_bottom > bottom
      current_left = container_region.left
      while current_left < right
        current_right = current_left + sub_region.width
        current_right = right if current_right > right

        current_height = current_bottom - current_top
        current_width = current_right - current_left

        y << new(current_left, current_top, current_width, current_height)

        current_left += sub_region.width
      end
      current_top += sub_region.height
    end
  end
end

Instance Method Details

#bottomObject



56
57
58
# File 'lib/applitools/core/region.rb', line 56

def bottom
  top + height
end

#contains?(other_left, other_top) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/applitools/core/region.rb', line 84

def contains?(other_left, other_top)
  other_left >= left && other_left <= right && other_top >= top && other_top <= bottom
end

#empty?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/applitools/core/region.rb', line 35

def empty?
  @left == EMPTY.left && @top == EMPTY.top && @width == EMPTY.width && @height == EMPTY.height
end

#intersect(other) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/applitools/core/region.rb', line 65

def intersect(other)
  unless intersecting?(other)
    make_empty

    return
  end

  i_left = left >= other.left ? left : other.left
  i_right = right <= other.right ? right : other.right
  i_top = top >= other.top ? top : other.top
  i_bottom = bottom <= other.bottom ? bottom : other.bottom

  @left = i_left
  @top = i_top
  @width = i_right - i_left
  @height = i_bottom - i_top
  self
end

#intersecting?(other) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/applitools/core/region.rb', line 60

def intersecting?(other)
  ((left <= other.left && other.left <= right) || (other.left <= left && left <= other.right)) &&
    ((top <= other.top && other.top <= bottom) || (other.top <= top && top <= other.bottom))
end

#locationObject



43
44
45
# File 'lib/applitools/core/region.rb', line 43

def location
  Applitools::Location.new left, top
end

#location=(other_location) ⇒ Object



47
48
49
50
# File 'lib/applitools/core/region.rb', line 47

def location=(other_location)
  self.left = other_location.left
  self.top = other_location.top
end

#make_emptyObject



28
29
30
31
32
33
# File 'lib/applitools/core/region.rb', line 28

def make_empty
  @left = EMPTY.left
  @top = EMPTY.top
  @width = EMPTY.width
  @height = EMPTY.height
end

#middle_offsetObject



88
89
90
91
92
# File 'lib/applitools/core/region.rb', line 88

def middle_offset
  mid_x = width / 2
  mid_y = height / 2
  Applitools::Location.for(mid_x.round, mid_y.round)
end

#padding(padding = nil) ⇒ Applitools::Region

Sets padding for a current region. If called without any argument, all paddings will be set to 0

Parameters:

Returns:



127
128
129
130
131
132
# File 'lib/applitools/core/region.rb', line 127

def padding(padding = nil)
  padding = Applitools::PaddingBounds::ZERO_PADDING unless padding
  Applitools::ArgumentGuard.is_a?(padding, 'padding', Applitools::PaddingBounds)
  @padding = padding
  self
end

#rightObject



52
53
54
# File 'lib/applitools/core/region.rb', line 52

def right
  left + width
end

#scale_it!(scale_factor) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/applitools/core/region.rb', line 94

def scale_it!(scale_factor)
  @left = (@left * scale_factor).to_i
  @top = (@top * scale_factor).to_i
  @width = (@width * scale_factor).to_i
  @height = (@height * scale_factor).to_i
  self
end

#sizeObject



39
40
41
# File 'lib/applitools/core/region.rb', line 39

def size
  Applitools::RectangleSize.new width, height
end

#size_equals?(region) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/applitools/core/region.rb', line 120

def size_equals?(region)
  width == region.width && height == region.height
end

#sub_regions(subregion_size, is_fixed_size = false) ⇒ Object



102
103
104
105
# File 'lib/applitools/core/region.rb', line 102

def sub_regions(subregion_size, is_fixed_size = false)
  return self.class.sub_regions_with_fixed_size self, subregion_size if is_fixed_size
  self.class.sub_regions_with_varying_size self, subregion_size
end

#to_hashObject



107
108
109
110
111
112
113
114
# File 'lib/applitools/core/region.rb', line 107

def to_hash
  {
    left: left,
    top: top,
    height: height,
    width: width
  }
end

#to_sObject



116
117
118
# File 'lib/applitools/core/region.rb', line 116

def to_s
  "(#{left}, #{top}), #{width} x #{height}"
end

#with_paddingObject



134
135
136
137
138
139
# File 'lib/applitools/core/region.rb', line 134

def with_padding
  Applitools::Region.from_location_size(
    Applitools::Location.new(left - padding_left, top - padding_top),
    Applitools::RectangleSize.new(width + padding_left + padding_right, height + padding_top + padding_bottom)
  )
end