smalruby 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of smalruby might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b4e041e1339e93000745877678c14cef4a65259
4
- data.tar.gz: 008b2e5051dbb6bb5c18a7f23befd3d7a68d9bc1
3
+ metadata.gz: a6ffd138d50392da1d959baaaf5434e0dbb8d498
4
+ data.tar.gz: e656644b4616ec34134827b4e8611e25200433ce
5
5
  SHA512:
6
- metadata.gz: a063982bd0fa45e6aa5aa591899e15c501884ca2736c4c93fcceaec61c6b7325783535455a4c42c9ce22e9bd5334c5522bf5f89d4428a63c578cb35819188672
7
- data.tar.gz: 67ed9dd5e0ac013130194208b35df444d7940387006a31d758ee9f86d364e5952b4dda1d13bc6f2d536b691f575fba1efc42ee7d8f390bc100c0dd0437b843e7
6
+ metadata.gz: 81c3ab25a45088f9d0b3d0d693e1c43d295ada2d76089646fd43ef9598fd0319a59164012a1cc4768dc11e54861202d6613c72f305d2fd2b0e7027bafd7aeee9
7
+ data.tar.gz: da6a8060951a5699c1ad67c3ddb58df91e7983dd62055c366155f3b1d1da49968a0c5ea2b34f7dc388cdc5db3c64b15130997b6226cb8bcb0beebe32355b7563
@@ -4,84 +4,118 @@ module Smalruby
4
4
  # お絵かきを表現するクラス
5
5
  class Canvas < Character
6
6
  def initialize(option = {})
7
- opt = {
7
+ defaults = {
8
8
  x: 0,
9
9
  y: 0,
10
10
  width: Window.width,
11
11
  height: Window.height,
12
- }.merge(option)
12
+ }
13
+ opt = process_optional_arguments(option, defaults)
14
+
13
15
  opt[:costume] = Image.new(opt[:width], opt[:height])
14
- super(opt)
16
+ super(opt.reject { |k, v| [:width, :height].include?(k) })
15
17
  image.set_color_key([0, 0, 0])
16
18
  end
17
19
 
18
20
  # @!group ペン
19
21
 
20
22
  def draw_font(option)
21
- opt = {
23
+ defaults = {
22
24
  x: 0,
23
25
  y: 0,
24
26
  string: "",
25
27
  size: 32,
26
- color: "white",
27
- }.merge(option)
28
+ }.merge(DEFAULT_COLOR_OPTION)
29
+ opt = process_optional_arguments(option, defaults)
30
+
28
31
  image.draw_font(opt[:x], opt[:y], opt[:string], new_font(opt[:size]),
29
32
  Color.smalruby_to_dxruby(opt[:color]))
30
33
  end
31
34
 
32
35
  def line(option)
33
- opt = {
34
- color: "white",
35
- }.merge(verify_rect_option(option))
36
+ opt = process_rect_option(option)
37
+
36
38
  image.line(opt[:left], opt[:top], opt[:right], opt[:bottom],
37
39
  Color.smalruby_to_dxruby(opt[:color]))
38
40
  end
39
41
 
40
42
  def box(option)
41
- opt = {
42
- color: "white",
43
- }.merge(verify_rect_option(option))
43
+ opt = process_rect_option(option)
44
+
44
45
  image.box(opt[:left], opt[:top], opt[:right], opt[:bottom],
45
46
  Color.smalruby_to_dxruby(opt[:color]))
46
47
  end
47
48
 
48
49
  def box_fill(option)
49
- opt = {
50
- color: "white",
51
- }.merge(verify_rect_option(option))
50
+ opt = process_rect_option(option)
51
+
52
52
  image.box_fill(opt[:left], opt[:top], opt[:right], opt[:bottom],
53
53
  Color.smalruby_to_dxruby(opt[:color]))
54
54
  end
55
55
 
56
56
  def circle(option)
57
- opt = {
58
- color: "white",
59
- }.merge(option)
57
+ opt = process_circle_option(option)
58
+
60
59
  image.circle(opt[:x], opt[:y], opt[:r],
61
60
  Color.smalruby_to_dxruby(opt[:color]))
62
61
  end
63
62
 
64
63
  def circle_fill(option)
65
- opt = {
66
- color: "white",
67
- }.merge(option)
64
+ opt = process_circle_option(option)
65
+
68
66
  image.circle_fill(opt[:x], opt[:y], opt[:r],
69
67
  Color.smalruby_to_dxruby(opt[:color]))
70
68
  end
71
69
 
70
+ def fill(option)
71
+ opt = process_optional_arguments(option, DEFAULT_COLOR_OPTION)
72
+
73
+ image.box_fill(0, 0, image.width, image.height,
74
+ Color.smalruby_to_dxruby(opt[:color]))
75
+ end
76
+
72
77
  def_delegators :image, :clear
73
78
 
74
79
  # @!endgroup
75
80
 
76
81
  private
77
82
 
78
- def verify_rect_option(option)
79
- return {
83
+ DEFAULT_COLOR_OPTION = {
84
+ color: 'white',
85
+ }
86
+ private_constant :DEFAULT_COLOR_OPTION
87
+
88
+ DEFAULT_RECT_OPTION = {
89
+ left: nil,
90
+ top: nil,
91
+ right: nil,
92
+ bottom: nil,
93
+
94
+ x1: nil,
95
+ y1: nil,
96
+ x2: nil,
97
+ y2: nil,
98
+ }
99
+ private_constant :DEFAULT_RECT_OPTION
100
+
101
+ def process_rect_option(option)
102
+ defaults = DEFAULT_COLOR_OPTION.merge(DEFAULT_RECT_OPTION)
103
+ opt = {
80
104
  left: option[:x1],
81
105
  top: option[:y1],
82
106
  right: option[:x2],
83
107
  bottom: option[:y2],
84
108
  }.merge(option)
109
+ process_optional_arguments(opt, defaults)
110
+ end
111
+
112
+ def process_circle_option(option)
113
+ defaults = {
114
+ x: 5,
115
+ y: 5,
116
+ r: 5,
117
+ }.merge(DEFAULT_COLOR_OPTION)
118
+ process_optional_arguments(option, defaults)
85
119
  end
86
120
  end
87
121
  end
@@ -16,12 +16,13 @@ module Smalruby
16
16
  attr_accessor :checking_hit_targets
17
17
 
18
18
  def initialize(option = {})
19
- opt = {
19
+ defaults = {
20
20
  x: 0,
21
21
  y: 0,
22
22
  costume: nil,
23
23
  visible: true
24
- }.merge(option)
24
+ }
25
+ opt = process_optional_arguments(option, defaults)
25
26
 
26
27
  # TODO: コスチュームの配列に対応する
27
28
  if opt[:costume].is_a?(String)
@@ -130,8 +131,13 @@ module Smalruby
130
131
  # @!group 音
131
132
 
132
133
  def play(option = {})
134
+ defaults = {
135
+ name: 'piano_do.wav',
136
+ }
137
+ opt = process_optional_arguments(option, defaults)
138
+
133
139
  @sound_cache ||= {}
134
- (@sound_cache[option[:name]] ||= Sound.new(asset_path(option[:name])))
140
+ (@sound_cache[opt[:name]] ||= Sound.new(asset_path(opt[:name])))
135
141
  .play
136
142
  end
137
143
 
@@ -208,15 +214,27 @@ module Smalruby
208
214
  objects = check(@checking_hit_targets)
209
215
  return if objects.empty?
210
216
  @event_handlers[:hit].try(:each) do |h|
211
- if h.options.length == 0 && !h.options.any? { |o| objects.include?(o) }
217
+ if h.options.length > 0 && !h.options.any? { |o| objects.include?(o) }
212
218
  next
213
219
  end
214
- @threads << h.call(objects)
220
+ @threads << h.call(h.options & objects)
215
221
  end
216
222
  end
217
223
 
218
224
  def alive?
219
- return @threads.any?(&:alive?)
225
+ @threads.delete_if { |t|
226
+ if t.alive?
227
+ false
228
+ else
229
+ begin
230
+ t.join
231
+ rescue => e
232
+ print_exception(e)
233
+ end
234
+ true
235
+ end
236
+ }
237
+ @threads.length > 0
220
238
  end
221
239
 
222
240
  def join
@@ -263,5 +281,19 @@ module Smalruby
263
281
  @balloon.draw
264
282
  end
265
283
  end
284
+
285
+ def process_optional_arguments(options, defaults)
286
+ unknown_keys = options.keys - defaults.keys
287
+ if unknown_keys.length > 0
288
+ s = unknown_keys.map { |k| "#{k}: #{options[k].inspect}" }.join(', ')
289
+ fail ArgumentError, "Unknown options: #{s}"
290
+ end
291
+ defaults.merge(options)
292
+ end
293
+
294
+ def print_exception(exception)
295
+ $stderr.puts("#{exception.class}: #{exception.message}")
296
+ $stderr.puts(" #{exception.backtrace.join("\n ")}")
297
+ end
266
298
  end
267
299
  end
@@ -1,5 +1,5 @@
1
1
  # -*- coding: utf-8 -*-
2
2
 
3
3
  module Smalruby
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.5'
5
5
  end
data/lib/smalruby.rb CHANGED
@@ -168,7 +168,5 @@ end
168
168
  include Smalruby
169
169
 
170
170
  at_exit do
171
- if !Smalruby.started?
172
- Smalruby.start
173
- end
171
+ Smalruby.start if !$ERROR_INFO && !Smalruby.started?
174
172
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smalruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouji Takao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-11 00:00:00.000000000 Z
11
+ date: 2014-01-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler