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

Friday 20 September 2013

Reading and writing to a yaml file ruby-processing

The processing sketch
require "yaml"

attr_reader :bubbles, :bubble_data

def setup()
  size(640, 360)
  # read source_string from file
  load_data
end

def draw
  background 255
  bubbles.each do|bubble|
    bubble.display
    bubble.rollover(mouse_x, mouse_y)
  end
end

def load_data
  yaml = YAML.load_file("data/data.yaml")
  # parse the source string
  @bubble_data = BubbleData.new "bubbles"

  # get the bubble_data from the top level hash
  data = bubble_data.extract_data yaml
  @bubbles = []
  # iterate the bubble_data array, and create an array of bubbles
  data.each do |point|
    bubbles << Bubble.new(
      point["position"]["x"],
      point["position"]["y"],
      point["diameter"],
      point["label"])
  end
end

def save_data
  # demonstrate how easy it is to create json object from a hash in ruby
  yaml = bubble_data.to_hash.to_yaml
  # overwite existing 'data.json' 
  open("data/data.yaml", 'w') {|f| f.write(yaml) }
end

def mouse_pressed
  # create a new bubble instance, where mouse was clicked
  @bubble_data.add_bubble(Bubble.new(mouse_x, mouse_y, rand(40 .. 80), "new label"))
  save_data
  # reload the json data from the freshly created file
  load_data
end

class BubbleData
  attr_reader :name, :data
  def initialize name = "bubbles"
    @name = name
    @data = []
  end

  def add_bubble bubble
    data << bubble
  end

  def extract_data yaml
    @data = yaml[name]
  end

  def to_hash
    {name => data.map{|point| point.to_hash}}
  end

end

class Bubble
  attr_reader :x, :y, :diameter, :name, :over

  def initialize(x, y, diameter, name)
    @x, @y, @diameter, @name = x, y, diameter, name
    @over = false
  end

  def rollover px, py
    d = dist px, py, x, y
    @over = (d < diameter / 2.0)
  end

  def display
    stroke 0
    stroke_weight 2
    no_fill
    ellipse x, y, diameter, diameter
    if over
      fill 0
      text_align CENTER
      text(name, x, y + diameter / 2.0 + 20)
    end
  end

  def to_hash
    {"position" => {"x" => x, "y" => y}, "diameter" => diameter, "label" => name}
  end
end


The data file "data.yaml"
---
bubbles:
- position:
    x: 160
    y: 103
  diameter: 43.19838
  label: Happy
- position:
    x: 372
    y: 137
  diameter: 52.42526
  label: Sad
- position:
    x: 273
    y: 235
  diameter: 61.14072
  label: Joyous
- position:
    x: 121
    y: 179
  diameter: 44.758068
  label: Melancholy
See more up to date version here.

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