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').
5
5
# @author Martin Prout, after Daniel Shiffmans version for processing
6
- #
6
+ require 'forwardable'
7
7
require 'json'
8
8
9
9
load_library :bubble
@@ -28,66 +28,64 @@ def mouse_pressed
28
28
bubble_data . create_new_bubble ( mouse_x , mouse_y )
29
29
end
30
30
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 )
32
35
include Enumerable
33
-
36
+
34
37
MAX_BUBBLE = 10
35
-
38
+
36
39
attr_reader :key , :path , :bubbles
37
-
40
+
38
41
# @param key String for top level hash
39
-
40
- def initialize key
42
+
43
+ def initialize ( key )
41
44
@key = key
42
45
@bubbles = [ ]
43
46
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
52
51
load_data path
53
52
end
54
-
55
- def display x , y
56
- self . each do |bubble |
53
+
54
+ def display ( x , y )
55
+ each do |bubble |
57
56
bubble . display
58
57
bubble . rollover ( x , y )
59
58
end
60
59
end
61
-
60
+
62
61
# @param path to json file
63
-
64
- def load_data path
62
+
63
+ def load_data ( path )
65
64
@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
69
68
# iterate the bubble_data array, and create an array of bubbles
70
69
data . each do |point |
71
- self . add Bubble . new (
70
+ add Bubble . new (
72
71
point [ 'position' ] [ 'x' ] ,
73
72
point [ 'position' ] [ 'y' ] ,
74
73
point [ 'diameter' ] ,
75
74
point [ 'label' ] )
76
75
end
77
76
end
78
-
79
- def add bubble
77
+
78
+ def add ( bubble )
80
79
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
+
86
85
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
90
90
end
91
-
92
91
end
93
-
0 commit comments