|
1 | 1 | /**
|
2 | 2 | * The purpose of this tool is to allow JRubyArt users to use an alternative
|
3 |
| - * to processing.org map, lerp and norm methods in their sketches |
4 |
| - * Copyright (c) 2015-16 Martin Prout. This tool is free software; you can |
| 3 | + * to processing.org map, lerp and norm methods in their sketches and to implement |
| 4 | + * JRubyArt convenenience method grid(width, height, stepW, stepH) { |x, y| do stuff } |
| 5 | + * Copyright (c) 2015-17 Martin Prout. This tool is free software; you can |
5 | 6 | * redistribute it and/or modify it under the terms of the GNU Lesser General
|
6 | 7 | * Public License as published by the Free Software Foundation; either version
|
7 | 8 | * 2.1 of the License, or (at your option) any later version.
|
|
17 | 18 | import org.jruby.RubyRange;
|
18 | 19 | import org.jruby.anno.JRubyMethod;
|
19 | 20 | import org.jruby.anno.JRubyModule;
|
| 21 | +import org.jruby.runtime.Block; |
20 | 22 | import org.jruby.runtime.ThreadContext;
|
21 | 23 | import org.jruby.runtime.builtin.IRubyObject;
|
22 | 24 |
|
@@ -201,4 +203,35 @@ public static IRubyObject constrainValue(ThreadContext context, IRubyObject recv
|
201 | 203 | return args[1];
|
202 | 204 | }
|
203 | 205 | }
|
| 206 | + |
| 207 | + /** |
| 208 | + * Provides JRubyArt grid method as a ruby module method |
| 209 | + * |
| 210 | + * @param context ThreadContext |
| 211 | + * @param recv IRubyObject |
| 212 | + * @param args array of args should be Fixnum |
| 213 | + * @param block { |x, y| `do something` } |
| 214 | + * @return nil |
| 215 | + */ |
| 216 | + @JRubyMethod(name = "grid", rest = true, module = true) |
| 217 | + public static IRubyObject createGrid(ThreadContext context, IRubyObject recv, IRubyObject[] args, Block block) { |
| 218 | + int row = (Integer) args[0].toJava(Integer.class); |
| 219 | + int column = (Integer) args[1].toJava(Integer.class); |
| 220 | + int rowStep = 1; |
| 221 | + int colStep = 1; |
| 222 | + if (args.length == 4){ |
| 223 | + rowStep = (Integer) args[2].toJava(Integer.class); |
| 224 | + colStep = (Integer) args[3].toJava(Integer.class); |
| 225 | + } |
| 226 | + if (block.isGiven()) { |
| 227 | + for (int x = 0; x < row / rowStep; x++){ |
| 228 | + for (int y = 0; y < column / colStep; y++){ |
| 229 | + block.yieldSpecific(context, context.runtime.newFixnum(x * rowStep), context.runtime.newFixnum(y * colStep)); |
| 230 | + |
| 231 | + } |
| 232 | + } |
| 233 | + } |
| 234 | + return context.nil; |
| 235 | + |
| 236 | + } |
204 | 237 | }
|
0 commit comments