Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Saturday, 4 September 2010

Using the control panel and Toxi Processing Libraries in ruby processing

Here is a sketch that shows how easy it is to add controls to your sketch in ruby-processing.


#
# <p>GrayScottControl uses the seedImage() method to use a bitmap as simulation seed.
# In this demo the image is re-applied every frame and the user can adjust the
# F coefficient of the reaction diffusion to produce different patterns emerging
# from the boundary of the bitmapped seed. Unlike some other GS demos provided,
# this one also uses a wrapped simulation space, creating tiled patterns.</p>
#
# <p><strong>usage:</strong></p>
# <ul>
# <li>click + drag mouse to locally disturb the simulation</li>
# <li>Use slider to adjust the F parameter of the simulation</li>
# <li>press reset! to do what?</li>
# <li>press save_graycsott to do save</li>
# </ul>
#

#
# Copyright (c) 2010 Karsten Schmidt (rubified by Martin Prout)
#
# This demo & library is free software you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation either
# version 2.1 of the License, or (at your option) any later version.
#
# http:#creativecommons.org/licenses/LGPL/2.1/
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

class GrayScottControl < Processing::App
  load_libraries 'simutils','toxiclibscore','colorutils','control_panel'
  include_package "toxi.sim.grayscott"
  include_package "toxi.math"
  include_package "toxi.color"

  attr_reader :gs, :tone_map, :img, :set_f

  def setup
    size 256, 256
    setup_control
    @gs = GrayScott.new width, height, true
    @img = load_image "two_prong.png"
    # create a duo-tone gradient map with 256 steps
    # NB: use '::' in place of '.' here
    @tone_map = ToneMap.new(0, 0.33, NamedColor::CRIMSON, NamedColor::WHITE, 256)
  end

  def draw
    @gs.seed_image(img.pixels, img.width, img.height)
    @gs.set_rect(mouse_x, mouse_y, 20, 20) if mouse_pressed?
    load_pixels
    10.times { @gs.update(1) }
    # read out the V result array
    # and use tone map to render colours
    gs.v.length.times do |i|
      pixels[i]=tone_map.getARGBToneFor(gs.v[i])  # NB: don't camel case convert here
    end
    @gs.setF(0.02 + set_f * 0.001)
    update_pixels
  end

  def setup_control
    control_panel do |c|
      c.title = "Control Panel"
      c.slider :set_f, 0..9, 8
      c.button :save_graycsott
      c.button :reset!
    end
  end

  def save_graycsott
    no_loop
    save_frame "toxi.png"
    loop
  end

  def reset!
    @gs.reset
  end

end




No comments:

Post a Comment

Followers

Blog Archive

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2