1- # This example demonstrates how easily 'sketch data' can be retrieved from a json file
2- # in ruby-processing. Note this sketch re-uses the Bubble class from the bubble library.
3- # The BubbleData class, can load, store and create instances of Bubble (and request them
4- # to display and/or show their label, when 'mouse over').
1+ # This example demonstrates how 'sketch data' can be retrieved from a json file
2+ # in ruby-processing. Note this sketch re-uses the Bubble class from the bubble
3+ # library. The BubbleData class, can load, store and create instances of Bubble
4+ # (and request them to display and/or show their label, when 'mouse over').
55# @author Martin Prout, after Daniel Shiffmans version for processing
6- #
6+ require 'forwardable'
77require 'json'
88
99load_library :bubble
@@ -28,66 +28,64 @@ def mouse_pressed
2828 bubble_data . create_new_bubble ( mouse_x , mouse_y )
2929end
3030
31- class BubbleData
31+ # This class can load store and create instances of Bubble
32+ class BubbleData
33+ extend Forwardable
34+ def_delegators ( :@bubbles , :each , :<< , :size , :shift , :clear )
3235 include Enumerable
33-
36+
3437 MAX_BUBBLE = 10
35-
38+
3639 attr_reader :key , :path , :bubbles
37-
40+
3841 # @param key String for top level hash
39-
40- def initialize key
42+
43+ def initialize ( key )
4144 @key = key
4245 @bubbles = [ ]
4346 end
44-
45- def each &block
46- bubbles . each &block
47- end
48-
49- def create_new_bubble x , y
50- self . add Bubble . new ( x , y , rand ( 40 ..80 ) , 'new label' )
51- save_data
47+
48+ def create_new_bubble ( x , y )
49+ add Bubble . new ( x , y , rand ( 40 ..80 ) , 'new label' )
50+ save_data
5251 load_data path
5352 end
54-
55- def display x , y
56- self . each do |bubble |
53+
54+ def display ( x , y )
55+ each do |bubble |
5756 bubble . display
5857 bubble . rollover ( x , y )
5958 end
6059 end
61-
60+
6261 # @param path to json file
63-
64- def load_data path
62+
63+ def load_data ( path )
6564 @path = path
66- source_string = open ( path , 'r' ) { | file | file . read }
67- data = JSON . parse ( source_string ) [ key ]
68- bubbles . clear
65+ file = File . read ( path )
66+ data = JSON . parse ( file ) [ key ]
67+ clear
6968 # iterate the bubble_data array, and create an array of bubbles
7069 data . each do |point |
71- self . add Bubble . new (
70+ add Bubble . new (
7271 point [ 'position' ] [ 'x' ] ,
7372 point [ 'position' ] [ 'y' ] ,
7473 point [ 'diameter' ] ,
7574 point [ 'label' ] )
7675 end
7776 end
78-
79- def add bubble
77+
78+ def add ( bubble )
8079 bubbles << bubble
81- bubbles . shift if bubbles . size > MAX_BUBBLE
82- end
83-
84- private
85-
80+ shift if size > MAX_BUBBLE
81+ end
82+
83+ private
84+
8685 def save_data
87- hash = { key => self . map { |point | point . to_hash } }
88- json = JSON . pretty_generate ( hash ) # generate pretty output
89- open ( path , 'w' ) { |f | f . write ( json ) }
86+ hash = { key => map ( &:to_hash ) }
87+ File . open ( path , 'w' ) do |json |
88+ json . write ( JSON . pretty_generate ( hash ) ) # generate pretty output
89+ end
9090 end
91-
9291end
93-
0 commit comments