ruby2d 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/ruby2d/square.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # square.rb
2
2
 
3
3
  module Ruby2D
4
- class Square < Ruby2D::Rectangle
4
+ class Square < Rectangle
5
5
 
6
6
  attr_reader :size
7
7
 
@@ -11,10 +11,7 @@ module Ruby2D
11
11
  @width = @height = @size = s
12
12
  update_coords(x, y, s, s)
13
13
  update_color(c)
14
-
15
- if defined? Ruby2D::DSL
16
- Ruby2D::Application.add(self)
17
- end
14
+ add
18
15
  end
19
16
 
20
17
  def size=(s)
data/lib/ruby2d/text.rb CHANGED
@@ -3,38 +3,38 @@
3
3
  module Ruby2D
4
4
  class Text
5
5
 
6
- attr_accessor :x, :y, :size, :text
6
+ attr_accessor :x, :y, :data
7
+ attr_reader :text, :size, :font, :color
7
8
 
8
- def initialize(x=0, y=0, size=20, msg="Hello World!", font="Arial", c="white")
9
+ def initialize(x=0, y=0, text="Hello World!", size=20, font=nil, c="white")
9
10
 
10
- if File.exists? font
11
+ # if File.exists? font
11
12
  @font = font
12
- else
13
- @font = resolve_path(font)
14
- end
13
+ # else
14
+ # @font = resolve_path(font)
15
+ # end
15
16
 
16
- @type_id = 4
17
+ @type_id = 5
17
18
  @x, @y, @size = x, y, size
18
- @text, @color = msg, c
19
- update_color(c)
20
-
21
- if defined? Ruby2D::DSL
22
- Ruby2D::Application.add(self)
23
- end
19
+ @text = text
20
+ @color = Color.new(c)
21
+ init
22
+ add
24
23
  end
25
24
 
26
25
  def color=(c)
27
- @color = c
28
- update_color(c)
26
+ @color = Color.new(c)
29
27
  end
30
28
 
31
- def text=(t)
32
-
29
+ def add
30
+ if Module.const_defined? :DSL
31
+ Application.add(self)
32
+ end
33
33
  end
34
34
 
35
35
  def remove
36
- if defined? Ruby2D::DSL
37
- Ruby2D::Application.remove(self)
36
+ if Module.const_defined? :DSL
37
+ Application.remove(self)
38
38
  end
39
39
  end
40
40
 
@@ -55,9 +55,5 @@ module Ruby2D
55
55
  end
56
56
  end
57
57
 
58
- def update_color(c)
59
- @c = Ruby2D::Color.new(c)
60
- end
61
-
62
58
  end
63
59
  end
@@ -6,7 +6,7 @@ module Ruby2D
6
6
  attr_accessor :x1, :y1, :c1,
7
7
  :x2, :y2, :c2,
8
8
  :x3, :y3, :c3
9
- attr_reader :color
9
+ attr_reader :color, :type_id
10
10
 
11
11
  def initialize(x1=50, y1=0, x2=100, y2=100, x3=0, y3=100, c='white')
12
12
  @type_id = 1
@@ -15,10 +15,7 @@ module Ruby2D
15
15
  @x3, @y3 = x3, y3
16
16
  @color = c
17
17
  update_color(c)
18
-
19
- if defined? Ruby2D::DSL
20
- Ruby2D::Application.add(self)
21
- end
18
+ add
22
19
  end
23
20
 
24
21
  def color=(c)
@@ -26,25 +23,42 @@ module Ruby2D
26
23
  @color = c
27
24
  end
28
25
 
26
+ def add
27
+ if Module.const_defined? :DSL
28
+ Application.add(self)
29
+ end
30
+ end
31
+
29
32
  def remove
30
- if defined? Ruby2D::DSL
31
- Ruby2D::Application.remove(self)
33
+ if Module.const_defined? :DSL
34
+ Application.remove(self)
32
35
  end
33
36
  end
34
37
 
35
38
  private
36
39
 
37
40
  def update_color(c)
38
- # If a 2D array
39
- if c.class == Array && c.all? { |el| el.class == Array }
40
- @c1 = Ruby2D::Color.new(c[0])
41
- @c2 = Ruby2D::Color.new(c[1])
42
- @c3 = Ruby2D::Color.new(c[2])
41
+
42
+ # If a valid color, use it for each vertex
43
+ if Color.is_valid? c
44
+ @c1 = Color.new(c)
45
+ @c2 = Color.new(c)
46
+ @c3 = Color.new(c)
47
+
48
+ elsif c.class == Array && c.length < 3
49
+ raise Error, "Triangles require 3 colors, one for each vertex. Only " <<
50
+ "#{c.length} were given."
51
+
52
+ # If a valid array of colors, assign them to each vertex, respectively
53
+ elsif c.all? { |el| Color.is_valid? el }
54
+ @c1 = Color.new(c[0])
55
+ @c2 = Color.new(c[1])
56
+ @c3 = Color.new(c[2])
57
+
43
58
  else
44
- @c1 = Ruby2D::Color.new(c)
45
- @c2 = Ruby2D::Color.new(c)
46
- @c3 = Ruby2D::Color.new(c)
59
+ raise Error, "Not a valid color for #{self.class}"
47
60
  end
61
+
48
62
  end
49
63
 
50
64
  end
@@ -1,5 +1,5 @@
1
1
  # version.rb
2
2
 
3
3
  module Ruby2D
4
- VERSION = '0.2.1'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/ruby2d/window.rb CHANGED
@@ -2,52 +2,69 @@
2
2
 
3
3
  module Ruby2D
4
4
  class Window
5
- attr_reader :title, :width, :height, :mouse_x, :mouse_y
6
-
7
- def initialize(width: 640, height: 480, title: "Ruby 2D", fps: 60, vsync: true)
8
- @width, @height, @title = width, height, title
9
- @mouse_x = @mouse_y = 0
10
- @fps_cap = fps
11
- @fps = 60
12
- @vsync = vsync
13
-
14
- @objects = []
15
- @keys = {}
16
- @keys_down = {}
17
- @controller = {}
18
- @update_proc = Proc.new {}
5
+
6
+ attr_reader :objects
7
+ attr_accessor :mouse_x, :mouse_y, :frames, :fps
8
+
9
+ def initialize(args = {})
10
+ @title = args[:title] || "Ruby 2D"
11
+ @background = Color.new([0.0, 0.0, 0.0, 1.0])
12
+ @width = args[:width] || 640
13
+ @height = args[:height] || 480
14
+ @viewport_width, @viewport_height = nil, nil
15
+ @resizable = false
16
+ @borderless = false
17
+ @fullscreen = false
18
+ @highdpi = false
19
+ @frames = 0
20
+ @fps_cap = args[:fps] || 60
21
+ @fps = @fps_cap
22
+ @vsync = args[:vsync] || true
23
+ @mouse_x = 0; @mouse_y = 0
24
+ @objects = []
25
+ @keys_down, @keys, @keys_up, @mouse, @controller = {}, {}, {}, {}, {}
26
+ @on_key_proc = Proc.new {}
27
+ @on_controller_proc = Proc.new {}
28
+ @update_proc = Proc.new {}
29
+ @diagnostics = false
19
30
  end
20
31
 
21
32
  def get(sym)
22
33
  case sym
23
- when :window
24
- return self
25
- when :title
26
- return @title
27
- when :width
28
- return @width
29
- when :height
30
- return @height
31
- when :fps
32
- return @fps
33
- when :mouse_x
34
- return @mouse_x
35
- when :mouse_y
36
- return @mouse_y
34
+ when :window; self
35
+ when :title; @title
36
+ when :background; @background
37
+ when :width; @width
38
+ when :height; @height
39
+ when :viewport_width; @viewport_width
40
+ when :viewport_height; @viewport_height
41
+ when :resizable; @resizable
42
+ when :borderless; @borderless
43
+ when :fullscreen; @fullscreen
44
+ when :highdpi; @highdpi
45
+ when :frames; @frames
46
+ when :fps; @fps
47
+ when :mouse_x; @mouse_x
48
+ when :mouse_y; @mouse_y
49
+ when :diagnostics; @diagnostics
37
50
  end
