PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gosu/jruby/FontProxy.java

http://jgosu.googlecode.com/
Java | 179 lines | 144 code | 35 blank | 0 comment | 10 complexity | b77c0e7cb40b9596d893a3e99762f106 MD5 | raw file
  1import gosu.Font;
  2import gosu.Color;
  3import gosu.Image;
  4
  5import org.jruby.*;
  6import org.jruby.runtime.*;
  7import org.jruby.runtime.builtin.IRubyObject;
  8import org.jruby.javasupport.JavaUtil;
  9import org.jruby.anno.JRubyMethod;
 10import static org.jruby.RubyNumeric.*;
 11
 12
 13public class FontProxy extends RubyObject {
 14  private static ObjectAllocator ALLOCATOR = new ObjectAllocator() {
 15    public IRubyObject allocate(Ruby runtime, RubyClass klass) {
 16      return new FontProxy(runtime, klass);
 17    }
 18  };
 19
 20  private Font _font;
 21
 22  private Ruby _runtime;
 23
 24  public static void createFontClass(Ruby runtime) {
 25    RubyModule mGosu = runtime.getModule("Gosu");
 26    RubyClass cFont = mGosu.defineClassUnder("Font", runtime.getObject(), ALLOCATOR);
 27
 28    cFont.defineAnnotatedMethods(FontProxy.class);
 29  }
 30
 31
 32  public FontProxy(Ruby runtime, RubyClass type) {
 33    super(runtime, type);
 34    _runtime = runtime;
 35  }
 36
 37
 38  public Font getFont() { return _font; }
 39
 40
 41  @JRubyMethod(name = "initialize",
 42               required = 3,
 43               frame = true,
 44               visibility = Visibility.PRIVATE)
 45  public IRubyObject initialize(IRubyObject window, IRubyObject fontName, IRubyObject height) {
 46    try {
 47      _font = new Font(((WindowProxy) window).getWindow(), ((RubyString) fontName).toString(), fix2int(height));
 48    } catch (RuntimeException e) {
 49      throw _runtime.newRuntimeError(e.getMessage());
 50    }
 51    return this;
 52  }
 53
 54
 55  @JRubyMethod(name = "draw",
 56               required = 4,
 57               optional = 4)
 58  public IRubyObject draw(IRubyObject[] args) {
 59    String text = args[0].asString().getUnicodeValue();
 60    double x = num2dbl(args[1]);
 61    double y = num2dbl(args[2]);
 62    double z = num2dbl(args[3]);
 63
 64    switch (args.length) {
 65      case 4:
 66        _font.draw(text, x, y, z);
 67        break;
 68      case 5:
 69        _font.draw(text, x, y, z, num2dbl(args[4]), 1.0);
 70        break;
 71      case 6:
 72        _font.draw(text, x, y, z, num2dbl(args[4]), num2dbl(args[5]));
 73        break;
 74      default: { // 7 or 8
 75        Color c = WindowProxy.getColorFrom(args[6]);
 76
 77        int mode = Image.DEFAULT;
 78        if (args.length == 8) {
 79          mode = WindowProxy.getAlphaModeFrom(_runtime, args[7]);
 80        }
 81
 82        _font.draw(text, x, y, z, num2dbl(args[4]), num2dbl(args[5]), c, mode);
 83      }
 84    }
 85
 86    return _runtime.getNil();
 87  }
 88
 89
 90  @JRubyMethod(name = "draw_rot",
 91               required = 5,
 92               optional = 4)
 93  public IRubyObject drawRot(IRubyObject[] args) {
 94    String text = args[0].asString().getUnicodeValue();
 95    double x = num2dbl(args[1]);
 96    double y = num2dbl(args[2]);
 97    double z = num2dbl(args[3]);
 98    double angle = num2dbl(args[4]);
 99
100    switch (args.length) {
101      case 5:
102        _font.drawRot(text, x, y, z, angle);
103        break;
104      case 6:
105        _font.drawRot(text, x, y, z, angle, num2dbl(args[5]), 1.0);
106        break;
107      case 7:
108        _font.drawRot(text, x, y, z, angle, num2dbl(args[5]), num2dbl(args[6]));
109        break;
110      default: { // 8 or 9
111        Color c = WindowProxy.getColorFrom(args[7]);
112
113        int mode = Image.DEFAULT;
114        if (args.length == 9) {
115          mode = WindowProxy.getAlphaModeFrom(_runtime, args[8]);
116        }
117
118        _font.drawRot(text, x, y, z, angle, num2dbl(args[5]), num2dbl(args[6]), c, mode);
119      }
120    }
121
122    return _runtime.getNil();
123  }
124
125
126  @JRubyMethod(name = "draw_rel",
127               required = 6,
128               optional = 4)
129  public IRubyObject drawRel(IRubyObject[] args) {
130    String text = args[0].asString().getUnicodeValue();
131    double x = num2dbl(args[1]);
132    double y = num2dbl(args[2]);
133    double z = num2dbl(args[3]);
134    double relX = num2dbl(args[4]);
135    double relY = num2dbl(args[5]);
136
137    switch (args.length) {
138      case 6:
139        _font.drawRel(text, x, y, z, relX, relY);
140        break;
141      case 7:
142        _font.drawRel(text, x, y, z, relX, relY, num2dbl(args[6]), 1.0);
143        break;
144      case 8:
145        _font.drawRel(text, x, y, z, relX, relY, num2dbl(args[6]), num2dbl(args[7]));
146        break;
147      default: { // 9 or 10
148        Color c = WindowProxy.getColorFrom(args[8]);
149
150        int mode = Image.DEFAULT;
151        if (args.length == 10) {
152          mode = WindowProxy.getAlphaModeFrom(_runtime, args[9]);
153        }
154
155        _font.drawRel(text, x, y, z, relX, relY, num2dbl(args[6]), num2dbl(args[7]), c, mode);
156      }
157    }
158
159    return _runtime.getNil();
160  }
161
162
163  @JRubyMethod(name = "text_width",
164               required = 1,
165               optional = 1)
166  public IRubyObject textWidth(IRubyObject[] args) {
167    double width = _font.textWidth(args[0].toString());
168    if (args.length > 1) {
169      return _runtime.newFloat(width * num2dbl(args[1]));
170    }
171    return _runtime.newFloat(width);
172  }
173
174
175  @JRubyMethod(name = "height")
176  public IRubyObject height() {
177    return _runtime.newFixnum(_font.getHeight());
178  }
179}