/src/gosu/jruby/SampleProxy.java

http://jgosu.googlecode.com/ · Java · 98 lines · 77 code · 21 blank · 0 comment · 2 complexity · 066c751e6e4a563d212b5185c7cce268 MD5 · raw file

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