38
51
  end
39
52
 
40
53
  def set(opts)
41
- valid_keys = [:title, :width, :height]
42
- valid_opts = opts.reject { |k| !valid_keys.include?(k) }
43
- if !valid_opts.empty?
44
- @title = valid_opts[:title]
45
- @width = valid_opts[:width]
46
- @height = valid_opts[:height]
47
- return true
48
- else
49
- return false
54
+ # Store new window attributes, or ignore if nil
55
+ @title = opts[:title] || @title
56
+ if Color.is_valid? opts[:background]
57
+ @background = Color.new(opts[:background])
50
58
  end
59
+ @width = opts[:width] || @width
60
+ @height = opts[:height] || @height
61
+ @viewport_width = opts[:viewport_width] || @viewport_width
62
+ @viewport_height = opts[:viewport_height] || @viewport_height
63
+ @resizable = opts[:resizable] || @resizable
64
+ @borderless = opts[:borderless] || @borderless
65
+ @fullscreen = opts[:fullscreen] || @fullscreen
66
+ @highdpi = opts[:highdpi] || @highdpi
67
+ @diagnostics = opts[:diagnostics] || @diagnostics
51
68
  end
52
69
 
53
70
  def add(o)
@@ -83,17 +100,28 @@ module Ruby2D
83
100
  true
84
101
  end
85
102
 
86
- def on(mouse: nil, key: nil, key_down: nil, controller: nil, &proc)
103
+ # def on(mouse: nil, key: nil, key_up: nil, key_down: nil, controller: nil, &proc)
104
+ def on(args = {}, &proc)
105
+ mouse = args[:mouse]
106
+ key = args[:key]
107
+ key_up = args[:key_up]
108
+ key_down = args[:key_down]
109
+ controller = args[:controller]
110
+
87
111
  unless mouse.nil?
88
- # reg_mouse(btn, &proc)
112
+ reg_mouse(mouse, &proc)
113
+ end
114
+
115
+ unless key_down.nil?
116
+ reg_key_down(key_down, &proc)
89
117
  end
90
118
 
91
119
  unless key.nil?
92
120
  reg_key(key, &proc)
93
121
  end
94
122
 
95
- unless key_down.nil?
96
- reg_key_down(key_down, &proc)
123
+ unless key_up.nil?
124
+ reg_key_up(key_up, &proc)
97
125
  end
98
126
 
99
127
  unless controller.nil?
@@ -101,43 +129,57 @@ module Ruby2D
101
129
  end
102
130
  end
103
131
 
104
- def key_callback(key)
105
- key.downcase!
106
- if @keys.has_key? key
107
- @keys[key].call
132
+ def mouse_callback(btn, x, y)
133
+ if @mouse.has_key? 'any'
134
+ @mouse[btn].call(x, y)
108
135
  end
109
136
  end
110
137
 
138
+ def on_key(&proc)
139
+ @on_key_proc = proc
140
+ true
141
+ end
142
+
111
143
  def key_down_callback(key)
112
- key.downcase!
144
+ key = key.downcase
145
+ if @keys_down.has_key? 'any'
146
+ @keys_down['any'].call
147
+ end
113
148
  if @keys_down.has_key? key
114
149
  @keys_down[key].call
115
150
  end
116
151
  end
117
152
 
118
- def controller_callback(is_axis, axis, val, is_btn, btn)
119
-
120
- # puts "is_axis: #{is_axis}, axis: #{axis}, val: #{val}, is_btn: #{is_btn}, btn: #{btn}"
121
-
122
- if is_axis
123
- if axis == 0 && val == -32768
124
- event = 'left'
125
- elsif axis == 0 && val == 32767
126
- event = 'right'
127
- elsif axis == 1 && val == -32768
128
- event = 'up'
129
- elsif axis == 1 && val == 32767
130
- event = 'down'
131
- end
132
- elsif is_btn
133
- event = btn
153
+ def key_callback(key)
154
+ key = key.downcase
155
+ @on_key_proc.call(key)
156
+ if @keys.has_key? 'any'
157
+ @keys['any'].call
134
158
  end
135
-
136
- if @controller.has_key? event
137
- @controller[event].call
159
+ if @keys.has_key? key
160
+ @keys[key].call
161
+ end
162
+ end
163
+
164
+ def key_up_callback(key)
165
+ key = key.downcase
166
+ if @keys_up.has_key? 'any'
167
+ @keys_up['any'].call
168
+ end
169
+ if @keys_up.has_key? key
170
+ @keys_up[key].call
138
171
  end
139
172
  end
140
173
 
174
+ def on_controller(&proc)
175
+ @on_controller_proc = proc
176
+ true
177
+ end
178
+
179
+ def controller_callback(which, is_axis, axis, val, is_btn, btn, pressed)
180
+ @on_controller_proc.call(which, is_axis, axis, val, is_btn, btn, pressed)
181
+ end
182
+
141
183
  def update_callback
142
184
  @update_proc.call
143
185
  end
@@ -153,6 +195,12 @@ module Ruby2D
153
195
  end
154
196
  end
155
197
 
198
+ # Register key string with proc
199
+ def reg_key_down(key, &proc)
200
+ @keys_down[key] = proc
201
+ true
202
+ end
203
+
156
204
  # Register key string with proc
157
205
  def reg_key(key, &proc)
158
206
  @keys[key] = proc
@@ -160,8 +208,14 @@ module Ruby2D
160
208
  end
161
209
 
162
210
  # Register key string with proc
163
- def reg_key_down(key, &proc)
164
- @keys_down[key] = proc
211
+ def reg_key_up(key, &proc)
212
+ @keys_up[key] = proc
213
+ true
214
+ end
215
+
216
+ # Register mouse button string with proc
217
+ def reg_mouse(btn, &proc)
218
+ @mouse[btn] = proc
165
219
  true
166
220
  end
167
221
 
metadata CHANGED
@@ -1,40 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Black
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-20 00:00:00.000000000 Z
11
+ date: 2017-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: opal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rspec
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: '3.4'
33
+ version: '3.5'
20
34
  type: :development
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: '3.4'
40
+ version: '3.5'
27
41
  description: Make cross-platform 2D applications in Ruby
28
- email: "@blacktm"
42
+ email: tom@blacktm.com
29
43
  executables:
30
44
  - ruby2d
31
45
  extensions:
32
46
  - ext/ruby2d/extconf.rb
33
47
  extra_rdoc_files: []
34
48
  files:
49
+ - assets/README.md
35
50
  - assets/app.icns
51
+ - assets/opal.js
52
+ - assets/simple2d.js
53
+ - assets/template.html
36
54
  - bin/ruby2d
37
55
  - ext/ruby2d/extconf.rb
56
+ - ext/ruby2d/ruby2d-opal.rb
38
57
  - ext/ruby2d/ruby2d.c
39
58
  - lib/ruby2d.rb
40
59
  - lib/ruby2d/application.rb
@@ -42,9 +61,11 @@ files:
42
61
  - lib/ruby2d/dsl.rb
43
62
  - lib/ruby2d/exceptions.rb
44
63
  - lib/ruby2d/image.rb
64
+ - lib/ruby2d/music.rb
45
65
  - lib/ruby2d/quad.rb
46
66
  - lib/ruby2d/rectangle.rb
47
67
  - lib/ruby2d/sound.rb
68
+ - lib/ruby2d/sprite.rb
48
69
  - lib/ruby2d/square.rb
49
70
  - lib/ruby2d/text.rb
50
71
  - lib/ruby2d/triangle.rb
@@ -70,9 +91,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
91
  version: '0'
71
92
  requirements: []
72
93
  rubyforge_project:
73
- rubygems_version: 2.6.3
94
+ rubygems_version: 2.6.10
74
95
  signing_key:
75
96
  specification_version: 4
76
97
  summary: Ruby 2D
77
98
  test_files: []
78
- has_rdoc: