PageRenderTime 39ms CodeModel.GetById 20ms app.highlight 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/gosu/jruby/SampleProxy.java

http://jgosu.googlecode.com/
Java | 98 lines | 77 code | 21 blank | 0 comment | 2 complexity | 066c751e6e4a563d212b5185c7cce268 MD5 | raw file
 1import gosu.Sample;
 2import gosu.SampleInstance;
 3
 4import org.jruby.*;
 5import org.jruby.runtime.*;
 6import org.jruby.runtime.builtin.IRubyObject;
 7import org.jruby.anno.JRubyMethod;
 8import static org.jruby.RubyNumeric.*;
 9
10
11public class SampleProxy extends RubyObject {
12  private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
13    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
14      return new SampleProxy(runtime, klass);
15    }
16  };
17
18  private Sample _sample;
19
20  private final Ruby _runtime;
21
22  public static void createSampleClass(Ruby runtime) {
23    RubyModule mGosu = runtime.getModule("Gosu");
24    RubyClass cSample = mGosu.defineClassUnder("Sample", runtime.getObject(), ALLOCATOR);
25
26    cSample.defineAnnotatedMethods(SampleProxy.class);
27  }
28
29
30  public SampleProxy(Ruby runtime, RubyClass type) {
31    super(runtime, type);
32    _runtime = runtime;
33  }
34
35
36  public Sample getSample() { return _sample; }
37
38
39  @JRubyMethod(name = "initialize",
40               required = 2,
41               visibility = Visibility.PRIVATE)
42  public IRubyObject initialize(IRubyObject window, IRubyObject filename) {
43    try {
44      _sample = new Sample(((WindowProxy) window).getWindow(), ((RubyString) filename).toString());
45    } catch (RuntimeException e) {
46      throw _runtime.newRuntimeError(e.toString());
47    }
48    return this;
49  }
50
51
52  @JRubyMethod(name = "play",
53               required = 0,
54               optional = 2)
55  public IRubyObject play(IRubyObject[] args) {
56    SampleInstance si = null;
57
58    switch (args.length) {
59      case 0:
60        si = _sample.play();
61        break;
62      case 1:
63        si = _sample.play((float) num2dbl(args[0]));
64        break;
65      case 2:
66        si = _sample.play((float) num2dbl(args[0]), (float) num2dbl(args[1]));
67        break;
68    }
69
70    SampleInstanceProxy proxy = (SampleInstanceProxy) _runtime.getModule("Gosu").getClass("SampleInstance").allocate();
71    proxy.setSampleInstance(si);
72    return proxy;
73  }
74
75
76  @JRubyMethod(name = "play_pan",
77               required = 1,
78               optional = 2)
79  public IRubyObject playPan(IRubyObject[] args) {
80    SampleInstance si = null;
81
82    switch (args.length) {
83      case 1:
84        si = _sample.playPan((float) num2dbl(args[0]));
85        break;
86      case 2:
87        si = _sample.playPan((float) num2dbl(args[0]), (float) num2dbl(args[1]));
88        break;
89      case 3:
90        si = _sample.playPan((float) num2dbl(args[0]), (float) num2dbl(args[1]), (float) num2dbl(args[2]));
91        break;
92    }
93
94    SampleInstanceProxy proxy = (SampleInstanceProxy) _runtime.getModule("Gosu").getClass("SampleInstance").allocate();
95    proxy.setSampleInstance(si);
96    return proxy;
97  }
98}