gosu-keyboard 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ /pkg
2
+ .rvmrc
3
+ .DS_Store
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'bundler'
7
+ gem 'rspec'
8
+ end
@@ -0,0 +1,165 @@
1
+ # Gosu-Keyboard
2
+
3
+ A DSL for easily handling keyboard events within a `Gosu::Window`.
4
+
5
+ ## Install
6
+
7
+ ### Bundler
8
+
9
+ Add the following to your `Gemfile`:
10
+
11
+ ```ruby
12
+ gem 'gosu'
13
+ gem 'gosu-keyboard', require: 'gosu/keyboard'
14
+ ```
15
+
16
+ ### RubyGems
17
+
18
+ Install with:
19
+
20
+ ```sh
21
+ $ gem install gosu-keyboard
22
+ ```
23
+
24
+ and `require` with:
25
+
26
+ ```ruby
27
+ require 'gosu/keyboard'
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ### Sample usage within a `Gosu::Window`
33
+
34
+ `my_game/lib/keyboard_helpers.rb`
35
+
36
+ ```ruby
37
+ module MyGame
38
+ module KeyboardHelpers
39
+ def left
40
+ super || a
41
+ end
42
+
43
+ def right
44
+ super || d
45
+ end
46
+
47
+ def direction
48
+ left || right
49
+ end
50
+
51
+ def walk
52
+ direction && !shift && !control
53
+ end
54
+
55
+ def run
56
+ direction && shift && !control
57
+ end
58
+
59
+ def crouch
60
+ alt
61
+ end
62
+ end
63
+ end
64
+
65
+ Gosu::Keyboard.register(MyGame::KeyboardHelpers)
66
+ ```
67
+
68
+ `my_game/lib/player.rb`
69
+
70
+ ```ruby
71
+ module MyGame
72
+ class Player
73
+ attr_accessor :state, :direction
74
+
75
+ def initialize
76
+ @state, @direction = :standing, :right
77
+ end
78
+ end
79
+ end
80
+ ```
81
+
82
+ `my_game/lib/game_window.rb`
83
+
84
+ ```ruby
85
+ module MyGame
86
+ class GameWindow < Gosu::Window
87
+
88
+ def initialize
89
+ super(Gosu::screen_width, Gosu::screen_height, false)
90
+
91
+ self.caption = "Gosu::Keyboard Test"
92
+
93
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 40)
94
+
95
+ @player = Player.new
96
+ end
97
+
98
+ def update
99
+ @player.state = :standing
100
+
101
+ Gosu::Keyboard.handle_keys(self) do
102
+ down?(escape) { close }
103
+ down?(left) { @player.direction = :left }
104
+ down?(right) { @player.direction = :right }
105
+ down?(walk) { @player.state = :walking }
106
+ down?(run) { @player.state = :running }
107
+ down?(crouch) { @player.state = :crouching }
108
+ end
109
+ end
110
+
111
+ def draw
112
+ @font.draw "Player State: #{@player.state}", 0, 0, 0, 1, 1, Gosu::Color::BLUE
113
+ @font.draw "Player Direction: #{@player.direction}", 0, 30, 0, 1, 1, Gosu::Color::BLUE
114
+ @font.draw "Left, A - Change direction of player to 'left' and state to 'walk'", 0, 90, 0, 1, 1, Gosu::Color::RED
115
+ @font.draw "Right, B - Change direction of player to 'right' and state to 'walk'", 0, 120, 0, 1, 1, Gosu::Color::RED
116
+ @font.draw "Shift - Change state of player to 'running'", 0, 150, 0, 1, 1, Gosu::Color::RED
117
+ @font.draw "Alt - Change state of player to 'crouching'", 0, 180, 0, 1, 1, Gosu::Color::RED
118
+ end
119
+
120
+ # Always show the system mouse cursor, for aesthetic
121
+ # reasons. Has nothing to do with example.
122
+ def needs_cursor?
123
+ true
124
+ end
125
+
126
+ end
127
+ end
128
+ ```
129
+
130
+ `my_game/lib/my_game`
131
+
132
+ ```ruby
133
+ require 'pathname'
134
+
135
+ require 'bundler/setup'
136
+ Bundler.require(:default)
137
+
138
+ require 'gosu'
139
+ require 'gosu/keyboard'
140
+
141
+ __LIB__ ||= Pathname.new(__FILE__).join('..', '..', 'lib').expand_path
142
+ $:.unshift(__LIB__.to_s) unless $:.include?(__LIB__.to_s)
143
+
144
+ require 'my_game/keyboard_helpers'
145
+ require 'my_game/player'
146
+ require 'my_game/game_window'
147
+ ```
148
+
149
+ `my_game/bin/my_game`
150
+
151
+ ```ruby
152
+ #!/usr/bin/env ruby
153
+
154
+ require 'pathname'
155
+
156
+ __LIB__ = Pathname.new(__FILE__).join('..', '..', 'lib').expand_path
157
+
158
+ $:.unshift(__LIB__.to_s) unless $:.include?(__LIB__.to_s)
159
+
160
+ require 'my_game'
161
+
162
+ window = MyGame::GameWindow.new
163
+ window.show
164
+ ```
165
+
@@ -0,0 +1,39 @@
1
+ require 'rake/version_task'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubygems/package_task'
4
+ require 'pathname'
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = 'gosu-keyboard'
8
+ s.version = Pathname.new(__FILE__).dirname.join('VERSION').read.strip
9
+ s.author = 'Ryan Scott Lewis'
10
+ s.email = 'ryan@rynet.us'
11
+ s.homepage = "http://github.com/c00lryguy/#{s.name}"
12
+ s.summary = "A nice DSL for handling a Gosu::Window's keyboard events"
13
+ s.description = "A DSL for easily defining keyboard events for a Gosu::Window"
14
+ s.require_path = 'lib'
15
+ s.files = `git ls-files`.lines.to_a.collect { |s| s.strip }
16
+ s.executables = `git ls-files -- bin/*`.lines.to_a.collect { |s| File.basename(s.strip) }
17
+
18
+ s.add_dependency 'version', '~> 1.0.0'
19
+ s.add_dependency 'active_support', '~> 3'
20
+ s.add_dependency 'gosu', '~> 0.7.45'
21
+ s.add_development_dependency 'rake', '~> 0.9'
22
+ # s.add_development_dependency 'guard-rspec', '~> 2.1.1'
23
+ # s.add_development_dependency 'fuubar', '~> 1.1'
24
+ # s.add_development_dependency 'at', '~> 0.1.2'
25
+ end
26
+
27
+ Rake::VersionTask.new do |t|
28
+ t.with_git_tag = true
29
+ t.with_gemspec = spec
30
+ end
31
+
32
+ RSpec::Core::RakeTask.new
33
+
34
+ Gem::PackageTask.new(spec) do |t|
35
+ t.need_zip = false
36
+ t.need_tar = false
37
+ end
38
+
39
+ task default: :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.7
@@ -0,0 +1,93 @@
1
+ require 'pathname'
2
+
3
+ __LIB__ ||= Pathname.new(__FILE__).join('..', '..', 'lib').expand_path
4
+ $:.unshift(__LIB__.to_s) unless $:.include?(__LIB__.to_s)
5
+
6
+ require 'gosu'
7
+ require 'gosu/keyboard'
8
+
9
+ module Simple
10
+ module KeyboardHelpers
11
+ def left
12
+ super || a
13
+ end
14
+
15
+ def right
16
+ super || d
17
+ end
18
+
19
+ def direction
20
+ left || right
21
+ end
22
+
23
+ def walk
24
+ direction && !shift && !control
25
+ end
26
+
27
+ def run
28
+ direction && shift && !control
29
+ end
30
+
31
+ def crouch
32
+ alt
33
+ end
34
+ end
35
+ end
36
+
37
+ Gosu::Keyboard.register(Simple::KeyboardHelpers)
38
+
39
+ module Simple
40
+ class Player
41
+ attr_accessor :state, :direction
42
+
43
+ def initialize
44
+ @state, @direction = :standing, :right
45
+ end
46
+ end
47
+ end
48
+
49
+ module Simple
50
+ class GameWindow < Gosu::Window
51
+
52
+ def initialize
53
+ super(Gosu::screen_width, Gosu::screen_height, false)
54
+
55
+ self.caption = "Gosu::Keyboard Test"
56
+
57
+ @font = Gosu::Font.new(self, Gosu::default_font_name, 40)
58
+
59
+ @player = Player.new
60
+ end
61
+
62
+ def update
63
+ @player.state = :standing
64
+
65
+ Gosu::Keyboard.handle_keys(self) do
66
+ down?(escape) { close }
67
+ down?(left) { @player.direction = :left }
68
+ down?(right) { @player.direction = :right }
69
+ down?(walk) { @player.state = :walking }
70
+ down?(run) { @player.state = :running }
71
+ down?(crouch) { @player.state = :crouching }
72
+ end
73
+ end
74
+
75
+ def draw
76
+ @font.draw "Player State: #{@player.state}", 0, 0, 0, 1, 1, Gosu::Color::BLUE
77
+ @font.draw "Player Direction: #{@player.direction}", 0, 30, 0, 1, 1, Gosu::Color::BLUE
78
+ @font.draw "Left, A - Change direction of player to 'left' and state to 'walk'", 0, 90, 0, 1, 1, Gosu::Color::RED
79
+ @font.draw "Right, B - Change direction of player to 'right' and state to 'walk'", 0, 120, 0, 1, 1, Gosu::Color::RED
80
+ @font.draw "Shift - Change state of player to 'running'", 0, 150, 0, 1, 1, Gosu::Color::RED
81
+ @font.draw "Alt - Change state of player to 'crouching'", 0, 180, 0, 1, 1, Gosu::Color::RED
82
+ end
83
+
84
+ # Always show the system mouse cursor, for aesthetic reasons..
85
+ def needs_cursor?
86
+ true
87
+ end
88
+
89
+ end
90
+ end
91
+
92
+ window = Simple::GameWindow.new
93
+ window.show
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "gosu-keyboard"
5
+ s.version = "0.1.7"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Ryan Scott Lewis"]
9
+ s.date = "2012-11-13"
10
+ s.description = "A DSL for easily defining keyboard events for a Gosu::Window"
11
+ s.email = "ryan@rynet.us"
12
+ s.files = [".gitignore", "Gemfile", "README.md", "Rakefile", "VERSION", "examples/simple.rb", "gosu-keyboard.gemspec", "lib/gosu/keyboard.rb", "lib/gosu/keyboard/dsl.rb", "lib/gosu/keyboard/dsl/helpers.rb"]
13
+ s.homepage = "http://github.com/c00lryguy/gosu-keyboard"
14
+ s.require_paths = ["lib"]
15
+ s.rubygems_version = "1.8.24"
16
+ s.summary = "A nice DSL for handling a Gosu::Window's keyboard events"
17
+
18
+ if s.respond_to? :specification_version then
19
+ s.specification_version = 3
20
+
21
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
22
+ s.add_runtime_dependency(%q<version>, ["~> 1.0.0"])
23
+ s.add_runtime_dependency(%q<active_support>, ["~> 3"])
24
+ s.add_runtime_dependency(%q<gosu>, ["~> 0.7.45"])
25
+ s.add_development_dependency(%q<rake>, ["~> 0.9"])
26
+ else
27
+ s.add_dependency(%q<version>, ["~> 1.0.0"])
28
+ s.add_dependency(%q<active_support>, ["~> 3"])
29
+ s.add_dependency(%q<gosu>, ["~> 0.7.45"])
30
+ s.add_dependency(%q<rake>, ["~> 0.9"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<version>, ["~> 1.0.0"])
34
+ s.add_dependency(%q<active_support>, ["~> 3"])
35
+ s.add_dependency(%q<gosu>, ["~> 0.7.45"])
36
+ s.add_dependency(%q<rake>, ["~> 0.9"])
37
+ end
38
+ end
@@ -0,0 +1,55 @@
1
+ require 'forwardable'
2
+ require 'pathname'
3
+
4
+ require 'bundler/setup'
5
+ require 'active_support/core_ext/string/inflections'
6
+ require 'active_support/hash_with_indifferent_access'
7
+ require 'gosu'
8
+
9
+ __LIB__ = Pathname.new(__FILE__).join('..', '..').expand_path
10
+ $:.unshift(__LIB__.to_s) unless $:.include?(__LIB__.to_s)
11
+
12
+ require 'gosu/keyboard/dsl'
13
+ require 'gosu/keyboard/dsl/helpers'
14
+
15
+ module Gosu
16
+ module Keyboard
17
+
18
+ class << self
19
+
20
+ def keys
21
+ @keys ||= Gosu.constants.collect(&:to_s).grep(/^Kb/).each_with_object({}) do |constant_name, memo|
22
+ constant = Gosu.const_get(constant_name)
23
+ key_name = constant_name.gsub(/^Kb/, '').underscore.gsub(/numpad(\d)/, 'numpad_\1')
24
+
25
+ key_name = case key_name
26
+ when '0' then :zero
27
+ when '1' then :one
28
+ when '2' then :two
29
+ when '3' then :three
30
+ when '4' then :four
31
+ when '5' then :five
32
+ when '6' then :six
33
+ when '7' then :seven
34
+ when '8' then :eight
35
+ when '9' then :nine
36
+ else
37
+ key_name.to_sym
38
+ end
39
+
40
+ memo[key_name] = constant
41
+ end
42
+ end
43
+
44
+ def handle_keys(window, &blk)
45
+ DSL.new(window, &blk)
46
+ end
47
+
48
+ def register(mod)
49
+ DSL.send(:include, mod)
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,25 @@
1
+ module Gosu
2
+ module Keyboard
3
+ class NoKeyError < NoMethodError; end
4
+
5
+ class DSL
6
+
7
+ def initialize(window, &blk)
8
+ @window = window
9
+
10
+ instance_eval(&blk)
11
+ end
12
+
13
+ def method_missing(key_name, &blk)
14
+ raise NoKeyError unless Keyboard.keys.has_key?(key_name)
15
+
16
+ @window.button_down?( Keyboard.keys[key_name] )
17
+ end
18
+
19
+ def down?(condition, &blk)
20
+ @window.instance_eval(&blk) if condition
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ module Gosu
2
+ module Keyboard
3
+ class DSL
4
+
5
+ module Helpers
6
+
7
+ def shift
8
+ left_shift || right_shift
9
+ end
10
+
11
+ def alt
12
+ left_alt || right_alt
13
+ end
14
+
15
+ def control
16
+ left_control || right_control
17
+ end
18
+
19
+ end
20
+
21
+ include Helpers
22
+
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gosu-keyboard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Scott Lewis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: version
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 1.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 1.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: active_support
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: gosu
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.7.45
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.45
62
+ - !ruby/object:Gem::Dependency
63
+ name: rake
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.9'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.9'
78
+ description: A DSL for easily defining keyboard events for a Gosu::Window
79
+ email: ryan@rynet.us
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - .gitignore
85
+ - Gemfile
86
+ - README.md
87
+ - Rakefile
88
+ - VERSION
89
+ - examples/simple.rb
90
+ - gosu-keyboard.gemspec
91
+ - lib/gosu/keyboard.rb
92
+ - lib/gosu/keyboard/dsl.rb
93
+ - lib/gosu/keyboard/dsl/helpers.rb
94
+ homepage: http://github.com/c00lryguy/gosu-keyboard
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ segments:
107
+ - 0
108
+ hash: 3963561981835123036
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.24
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: A nice DSL for handling a Gosu::Window's keyboard events
121
+ test_files: []