/src/gosu/jruby/TextInputProxy.java

http://jgosu.googlecode.com/ · Java · 73 lines · 51 code · 22 blank · 0 comment · 0 complexity · 52fa988dda98a778fc986cb7813082d7 MD5 · raw file

  1. import gosu.TextInput;
  2. import org.jruby.*;
  3. import org.jruby.runtime.*;
  4. import org.jruby.runtime.builtin.IRubyObject;
  5. import org.jruby.javasupport.JavaUtil;
  6. import org.jruby.anno.JRubyMethod;
  7. import static org.jruby.RubyNumeric.*;
  8. public class TextInputProxy extends RubyObject {
  9. private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
  10. public IRubyObject allocate(Ruby runtime, RubyClass klass) {
  11. return new TextInputProxy(runtime, klass);
  12. }
  13. };
  14. private TextInput _input;
  15. private final Ruby _runtime;
  16. public static void createTextInputClass(Ruby runtime) {
  17. RubyModule mGosu = runtime.getModule("Gosu");
  18. RubyClass cTextInput = mGosu.defineClassUnder("TextInput", runtime.getObject(), ALLOCATOR);
  19. cTextInput.defineAnnotatedMethods(TextInputProxy.class);
  20. }
  21. public TextInputProxy(Ruby runtime, RubyClass type) {
  22. super(runtime, type);
  23. _runtime = runtime;
  24. }
  25. public TextInput getTextInput() { return _input; }
  26. public void setTextInput(TextInput input) { _input = input; }
  27. @JRubyMethod(name = "initialize",
  28. visibility = Visibility.PRIVATE)
  29. public IRubyObject initialize() {
  30. _input = new TextInput();
  31. return this;
  32. }
  33. @JRubyMethod(name = "text")
  34. public IRubyObject getText() {
  35. return _runtime.newString(_input.getText());
  36. }
  37. @JRubyMethod(name = "text=",
  38. required = 1)
  39. public IRubyObject setText(IRubyObject arg) {
  40. _input.setText(arg.toString());
  41. return arg;
  42. }
  43. @JRubyMethod(name = "caret_pos")
  44. public IRubyObject getCaretPos() {
  45. return _runtime.newFixnum(_input.getCaretPos());
  46. }
  47. @JRubyMethod(name = "selection_start")
  48. public IRubyObject getSelectionStart() {
  49. return _runtime.newFixnum(_input.getSelectionStart());
  50. }
  51. }