From 161894e9b540b4f743db9f512a1ac3549e87b501 Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 10 Jul 2016 06:49:22 +0100 Subject: [PATCH 01/19] first steps --- jruby_art.gemspec | 2 +- lib/jruby_art/runner.rb | 254 +++++++++++++++++++-------------------- lib/jruby_art/version.rb | 2 +- pom.rb | 2 +- 4 files changed, 130 insertions(+), 130 deletions(-) diff --git a/jruby_art.gemspec b/jruby_art.gemspec index 5ce85e7f..0733a91d 100644 --- a/jruby_art.gemspec +++ b/jruby_art.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |spec| JRubyArt is a ruby wrapper for the processing art framework. This version supports processing-3.1.1, and uses jruby-9.1.2.0 as the glue between ruby and java. You can use both processing libraries and ruby gems - in your sketches. Features create/run/watch/live modes. The "watch" mode, + in your sketches. Features create/run/watch/live modes. The "--watch" mode, provides a nice REPL-ish way to work on your processing sketches. Includes:- A "Control Panel" library, so that you can easily create sliders, buttons, checkboxes and drop-down menus, and hook them into your sketch's instance diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 787d9847..b919da6a 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -1,6 +1,6 @@ # frozen_string_literal: false -require 'ostruct' +require 'optparse' require 'fileutils' require 'rbconfig' require_relative '../jruby_art/config' @@ -13,185 +13,185 @@ module Processing # Utility class to handle the different commands that the 'k9' command # offers. Able to run, watch, live, create, app, and unpack class Runner - HELP_MESSAGE ||= <<-EOS - Version: #{JRubyArt::VERSION} - - JRubyArt is a little shim between Processing and JRuby that helps - you create sketches of code art. - - Usage: - k9 [choice] sketch - - choice:- - run: run sketch once - watch: watch for changes on the file and relaunch it on the fly - live: run sketch and open a pry console bound to $app - create [width height][mode][flag]: create a new sketch. - setup: check / install / unpack_samples - - Common options: - --nojruby: use jruby-complete in place of an installed version of jruby - (Set [JRUBY: 'false'] in .jruby_art/config.yml to make using jruby-complete default) - - Examples: - k9 setup unpack_samples - k9 run rp_samples/samples/contributed/jwishy.rb - k9 create some_new_sketch 640 480 p3d (P3D mode example) - k9 create some_new_sketch 640 480 --wrap (a class wrapped default sketch) - k9 watch some_new_sketch.rb - - Everything Else: - https://ruby-processing.github.io/ - - EOS - + WIN_PATTERNS = [ - /bccwin/i, - /cygwin/i, - /djgpp/i, - /ming/i, - /mswin/i, - /wince/i + /bccwin/i, + /cygwin/i, + /djgpp/i, + /ming/i, + /mswin/i, + /wince/i ] - - attr_reader :os - - # Start running a jruby_art sketch from the passed-in arguments + + attr_reader :options, :argc, :filename, :os + + def initialize + @options = {} + end + + # Start running a jruby_art filename from the passed-in arguments def self.execute runner = new runner.parse_options(ARGV) runner.execute! end - + # Dispatch central. def execute! - case @options.action - when 'run' then run(@options.path, @options.args) - when 'live' then live(@options.path, @options.args) - when 'watch' then watch(@options.path, @options.args) - when 'create' then create(@options.path, @options.args) - when 'setup' then setup(@options.path) - when /-v/ then show_version - when /-h/ then show_help - else - show_help - end - end - + show_help if options.empty? + show_version if options[:version] + run_sketch if options[:run] + watch_sketch if options[:watch] + create if options[:create] + install if options[:install] + end + + # Parse the command-line options. Keep it simple. def parse_options(args) - @options = OpenStruct.new - @options.emacs = !args.delete('--emacs').nil? - @options.wrap = !args.delete('--wrap').nil? - @options.inner = !args.delete('--inner').nil? - @options.jruby = !args.delete('--jruby').nil? - @options.nojruby = !args.delete('--nojruby').nil? - @options.action = args[0] || nil - @options.path = args[1] || File.basename(Dir.pwd + '.rb') - @options.args = args[2..-1] || [] + opt_parser = OptionParser.new do |opts| + # Set a banner, displayed at the top + # of the help screen. + opts.banner = 'Usage: k9 [options] []' + # Define the options, and what they do + options[:version] = false + opts.on('-v', '--version', 'JRubyArt Version') do + options[:version] = true + end + + options[:install] = false + opts.on('-i', '--install', 'Installs jruby-complete and examples') do + options[:install] = true + end + + options[:check] = false + opts.on('-i', '--install', 'Prints configuration') do + options[:check] = true + end + + options[:watch] = false + opts.on('-w', '--watch', 'Watch/run the sketch') do + options[:watch] = true + end + + options[:run] = false + opts.on('-r', '--run', 'Run the filename using jruby-complete') do + options[:run] = true + end + + options[:create] = false + opts.on('-c', '--create', 'Create new bare filename outline') do + options[:create] = true + end + + # This displays the help screen, all programs are + # assumed to have this option. + opts.on('-h', '--help', 'Display this screen') do + puts opts + exit + end + end + @argc = opt_parser.parse(args) + @filename = argc.shift end - - # Create a fresh JRubyArt sketch, with the necessary + + # Create a fresh JRubyArt filename, with the necessary # boilerplate filled out. - def create(sketch, args) + def create require_relative '../jruby_art/creators/creator' - return Creator::Inner.new.create!(sketch, args) if @options.inner - return Creator::ClassSketch.new.create!(sketch, args) if @options.wrap - return Creator::EmacsSketch.new.create!(sketch, args) if @options.emacs - Creator::BasicSketch.new.create!(sketch, args) - end - - # Just simply run a JRubyArt sketch. - def run(sketch, args) - ensure_exists(sketch) - spin_up('run.rb', sketch, args) + Creator::BasicSketch.new.create!(filename, argc) end - - # Just simply run a JRubyArt sketch. - def live(sketch, args) - ensure_exists(sketch) - spin_up('live.rb', sketch, args) + + def create_emacs + require_relative '../jruby_art/creators/creator' + Creator::BasicSketch.new.create!(filename, argc) end - - # Run a sketch, keeping an eye on it's file, and reloading + + def create_wrap + require_relative '../jruby_art/creators/creator' + Creator::BasicSketch.new.create!(filename, argc) + end + + # Just simply run a JRubyArt filename. + def run_sketch + ensure_exists(filename) + spin_up('run.rb', filename, argc) + end + + # Just simply run a JRubyArt filename. + def live + ensure_exists(filename) + spin_up('live.rb', filename, argc) + end + + # Run a filename, keeping an eye on it's file, and reloading # whenever it changes. - def watch(sketch, args) - ensure_exists(sketch) - spin_up('watch.rb', sketch, args) + def watch_sketch + ensure_exists(filename) + spin_up('watch.rb', filename, argc) end - - def setup(choice) - return Check.new(K9_ROOT, host_os).install if choice =~ /check/ - return JRubyComplete.new(K9_ROOT, host_os).install if choice =~ /install/ - return UnpackSamples.new(K9_ROOT, host_os).install if choice =~ /unpack_sample/ - Installer.new(K9_ROOT, host_os).install + + def install + JRubyComplete.new(K9_ROOT, host_os).install + UnpackSamples.new(K9_ROOT, host_os).install end - + + def check + Check.new(K9_ROOT, host_os).install + end + # Show the standard help/usage message. def show_help puts HELP_MESSAGE end - + def show_version puts format('JRubyArt version %s', JRubyArt::VERSION) end - + private - + # Trade in this Ruby instance for a JRuby instance, loading in a starter # script and passing it some arguments. Unless '--nojruby' is passed, the # installed version of jruby is used instead of our vendored one. To use # jruby-complete by default set JRUBY: false in ~/.jruby_art/config.yml # (however that might make using other gems in your sketches hard....) - def spin_up(starter_script, sketch, args) + def spin_up(starter_script, filename, argc) runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" - @options.nojruby = true if Processing::RP_CONFIG['JRUBY'] == 'false' - if @options.nojruby - opts = JavaOpts.new(SKETCH_ROOT).opts - command = ['java', - opts, - '-cp', - jruby_complete, - 'org.jruby.Main', - runner, - sketch, - args].flatten - else - opts = JRubyOpts.new(SKETCH_ROOT).opts - command = ['jruby', - opts, - runner, - sketch, - args].flatten - end + opts = JRubyOpts.new(SKETCH_ROOT).opts + command = ['jruby', + opts, + runner, + filename, + argc].flatten begin exec(*command) # exec replaces the Ruby process with the JRuby one. rescue Java::JavaLang::ClassNotFoundException end end - + # NB: We really do mean to use 'and' not '&&' for flow control purposes - - def ensure_exists(sketch) - puts "Couldn't find: #{sketch}" and exit unless FileTest.exist?(sketch) + + def ensure_exists(filename) + puts "Couldn't find: #{filename}" and exit unless FileTest.exist?(filename) end - + def jruby_complete rcomplete = File.join(K9_ROOT, 'lib/ruby/jruby-complete.jar') return [rcomplete] if FileTest.exist?(rcomplete) warn "#{rcomplete} does not exist\nTry running `k9 setup install`" exit end - + def libraries %w(video sound).map { |library| sketchbook_library(library) }.flatten end - + def sketchbook_library(name) Dir["#{Processing::RP_CONFIG['sketchbook_path']}/libraries/#{name}/library/\*.jar"] end - + def host_os detect_os = RbConfig::CONFIG['host_os'] case detect_os diff --git a/lib/jruby_art/version.rb b/lib/jruby_art/version.rb index cd7c853d..4a19f952 100644 --- a/lib/jruby_art/version.rb +++ b/lib/jruby_art/version.rb @@ -2,5 +2,5 @@ # frozen_string_literal: true # A wrapper for version module JRubyArt - VERSION = '1.1.4'.freeze + VERSION = '1.2.0.pre'.freeze end diff --git a/pom.rb b/pom.rb index 7a7169a7..c2d223a7 100644 --- a/pom.rb +++ b/pom.rb @@ -5,7 +5,7 @@ project 'rp5extras', 'https://github.com/ruby-processing/JRubyArt' do model_version '4.0.0' - id 'ruby-processing:rp5extras', '1.1.4' + id 'ruby-processing:rp5extras', '1.2.0.pre' packaging 'jar' description 'rp5extras for JRubyArt' From 345ebdcf98be82122ec438c90f485dee1ef29a1a Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 10 Jul 2016 06:58:37 +0100 Subject: [PATCH 02/19] update --run tests --- pom.xml | 2 +- test/#k9_run_test.rb# | 104 ++++++++++++++++++++++++++++++++++++++++++ test/k9_run_test.rb | 19 ++++---- 3 files changed, 114 insertions(+), 11 deletions(-) create mode 100644 test/#k9_run_test.rb# diff --git a/pom.xml b/pom.xml index e56141f4..837b928f 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ DO NOT MODIFIY - GENERATED CODE 4.0.0 ruby-processing rp5extras - 1.1.4 + 1.2.0.pre rp5extras rp5extras for JRubyArt https://github.com/ruby-processing/JRubyArt diff --git a/test/#k9_run_test.rb# b/test/#k9_run_test.rb# new file mode 100644 index 00000000..70a9d8ca --- /dev/null +++ b/test/#k9_run_test.rb# @@ -0,0 +1,104 @@ +gem 'minitest' # don't use bundled minitest +require 'minitest/autorun' +require 'minitest/pride' + +Dir.chdir(File.dirname(__FILE__)) + +class Rp5Test < Minitest::Test + + def test_normal + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/basic.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert_match(/ok/, out, 'Failed Basic Sketch') + end + + def test_sketch_path + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/sketch_path.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert_match('/home/sid/JRubyArt/test', out, 'Failed Sketch Path Sketch') + end + + def test_on_top + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/on_top.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert_match(/ok/, out, 'Failed On Top Sketch') + end + + def test_p2d + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/p2d.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert_match(/ok/, out, 'Failed P2D sketch') + end + + def test_proc_root + require 'psych' + path = File.expand_path('~/.jruby_art/config.yml') + config = FileTest.exist?(path)? Psych.load_file(path) : {} + root = config.empty? ? '' : config['PROCESSING_ROOT'] + assert root =~ /processing/, 'You need to set your PROCESSING_ROOT in .rp5rc' + end + + def test_fx2d + out, _err = capture_io do + open('|../bin/k9 -r sketches/fx2d.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + end + + def test_p3d + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/p3d.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert_match(/ok/, out, 'Failed P3D sketch') + end + + def test_graphics + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/graphics.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert out[0].to_i >= 3, "Graphics capability #{out} may be sub-optimal" + end + + def test_setup_exception + out, _err_ = capture_io do + open('|../bin/k9 -r sketches/setup_ex.rb', 'r') do |io| + while l = io.gets + puts(l.chop) + end + end + end + assert out.index("undefined method `unknown_method'"), 'Failed to raise exception?' + end +end +rake \ No newline at end of file diff --git a/test/k9_run_test.rb b/test/k9_run_test.rb index 46fd73bd..2a9bbea5 100644 --- a/test/k9_run_test.rb +++ b/test/k9_run_test.rb @@ -8,7 +8,7 @@ class Rp5Test < Minitest::Test def test_normal out, _err_ = capture_io do - open('|../bin/k9 run sketches/basic.rb', 'r') do |io| + open('|../bin/k9 -r sketches/basic.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -19,18 +19,18 @@ def test_normal def test_sketch_path out, _err_ = capture_io do - open('|../bin/k9 run sketches/sketch_path.rb', 'r') do |io| + open('|../bin/k9 -r sketches/sketch_path.rb', 'r') do |io| while l = io.gets puts(l.chop) end end end - assert_match('/home/tux/JRubyArt/test', out, 'Failed Sketch Path Sketch') + assert_match('/home/sid/JRubyArt/test', out, 'Failed Sketch Path Sketch') end def test_on_top out, _err_ = capture_io do - open('|../bin/k9 run sketches/on_top.rb', 'r') do |io| + open('|../bin/k9 -r sketches/on_top.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -41,7 +41,7 @@ def test_on_top def test_p2d out, _err_ = capture_io do - open('|../bin/k9 run sketches/p2d.rb', 'r') do |io| + open('|../bin/k9 -r sketches/p2d.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -60,7 +60,7 @@ def test_proc_root def test_fx2d out, _err = capture_io do - open('|../bin/k9 run sketches/fx2d.rb', 'r') do |io| + open('|../bin/k9 -r sketches/fx2d.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -70,7 +70,7 @@ def test_fx2d def test_p3d out, _err_ = capture_io do - open('|../bin/k9 run sketches/p3d.rb', 'r') do |io| + open('|../bin/k9 -r sketches/p3d.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -81,7 +81,7 @@ def test_p3d def test_graphics out, _err_ = capture_io do - open('|../bin/k9 run sketches/graphics.rb', 'r') do |io| + open('|../bin/k9 -r sketches/graphics.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -92,7 +92,7 @@ def test_graphics def test_setup_exception out, _err_ = capture_io do - open('|../bin/k9 run sketches/setup_ex.rb', 'r') do |io| + open('|../bin/k9 -r sketches/setup_ex.rb', 'r') do |io| while l = io.gets puts(l.chop) end @@ -101,4 +101,3 @@ def test_setup_exception assert out.index("undefined method `unknown_method'"), 'Failed to raise exception?' end end - From 71e4d8a11d70191ba0b85cbe2a8a664bb69bfd3a Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 10 Jul 2016 06:59:44 +0100 Subject: [PATCH 03/19] update --run tests --- test/#k9_run_test.rb# | 104 ------------------------------------------ 1 file changed, 104 deletions(-) delete mode 100644 test/#k9_run_test.rb# diff --git a/test/#k9_run_test.rb# b/test/#k9_run_test.rb# deleted file mode 100644 index 70a9d8ca..00000000 --- a/test/#k9_run_test.rb# +++ /dev/null @@ -1,104 +0,0 @@ -gem 'minitest' # don't use bundled minitest -require 'minitest/autorun' -require 'minitest/pride' - -Dir.chdir(File.dirname(__FILE__)) - -class Rp5Test < Minitest::Test - - def test_normal - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/basic.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert_match(/ok/, out, 'Failed Basic Sketch') - end - - def test_sketch_path - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/sketch_path.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert_match('/home/sid/JRubyArt/test', out, 'Failed Sketch Path Sketch') - end - - def test_on_top - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/on_top.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert_match(/ok/, out, 'Failed On Top Sketch') - end - - def test_p2d - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/p2d.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert_match(/ok/, out, 'Failed P2D sketch') - end - - def test_proc_root - require 'psych' - path = File.expand_path('~/.jruby_art/config.yml') - config = FileTest.exist?(path)? Psych.load_file(path) : {} - root = config.empty? ? '' : config['PROCESSING_ROOT'] - assert root =~ /processing/, 'You need to set your PROCESSING_ROOT in .rp5rc' - end - - def test_fx2d - out, _err = capture_io do - open('|../bin/k9 -r sketches/fx2d.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - end - - def test_p3d - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/p3d.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert_match(/ok/, out, 'Failed P3D sketch') - end - - def test_graphics - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/graphics.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert out[0].to_i >= 3, "Graphics capability #{out} may be sub-optimal" - end - - def test_setup_exception - out, _err_ = capture_io do - open('|../bin/k9 -r sketches/setup_ex.rb', 'r') do |io| - while l = io.gets - puts(l.chop) - end - end - end - assert out.index("undefined method `unknown_method'"), 'Failed to raise exception?' - end -end -rake \ No newline at end of file From 0b6c0b6509db5cf8e07d751c1732b671dceef74d Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 10 Jul 2016 18:35:21 +0100 Subject: [PATCH 04/19] fix travis? --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 52623004..7a1c5a93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,7 +6,7 @@ addons: -oracle-java8-installer rvm: - - jruby-head + - jruby-9.1.2.0 jdk: - oraclejdk8 os: From fedea13db193969e3f78747a145abfdc96daab96 Mon Sep 17 00:00:00 2001 From: monkstone Date: Mon, 11 Jul 2016 15:44:17 +0100 Subject: [PATCH 05/19] refactor sketch creator to sketch_writer --- lib/jruby_art/creators/creator.rb | 277 ------------------------ lib/jruby_art/creators/sketch_writer.rb | 117 ++++++++++ lib/jruby_art/installer.rb | 4 + lib/jruby_art/runner.rb | 25 +-- 4 files changed, 129 insertions(+), 294 deletions(-) delete mode 100644 lib/jruby_art/creators/creator.rb create mode 100644 lib/jruby_art/creators/sketch_writer.rb diff --git a/lib/jruby_art/creators/creator.rb b/lib/jruby_art/creators/creator.rb deleted file mode 100644 index 0f08aeab..00000000 --- a/lib/jruby_art/creators/creator.rb +++ /dev/null @@ -1,277 +0,0 @@ -# frozen_string_literal: false - -BASIC = <<-CODE -def setup - sketch_title '%s' -end - -def draw - -end - -def settings - size %s, %s - # pixel_density(2) # here for hi-dpi displays only - # smooth # here -end - -CODE - -BASIC_MODE = <<-CODE -def setup - sketch_title '%s' -end - -def draw - -end - -def settings - size %s, %s, %s - # smooth # here -end - -CODE - -CLASS_BASIC = <<-CODE -# encoding: utf-8 -# frozen_string_literal: false -class %s < Processing::App - def setup - sketch_title '%s' - end - - def draw - - end - - def settings - size %s, %s - # pixel_density(2) # here for hi-dpi displays only - # smooth # here - end -end -CODE - -EMACS_BASIC = <<-CODE -# encoding: utf-8 -# frozen_string_literal: false -require 'jruby_art' -require 'jruby_art/app' - -Processing::App::SKETCH_PATH = __FILE__.freeze - -class %s < Processing::App - def setup - sketch_title '%s' - end - - def draw - - end - - def settings - size %s, %s - # smooth # here - end -end - -%s.new unless defined? $app -CODE - -CLASS_MODE = <<-CODE -# encoding: utf-8 -# frozen_string_literal: false -class %s < Processing::App - def setup - sketch_title '%s' - end - - def draw - - end - - def settings - size %s, %s, %s - # smooth # here - end -end -CODE - -EMACS_MODE = <<-CODE -# encoding: utf-8 -# frozen_string_literal: false -require 'jruby_art' -require 'jruby_art/app' - -Processing::App::SKETCH_PATH = __FILE__.freeze - -class %s < Processing::App - def setup - sketch_title '%s' - end - - def draw - - end - - def settings - size %s, %s, %s - # smooth # here - end -end - -%s.new unless defined? $app -CODE - -INNER = <<-CODE -# encoding: utf-8 -# frozen_string_literal: false -class %s - include Processing::Proxy - -end -CODE - -# creator wrapper module using StringExtra -module Creator - require_relative '../helpers/string_extra' - # Write file to disk - class SketchWriter - attr_reader :file - - def initialize(path) - underscore = StringExtra.new(path).underscore - @file = "#{File.dirname(path)}/#{underscore}.rb" - end - - def save(template) - File.open(file, 'w+') do |f| - f.write(template) - end - end - end - - # An abstract class providing common methods for real creators - class Base - ALL_DIGITS = /\A\d+\Z/ - def already_exist(path) - underscore = StringExtra.new(path).underscore - new_file = "#{File.dirname(path)}/#{underscore}.rb" - return if !FileTest.exist?(path) && !FileTest.exist?(new_file) - puts 'That file already exists!' - exit - end - - # Show the help/usage message for create. - def usage - puts <<-USAGE - - Usage: k9 create - mode can be P2D / P3D. - Use --wrap for a sketch wrapped as a class - Use --inner to generated a ruby version of 'java' Inner class - Examples: k9 create app 800 600 - k9 create app 800 600 p3d --wrap - k9 create inner_class --inner - - USAGE - end - end - - # This class creates bare sketches, with an optional render mode - class BasicSketch < Base - # Create a blank sketch, given a path. - def basic_template - format(BASIC, @title, @width, @height) - end - - def basic_template_mode - format(BASIC_MODE, @title, @width, @height, @mode) - end - - def create!(path, args) - return usage if /\?/ =~ path || /--help/ =~ path - # Check to make sure that the main file doesn't exist already - already_exist(path) - main_file = File.basename(path, '.rb') # allow uneeded extension input - writer = SketchWriter.new(main_file) - @title = StringExtra.new(main_file).titleize - @width = args[0] - @height = args[1] - return writer.save(basic_template) if args[2].nil? - @mode = args[2].upcase - writer.save(basic_template_mode) - end - end - - # This class creates class wrapped sketches, with an optional render mode - class ClassSketch < Base - def class_template - format(CLASS_BASIC, @name, @title, @width, @height) - end - - def class_template_mode - format(CLASS_MODE, @name, @title, @width, @height, @mode) - end - - # Create a class wrapped sketch, given a path. - def create!(path, args) - return usage if /\?/ =~ path || /--help/ =~ path - main_file = File.basename(path, '.rb') # allow uneeded extension input - # Check to make sure that the main file doesn't exist already - already_exist(path) - @name = StringExtra.new(main_file).camelize - writer = SketchWriter.new(main_file) - @title = StringExtra.new(main_file).titleize - @width, @height = args[0], args[1] - @mode = args[2].upcase unless args[2].nil? - template = @mode.nil? ? class_template : class_template_mode - writer.save(template) - end - end - - # This class creates class wrapped sketches, with an optional render mode - class EmacsSketch < Base - def emacs_template - format(EMACS_BASIC, @name, @title, @width, @height, @name) - end - - def emacs_template_mode - format(EMACS_MODE, @name, @title, @width, @height, @mode, @name) - end - # Create a class wrapped sketch, given a path. - def create!(path, args) - return usage if /\?/ =~ path || /--help/ =~ path - main_file = File.basename(path, '.rb') # allow uneeded extension input - # Check to make sure that the main file doesn't exist already - already_exist(path) - @name = StringExtra.new(main_file).camelize - writer = SketchWriter.new(main_file) - @title = StringExtra.new(main_file).titleize - @width, @height = args[0], args[1] - @mode = args[2].upcase unless args[2].nil? - template = @mode.nil? ? emacs_template : emacs_template_mode - writer.save(template) - end - end - - # This class creates a pseudo 'java inner class' of the sketch - class InnerSketch < Base - def inner_class_template - format(INNER, @name) - end - - # Create a pseudo inner class, given a path. - def create!(path, _args_) - return usage if /\?/ =~ path || /--help/ =~ path - main_file = File.basename(path, '.rb') # allow uneeded extension input - # Check to make sure that the main file doesn't exist already - already_exist(path) - @name = main_file.camelize - writer = SketchWriter.new(main_file) - template = inner_class_template - writer.save(template) - end - end -end diff --git a/lib/jruby_art/creators/sketch_writer.rb b/lib/jruby_art/creators/sketch_writer.rb new file mode 100644 index 00000000..2d2f88cb --- /dev/null +++ b/lib/jruby_art/creators/sketch_writer.rb @@ -0,0 +1,117 @@ +EMACS = <<-CODE +# frozen_string_literal: false +require 'jruby_art' +require 'jruby_art/app' + +Processing::App::SKETCH_PATH = __FILE__.freeze + +class %s < Processing::App + def settings + %s + end + + def setup + %s + end + + def draw + end +end + +%s.new unless defined? $app + +CODE + +CLASS = <<-CODE +class %s < Processing::App + def settings + %s + end + + def setup + %s + end + + def draw + end +end + +CODE + + +METHOD = <<-CODE +def %s + %s +end + +CODE + +require_relative '../helpers/string_extra' + +class SketchWriter + attr_reader :file, :args, :sketch, :name + + def initialize(path, args) + @args = args + @name = path + underscore = StringExtra.new(path).underscore + @file = format('%s/%s.rb', File.dirname(path), underscore) + end + + def create!(type) + case type + when /bare/ + @sketch = Sketch.new.bare(name, args) + when /class/ + @sketch = Sketch.new.class_wrapped(name, args) + when /emacs/ + @sketch = Sketch.new.emacs(name, args) + end + save(sketch) + end + + def save(sketch) + File.open(file, 'w+') do |f| + f.write(sketch) + end + end +end + +class Sketch + def bare(name = 'sketch', args = []) + [settings(args), setup(name), draw].join + end + + def class_wrapped(name = 'sketch', args = []) + class_name = StringExtra.new(name).camelize + format(CLASS, class_name, size(args[0], args[1], args[2]), name(name)) + end + + def emacs(name = 'sketch', args = []) + class_name = StringExtra.new(name).camelize + format(EMACS, class_name, size(args[0], args[1], args[2]), name(name), class_name) + end + + def size(width = 200, height = 200, mode = nil) + return format('size %s, %s', width, height) if mode.nil? + format('size %s, %s, %s', width, height, mode.upcase) + end + + def settings(args = []) + return format(METHOD, 'settings', size) if args.empty? + return format(METHOD, 'settings', size(args[0], args[1])) if args.length == 2 + format(METHOD, 'settings', size(args[0], args[1], args[2])) + end + + def name(title = 'Sketch') + format("sketch_title '%s'", StringExtra.new(title).humanize) + end + + def setup(title) + return format(METHOD, 'setup', name(title)) + end + + def draw + format(METHOD, 'draw', '') + end +end diff --git a/lib/jruby_art/installer.rb b/lib/jruby_art/installer.rb index 8c0a7535..4e61b89e 100644 --- a/lib/jruby_art/installer.rb +++ b/lib/jruby_art/installer.rb @@ -64,12 +64,16 @@ def install proot = ' PROCESSING_ROOT = Not Set!!!' unless root_exist? proot ||= " PROCESSING_ROOT = #{config['PROCESSING_ROOT']}" sketchbook = " sketchbook_path = #{config['sketchbook_path']}" + template = " template = #{config['template']}" + java_args = " java_args = #{config['java_args']}" max_watch = " MAX_WATCH = #{config['MAX_WATCH']}" jruby = config['JRUBY'] puts proot puts " JRUBY = #{jruby}" unless jruby.nil? puts " jruby-complete installed = #{installed}" puts sketchbook + puts template + puts java_args puts max_watch end end diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index b919da6a..1485bfa8 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -5,7 +5,7 @@ require 'rbconfig' require_relative '../jruby_art/config' require_relative '../jruby_art/version' -require_relative '../jruby_art/installer' + require_relative '../jruby_art/java_opts' # processing wrapper module @@ -65,7 +65,7 @@ def parse_options(args) end options[:check] = false - opts.on('-i', '--install', 'Prints configuration') do + opts.on('-?', '--check', 'Prints configuration') do options[:check] = true end @@ -95,21 +95,10 @@ def parse_options(args) @filename = argc.shift end - # Create a fresh JRubyArt filename, with the necessary - # boilerplate filled out. - def create - require_relative '../jruby_art/creators/creator' - Creator::BasicSketch.new.create!(filename, argc) - end - - def create_emacs - require_relative '../jruby_art/creators/creator' - Creator::BasicSketch.new.create!(filename, argc) - end - - def create_wrap - require_relative '../jruby_art/creators/creator' - Creator::BasicSketch.new.create!(filename, argc) + def create + require_relative '../jruby_art/creators/sketch_writer' + config = Processing::RP_CONFIG.fetch('template', 'basic') + SketchWriter.new(filename, argc).create!(config) end # Just simply run a JRubyArt filename. @@ -132,11 +121,13 @@ def watch_sketch end def install + require_relative '../jruby_art/installer' JRubyComplete.new(K9_ROOT, host_os).install UnpackSamples.new(K9_ROOT, host_os).install end def check + require_relative '../jruby_art/installer' Check.new(K9_ROOT, host_os).install end From 5b7ceae5667081576ebcefdde224da54f4eb683c Mon Sep 17 00:00:00 2001 From: monkstone Date: Mon, 11 Jul 2016 15:56:17 +0100 Subject: [PATCH 06/19] rubocop --- lib/jruby_art/creators/sketch_writer.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/jruby_art/creators/sketch_writer.rb b/lib/jruby_art/creators/sketch_writer.rb index 2d2f88cb..1d83f89d 100644 --- a/lib/jruby_art/creators/sketch_writer.rb +++ b/lib/jruby_art/creators/sketch_writer.rb @@ -38,7 +38,6 @@ def draw CODE - METHOD = <<-CODE def %s %s @@ -48,6 +47,7 @@ def %s require_relative '../helpers/string_extra' +# Sketch writer class class SketchWriter attr_reader :file, :args, :sketch, :name @@ -57,7 +57,7 @@ def initialize(path, args) underscore = StringExtra.new(path).underscore @file = format('%s/%s.rb', File.dirname(path), underscore) end - + def create!(type) case type when /bare/ @@ -69,7 +69,7 @@ def create!(type) end save(sketch) end - + def save(sketch) File.open(file, 'w+') do |f| f.write(sketch) @@ -77,6 +77,7 @@ def save(sketch) end end +# Sketch class class Sketch def bare(name = 'sketch', args = []) [settings(args), setup(name), draw].join @@ -89,7 +90,13 @@ def class_wrapped(name = 'sketch', args = []) def emacs(name = 'sketch', args = []) class_name = StringExtra.new(name).camelize - format(EMACS, class_name, size(args[0], args[1], args[2]), name(name), class_name) + format( + EMACS, + class_name, + size(args[0], args[1], args[2]), + name(name), + class_name + ) end def size(width = 200, height = 200, mode = nil) @@ -99,7 +106,11 @@ def size(width = 200, height = 200, mode = nil) def settings(args = []) return format(METHOD, 'settings', size) if args.empty? - return format(METHOD, 'settings', size(args[0], args[1])) if args.length == 2 + return format( + METHOD, + 'settings', + size(args[0], args[1]) + ) if args.length == 2 format(METHOD, 'settings', size(args[0], args[1], args[2])) end @@ -108,7 +119,7 @@ def name(title = 'Sketch') end def setup(title) - return format(METHOD, 'setup', name(title)) + format(METHOD, 'setup', name(title)) end def draw From fc2c1bfba4f069285dd6c79260794e462583d1b3 Mon Sep 17 00:00:00 2001 From: monkstone Date: Mon, 11 Jul 2016 19:25:13 +0100 Subject: [PATCH 07/19] enable check --- lib/jruby_art/runner.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 1485bfa8..8b417159 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -43,6 +43,7 @@ def execute! run_sketch if options[:run] watch_sketch if options[:watch] create if options[:create] + check if options[:check] install if options[:install] end From abbe6de6132d7a97100e4e4b6f75e438050ad2b5 Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 06:25:28 +0100 Subject: [PATCH 08/19] enable live, add back run with JRubyComplete --- lib/jruby_art/runner.rb | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 8b417159..ffe2c313 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -42,6 +42,7 @@ def execute! show_version if options[:version] run_sketch if options[:run] watch_sketch if options[:watch] + live if options[:live] create if options[:create] check if options[:check] install if options[:install] @@ -144,18 +145,30 @@ def show_version private # Trade in this Ruby instance for a JRuby instance, loading in a starter - # script and passing it some arguments. Unless '--nojruby' is passed, the - # installed version of jruby is used instead of our vendored one. To use - # jruby-complete by default set JRUBY: false in ~/.jruby_art/config.yml - # (however that might make using other gems in your sketches hard....) + # script and passing it some arguments. Unless you set JRUBY: false in + # ~/.jruby_art/config.yml, an installed version of jruby is used instead + # of our vendored one. Note the use of jruby-complete might make using + # other gems in your sketches hard (but not impossible).... def spin_up(starter_script, filename, argc) - runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" - opts = JRubyOpts.new(SKETCH_ROOT).opts - command = ['jruby', - opts, - runner, - filename, - argc].flatten + runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" + if Processing::RP_CONFIG.fetch('JRUBY', 'true') == 'false' + opts = JavaOpts.new(SKETCH_ROOT).opts + command = ['java', + opts, + '-cp', + jruby_complete, + 'org.jruby.Main', + runner, + sketch, + args].flatten + else + opts = JRubyOpts.new(SKETCH_ROOT).opts + command = ['jruby', + opts, + runner, + filename, + argc].flatten + end begin exec(*command) # exec replaces the Ruby process with the JRuby one. From ad3a72d88abaa2440d0d50455f556bfd0da73300 Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 06:40:03 +0100 Subject: [PATCH 09/19] no really enable live --- lib/jruby_art/runner.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index ffe2c313..04af836c 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -77,10 +77,15 @@ def parse_options(args) end options[:run] = false - opts.on('-r', '--run', 'Run the filename using jruby-complete') do + opts.on('-r', '--run', 'Run the sketch') do options[:run] = true end + options[:live] = false + opts.on('-l', '--live', 'As above, with pry console bound to $app') do + options[:live] = true + end + options[:create] = false opts.on('-c', '--create', 'Create new bare filename outline') do options[:create] = true From 8203e21bc4b9710abf639f6a5f27242ccea29f45 Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 10:54:15 +0100 Subject: [PATCH 10/19] update post install message --- jruby_art.gemspec | 2 +- lib/jruby_art/installer.rb | 11 ++++------- lib/jruby_art/runner.rb | 6 +++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/jruby_art.gemspec b/jruby_art.gemspec index 0733a91d..4f3d078d 100644 --- a/jruby_art.gemspec +++ b/jruby_art.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| EOS spec.summary = %q{Code as Art, Art as Code. Processing and Ruby are meant for each other.} spec.homepage = "https://ruby-processing.github.io/" - spec.post_install_message = %q{Use 'k9 setup install' to install jruby-complete, and 'k9 setup check' to check config.} + spec.post_install_message = %q{Use 'k9 --install' to install jruby-complete, and 'k9 --check' to check config.} spec.license = 'MIT' spec.files = FileList['bin/**/*', 'lib/**/*', 'library/**/*', 'samples/**/*', 'vendors/Rakefile'].exclude(/jar/).to_a diff --git a/lib/jruby_art/installer.rb b/lib/jruby_art/installer.rb index 4e61b89e..d23fa769 100644 --- a/lib/jruby_art/installer.rb +++ b/lib/jruby_art/installer.rb @@ -24,12 +24,9 @@ def set_processing_root proot = "#{home}/processing-#{VERSION}" proot = "/Java/Processing-#{VERSION}" if os == :windows proot = "/Applications/Processing.app/Contents/Java" if os == :mac - data = { - 'PROCESSING_ROOT' => proot, - 'JRUBY' => true.to_s, - 'sketchbook_path' => sketch, - 'MAX_WATCH' => '20' - } + settings = %i(PROCESSING_ROOT JRUBY sketchbook_path template MAX_WATCH) + values = [proot, true, sketch, 'bare', 32] + data = settings.zip(values).to_h open(path, 'w:UTF-8') { |file| file.write(data.to_yaml) } end @@ -67,7 +64,7 @@ def install template = " template = #{config['template']}" java_args = " java_args = #{config['java_args']}" max_watch = " MAX_WATCH = #{config['MAX_WATCH']}" - jruby = config['JRUBY'] + jruby = config.fetch('JRUBY', true) puts proot puts " JRUBY = #{jruby}" unless jruby.nil? puts " jruby-complete installed = #{installed}" diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 04af836c..fc9695e5 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -87,7 +87,7 @@ def parse_options(args) end options[:create] = false - opts.on('-c', '--create', 'Create new bare filename outline') do + opts.on('-c', '--create', 'Create new outline sketch') do options[:create] = true end @@ -104,7 +104,7 @@ def parse_options(args) def create require_relative '../jruby_art/creators/sketch_writer' - config = Processing::RP_CONFIG.fetch('template', 'basic') + config = Processing::RP_CONFIG.fetch('template', 'bare') SketchWriter.new(filename, argc).create!(config) end @@ -156,7 +156,7 @@ def show_version # other gems in your sketches hard (but not impossible).... def spin_up(starter_script, filename, argc) runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" - if Processing::RP_CONFIG.fetch('JRUBY', 'true') == 'false' + unless Processing::RP_CONFIG.fetch('JRUBY', true) opts = JavaOpts.new(SKETCH_ROOT).opts command = ['java', opts, From 35610822b66032dcf0cfe5a85369e38396245c98 Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 11:13:12 +0100 Subject: [PATCH 11/19] don't use symbols in yaml --- lib/jruby_art/installer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jruby_art/installer.rb b/lib/jruby_art/installer.rb index d23fa769..28fe67ad 100644 --- a/lib/jruby_art/installer.rb +++ b/lib/jruby_art/installer.rb @@ -24,7 +24,7 @@ def set_processing_root proot = "#{home}/processing-#{VERSION}" proot = "/Java/Processing-#{VERSION}" if os == :windows proot = "/Applications/Processing.app/Contents/Java" if os == :mac - settings = %i(PROCESSING_ROOT JRUBY sketchbook_path template MAX_WATCH) + settings = %w(PROCESSING_ROOT JRUBY sketchbook_path template MAX_WATCH) values = [proot, true, sketch, 'bare', 32] data = settings.zip(values).to_h open(path, 'w:UTF-8') { |file| file.write(data.to_yaml) } From 16a10cbeead8d1fadf00111a8b3ab2a8247dcada Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 11:35:25 +0100 Subject: [PATCH 12/19] update message in gemspec --- jruby_art.gemspec | 4 ++-- lib/jruby_art/runner.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/jruby_art.gemspec b/jruby_art.gemspec index 4f3d078d..537d88df 100644 --- a/jruby_art.gemspec +++ b/jruby_art.gemspec @@ -11,8 +11,8 @@ Gem::Specification.new do |spec| spec.email = 'mamba2928@yahoo.co.uk' spec.description = <<-EOS JRubyArt is a ruby wrapper for the processing art framework. - This version supports processing-3.1.1, and uses jruby-9.1.2.0 as the glue - between ruby and java. You can use both processing libraries and ruby gems + This version uses 'optparse', run becomes --run (or -r) and 'setup install' + becomes '--install' or (-i) etc. Use both processing libraries and ruby gems in your sketches. Features create/run/watch/live modes. The "--watch" mode, provides a nice REPL-ish way to work on your processing sketches. Includes:- A "Control Panel" library, so that you can easily create sliders, buttons, diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index fc9695e5..65ccd993 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -164,8 +164,8 @@ def spin_up(starter_script, filename, argc) jruby_complete, 'org.jruby.Main', runner, - sketch, - args].flatten + filename, + argc].flatten else opts = JRubyOpts.new(SKETCH_ROOT).opts command = ['jruby', From fc2b6226e70329f55e4d14cd979b92c95c5de1bf Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 13:40:34 +0100 Subject: [PATCH 13/19] Export to be implemented --- lib/jruby_art/runner.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 65ccd993..b2d31041 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -71,6 +71,11 @@ def parse_options(args) options[:check] = true end + options[:app] = false + opts.on('-a', '--app', 'Export as app NOT IMPLEMENTED YET') do + options[:export] = true + end + options[:watch] = false opts.on('-w', '--watch', 'Watch/run the sketch') do options[:watch] = true @@ -108,6 +113,12 @@ def create SketchWriter.new(filename, argc).create!(config) end + # Export as app not implemented + def export + ensure_exists(filename) + puts 'Not implemented yet' + end + # Just simply run a JRubyArt filename. def run_sketch ensure_exists(filename) From df00799b2b9028cba2dd9e03dce8429d5edab862 Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 14:01:10 +0100 Subject: [PATCH 14/19] tidy up --- CHANGELOG.md | 2 + lib/jruby_art/installer.rb | 14 ++--- lib/jruby_art/runner.rb | 117 ++++++++++++++++++------------------- 3 files changed, 66 insertions(+), 67 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60eb729a..6a56e9a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,6 @@ +**v1.2.0.pre** Use optparse to parse command line options, and related re-factoring. + **v1.1.3** Revert using String refinements in `creator.rb`. Refactor java options to `java_opts.rb`. **v1.1.2** Refactor `runner.rb` to `runner.rb`, `args.rb` and `installer.rb`. The `Installer` classes have the role of installing `jruby-complete`, the examples and providing `setup check` functionality. Refactored and improved default `config.yml` tool, all should make it easier for `collaborators/successors` to follow the code. Refactored `Vec2D` and `Vec3D` `==` and `eql?` methods. New `chooser` library makes it possible to use `select_input` reflection method. diff --git a/lib/jruby_art/installer.rb b/lib/jruby_art/installer.rb index 28fe67ad..d81e3835 100644 --- a/lib/jruby_art/installer.rb +++ b/lib/jruby_art/installer.rb @@ -1,7 +1,7 @@ # frozen_string_literal: false require 'yaml' -VERSION = '3.1.1' # processing version +VERSION = '3.1.1'.freeze # processing version # Abstract Installer class class Installer @@ -14,7 +14,7 @@ def initialize(root, os) @sketch = "#{home}/My Documents/Processing" if os == :windows @sketch = "#{home}/Documents/Processing" if os == :mac end - + # Optimistically set processing root def set_processing_root require 'psych' @@ -29,23 +29,23 @@ def set_processing_root data = settings.zip(values).to_h open(path, 'w:UTF-8') { |file| file.write(data.to_yaml) } end - + def root_exist? return false if config.nil? File.exist? config['PROCESSING_ROOT'] end - + def config k9config = File.expand_path("#{home}/.jruby_art/config.yml") return nil unless File.exist? k9config YAML.load_file(k9config) end - + # in place of default installer class def install puts 'Usage: k9 setup [check | install | unpack_samples]' end - + # Display the current version of JRubyArt. def show_version puts format('JRubyArt version %s', JRubyArt::VERSION) @@ -53,7 +53,7 @@ def show_version end # Configuration checker -class Check < Installer +class Check < Installer def install show_version return super unless config diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index b2d31041..488f5fad 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -5,7 +5,6 @@ require 'rbconfig' require_relative '../jruby_art/config' require_relative '../jruby_art/version' - require_relative '../jruby_art/java_opts' # processing wrapper module @@ -13,29 +12,28 @@ module Processing # Utility class to handle the different commands that the 'k9' command # offers. Able to run, watch, live, create, app, and unpack class Runner - WIN_PATTERNS = [ - /bccwin/i, - /cygwin/i, - /djgpp/i, - /ming/i, - /mswin/i, - /wince/i - ] - + /bccwin/i, + /cygwin/i, + /djgpp/i, + /ming/i, + /mswin/i, + /wince/i + ].freeze + attr_reader :options, :argc, :filename, :os - + def initialize @options = {} end - + # Start running a jruby_art filename from the passed-in arguments def self.execute runner = new runner.parse_options(ARGV) runner.execute! end - + # Dispatch central. def execute! show_help if options.empty? @@ -47,9 +45,8 @@ def execute! check if options[:check] install if options[:install] end - - - # Parse the command-line options. Keep it simple. + + # Parse the command-line options. def parse_options(args) opt_parser = OptionParser.new do |opts| # Set a banner, displayed at the top @@ -60,42 +57,42 @@ def parse_options(args) opts.on('-v', '--version', 'JRubyArt Version') do options[:version] = true end - + options[:install] = false opts.on('-i', '--install', 'Installs jruby-complete and examples') do options[:install] = true end - + options[:check] = false opts.on('-?', '--check', 'Prints configuration') do options[:check] = true end - + options[:app] = false opts.on('-a', '--app', 'Export as app NOT IMPLEMENTED YET') do options[:export] = true - end - + end + options[:watch] = false opts.on('-w', '--watch', 'Watch/run the sketch') do options[:watch] = true end - + options[:run] = false opts.on('-r', '--run', 'Run the sketch') do options[:run] = true end - + options[:live] = false opts.on('-l', '--live', 'As above, with pry console bound to $app') do options[:live] = true end - + options[:create] = false opts.on('-c', '--create', 'Create new outline sketch') do options[:create] = true - end - + end + # This displays the help screen, all programs are # assumed to have this option. opts.on('-h', '--help', 'Display this screen') do @@ -106,68 +103,75 @@ def parse_options(args) @argc = opt_parser.parse(args) @filename = argc.shift end - - def create + + def create require_relative '../jruby_art/creators/sketch_writer' config = Processing::RP_CONFIG.fetch('template', 'bare') SketchWriter.new(filename, argc).create!(config) end - + # Export as app not implemented def export - ensure_exists(filename) + ensure_exists(filename) puts 'Not implemented yet' end - + # Just simply run a JRubyArt filename. def run_sketch - ensure_exists(filename) + ensure_exists(filename) spin_up('run.rb', filename, argc) end - + # Just simply run a JRubyArt filename. def live - ensure_exists(filename) + ensure_exists(filename) spin_up('live.rb', filename, argc) end - + # Run a filename, keeping an eye on it's file, and reloading # whenever it changes. def watch_sketch - ensure_exists(filename) + ensure_exists(filename) spin_up('watch.rb', filename, argc) end - + def install require_relative '../jruby_art/installer' JRubyComplete.new(K9_ROOT, host_os).install UnpackSamples.new(K9_ROOT, host_os).install end - + def check require_relative '../jruby_art/installer' Check.new(K9_ROOT, host_os).install end - + # Show the standard help/usage message. def show_help puts HELP_MESSAGE end - + def show_version puts format('JRubyArt version %s', JRubyArt::VERSION) end - + private - + # Trade in this Ruby instance for a JRuby instance, loading in a starter - # script and passing it some arguments. Unless you set JRUBY: false in - # ~/.jruby_art/config.yml, an installed version of jruby is used instead - # of our vendored one. Note the use of jruby-complete might make using + # script and passing it some arguments. Unless you set JRUBY: false in + # ~/.jruby_art/config.yml, an installed version of jruby is used instead + # of our vendored one. Note the use of jruby-complete might make using # other gems in your sketches hard (but not impossible).... def spin_up(starter_script, filename, argc) - runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" - unless Processing::RP_CONFIG.fetch('JRUBY', true) + runner = "#{K9_ROOT}/lib/jruby_art/runners/#{starter_script}" + if Processing::RP_CONFIG.fetch('JRUBY', true) + opts = JRubyOpts.new(SKETCH_ROOT).opts + command = ['jruby', + opts, + runner, + filename, + argc].flatten + else opts = JavaOpts.new(SKETCH_ROOT).opts command = ['java', opts, @@ -177,13 +181,6 @@ def spin_up(starter_script, filename, argc) runner, filename, argc].flatten - else - opts = JRubyOpts.new(SKETCH_ROOT).opts - command = ['jruby', - opts, - runner, - filename, - argc].flatten end begin exec(*command) @@ -191,28 +188,28 @@ def spin_up(starter_script, filename, argc) rescue Java::JavaLang::ClassNotFoundException end end - + # NB: We really do mean to use 'and' not '&&' for flow control purposes - + def ensure_exists(filename) puts "Couldn't find: #{filename}" and exit unless FileTest.exist?(filename) end - + def jruby_complete rcomplete = File.join(K9_ROOT, 'lib/ruby/jruby-complete.jar') return [rcomplete] if FileTest.exist?(rcomplete) warn "#{rcomplete} does not exist\nTry running `k9 setup install`" exit end - + def libraries %w(video sound).map { |library| sketchbook_library(library) }.flatten end - + def sketchbook_library(name) Dir["#{Processing::RP_CONFIG['sketchbook_path']}/libraries/#{name}/library/\*.jar"] end - + def host_os detect_os = RbConfig::CONFIG['host_os'] case detect_os From a94fd7ddcc996d060bcbb4cfd140337de78dedcb Mon Sep 17 00:00:00 2001 From: monkstone Date: Tue, 12 Jul 2016 15:54:36 +0100 Subject: [PATCH 15/19] update examples --- vendors/Rakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vendors/Rakefile b/vendors/Rakefile index e988aaa0..7b7d4594 100644 --- a/vendors/Rakefile +++ b/vendors/Rakefile @@ -9,7 +9,7 @@ WARNING = <<-EOS EOS JRUBYC_VERSION = '9.1.2.0' -EXAMPLES = '1.2' +EXAMPLES = '1.3' HOME_DIR = ENV['HOME'] MAC_OR_LINUX = /linux|mac|darwin/ =~ RbConfig::CONFIG['host_os'] From ccbc83a69428555a99085a291fe3ca1bbf623fe6 Mon Sep 17 00:00:00 2001 From: monkstone Date: Thu, 14 Jul 2016 11:43:36 +0100 Subject: [PATCH 16/19] link to gh-pages! --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a94e10c4..86699d66 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # JRubyArt [![Gem Version](https://badge.fury.io/rb/jruby_art.svg)](http://badge.fury.io/rb/jruby_art) ![Travis CI](https://travis-ci.org/ruby-processing/JRubyArt.svg) - +See [alternative website](http://ruby-processing.github.io/JRubyArt/) ## Requirements A clean start for `jruby_art` that works best with the latest version of [processing-3.1.1](https://github.com/processing/processing/releases) and [jruby-9.1.2.0](http://jruby.org/download) see [wiki](https://github.com/ruby-processing/JRubyArt/wiki/Building-latest-gem) for building gem from this repo. Changes from processing- 2.0 to [processing-3.0 here](https://github.com/processing/processing/wiki/Changes-in-3.0). Should work on same platforms as vanilla processing (windows, mac, linux) for Android see Yuki Morohoshi [rubuto-processing3][]. ## Requirements From 7d2aa2099909ef68927cfb1713cbe6ed376e1841 Mon Sep 17 00:00:00 2001 From: monkstone Date: Sat, 16 Jul 2016 09:27:25 +0100 Subject: [PATCH 17/19] avoid using toJava(Double) --- src/monkstone/vecmath/vec2/Vec2.java | 117 +++++++++++++++------------ src/monkstone/vecmath/vec3/Vec3.java | 82 ++++++++++++------- 2 files changed, 120 insertions(+), 79 deletions(-) diff --git a/src/monkstone/vecmath/vec2/Vec2.java b/src/monkstone/vecmath/vec2/Vec2.java index d0ca7eea..ff1c6f17 100644 --- a/src/monkstone/vecmath/vec2/Vec2.java +++ b/src/monkstone/vecmath/vec2/Vec2.java @@ -19,10 +19,11 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyClass; +import org.jruby.RubyFixnum; +import org.jruby.RubyFloat; import org.jruby.RubyObject; import org.jruby.RubySymbol; import org.jruby.anno.JRubyClass; @@ -54,12 +55,12 @@ public static void createVec2(final Ruby runtime) { RubyClass vec2Cls = runtime.defineClass("Vec2D", runtime.getObject(), (Ruby runtime1, RubyClass rubyClass) -> new Vec2(runtime1, rubyClass)); vec2Cls.defineAnnotatedMethods(Vec2.class); } - - public double javax(){ + + public double javax() { return jx; } - - public double javay(){ + + public double javay() { return jy; } @@ -88,8 +89,10 @@ public Vec2(Ruby runtime, RubyClass klass) { void init(ThreadContext context, IRubyObject[] args) { if (Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 2) == 2) { - jx = (Double) args[0].toJava(Double.class); - jy = (Double) args[1].toJava(Double.class); + jx = (args[0] instanceof RubyFloat) + ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + jy = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); } } @@ -150,11 +153,9 @@ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject valu Ruby runtime = context.getRuntime(); if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { - jx = (Double) value.toJava(Double.class); + return runtime.newFloat(jx); } else if (key == RubySymbol.newSymbol(runtime, "y")) { - jy = (Double) value.toJava(Double.class); - } else { - throw runtime.newIndexError("invalid key"); + return runtime.newFloat(jy); } } else { throw runtime.newIndexError("invalid key"); @@ -171,7 +172,11 @@ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject valu @JRubyMethod(name = "x=") public IRubyObject setX(ThreadContext context, IRubyObject other) { - jx = (Double) other.toJava(Double.class); + if (other instanceof RubyFloat) { + jx = ((RubyFloat) other).getValue(); + } else { + jx = ((RubyFixnum) other).getDoubleValue(); + } return other; } @@ -184,7 +189,11 @@ public IRubyObject setX(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "y=") public IRubyObject setY(ThreadContext context, IRubyObject other) { - jy = (Double) other.toJava(Double.class); + if (other instanceof RubyFloat) { + jy = ((RubyFloat) other).getValue(); + } else { + jy = ((RubyFixnum) other).getDoubleValue(); + } return other; } @@ -298,7 +307,8 @@ public IRubyObject op_minus(ThreadContext context, IRubyObject other) { public IRubyObject op_mul(ThreadContext context, IRubyObject other) { Ruby runtime = context.getRuntime(); - double scalar = (Double) other.toJava(Double.class); + double scalar = (other instanceof RubyFloat) + ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{runtime.newFloat(jx * scalar), runtime.newFloat(jy * scalar)}); @@ -314,7 +324,8 @@ public IRubyObject op_mul(ThreadContext context, IRubyObject other) { public IRubyObject op_div(ThreadContext context, IRubyObject other) { Ruby runtime = context.getRuntime(); - double scalar = (Double) other.toJava(Double.class); + double scalar = (other instanceof RubyFloat) + ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); if (Math.abs(scalar) < Vec2.EPSILON) { return this; } @@ -450,24 +461,25 @@ public IRubyObject normalize(ThreadContext context) { * here!!! * * @param context ThreadContext - * @param klazz IRubyObject - * @param other input angle in radians + * @param klazz IRubyObject + * @param scalar input angle in radians * @return new Vec2 object (ruby) */ @JRubyMethod(name = "from_angle", meta = true) - public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject other) { + public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) { Ruby runtime = context.getRuntime(); - double scalar = (Double) other.toJava(Double.class); + double angle = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); return Vec2.rbNew(context, klazz, new IRubyObject[]{ - runtime.newFloat(Math.cos(scalar)), - runtime.newFloat(Math.sin(scalar))}); + runtime.newFloat(Math.cos(angle)), + runtime.newFloat(Math.sin(angle))}); } /** - * Example of a regular ruby class method + * Example of a regular ruby class method * * @param context ThreadContext - * @param klazz IRubyObject + * @param klazz IRubyObject * @return new Vec2 object (ruby) */ @JRubyMethod(name = "random", meta = true) @@ -482,12 +494,13 @@ public static IRubyObject random_direction(ThreadContext context, IRubyObject kl /** * * @param context ThreadContext - * @param other IRubyObject + * @param scalar IRubyObject * @return this Vec2 object rotated */ @JRubyMethod(name = "rotate!") - public IRubyObject rotate_bang(ThreadContext context, IRubyObject other) { - double theta = (Double) other.toJava(Double.class); + public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) { + double theta = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); double x = (jx * Math.cos(theta) - jy * Math.sin(theta)); double y = (jx * Math.sin(theta) + jy * Math.cos(theta)); jx = x; @@ -498,13 +511,14 @@ public IRubyObject rotate_bang(ThreadContext context, IRubyObject other) { /** * * @param context ThreadContext - * @param other IRubyObject + * @param scalar IRubyObject * @return a new Vec2 object rotated */ @JRubyMethod(name = "rotate") - public IRubyObject rotate(ThreadContext context, IRubyObject other) { + public IRubyObject rotate(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.getRuntime(); - double theta = (Double) other.toJava(Double.class); + double theta = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); IRubyObject[] ary = new IRubyObject[]{ runtime.newFloat(jx * Math.cos(theta) - jy * Math.sin(theta)), runtime.newFloat(jx * Math.sin(theta) + jy * Math.cos(theta))}; @@ -514,7 +528,7 @@ public IRubyObject rotate(ThreadContext context, IRubyObject other) { /** * * @param context ThreadContext - * @param args IRubyObject[] + * @param args IRubyObject[] * @return as a new Vec2 object (ruby) */ @JRubyMethod(name = "lerp", rest = true) @@ -522,27 +536,29 @@ public IRubyObject lerp(ThreadContext context, IRubyObject[] args) { Ruby runtime = context.getRuntime(); Arity.checkArgumentCount(runtime, args, 2, 2); Vec2 vec = (Vec2) args[0].toJava(Vec2.class); - double scalar = (Double) args[1].toJava(Double.class); + double scalar = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); assert (scalar >= 0 && scalar < 1.0) : - "Lerp value " + scalar + " out of range 0 .. 1.0"; + "Lerp value " + scalar + " out of range 0..1.0"; return Vec2.rbNew(context, this.getMetaClass(), new IRubyObject[]{ - runtime.newFloat((1 - scalar) * jx + vec.jx * scalar), - runtime.newFloat((1 - scalar) * jy + vec.jy * scalar)}); + runtime.newFloat(jx + (vec.jx - jx) * scalar), + runtime.newFloat(jy + (vec.jy - jy) * scalar)}); } /** * * @param context ThreadContext - * @param args IRubyObject[] + * @param args IRubyObject[] * @return this IRubyObject */ @JRubyMethod(name = "lerp!", rest = true) public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) { Arity.checkArgumentCount(context.getRuntime(), args, 2, 2); Vec2 vec = (Vec2) args[0].toJava(Vec2.class); - double scalar = (Double) args[1].toJava(Double.class); + double scalar = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); assert (scalar >= 0 && scalar < 1.0) : - "Lerp value " + scalar + " out of range 0 .. 1.0"; + "Lerp value " + scalar + " out of range 0..1.0"; jx += (vec.jx - jx) * scalar; jy += (vec.jy - jy) * scalar; return this; @@ -598,6 +614,7 @@ public IRubyObject toArray(ThreadContext context) { /** * * To vertex + * * @param context ThreadContext * @param object IRubyObject vertex renderer */ @@ -611,6 +628,7 @@ public void toVertex(ThreadContext context, IRubyObject object) { /** * * To curve vertex + * * @param context ThreadContext * @param object IRubyObject vertex renderer */ @@ -648,21 +666,21 @@ public int hashCode() { /** * * Java Equals + * * @param obj Object * @return result boolean */ @Override public boolean equals(Object obj) { - if (obj == this){ - return true; + if (obj == this) { + return true; } if (obj instanceof Vec2) { final Vec2 other = (Vec2) obj; - if ((Double.compare(jx,(Double)other.jx) == 0) - && (Double.compare(jy,(Double)other.jy) == 0)) - { + if ((Double.compare(jx, (Double) other.jx) == 0) + && (Double.compare(jy, (Double) other.jy) == 0)) { return true; - } + } } return false; } @@ -676,16 +694,15 @@ public boolean equals(Object obj) { @JRubyMethod(name = "eql?", required = 1) public IRubyObject eql_p(ThreadContext context, IRubyObject other) { Ruby runtime = context.getRuntime(); - if (other == this){ + if (other == this) { return runtime.newBoolean(true); } if (other instanceof Vec2) { Vec2 v = (Vec2) other.toJava(Vec2.class); - if ((Double.compare(jx,(Double)v.jx) == 0) - && (Double.compare(jy,(Double)v.jy) == 0)) - { + if ((Double.compare(jx, (Double) v.jx) == 0) + && (Double.compare(jy, (Double) v.jy) == 0)) { return runtime.newBoolean(true); - } + } } return runtime.newBoolean(false); } @@ -701,8 +718,8 @@ public IRubyObject eql_p(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { Ruby runtime = context.getRuntime(); - if (other == this){ - return runtime.newBoolean(true); + if (other == this) { + return runtime.newBoolean(true); } if (other instanceof Vec2) { Vec2 v = (Vec2) other.toJava(Vec2.class); diff --git a/src/monkstone/vecmath/vec3/Vec3.java b/src/monkstone/vecmath/vec3/Vec3.java index e7ab4459..21be3e11 100644 --- a/src/monkstone/vecmath/vec3/Vec3.java +++ b/src/monkstone/vecmath/vec3/Vec3.java @@ -19,10 +19,11 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ - import org.jruby.Ruby; import org.jruby.RubyArray; import org.jruby.RubyClass; +import org.jruby.RubyFixnum; +import org.jruby.RubyFloat; import org.jruby.RubyObject; import org.jruby.RubySymbol; import org.jruby.anno.JRubyClass; @@ -83,11 +84,14 @@ public Vec3(Ruby runtime, RubyClass klass) { void init(ThreadContext context, IRubyObject[] args) { int count = Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 3); if (count >= 2) { - jx = (Double) args[0].toJava(Double.class); - jy = (Double) args[1].toJava(Double.class); + jx = (args[0] instanceof RubyFloat) + ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + jy = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); } if (count == 3) { - jz = (Double) args[2].toJava(Double.class); + jz = (args[2] instanceof RubyFloat) + ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); } } @@ -132,8 +136,12 @@ public IRubyObject getZ(ThreadContext context) { @JRubyMethod(name = "x=") public IRubyObject setX(ThreadContext context, IRubyObject other) { - jx = (Double) other.toJava(Double.class); - return context.getRuntime().newFloat(jx); + if (other instanceof RubyFloat) { + jx = ((RubyFloat) other).getValue(); + } else { + jx = ((RubyFixnum) other).getDoubleValue(); + } + return other; } /** @@ -145,8 +153,12 @@ public IRubyObject setX(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "y=") public IRubyObject setY(ThreadContext context, IRubyObject other) { - jy = (Double) other.toJava(Double.class); - return context.getRuntime().newFloat(jy); + if (other instanceof RubyFloat) { + jy = ((RubyFloat) other).getValue(); + } else { + jy = ((RubyFixnum) other).getDoubleValue(); + } + return other; } /** @@ -157,8 +169,12 @@ public IRubyObject setY(ThreadContext context, IRubyObject other) { */ @JRubyMethod(name = "z=") public IRubyObject setZ(ThreadContext context, IRubyObject other) { - jz = (Double) other.toJava(Double.class); - return context.getRuntime().newFloat(jz); + if (other instanceof RubyFloat) { + jz = ((RubyFloat) other).getValue(); + } else { + jz = ((RubyFixnum) other).getDoubleValue(); + } + return other; } /** @@ -198,11 +214,14 @@ public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject valu Ruby runtime = context.getRuntime(); if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { - jx = (Double) value.toJava(Double.class); + jx = (value instanceof RubyFloat) + ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue(); } else if (key == RubySymbol.newSymbol(runtime, "y")) { - jy = (Double) value.toJava(Double.class); + jy = (value instanceof RubyFloat) + ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue(); } else if (key == RubySymbol.newSymbol(runtime, "z")) { - jz = (Double) value.toJava(Double.class); + jz = (value instanceof RubyFloat) + ? ((RubyFloat) value).getValue() : ((RubyFixnum) value).getDoubleValue(); } else { throw runtime.newIndexError("invalid key"); } @@ -336,38 +355,40 @@ public IRubyObject op_sub(ThreadContext context, IRubyObject other) { /** * * @param context ThreadContext - * @param other IRubyObject + * @param scalar IRubyObject * @return new Vec3 object (ruby) */ @JRubyMethod(name = "*", required = 1) - public IRubyObject op_mul(ThreadContext context, IRubyObject other) { + public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.getRuntime(); - double scalar = (Double) other.toJava(Double.class); + double multi = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ - runtime.newFloat(jx * scalar), - runtime.newFloat(jy * scalar), - runtime.newFloat(jz * scalar)}); + runtime.newFloat(jx * multi), + runtime.newFloat(jy * multi), + runtime.newFloat(jz * multi)}); } /** * * @param context ThreadContext - * @param other IRubyObject + * @param scalar IRubyObject * @return new Vec3 object (ruby) */ @JRubyMethod(name = "/", required = 1) - public IRubyObject op_div(ThreadContext context, IRubyObject other) { + public IRubyObject op_div(ThreadContext context, IRubyObject scalar) { Ruby runtime = context.getRuntime(); - double scalar = (Double) other.toJava(Double.class); - if (Math.abs(scalar) < Vec3.EPSILON) { + double divisor = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); + if (Math.abs(divisor) < Vec3.EPSILON) { return this; } return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ - runtime.newFloat(jx / scalar), - runtime.newFloat(jy / scalar), - runtime.newFloat(jz / scalar)}); + runtime.newFloat(jx / divisor), + runtime.newFloat(jy / divisor), + runtime.newFloat(jz / divisor)}); } /** @@ -409,7 +430,8 @@ public IRubyObject set_mag(ThreadContext context, IRubyObject scalar, Block bloc return this; } } - double new_mag = (Double) scalar.toJava(Double.class); + double new_mag = (scalar instanceof RubyFloat) + ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); double current = Math.sqrt(jx * jx + jy * jy + jz * jz); if (current > EPSILON) { jx *= new_mag / current; @@ -583,8 +605,10 @@ public void toVertexUV(ThreadContext context, IRubyObject[] args) { double u = 0; double v = 0; if (count == 3) { - u = (Double) args[1].toJava(Double.class); - v = (Double) args[2].toJava(Double.class); + u = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + v = (args[2] instanceof RubyFloat) + ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); } if (count == 2) { Vec2 texture = (Vec2) args[1].toJava(Vec2.class); From 8ae9ac217b41be4f84d27aeb53268753875d62fe Mon Sep 17 00:00:00 2001 From: monkstone Date: Sat, 16 Jul 2016 14:12:14 +0100 Subject: [PATCH 18/19] request long double values rather than cast to Objects --- src/monkstone/MathToolModule.java | 110 +++++++++++++++------------ src/monkstone/arcball/Rarcball.java | 25 +++--- src/monkstone/fastmath/Deglut.java | 10 +-- src/monkstone/vecmath/vec2/Vec2.java | 50 ++++++------ src/monkstone/vecmath/vec3/Vec3.java | 50 ++++++------ 5 files changed, 134 insertions(+), 111 deletions(-) diff --git a/src/monkstone/MathToolModule.java b/src/monkstone/MathToolModule.java index 83a4bdd7..64184568 100644 --- a/src/monkstone/MathToolModule.java +++ b/src/monkstone/MathToolModule.java @@ -11,6 +11,7 @@ package monkstone; import org.jruby.Ruby; +import org.jruby.RubyFixnum; import org.jruby.RubyFloat; import org.jruby.RubyModule; import org.jruby.RubyRange; @@ -23,14 +24,13 @@ * * @author Martin Prout */ -@JRubyModule(name = "MathTool") -public class MathToolModule { - - /** - * - * @param runtime Ruby - */ - +@JRubyModule(name = "MathTool") +public class MathToolModule { + + /** + * + * @param runtime Ruby + */ public static void createMathToolModule(Ruby runtime) { RubyModule mtModule = runtime.defineModule("MathTool"); mtModule.defineAnnotatedMethods(MathToolModule.class); @@ -39,38 +39,47 @@ public static void createMathToolModule(Ruby runtime) { /** * * @param context ThreadContext - * @param recv IRubyObject + * @param recv IRubyObject * @param args array of RubyRange (must be be numeric) * @return mapped value RubyFloat */ @JRubyMethod(name = "map1d", rest = true, module = true) public static IRubyObject mapOneD(ThreadContext context, IRubyObject recv, IRubyObject[] args) { - double value = (Double) args[0].toJava(Double.class); + double value = (args[0] instanceof RubyFloat) + ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); RubyRange r1 = (RubyRange) args[1]; RubyRange r2 = (RubyRange) args[2]; - double first1 = (Double) r1.first(context).toJava(Double.class); - double first2 = (Double) r2.first(context).toJava(Double.class); - double last1 = (Double) r1.last(context).toJava(Double.class); - double last2 = (Double) r2.last(context).toJava(Double.class); + double first1 = (r1.first(context) instanceof RubyFloat) + ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue(); + double first2 = (r2.first(context) instanceof RubyFloat) + ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue(); + double last1 = (r1.last(context) instanceof RubyFloat) + ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue(); + double last2 = (r2.last(context) instanceof RubyFloat) + ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue(); return mapMt(context, value, first1, last1, first2, last2); } /** * * @param context ThreadContext - * @param recv IRubyObject + * @param recv IRubyObject * @param args array of RubyRange (must be be numeric) * @return mapped value RubyFloat */ @JRubyMethod(name = "constrained_map", rest = true, module = true) public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv, IRubyObject[] args) { - double value = (Double) args[0].toJava(Double.class); + double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); RubyRange r1 = (RubyRange) args[1]; RubyRange r2 = (RubyRange) args[2]; - double first1 = (Double) r1.first(context).toJava(Double.class); - double first2 = (Double) r2.first(context).toJava(Double.class); - double last1 = (Double) r1.last(context).toJava(Double.class); - double last2 = (Double) r2.last(context).toJava(Double.class); + double first1 = (r1.first(context) instanceof RubyFloat) + ? ((RubyFloat) r1.first(context)).getValue() : ((RubyFixnum) r1.first(context)).getDoubleValue(); + double first2 = (r2.first(context) instanceof RubyFloat) + ? ((RubyFloat) r2.first(context)).getValue() : ((RubyFixnum) r2.first(context)).getDoubleValue(); + double last1 = (r1.last(context) instanceof RubyFloat) + ? ((RubyFloat) r1.last(context)).getValue() : ((RubyFixnum) r1.last(context)).getDoubleValue(); + double last2 = (r2.last(context) instanceof RubyFloat) + ? ((RubyFloat) r2.last(context)).getValue() : ((RubyFixnum) r2.last(context)).getDoubleValue(); double max = Math.max(first1, last1); double min = Math.min(first1, last1); if (value < min) { @@ -79,7 +88,7 @@ public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv if (value > max) { value = max; } - return mapMt(context, value, first1, last1, first2, last2); + return mapMt(context, value, first1, last1, first2, last2); } /** @@ -91,37 +100,42 @@ public static IRubyObject constrainedMap(ThreadContext context, IRubyObject recv */ @JRubyMethod(name = "p5map", rest = true, module = true) public static IRubyObject mapProcessing(ThreadContext context, IRubyObject recv, IRubyObject[] args) { - double value = (Double) args[0].toJava(Double.class); - double first1 = (Double) args[1].toJava(Double.class); - double first2 = (Double) args[3].toJava(Double.class); - double last1 = (Double) args[2].toJava(Double.class); - double last2 = (Double) args[4].toJava(Double.class); + double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + double first1 = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double first2 = (args[3] instanceof RubyFloat) ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue(); + double last1 = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); + double last2 = (args[4] instanceof RubyFloat) ? ((RubyFloat) args[4]).getValue() : ((RubyFixnum) args[4]).getDoubleValue(); return mapMt(context, value, first1, last1, first2, last2); } - /** * A more correct version than processing.org version + * * @param context ThreadContext * @param recv self IRubyObject - * @param args args[2] should be between 0 and 1.0 if not returns start or stop + * @param args args[2] should be between 0 and 1.0 if not returns start or + * stop * @return lerped value RubyFloat */ @JRubyMethod(name = "lerp", rest = true, module = true) public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyObject[] args) { - double start = (Double) args[0].toJava(Double.class); - double stop = (Double) args[1].toJava(Double.class); - double amount = (Double) args[2].toJava(Double.class); - if (amount <= 0) return args[0]; - if (amount >= 1.0) return args[1]; - return context.getRuntime().newFloat((1 - amount) * start + (stop * amount)); + double start = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + double stop = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double amount = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); + if (amount <= 0) { + return args[0]; + } + if (amount >= 1.0) { + return args[1]; + } + return context.runtime.newFloat((1 - amount) * start + (stop * amount)); } - /** - * Identical to p5map(value, low, high, 0, 1). - * Numbers outside of the range are not clamped to 0 and 1, - * because out-of-range values are often intentional and useful. + * Identical to p5map(value, low, high, 0, 1). Numbers outside of the range + * are not clamped to 0 and 1, because out-of-range values are often + * intentional and useful. + * * @param context ThreadContext * @param recv IRubyObject * @param args array of args must be be numeric @@ -129,15 +143,16 @@ public static IRubyObject lerpP(ThreadContext context, IRubyObject recv, IRubyOb */ @JRubyMethod(name = "norm", rest = true, module = true) public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyObject[] args) { - double value = (Double) args[0].toJava(Double.class); - double start = (Double) args[1].toJava(Double.class); - double stop = (Double) args[2].toJava(Double.class); + double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); return mapMt(context, value, start, stop, 0, 1.0); } /** - * Identical to p5map(value, low, high, 0, 1) but 'clamped'. - * Numbers outside of the range are clamped to 0 and 1, + * Identical to p5map(value, low, high, 0, 1) but 'clamped'. Numbers outside + * of the range are clamped to 0 and 1, + * * @param context ThreadContext * @param recv IRubyObject * @param args array of args must be be numeric @@ -146,9 +161,9 @@ public static IRubyObject normP(ThreadContext context, IRubyObject recv, IRubyOb @JRubyMethod(name = "norm_strict", rest = true, module = true) public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, IRubyObject[] args) { Ruby ruby = context.runtime; - double value = (Double) args[0].toJava(Double.class); - double start = (Double) args[1].toJava(Double.class); - double stop = (Double) args[2].toJava(Double.class); + double value = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); + double start = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double stop = (args[2] instanceof RubyFloat) ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); if (value <= start) { return new RubyFloat(ruby, 0); } else if (value >= stop) { @@ -160,11 +175,12 @@ public static IRubyObject norm_strict(ThreadContext context, IRubyObject recv, I static final RubyFloat mapMt(ThreadContext context, double value, double first1, double last1, double first2, double last2) { double result = first2 + (last2 - first2) * ((value - first1) / (last1 - first1)); - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } /** * Provides processing constrain method as a ruby module method + * * @param context ThreadContext * @param recv IRubyObject * @param args array of args must be be numeric diff --git a/src/monkstone/arcball/Rarcball.java b/src/monkstone/arcball/Rarcball.java index 5c4db963..716a69a5 100644 --- a/src/monkstone/arcball/Rarcball.java +++ b/src/monkstone/arcball/Rarcball.java @@ -22,6 +22,8 @@ import org.jruby.Ruby; import org.jruby.RubyClass; +import org.jruby.RubyFloat; +import org.jruby.RubyFixnum; import org.jruby.RubyModule; import org.jruby.RubyObject; import org.jruby.RubySymbol; @@ -69,18 +71,23 @@ public Rarcball(Ruby runtime, RubyClass metaClass) { @JRubyMethod(name = "init", meta = true, rest = true, required = 1, optional = 3) public static void init(ThreadContext context, IRubyObject self, IRubyObject args[]) { - int count = Arity.checkArgumentCount(context.getRuntime(), args, 1, 4); + int count = Arity.checkArgumentCount(context.runtime, args, 1, 4); if (count == 4) { PApplet parent = (PApplet) args[0].toJava(PApplet.class); - double cx = (double) args[1].toJava(Double.class); - double cy = (double) args[2].toJava(Double.class); - double radius = (double) args[3].toJava(Double.class); + double cx = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double cy = (args[2] instanceof RubyFloat) + ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); + double radius = (args[3] instanceof RubyFloat) + ? ((RubyFloat) args[3]).getValue() : ((RubyFixnum) args[3]).getDoubleValue(); new Arcball(parent, cx, cy, radius).setActive(true); } if (count == 3) { PApplet parent = (PApplet) args[0].toJava(PApplet.class); - double cx = (double) args[1].toJava(Double.class); - double cy = (double) args[2].toJava(Double.class); + double cx = (args[1] instanceof RubyFloat) + ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); + double cy = (args[2] instanceof RubyFloat) + ? ((RubyFloat) args[2]).getValue() : ((RubyFixnum) args[2]).getDoubleValue(); new Arcball(parent, cx, cy, parent.width * 0.8f).setActive(true); } if (count == 1) { @@ -98,9 +105,9 @@ public static void init(ThreadContext context, IRubyObject self, IRubyObject arg @JRubyMethod(name = "constrain", meta = true, rest = true, required = 1, optional = 1) public static void constrain(ThreadContext context, IRubyObject self, IRubyObject args[]) { - int count = Arity.checkArgumentCount(context.getRuntime(), args, 1, 2); - RubySymbol zaxis = RubySymbol.newSymbol(context.getRuntime(), "zaxis"); - RubySymbol xaxis = RubySymbol.newSymbol(context.getRuntime(), "xaxis"); + int count = Arity.checkArgumentCount(context.runtime, args, 1, 2); + RubySymbol zaxis = RubySymbol.newSymbol(context.runtime, "zaxis"); + RubySymbol xaxis = RubySymbol.newSymbol(context.runtime, "xaxis"); PApplet parent = (PApplet) args[0].toJava(PApplet.class); if (count == 2) { if (xaxis == args[1]) { diff --git a/src/monkstone/fastmath/Deglut.java b/src/monkstone/fastmath/Deglut.java index d1acf09c..562af227 100644 --- a/src/monkstone/fastmath/Deglut.java +++ b/src/monkstone/fastmath/Deglut.java @@ -21,7 +21,7 @@ package monkstone.fastmath; import org.jruby.Ruby; -import org.jruby.RubyClass; +import org.jruby.RubyInteger; import org.jruby.RubyModule; import org.jruby.anno.JRubyModule; import org.jruby.anno.JRubyMethod; @@ -89,7 +89,7 @@ public static void createDeglut(final Ruby runtime){ @JRubyMethod(name = "sin", module = true) public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObject other) { - int thet = (Integer) other.toJava(Integer.class); + int thet = (int) ((RubyInteger)other).getLongValue(); while (thet < 0) { thet += FULL; // Needed because negative modulus plays badly in java } @@ -98,7 +98,7 @@ public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObje double result = (theta < NINETY) ? SIN_DEG_LUT[y] : (theta < 180) ? SIN_DEG_LUT[NINETY - y] : (theta < 270) ? -SIN_DEG_LUT[y] : -SIN_DEG_LUT[NINETY - y]; - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } /** @@ -110,7 +110,7 @@ public static IRubyObject sin(ThreadContext context, IRubyObject recv, IRubyObje */ @JRubyMethod(name = "cos", module = true) public static IRubyObject cos(ThreadContext context, IRubyObject recv, IRubyObject other) { - int thet = (Integer) other.toJava(Integer.class); + int thet = (int) ((RubyInteger)other).getLongValue(); while (thet < 0) { thet += FULL; // Needed because negative modulus plays badly in java } @@ -119,6 +119,6 @@ public static IRubyObject cos(ThreadContext context, IRubyObject recv, IRubyObje double result = (theta < NINETY) ? SIN_DEG_LUT[NINETY - y] : (theta < 180) ? -SIN_DEG_LUT[y] : (theta < 270) ? -SIN_DEG_LUT[NINETY - y] : SIN_DEG_LUT[y]; - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } } diff --git a/src/monkstone/vecmath/vec2/Vec2.java b/src/monkstone/vecmath/vec2/Vec2.java index ff1c6f17..1b628431 100644 --- a/src/monkstone/vecmath/vec2/Vec2.java +++ b/src/monkstone/vecmath/vec2/Vec2.java @@ -88,7 +88,7 @@ public Vec2(Ruby runtime, RubyClass klass) { } void init(ThreadContext context, IRubyObject[] args) { - if (Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 2) == 2) { + if (Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 2) == 2) { jx = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); jy = (args[1] instanceof RubyFloat) @@ -104,7 +104,7 @@ void init(ThreadContext context, IRubyObject[] args) { @JRubyMethod(name = "x") public IRubyObject getX(ThreadContext context) { - return context.getRuntime().newFloat(jx); + return context.runtime.newFloat(jx); } /** @@ -115,7 +115,7 @@ public IRubyObject getX(ThreadContext context) { @JRubyMethod(name = "y") public IRubyObject getY(ThreadContext context) { - return context.getRuntime().newFloat(jy); + return context.runtime.newFloat(jy); } /** @@ -127,7 +127,7 @@ public IRubyObject getY(ThreadContext context) { @JRubyMethod(name = "[]", required = 1) public IRubyObject aref(ThreadContext context, IRubyObject key) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { return runtime.newFloat(jx); @@ -150,7 +150,7 @@ public IRubyObject aref(ThreadContext context, IRubyObject key) { @JRubyMethod(name = "[]=") public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { return runtime.newFloat(jx); @@ -207,7 +207,7 @@ public IRubyObject setY(ThreadContext context, IRubyObject other) { public IRubyObject dist(ThreadContext context, IRubyObject other) { Vec2 b = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { b = (Vec2) other.toJava(Vec2.class); } else { @@ -227,7 +227,7 @@ public IRubyObject dist(ThreadContext context, IRubyObject other) { public IRubyObject cross(ThreadContext context, IRubyObject other) { Vec2 b = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { b = (Vec2) other.toJava(Vec2.class); } else { @@ -246,7 +246,7 @@ public IRubyObject cross(ThreadContext context, IRubyObject other) { public IRubyObject dot(ThreadContext context, IRubyObject other) { Vec2 b = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { b = (Vec2) other.toJava(Vec2.class); } else { @@ -265,7 +265,7 @@ public IRubyObject dot(ThreadContext context, IRubyObject other) { public IRubyObject op_plus(ThreadContext context, IRubyObject other) { Vec2 b = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { b = (Vec2) other.toJava(Vec2.class); } else { @@ -286,7 +286,7 @@ public IRubyObject op_plus(ThreadContext context, IRubyObject other) { public IRubyObject op_minus(ThreadContext context, IRubyObject other) { Vec2 b = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { b = (Vec2) other.toJava(Vec2.class); } else { @@ -306,7 +306,7 @@ public IRubyObject op_minus(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "*") public IRubyObject op_mul(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double scalar = (other instanceof RubyFloat) ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); return Vec2.rbNew(context, this.getMetaClass(), @@ -323,7 +323,7 @@ public IRubyObject op_mul(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "/", required = 1) public IRubyObject op_div(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double scalar = (other instanceof RubyFloat) ? ((RubyFloat) other).getValue() : ((RubyFixnum) other).getDoubleValue(); if (Math.abs(scalar) < Vec2.EPSILON) { @@ -341,7 +341,7 @@ public IRubyObject op_div(ThreadContext context, IRubyObject other) { */ @JRubyMethod(name = "heading") public IRubyObject heading(ThreadContext context) { - return context.getRuntime().newFloat(Math.atan2(jy, jx)); + return context.runtime.newFloat(Math.atan2(jy, jx)); } /** @@ -363,7 +363,7 @@ public IRubyObject mag(ThreadContext context) { result = Math.abs(jx); } } - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } /** @@ -437,7 +437,7 @@ public IRubyObject normalize_bang(ThreadContext context) { public IRubyObject normalize(ThreadContext context) { double mag = 0; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (Math.abs(jx) > EPSILON && Math.abs(jy) > EPSILON) { mag = Math.hypot(jx, jy); } else { @@ -467,7 +467,7 @@ public IRubyObject normalize(ThreadContext context) { */ @JRubyMethod(name = "from_angle", meta = true) public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, IRubyObject scalar) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double angle = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); return Vec2.rbNew(context, klazz, new IRubyObject[]{ @@ -484,7 +484,7 @@ public static IRubyObject from_angle(ThreadContext context, IRubyObject klazz, I */ @JRubyMethod(name = "random", meta = true) public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double angle = Math.random() * Math.PI * 2; return Vec2.rbNew(context, klazz, new IRubyObject[]{ runtime.newFloat(Math.cos(angle)), @@ -516,7 +516,7 @@ public IRubyObject rotate_bang(ThreadContext context, IRubyObject scalar) { */ @JRubyMethod(name = "rotate") public IRubyObject rotate(ThreadContext context, IRubyObject scalar) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double theta = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); IRubyObject[] ary = new IRubyObject[]{ @@ -533,7 +533,7 @@ public IRubyObject rotate(ThreadContext context, IRubyObject scalar) { */ @JRubyMethod(name = "lerp", rest = true) public IRubyObject lerp(ThreadContext context, IRubyObject[] args) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Arity.checkArgumentCount(runtime, args, 2, 2); Vec2 vec = (Vec2) args[0].toJava(Vec2.class); double scalar = (args[1] instanceof RubyFloat) @@ -553,7 +553,7 @@ public IRubyObject lerp(ThreadContext context, IRubyObject[] args) { */ @JRubyMethod(name = "lerp!", rest = true) public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) { - Arity.checkArgumentCount(context.getRuntime(), args, 2, 2); + Arity.checkArgumentCount(context.runtime, args, 2, 2); Vec2 vec = (Vec2) args[0].toJava(Vec2.class); double scalar = (args[1] instanceof RubyFloat) ? ((RubyFloat) args[1]).getValue() : ((RubyFixnum) args[1]).getDoubleValue(); @@ -574,7 +574,7 @@ public IRubyObject lerp_bang(ThreadContext context, IRubyObject[] args) { public IRubyObject angleBetween(ThreadContext context, IRubyObject other) { Vec2 vec = null; - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other instanceof Vec2) { vec = (Vec2) other.toJava(Vec2.class); } else { @@ -606,7 +606,7 @@ public IRubyObject copy(ThreadContext context) { public IRubyObject toArray(ThreadContext context) { Ruby runtime = context.runtime; - return RubyArray.newArray(context.getRuntime(), new IRubyObject[]{ + return RubyArray.newArray(runtime, new IRubyObject[]{ runtime.newFloat(jx), runtime.newFloat(jy)}); } @@ -648,7 +648,7 @@ public void toCurveVertex(ThreadContext context, IRubyObject object) { @JRubyMethod(name = {"to_s", "inspect"}) public IRubyObject to_s(ThreadContext context) { - return context.getRuntime().newString(String.format("Vec2D(x = %4.4f, y = %4.4f)", jx, jy)); + return context.runtime.newString(String.format("Vec2D(x = %4.4f, y = %4.4f)", jx, jy)); } /** @@ -693,7 +693,7 @@ public boolean equals(Object obj) { */ @JRubyMethod(name = "eql?", required = 1) public IRubyObject eql_p(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other == this) { return runtime.newBoolean(true); } @@ -717,7 +717,7 @@ public IRubyObject eql_p(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other == this) { return runtime.newBoolean(true); } diff --git a/src/monkstone/vecmath/vec3/Vec3.java b/src/monkstone/vecmath/vec3/Vec3.java index 21be3e11..2fb5eab1 100644 --- a/src/monkstone/vecmath/vec3/Vec3.java +++ b/src/monkstone/vecmath/vec3/Vec3.java @@ -82,7 +82,7 @@ public Vec3(Ruby runtime, RubyClass klass) { } void init(ThreadContext context, IRubyObject[] args) { - int count = Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 3); + int count = Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 3); if (count >= 2) { jx = (args[0] instanceof RubyFloat) ? ((RubyFloat) args[0]).getValue() : ((RubyFixnum) args[0]).getDoubleValue(); @@ -103,7 +103,7 @@ void init(ThreadContext context, IRubyObject[] args) { @JRubyMethod(name = "x") public IRubyObject getX(ThreadContext context) { - return context.getRuntime().newFloat(jx); + return context.runtime.newFloat(jx); } /** @@ -114,7 +114,7 @@ public IRubyObject getX(ThreadContext context) { @JRubyMethod(name = "y") public IRubyObject getY(ThreadContext context) { - return context.getRuntime().newFloat(jy); + return context.runtime.newFloat(jy); } /** @@ -124,7 +124,7 @@ public IRubyObject getY(ThreadContext context) { */ @JRubyMethod(name = "z") public IRubyObject getZ(ThreadContext context) { - return context.getRuntime().newFloat(jz); + return context.runtime.newFloat(jz); } /** @@ -186,7 +186,7 @@ public IRubyObject setZ(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "[]", required = 1) public IRubyObject aref(ThreadContext context, IRubyObject key) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { return runtime.newFloat(jx); @@ -211,7 +211,7 @@ public IRubyObject aref(ThreadContext context, IRubyObject key) { @JRubyMethod(name = "[]=") public IRubyObject aset(ThreadContext context, IRubyObject key, IRubyObject value) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (key instanceof RubySymbol) { if (key == RubySymbol.newSymbol(runtime, "x")) { jx = (value instanceof RubyFloat) @@ -247,7 +247,7 @@ public IRubyObject dist(ThreadContext context, IRubyObject other) { throw context.runtime.newTypeError("argument should be Vec3D"); } double result = Math.sqrt((jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz)); - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } /** @@ -266,7 +266,7 @@ public IRubyObject dist_squared(ThreadContext context, IRubyObject other) { throw context.runtime.newTypeError("argument should be Vec3D"); } double result = (jx - b.jx) * (jx - b.jx) + (jy - b.jy) * (jy - b.jy) + (jz - b.jz) * (jz - b.jz); - return context.getRuntime().newFloat(result); + return context.runtime.newFloat(result); } /** @@ -279,7 +279,7 @@ public IRubyObject dist_squared(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "cross", required = 1) public IRubyObject cross(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Vec3 vec = null; if (other instanceof Vec3) { vec = (Vec3) other.toJava(Vec3.class); @@ -303,7 +303,7 @@ public IRubyObject cross(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "dot", required = 1) public IRubyObject dot(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Vec3 b = null; if (other instanceof Vec3) { b = (Vec3) other.toJava(Vec3.class); @@ -322,7 +322,7 @@ public IRubyObject dot(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "+", required = 1) public IRubyObject op_add(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Vec3 b = (Vec3) other.toJava(Vec3.class); return Vec3.rbNew(context, other.getMetaClass(), new IRubyObject[]{ runtime.newFloat(jx + b.jx), @@ -339,7 +339,7 @@ public IRubyObject op_add(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "-") public IRubyObject op_sub(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Vec3 b = null; if (other instanceof Vec3) { b = (Vec3) other.toJava(Vec3.class); @@ -361,7 +361,7 @@ public IRubyObject op_sub(ThreadContext context, IRubyObject other) { @JRubyMethod(name = "*", required = 1) public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double multi = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ @@ -379,7 +379,7 @@ public IRubyObject op_mul(ThreadContext context, IRubyObject scalar) { @JRubyMethod(name = "/", required = 1) public IRubyObject op_div(ThreadContext context, IRubyObject scalar) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double divisor = (scalar instanceof RubyFloat) ? ((RubyFloat) scalar).getValue() : ((RubyFixnum) scalar).getDoubleValue(); if (Math.abs(divisor) < Vec3.EPSILON) { @@ -399,7 +399,7 @@ public IRubyObject op_div(ThreadContext context, IRubyObject scalar) { @JRubyMethod(name = "mag_squared") public IRubyObject mag_squared(ThreadContext context) { - return context.getRuntime().newFloat(jx * jx + jy * jy + jz * jz); + return context.runtime.newFloat(jx * jx + jy * jy + jz * jz); } /** @@ -410,7 +410,7 @@ public IRubyObject mag_squared(ThreadContext context) { @JRubyMethod(name = "mag") public IRubyObject mag(ThreadContext context) { - return context.getRuntime().newFloat(Math.sqrt(jx * jx + jy * jy + jz * jz)); + return context.runtime.newFloat(Math.sqrt(jx * jx + jy * jy + jz * jz)); } /** @@ -467,7 +467,7 @@ public IRubyObject normalize_bang(ThreadContext context) { @JRubyMethod(name = "normalize") public IRubyObject normalize(ThreadContext context) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double mag = Math.sqrt(jx * jx + jy * jy + jz * jz); if (mag < EPSILON) { return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ @@ -491,7 +491,7 @@ public IRubyObject normalize(ThreadContext context) { @JRubyMethod(name = "random", meta = true) public static IRubyObject random_direction(ThreadContext context, IRubyObject klazz) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; double angle = Math.random() * Math.PI * 2; double vz = Math.random() * 2 - 1; double vx = Math.sqrt(1 - vz * vz) * Math.cos(angle); @@ -512,7 +512,7 @@ public static IRubyObject random_direction(ThreadContext context, IRubyObject kl @JRubyMethod(name = "angle_between") public IRubyObject angleBetween(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; Vec3 vec = (Vec3) other.toJava(Vec3.class); // We get NaN if we pass in a zero vector which can cause problems // Zero seems like a reasonable angle between a (0,0,0) vector and something else @@ -544,7 +544,7 @@ public IRubyObject angleBetween(ThreadContext context, IRubyObject other) { @JRubyMethod(name = {"copy", "dup"}) public IRubyObject copy(ThreadContext context) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; return Vec3.rbNew(context, this.getMetaClass(), new IRubyObject[]{ runtime.newFloat(jx), runtime.newFloat(jy), @@ -559,7 +559,7 @@ public IRubyObject copy(ThreadContext context) { @JRubyMethod(name = "to_a") public IRubyObject toArray(ThreadContext context) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; return RubyArray.newArray(context.runtime, new IRubyObject[]{ runtime.newFloat(jx), runtime.newFloat(jy), @@ -601,7 +601,7 @@ public void toCurveVertex(ThreadContext context, IRubyObject object) { @JRubyMethod(name = "to_vertex_uv", rest = true) public void toVertexUV(ThreadContext context, IRubyObject[] args) { - int count = Arity.checkArgumentCount(context.getRuntime(), args, Arity.OPTIONAL.getValue(), 3); + int count = Arity.checkArgumentCount(context.runtime, args, Arity.OPTIONAL.getValue(), 3); double u = 0; double v = 0; if (count == 3) { @@ -641,7 +641,7 @@ public void toNormal(ThreadContext context, IRubyObject object) { @JRubyMethod(name = {"to_s", "inspect"}) public IRubyObject to_s(ThreadContext context) { - return context.getRuntime().newString(String.format("Vec3D(x = %4.4f, y = %4.4f, z = %4.4f)", jx, jy, jz)); + return context.runtime.newString(String.format("Vec3D(x = %4.4f, y = %4.4f, z = %4.4f)", jx, jy, jz)); } /** @@ -690,7 +690,7 @@ public boolean equals(Object obj) { @JRubyMethod(name = "eql?", required = 1) public IRubyObject eql_p(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other == this) { return runtime.newBoolean(true); } @@ -716,7 +716,7 @@ public IRubyObject eql_p(ThreadContext context, IRubyObject other) { @Override public IRubyObject op_equal(ThreadContext context, IRubyObject other) { - Ruby runtime = context.getRuntime(); + Ruby runtime = context.runtime; if (other == this) { return runtime.newBoolean(true); } From c60847fbf6d9955768b75594a1264992a6a35799 Mon Sep 17 00:00:00 2001 From: monkstone Date: Sun, 17 Jul 2016 06:38:05 +0100 Subject: [PATCH 19/19] more lining up of ducksgit add lib! --- CHANGELOG.md | 2 +- jruby_art.gemspec | 2 +- lib/jruby_art/runner.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a56e9a0..521c57c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -**v1.2.0.pre** Use optparse to parse command line options, and related re-factoring. +**v1.2.0.pre** Use optparse to parse command line options, and related re-factoring. Avoid casting to java Double and Integer and using `to_java` for primitives in jruby extensions. **v1.1.3** Revert using String refinements in `creator.rb`. Refactor java options to `java_opts.rb`. diff --git a/jruby_art.gemspec b/jruby_art.gemspec index 537d88df..87e3011c 100644 --- a/jruby_art.gemspec +++ b/jruby_art.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| variables and hundreds of worked examples to get you started... EOS spec.summary = %q{Code as Art, Art as Code. Processing and Ruby are meant for each other.} - spec.homepage = "https://ruby-processing.github.io/" + spec.homepage = "http://ruby-processing.github.io/JRubyArt/" spec.post_install_message = %q{Use 'k9 --install' to install jruby-complete, and 'k9 --check' to check config.} spec.license = 'MIT' diff --git a/lib/jruby_art/runner.rb b/lib/jruby_art/runner.rb index 488f5fad..f41acc8a 100644 --- a/lib/jruby_art/runner.rb +++ b/lib/jruby_art/runner.rb @@ -198,7 +198,7 @@ def ensure_exists(filename) def jruby_complete rcomplete = File.join(K9_ROOT, 'lib/ruby/jruby-complete.jar') return [rcomplete] if FileTest.exist?(rcomplete) - warn "#{rcomplete} does not exist\nTry running `k9 setup install`" + warn "#{rcomplete} does not exist\nTry running `k9 --install`" exit end