Class: Samscript::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/samscript/screen.rb

Constant Summary collapse

DefaultParams =
{ match: true,
tolerance: 0,
interval: 100,
timeout: 1000 }

Class Method Summary collapse

Class Method Details

.capture(area = resolution) ⇒ Object



17
18
19
20
21
# File 'lib/samscript/screen.rb', line 17

def self.capture area = resolution
  rect = Rectangle.new area
  @robot ||= Robot.new
  @robot.create_screen_capture rect
end

.resolutionObject



12
13
14
15
# File 'lib/samscript/screen.rb', line 12

def self.resolution
  @tk ||= Java::JavaAwt::Toolkit.get_default_toolkit
  @tk.get_screen_size
end

.wait_for_pixel(params = {}) {|false| ... } ⇒ Object

Yields:

  • (false)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/samscript/screen.rb', line 23

def self.wait_for_pixel params = {}
  %w(location colour).map(&:to_sym).each do |sym|
    raise "invalid params" if params[sym].nil?
  end

  x, y      = params[:location][0], params[:location][1]
  colour    = params[:colour]
  tolerance = params[:tolerance] || DefaultParams[:tolerance]
  match     = params[:match]     || DefaultParams[:match]
  interval  = params[:interval]  || DefaultParams[:interval]
  timeout   = params[:timeout]   || DefaultParams[:timeout]
  attempts  = (timeout/interval+1).floor

  return if match == :any or tolerance >= 255

  attempts.times do
    screen = self.capture
    pixel = Samscript::Image.get_pixel(screen, x: x, y: y)
    if Samscript::Image.get_difference(pixel, colour) <= tolerance
      yield(true) if block_given?
      return
    end

    sleep(interval.to_f/1000)
  end

  yield(false) if block_given?
end