/src/gosu/jruby/SampleInstanceProxy.java

http://jgosu.googlecode.com/ · Java · 76 lines · 53 code · 23 blank · 0 comment · 0 complexity · 919af0ab9fa04b469d282f9e3d6f1643 MD5 · raw file

  1. import gosu.SampleInstance;
  2. import org.jruby.*;
  3. import org.jruby.runtime.*;
  4. import org.jruby.runtime.builtin.IRubyObject;
  5. import org.jruby.anno.JRubyMethod;
  6. import static org.jruby.RubyNumeric.*;
  7. public class SampleInstanceProxy extends RubyObject {
  8. private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
  9. public IRubyObject allocate(Ruby runtime, RubyClass klass) {
  10. return new SampleInstanceProxy(runtime, klass);
  11. }
  12. };
  13. private SampleInstance _sampleInstance;
  14. private final Ruby _runtime;
  15. public static void createSampleInstanceClass(Ruby runtime) {
  16. RubyModule mGosu = runtime.getModule("Gosu");
  17. RubyClass cSampleInstance = mGosu.defineClassUnder("SampleInstance", runtime.getObject(), ALLOCATOR);
  18. cSampleInstance.defineAnnotatedMethods(SampleInstanceProxy.class);
  19. }
  20. public SampleInstanceProxy(Ruby runtime, RubyClass type) {
  21. super(runtime, type);
  22. _runtime = runtime;
  23. }
  24. public SampleInstance getSampleInstance() { return _sampleInstance; }
  25. public void setSampleInstance(SampleInstance si) { _sampleInstance = si; }
  26. @JRubyMethod(name = "playing?")
  27. public IRubyObject isPlaying() {
  28. return _runtime.newBoolean(_sampleInstance.isPlaying());
  29. }
  30. @JRubyMethod(name = "stop")
  31. public IRubyObject stop() {
  32. _sampleInstance.stop();
  33. return _runtime.getNil();
  34. }
  35. @JRubyMethod(name = "volume=",
  36. required = 1)
  37. public IRubyObject setVolume(IRubyObject vol) {
  38. _sampleInstance.setVolume((float) num2dbl(vol));
  39. return vol;
  40. }
  41. @JRubyMethod(name = "pan=",
  42. required = 1)
  43. public IRubyObject setPan(IRubyObject pan) {
  44. _sampleInstance.setPan((float) num2dbl(pan));
  45. return pan;
  46. }
  47. @JRubyMethod(name = "speed=",
  48. required = 1)
  49. public IRubyObject setSpeed(IRubyObject speed) {
  50. _sampleInstance.setSpeed((float) num2dbl(speed));
  51. return speed;
  52. }
  53. }