ruby2d 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- # square.rb
1
+ # Ruby2D::Square
2
2
 
3
3
  module Ruby2D
4
4
  class Square < Rectangle
@@ -10,18 +10,18 @@ module Ruby2D
10
10
  @y = opts[:y] || 0
11
11
  @z = opts[:z] || 0
12
12
  @width = @height = @size = opts[:size] || 100
13
-
14
13
  self.color = opts[:color] || 'white'
15
-
16
14
  update_coords(@x, @y, @size, @size)
17
-
18
15
  add
19
16
  end
20
17
 
18
+ # Set the size of the square
21
19
  def size=(s)
22
20
  self.width = self.height = @size = s
23
21
  end
24
22
 
23
+ # Make the inherited width and height attribute accessors private
25
24
  private :width=, :height=
25
+
26
26
  end
27
27
  end
@@ -1,19 +1,19 @@
1
- # text.rb
1
+ # Ruby2D::Text
2
2
 
3
3
  module Ruby2D
4
4
  class Text
5
5
  include Renderable
6
6
 
7
- attr_accessor :x, :y, :data
8
7
  attr_reader :text, :size, :width, :height, :font, :color
8
+ attr_accessor :x, :y, :rotate, :data
9
9
 
10
10
  def initialize(opts = {})
11
11
  @x = opts[:x] || 0
12
12
  @y = opts[:y] || 0
13
13
  @z = opts[:z] || 0
14
- @text = (opts[:text] || "Hello World!").to_s
14
+ @text = (opts[:text] || "Hello Ruby!").to_s
15
15
  @size = opts[:size] || 20
16
-
16
+ @rotate = opts[:rotate] || 0
17
17
  @font = opts[:font]
18
18
 
19
19
  unless RUBY_ENGINE == 'opal'
@@ -1,13 +1,13 @@
1
- # triangle.rb
1
+ # Ruby2D::Triangle
2
2
 
3
3
  module Ruby2D
4
4
  class Triangle
5
5
  include Renderable
6
6
 
7
+ attr_reader :color
7
8
  attr_accessor :x1, :y1, :c1,
8
9
  :x2, :y2, :c2,
9
10
  :x3, :y3, :c3
10
- attr_reader :color, :type_id
11
11
 
12
12
  def initialize(opts= {})
13
13
  @x1 = opts[:x1] || 50
@@ -16,8 +16,7 @@ module Ruby2D
16
16
  @y2 = opts[:y2] || 100
17
17
  @x3 = opts[:x3] || 0
18
18
  @y3 = opts[:y3] || 100
19
- @z = opts[:z] || 0
20
-
19
+ @z = opts[:z] || 0
21
20
  self.color = opts[:color] || 'white'
22
21
  add
23
22
  end
@@ -61,5 +60,6 @@ module Ruby2D
61
60
  @c3 = c
62
61
  end
63
62
  end
63
+
64
64
  end
65
65
  end
@@ -1,5 +1,5 @@
1
- # version.rb
1
+ # Ruby2D::VERSION
2
2
 
3
3
  module Ruby2D
4
- VERSION = '0.5.1'
4
+ VERSION = '0.6.0'
5
5
  end
@@ -1,37 +1,70 @@
1
- # window.rb
1
+ # Ruby2D::Window
2
+ # Represents a window on screen, responsible for storing renderable graphics,
3
+ # event handlers, the update loop, showing and closing the window.
2
4
 
3
5
  module Ruby2D
4
6
  class Window
5
7
 
6
- attr_reader :objects
7
- attr_accessor :mouse_x, :mouse_y, :frames, :fps
8
-
8
+ # Event structures
9
9
  EventDescriptor = Struct.new(:type, :id)
10
10
  MouseEvent = Struct.new(:type, :button, :direction, :x, :y, :delta_x, :delta_y)
11
11
  KeyEvent = Struct.new(:type, :key)
12
- ControllerEvent = Struct.new(:which, :type, :axis, :value, :button)
12
+ ControllerEvent = Struct.new(:which, :type, :axis, :value, :button)
13
13
  ControllerAxisEvent = Struct.new(:which, :axis, :value)
14
14
  ControllerButtonEvent = Struct.new(:which, :button)
15
15
 
16
16
  def initialize(args = {})
17
- @title = args[:title] || "Ruby 2D"
17
+
18
+ # This window instance, stored so it can be called by the class methods
19
+ @@window = self
20
+
21
+ # Title of the window
22
+ @title = args[:title] || "Ruby 2D"
23
+
24
+ # Window background color
18
25
  @background = Color.new([0.0, 0.0, 0.0, 1.0])
19
- @width = args[:width] || 640
20
- @height = args[:height] || 480
21
- @viewport_width, @viewport_height = nil, nil
22
- @display_width, @display_height = nil, nil
23
- @resizable = false
26
+
27
+ # Window icon
28
+ @icon = nil
29
+
30
+ # Window size and characteristics
31
+ @width = args[:width] || 640
32
+ @height = args[:height] || 480
33
+ @resizable = false
24
34
  @borderless = false
25
35
  @fullscreen = false
26
- @highdpi = false
27
- @frames = 0
28
- @fps_cap = args[:fps] || 60
29
- @fps = @fps_cap
30
- @vsync = args[:vsync] || true
36
+ @highdpi = false
37
+
38
+ # Size of the window's viewport (the drawable area)
39
+ @viewport_width, @viewport_height = nil, nil
40
+
41
+ # Size of the computer's display
42
+ @display_width, @display_height = nil, nil
43
+
44
+ # Total number of frames that have been rendered
45
+ @frames = 0
46
+
47
+ # Frames per second upper limit, and the actual FPS
48
+ @fps_cap = args[:fps_cap] || 60
49
+ @fps = @fps_cap
50
+
51
+ # Vertical synchronization, set to prevent screen tearing (recommended)
52
+ @vsync = args[:vsync] || true
53
+
54
+ # Mouse X and Y position in the window
31
55
  @mouse_x, @mouse_y = 0, 0
32
- @objects = []
33
- @event_key = 0
34
- @events = {
56
+
57
+ # Controller axis and button mappings file
58
+ @controller_mappings = File.expand_path('~') + "/.ruby2d/controllers.txt"
59
+
60
+ # Renderable objects currently in the window, like a linear scene graph
61
+ @objects = []
62
+
63
+ # Unique ID for the input event being registered
64
+ @event_key = 0
65
+
66
+ # Registered input events
67
+ @events = {
35
68
  key: {},
36
69
  key_down: {},
37
70
  key_held: {},
@@ -46,15 +79,80 @@ module Ruby2D
46
79
  controller_button_down: {},
47
80
  controller_button_up: {}
48
81
  }
82
+
83
+ # The window update block
49
84
  @update_proc = Proc.new {}
85
+
86
+ # Whether diagnostic messages should be printed
50
87
  @diagnostics = false
51
- @controller_mappings_path = File.join(Dir.home, ".ruby2d", "controllers.txt")
52
88
  end
53
89
 
54
- def new_event_key
55
- @event_key = @event_key.next
90
+ # Class methods for convenient access to properties
91
+ class << self
92
+ def current; get(:window) end
93
+ def title; get(:title) end
94
+ def background; get(:background) end
95
+ def width; get(:width) end
96
+ def height; get(:height) end
97
+ def viewport_width; get(:viewport_width) end
98
+ def viewport_height; get(:viewport_height) end
99
+ def display_width; get(:display_width) end
100
+ def display_height; get(:display_height) end
101
+ def resizable; get(:resizable) end
102
+ def borderless; get(:borderless) end
103
+ def fullscreen; get(:fullscreen) end
104
+ def highdpi; get(:highdpi) end
105
+ def frames; get(:frames) end
106
+ def fps; get(:fps) end
107
+ def fps_cap; get(:fps_cap) end
108
+ def mouse_x; get(:mouse_x) end
109
+ def mouse_y; get(:mouse_y) end
110
+ def diagnostics; get(:diagnostics) end
111
+
112
+ def get(sym)
113
+ @@window.get(sym)
114
+ end
115
+
116
+ def set(opts)
117
+ @@window.set(opts)
118
+ end
119
+
120
+ def on(event, &proc)
121
+ @@window.on(event, &proc)
122
+ end
123
+
124
+ def off(event_descriptor)
125
+ @@window.off(event_descriptor)
126
+ end
127
+
128
+ def add(o)
129
+ @@window.add(o)
130
+ end
131
+
132
+ def remove(o)
133
+ @@window.remove(o)
134
+ end
135
+
136
+ def clear
137
+ @@window.clear
138
+ end
139
+
140
+ def update(&proc)
141
+ @@window.update(&proc)
142
+ end
143
+
144
+ def show
145
+ @@window.show
146
+ end
147
+
148
+ def close
149
+ @@window.close
150
+ end
56
151
  end
57
152
 
153
+ # Public instance methods
154
+
155
+ # Retrieve an attribute of the window
58
156
  def get(sym)
59
157
  case sym
60
158
  when :window; self
@@ -77,20 +175,24 @@ module Ruby2D
77
175
  when :highdpi; @highdpi
78
176
  when :frames; @frames
79
177
  when :fps; @fps
178
+ when :fps_cap; @fps_cap
80
179
  when :mouse_x; @mouse_x
81
180
  when :mouse_y; @mouse_y
82
181
  when :diagnostics; @diagnostics
83
182
  end
84
183
  end
85
184
 
185
+ # Set a window attribute
86
186
  def set(opts)
87
187
  # Store new window attributes, or ignore if nil
88
188
  @title = opts[:title] || @title
89
189
  if Color.is_valid? opts[:background]
90
190
  @background = Color.new(opts[:background])
91
191
  end
192
+ @icon = opts[:icon] || @icon
92
193
  @width = opts[:width] || @width
93
194
  @height = opts[:height] || @height
195
+ @fps_cap = opts[:fps_cap] || @fps_cap
94
196
  @viewport_width = opts[:viewport_width] || @viewport_width
95
197
  @viewport_height = opts[:viewport_height] || @viewport_height
96
198
  @resizable = opts[:resizable] || @resizable
@@ -100,6 +202,7 @@ module Ruby2D
100
202
  @diagnostics = opts[:diagnostics] || @diagnostics
101
203
  end
102
204
 
205
+ # Add an object to the window
103
206
  def add(o)
104
207
  case o
105
208
  when nil
@@ -111,6 +214,7 @@ module Ruby2D
111
214
  end
112
215
  end
113
216
 
217
+ # Remove an object from the window
114
218
  def remove(o)
115
219
  if o == nil
116
220
  raise Error, "Cannot remove '#{o.class}' from window!"
@@ -124,15 +228,23 @@ module Ruby2D
124
228
  end
125
229
  end
126
230
 
231
+ # Clear all objects from the window
127
232
  def clear
128
233
  @objects.clear
129
234
  end
130
235
 
236
+ # Set an update callback
131
237
  def update(&proc)
132
238
  @update_proc = proc
133
239
  true
134
240
  end
135
241
 
242
+ # Generate a new event key (ID)
243
+ def new_event_key
244
+ @event_key = @event_key.next
245
+ end
246
+
247
+ # Set an event handler
136
248
  def on(event, &proc)
137
249
  unless @events.has_key? event
138
250
  raise Error, "`#{event}` is not a valid event type"
@@ -142,13 +254,13 @@ module Ruby2D
142
254
  EventDescriptor.new(event, event_id)
143
255
  end
144
256
 
257
+ # Remove an event handler
145
258
  def off(event_descriptor)
146
259
  @events[event_descriptor.type].delete(event_descriptor.id)
147
260
  end
148
261
 
262
+ # Key callback method, called by the native and web extentions
149
263
  def key_callback(type, key)
150
- # puts "===", "type: #{type}", "key: #{key}"
151
-
152
264
  key = key.downcase
153
265
 
154
266
  # All key events
@@ -175,6 +287,7 @@ module Ruby2D
175
287
  end
176
288
  end
177
289
 
290
+ # Mouse callback method, called by the native and web extentions
178
291
  def mouse_callback(type, button, direction, x, y, delta_x, delta_y)
179
292
  # All mouse events
180
293
  @events[:mouse].each do |id, e|
@@ -205,6 +318,16 @@ module Ruby2D
205
318
  end
206
319
  end
207
320
 
321
+ # Add controller mappings from file
322
+ def add_controller_mappings
323
+ unless RUBY_ENGINE == 'opal'
324
+ if File.exists? @controller_mappings
325
+ ext_add_controller_mappings(@controller_mappings)
326
+ end
327
+ end
328
+ end
329
+
330
+ # Controller callback method, called by the native and web extentions
208
331
  def controller_callback(which, type, axis, value, button)
209
332
  # All controller events
210
333
  @events[:controller].each do |id, e|
@@ -230,20 +353,26 @@ module Ruby2D
230
353
  end
231
354
  end
232
355
 
356
+ # Update callback method, called by the native and web extentions
233
357
  def update_callback
234
358
  @update_proc.call
235
359
  end
236
360
 
361
+ # Show the window
237
362
  def show
238
363
  ext_show
239
364
  end
240
365
 
366
+ # Close the window
241
367
  def close
242
368
  ext_close
243
369
  end
244
370
 
371
+ # Private instance methods
372
+
245
373
  private
246
374
 
375
+ # An an object to the window, used by the public `add` method
247
376
  def add_object(o)
248
377
  if !@objects.include?(o)
249
378
  index = @objects.index do |object|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.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: 2018-01-30 00:00:00.000000000 Z
11
+ date: 2018-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.7'
33
+ version: '3.8'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.7'
40
+ version: '3.8'
41
41
  description: Make cross-platform 2D applications in Ruby
42
42
  email: tom@blacktm.com
43
43
  executables:
@@ -86,8 +86,6 @@ files:
86
86
  - assets/ios/MyApp.xcodeproj/project.pbxproj
87
87
  - assets/ios/MyApp.xcodeproj/project.xcworkspace/contents.xcworkspacedata
88
88
  - assets/ios/MyApp.xcodeproj/project.xcworkspace/xcshareddata/MyApp.xcscmblueprint
89
- - assets/ios/MyApp.xcodeproj/project.xcworkspace/xcuserdata/Tom.xcuserdatad/UserInterfaceState.xcuserstate
90
- - assets/ios/MyApp.xcodeproj/xcuserdata/Tom.xcuserdatad/xcschemes/xcschememanagement.plist
91
89
  - assets/ios/main.c
92
90
  - assets/opal.js
93
91
  - assets/simple2d.js
@@ -153,7 +151,7 @@ files:
153
151
  - ext/ruby2d/ruby2d-opal.rb
154
152
  - ext/ruby2d/ruby2d.c
155
153
  - lib/ruby2d.rb
156
- - lib/ruby2d/application.rb
154
+ - lib/ruby2d/circle.rb
157
155
  - lib/ruby2d/color.rb
158
156
  - lib/ruby2d/dsl.rb
159
157
  - lib/ruby2d/exceptions.rb
@@ -190,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
190
188
  version: '0'
191
189
  requirements: []
192
190
  rubyforge_project:
193
- rubygems_version: 2.7.4
191
+ rubygems_version: 2.7.7
194
192
  signing_key:
195
193
  specification_version: 4
196
194
  summary: Ruby 2D