samscript 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .tags
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in samscript.gemspec
4
+ gemspec
5
+
6
+ raise "gem must be run under jruby" unless RUBY_PLATFORM == "java"
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sam Broughton
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Samscript
2
+
3
+ [![Code Climate](https://codeclimate.com/github/sambooo/Samscript.png)](https://codeclimate.com/github/sambooo/Samscript)
4
+
5
+ A gem to replace AHK/Futscript for RS things
6
+
7
+ ## Installation
8
+
9
+ This gem will only work under JRuby.
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'samscript'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install samscript
22
+
23
+ ## Example Usage
24
+
25
+ The below script simply checks if the pixel at the mouse cursor is close to black every 100ms for 1 second, and prints true/false to console depending on the success.
26
+
27
+ ```ruby
28
+ require 'samscript'
29
+
30
+ Screen::wait_for_pixel(colour: java.awt.Color::BLACK,
31
+ location: Mouse.position(as: :vector)
32
+ tolerance: 10
33
+ interval: 100
34
+ timeout: 1000) { |status| puts status }
35
+ ```
36
+
37
+ ## Contributing
38
+
39
+ 1. Fork it
40
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
41
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
42
+ 4. Push to the branch (`git push origin my-new-feature`)
43
+ 5. Create new Pull Request
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Build CTags"
4
+ task :ctags do
5
+ paths = Bundler.load.specs.map(&:full_gem_path)
6
+ system("ctags -R -f .tags #{paths.join(' ')}")
7
+ end
@@ -0,0 +1,9 @@
1
+ require 'samscript/version'
2
+ require 'samscript/screen'
3
+ require 'samscript/image'
4
+ require 'samscript/mouse'
5
+ require 'samscript/wait_matcher'
6
+ require 'samscript/helpers'
7
+
8
+ module Samscript
9
+ end
@@ -0,0 +1,2 @@
1
+ Mouse = Samscript::Mouse
2
+ Screen = Samscript::Screen
@@ -0,0 +1,21 @@
1
+ require 'matrix'
2
+
3
+ module Samscript
4
+ class Image
5
+ java_import 'java.awt.Color'
6
+
7
+ def self.get_pixel image, params = {}
8
+ x = params[:x] || 0
9
+ y = params[:y] || 0
10
+
11
+ rgb = image.get_rgb(x, y)
12
+ Color.new rgb
13
+ end
14
+
15
+ def self.get_difference c1, c2
16
+ [c1.red - c2.red,
17
+ c1.green - c2.green,
18
+ c1.blue - c2.blue].map(&:abs).max
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,43 @@
1
+ require 'matrix'
2
+
3
+ module Samscript
4
+ class Mouse
5
+ java_import 'java.awt.MouseInfo'
6
+ java_import 'java.awt.Robot'
7
+ java_import 'java.awt.Point'
8
+ java_import 'java.awt.event.InputEvent'
9
+
10
+ def self.robot
11
+ @robot ||= Robot.new
12
+ end
13
+
14
+ def self.position params = {}
15
+ as = params[:as] || :point
16
+ point = MouseInfo.get_pointer_info.get_location
17
+ vector = Vector[point.get_x, point.get_y]
18
+
19
+ case as
20
+ when :point
21
+ return point
22
+ when :vector
23
+ return vector
24
+ end
25
+ end
26
+
27
+ def self.position= point
28
+ x, y = point[0], point[1] if point.is_a? Vector
29
+ x, y = point.get_x, point.get_y if point.is_a? Point
30
+
31
+ robot.mouse_move(x, y)
32
+ end
33
+
34
+ def self.click button = :left
35
+ masks = { left: InputEvent::BUTTON1_MASK,
36
+ right: InputEvent::BUTTON2_MASK }
37
+ mask = masks[button]
38
+
39
+ robot.mouse_press mask
40
+ robot.mouse_release mask
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,52 @@
1
+ module Samscript
2
+ class Screen
3
+ java_import 'java.awt.Toolkit'
4
+ java_import 'java.awt.Rectangle'
5
+ java_import 'java.awt.Robot'
6
+
7
+ DefaultParams = { match: true,
8
+ tolerance: 0,
9
+ interval: 100,
10
+ timeout: 1000 }
11
+
12
+ def self.resolution
13
+ @tk ||= Java::JavaAwt::Toolkit.get_default_toolkit
14
+ @tk.get_screen_size
15
+ end
16
+
17
+ def self.capture area = resolution
18
+ rect = Rectangle.new area
19
+ @robot ||= Robot.new
20
+ @robot.create_screen_capture rect
21
+ end
22
+
23
+ def self.wait_for_pixel params = {}
24
+ %w(location colour).map(&:to_sym).each do |sym|
25
+ raise "invalid params" if params[sym].nil?
26
+ end
27
+
28
+ x, y = params[:location][0], params[:location][1]
29
+ colour = params[:colour]
30
+ tolerance = params[:tolerance] || DefaultParams[:tolerance]
31
+ match = params[:match] || DefaultParams[:match]
32
+ interval = params[:interval] || DefaultParams[:interval]
33
+ timeout = params[:timeout] || DefaultParams[:timeout]
34
+ attempts = (timeout/interval+1).floor
35
+
36
+ return if match == :any or tolerance >= 255
37
+
38
+ attempts.times do
39
+ screen = self.capture
40
+ pixel = Samscript::Image.get_pixel(screen, x: x, y: y)
41
+ if Samscript::Image.get_difference(pixel, colour) <= tolerance
42
+ yield(true) if block_given?
43
+ return
44
+ end
45
+
46
+ sleep(interval.to_f/1000)
47
+ end
48
+
49
+ yield(false) if block_given?
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module Samscript
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'matrix'
2
+
3
+ module Samscript
4
+ class WaitMatcher
5
+ java_import 'java.awt.Color'
6
+
7
+ def self.validate_wait_params params
8
+ raise "no params" if params.empty?
9
+ end
10
+
11
+ def self.parse_location params
12
+ labels = [:at, :location, :coords, :point, :pixel]
13
+ loc = labels.reduce(nil) { |mem, sym| params[sym] || mem }
14
+
15
+ case loc
16
+ when Vector
17
+ return loc
18
+ when Array
19
+ return Vector[loc[0], loc[1]]
20
+ when Point
21
+ return Vector[loc.get_x, loc.get_y]
22
+ end
23
+ end
24
+
25
+ def self.parse_colour params
26
+ labels = [:colour, :color]
27
+ col = labels.reduce(nil) { |mem, sym| params[sym] || mem }
28
+
29
+ case col
30
+ when Color
31
+ return col
32
+ when Array
33
+ return Color.new(col[0], col[1], col[2])
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'samscript/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "samscript"
8
+ spec.version = Samscript::VERSION
9
+ spec.authors = ["Sam Broughton"]
10
+ spec.email = ["sam@26th-zerk.co.uk"]
11
+ spec.description = %q{Samscript}
12
+ spec.summary = %q{Samscript}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "shoulda"
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe Samscript::Image do
4
+ before :all do
5
+ java_import 'java.awt.Color'
6
+ end
7
+
8
+ let(:screenshot) { Samscript::Screen.capture }
9
+
10
+ it "getting pixel colour" do
11
+ pixel = Samscript::Image.get_pixel(screenshot, x: 200, y: 200)
12
+ pixel.should be_a(Color)
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ require 'matrix'
3
+
4
+ describe Samscript::Mouse do
5
+ before :all do
6
+ java_import 'java.awt.Point'
7
+ end
8
+
9
+ context "getting position" do
10
+ it "getting mouse position as point" do
11
+ Samscript::Mouse.position.should be_a(Point)
12
+ Samscript::Mouse.position(as: :point).should be_a(Point)
13
+ end
14
+
15
+ it "getting mouse position as vector" do
16
+ Samscript::Mouse.position(as: :vector).should be_a(Vector)
17
+ end
18
+ end
19
+
20
+ context "setting position" do
21
+ let(:point_pos) { Point.new(100, 100) }
22
+ let(:vector_pos) { Vector[100, 100] }
23
+
24
+ it "setting mouse position with point" do
25
+ Samscript::Mouse.position = point_pos
26
+ Samscript::Mouse.position.should == point_pos
27
+ end
28
+
29
+ it "setting mouse position with vector" do
30
+ Samscript::Mouse.position = vector_pos
31
+ Samscript::Mouse.position.should == point_pos
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe Samscript::Screen do
4
+ before :all do
5
+ java_import 'java.awt.Dimension'
6
+ java_import 'java.awt.image.BufferedImage'
7
+ java_import 'java.awt.Rectangle'
8
+ end
9
+
10
+ it "screen resolution return type" do
11
+ res = Samscript::Screen.resolution
12
+ res.should be_a(Dimension)
13
+ end
14
+
15
+ it "capture full screen" do
16
+ capture = Samscript::Screen.capture
17
+ capture.should be_a(BufferedImage)
18
+ end
19
+
20
+ it "capture part of screen" do
21
+ rect = Rectangle.new(100, 100, 100, 100)
22
+ capture = Samscript::Screen.capture rect
23
+ capture.should be_a(BufferedImage)
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ require 'samscript'
2
+
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+
8
+ # Run specs in random order to surface order dependencies. If you find an
9
+ # order dependency and want to debug it, you can fix the order by providing
10
+ # the seed, which is printed after each run.
11
+ # --seed 1234
12
+ config.order = 'random'
13
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'matrix'
3
+
4
+ describe Samscript::WaitMatcher do
5
+ before :all do
6
+ java_import 'java.awt.Point'
7
+ java_import 'java.awt.Color'
8
+ end
9
+
10
+ it "wait for pixel base" do
11
+ location = [100, 100]
12
+ colour = Color.new(100, 100, 100)
13
+ match = :any
14
+ tolerance = 0
15
+ interval = 50
16
+ timeout = 3000
17
+
18
+ args = { location: location,
19
+ colour: colour,
20
+ match: match,
21
+ tolerance: tolerance,
22
+ interval: interval,
23
+ timeout: timeout }
24
+
25
+ lambda {
26
+ Samscript::Screen.wait_for_pixel args
27
+ }.should_not raise_error
28
+ end
29
+
30
+ context "validation of wait_for_pixel params" do
31
+ let(:random) { Random.new }
32
+
33
+ it "no params" do
34
+ lambda {
35
+ Samscript::WaitMatcher.validate_wait_params({})
36
+ }.should raise_error
37
+ end
38
+
39
+ it "locations" do
40
+ x = random.rand(100)
41
+ y = random.rand(100)
42
+
43
+ locations = [[x, y], Vector[x, y], Point.new(x, y)]
44
+ labels = [:at, :location, :coords, :point, :pixel]
45
+
46
+ locations.each do |location|
47
+ labels.each do |label|
48
+ result = Samscript::WaitMatcher.parse_location(label => location)
49
+ result.should == Vector[x, y]
50
+ end
51
+ end
52
+ end
53
+
54
+ it "colours" do
55
+ r = random.rand(255)
56
+ g = random.rand(255)
57
+ b = random.rand(255)
58
+
59
+ colours = [[r, g, b], Color.new(r, g, b)]
60
+ labels = [:colour, :color]
61
+
62
+ colours.each do |colour|
63
+ labels.each do |label|
64
+ result = Samscript::WaitMatcher.parse_colour(label => colour)
65
+ result.should == Color.new(r, g, b)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: samscript
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.1
6
+ platform: ruby
7
+ authors:
8
+ - Sam Broughton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: '1.3'
21
+ none: false
22
+ requirement: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ none: false
28
+ prerelease: false
29
+ type: :development
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ none: false
38
+ requirement: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ none: false
44
+ prerelease: false
45
+ type: :development
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ none: false
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ none: false
60
+ prerelease: false
61
+ type: :development
62
+ - !ruby/object:Gem::Dependency
63
+ name: shoulda
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ none: false
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ none: false
76
+ prerelease: false
77
+ type: :development
78
+ description: Samscript
79
+ email:
80
+ - sam@26th-zerk.co.uk
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - .rspec
87
+ - Gemfile
88
+ - LICENSE.txt
89
+ - README.md
90
+ - Rakefile
91
+ - lib/samscript.rb
92
+ - lib/samscript/helpers.rb
93
+ - lib/samscript/image.rb
94
+ - lib/samscript/mouse.rb
95
+ - lib/samscript/screen.rb
96
+ - lib/samscript/version.rb
97
+ - lib/samscript/wait_matcher.rb
98
+ - samscript.gemspec
99
+ - spec/image_spec.rb
100
+ - spec/mouse_spec.rb
101
+ - spec/screen_spec.rb
102
+ - spec/spec_helper.rb
103
+ - spec/wait_for_pixel_spec.rb
104
+ homepage: ''
105
+ licenses:
106
+ - MIT
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - '>='
114
+ - !ruby/object:Gem::Version
115
+ segments:
116
+ - 0
117
+ hash: 2
118
+ version: '0'
119
+ none: false
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ segments:
125
+ - 0
126
+ hash: 2
127
+ version: '0'
128
+ none: false
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 1.8.24
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Samscript
135
+ test_files:
136
+ - spec/image_spec.rb
137
+ - spec/mouse_spec.rb
138
+ - spec/screen_spec.rb
139
+ - spec/spec_helper.rb
140
+ - spec/wait_for_pixel_spec.rb