Skip to content

Commit 5a3133e

Browse files
committed
Make it work again
1 parent 025d0df commit 5a3133e

File tree

2 files changed

+97
-82
lines changed

2 files changed

+97
-82
lines changed
Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
11
---
22
bubbles:
3-
- :position:
3+
- position:
44
x: 160
55
y: 103
6-
:diameter: 43.19838
7-
:label: Happy
8-
- :position:
6+
diameter: 43.19838
7+
label: Happy
8+
- position:
99
x: 372
1010
y: 137
11-
:diameter: 52.42526
12-
:label: Sad
13-
- :position:
11+
diameter: 52.42526
12+
label: Sad
13+
- position:
1414
x: 273
1515
y: 235
16-
:diameter: 61.14072
17-
:label: Joyous
18-
- :position:
16+
diameter: 61.14072
17+
label: Joyous
18+
- position:
1919
x: 121
2020
y: 179
21-
:diameter: 44.758068
22-
:label: Melancholy
23-
- :position:
24-
x: 532
25-
y: 137
26-
:diameter: 53
27-
:label: new label
21+
diameter: 44.758068
22+
label: Melancholy
Lines changed: 85 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,109 @@
1-
######################################
2-
# Yet another examples of reading and
3-
# writing to some form of markup,
4-
# appropriately yaml.
5-
# by Martin Prout after Dan Shiffman
6-
# ###################################
7-
require 'forwardable'
8-
require 'psych'
9-
load_library :bubble
10-
11-
attr_reader :bubble_data
1+
require 'yaml'
2+
3+
attr_reader :bubbles, :bubble_data
4+
5+
MAX_BUBBLE = 10
6+
7+
def settings
8+
size(640, 360)
9+
end
1210

1311
def setup
14-
sketch_title 'Load Save Yaml'
15-
# load data from file
16-
@bubble_data = BubbleData.new 'bubbles'
17-
bubble_data.load_data(data_path('data.yml'))
12+
sketch_title 'Bubble Yaml'
13+
# read source_string from file
14+
load_data
1815
end
1916

2017
def draw
2118
background 255
22-
bubble_data.display mouse_x, mouse_y
19+
bubbles.each do |bubble|
20+
bubble.display
21+
bubble.rollover(mouse_x, mouse_y)
22+
end
23+
end
24+
25+
def load_data
26+
yaml = YAML.load_file(data_path('data.yml'))
27+
# parse the source string
28+
@bubble_data = BubbleData.new 'bubbles'
29+
# get the bubble_data from the top level hash
30+
data = bubble_data.extract_data yaml
31+
# map the bubble_data array to an array of bubbles
32+
@bubbles = data.map do |point|
33+
pos = point['position']
34+
Bubble.new(pos['x'], pos['y'], point['diameter'], point['label'])
35+
end
36+
end
37+
38+
def save_data
39+
# demonstrate how easy it is to create json object from a hash in ruby
40+
hash = bubble_data.to_hash
41+
# overwite existing 'data.yml'
42+
open(data_path('data.yml'), 'w') { |file| file.write(hash.to_yaml) }
2343
end
2444

2545
def mouse_pressed
2646
# create a new bubble instance, where mouse was clicked
27-
@bubble_data.create_new_bubble(mouse_x, mouse_y)
47+
@bubble_data.add_bubble(
48+
Bubble.new(mouse_x, mouse_y, rand(40..80), 'new label')
49+
)
50+
save_data
51+
# reload the yaml data from the freshly created file
52+
load_data
2853
end
2954

30-
# Bubble class can create and display bubble data from a yaml file
55+
# Bubble data holder
3156
class BubbleData
32-
extend Forwardable
33-
def_delegators(:@bubbles, :each, :size)
34-
include Enumerable
57+
attr_reader :name, :data
58+
def initialize(name = 'bubbles')
59+
@name = name
60+
@bubbles = []
61+
end
3562

36-
MAX_BUBBLE = 10
63+
def add_bubble(bubble)
64+
bubbles << bubble
65+
bubbles.shift if bubbles.size > MAX_BUBBLE
66+
end
3767

38-
attr_reader :key
39-
def initialize(key)
40-
@bubbles = []
41-
@key = key
42-
super
68+
def extract_data(yaml)
69+
@bubbles = yaml[name]
4370
end
4471

45-
def create_new_bubble(x, y)
46-
add(Bubble.new(x, y, rand(40..80), 'new label'))
47-
save_data
48-
load_data path
72+
def to_hash
73+
{ name => bubbles.map(&:to_hash) }
4974
end
75+
end
76+
77+
# The Bubble class
78+
class Bubble
79+
attr_reader :x, :y, :diameter, :name, :over
5080

51-
def display(x, y)
52-
each do |bubble|
53-
bubble.display
54-
bubble.rollover(x, y)
55-
end
81+
def initialize(x, y, diameter, name)
82+
@x, @y, @diameter, @name = x, y, diameter, name
83+
@over = false
5684
end
5785

58-
# @param path to yaml file
59-
def load_data(path)
60-
yaml = Psych.load_file(path)
61-
data = yaml[key]
62-
clear
63-
# iterate the bubble_data array, and create an array of bubbles
64-
data.each do |point|
65-
add Bubble.new(
66-
point['position']['x'],
67-
point['position']['y'],
68-
point['diameter'],
69-
point['label'])
70-
end
71-
end
72-
73-
def add(bubble)
74-
bubbles << bubble
75-
bubbles.shift if bubbles.size > MAX_BUBBLE
76-
end
77-
78-
private
79-
80-
def save_data
81-
hash = { key => map(&:to_hash) }
82-
# overwite existing 'data.yaml'
83-
File.write(data_path('data.yml'), hash.to_yaml)
84-
end
86+
def rollover(px, py)
87+
distance = dist px, py, x, y
88+
@over = (distance < diameter / 2.0)
8589
end
8690

87-
def settings
88-
size 640, 360
91+
def display
92+
stroke 0
93+
stroke_weight 2
94+
no_fill
95+
ellipse x, y, diameter, diameter
96+
return unless over
97+
fill 0
98+
text_align CENTER
99+
text(name, x, y + diameter / 2.0 + 20)
89100
end
101+
102+
def to_hash
103+
{
104+
'position' => { 'x' => x, 'y' => y },
105+
'diameter' => diameter,
106+
'label' => name
107+
}
108+
end
109+
end

0 commit comments

Comments
 (0)