Skip to content

Commit 01f4557

Browse files
committed
More OO bubble yaml sketch
1 parent 5a3133e commit 01f4557

8 files changed

Lines changed: 109 additions & 90 deletions

File tree

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
---
2-
bubbles:
2+
emotions:
33
- position:
4-
x: 160
5-
y: 103
4+
- 160
5+
- 103
66
diameter: 43.19838
77
label: Happy
88
- position:
9-
x: 372
10-
y: 137
9+
- 372
10+
- 137
1111
diameter: 52.42526
1212
label: Sad
1313
- position:
14-
x: 273
15-
y: 235
14+
- 273
15+
- 235
1616
diameter: 61.14072
1717
label: Joyous
1818
- position:
19-
x: 121
20-
y: 179
19+
- 121
20+
- 179
2121
diameter: 44.758068
2222
label: Melancholy

processing_app/topics/advanced_data/library/bubble/bubble.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# encoding: utf-8
22
# frozen_string_literal: true
3-
4-
# The bubble library, include BubbleStruct
3+
# The bubble library
54
class Bubble
65
include Processing::Proxy
76

@@ -38,15 +37,7 @@ def to_a
3837
[x, y, diameter, label]
3938
end
4039

41-
def to_hash
42-
keys = %i[position diameter label]
43-
values = [[['x', x], ['y', y]].to_h, diameter, label]
44-
keys.zip(values).to_h
45-
end
46-
4740
def to_struct
4841
BubbleStruct.new(x, y, diameter, label)
4942
end
5043
end
51-
52-
BubbleStruct = Struct.new(:x, :y, :diameter, :label)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require_relative 'lib/bubble_reader.rb'
2+
require_relative 'lib/bubble_writer.rb'
3+
require_relative 'lib/bubble.rb'
4+
require_relative 'lib/bubble_data.rb'
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# The Bubble class
2+
class Bubble
3+
include Processing::Proxy
4+
attr_reader :pos, :diameter, :name, :over
5+
6+
def initialize(pos, diameter, name)
7+
@pos, @diameter, @name = pos, diameter, name
8+
@over = false
9+
end
10+
11+
def rollover(px, py)
12+
distance = dist px, py, pos[X], pos[Y]
13+
@over = (distance < diameter / 2.0)
14+
end
15+
16+
def display
17+
xpos = pos[X]
18+
ypos = pos[Y]
19+
display_bubble(xpos, ypos)
20+
return unless over
21+
display_label(xpos, ypos)
22+
end
23+
24+
def display_bubble(xpos, ypos)
25+
stroke 0
26+
stroke_weight 2
27+
no_fill
28+
ellipse xpos, ypos, diameter, diameter
29+
end
30+
31+
def display_label(xpos, ypos)
32+
fill 0
33+
text_align CENTER
34+
text(name, xpos, ypos + diameter / 2.0 + 20)
35+
end
36+
37+
def to_h
38+
{
39+
'position' => pos,
40+
'diameter' => diameter,
41+
'label' => name
42+
}
43+
end
44+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
MAX_BUBBLE = 10
2+
3+
# Bubble data holder
4+
class BubbleData
5+
attr_reader :bubbles
6+
def initialize(bubbles)
7+
@bubbles = bubbles
8+
end
9+
10+
def add_bubble(bubble)
11+
bubbles << bubble
12+
bubbles.shift if bubbles.size > MAX_BUBBLE
13+
end
14+
15+
def to_h
16+
bubbles.map(&:to_h)
17+
end
18+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'yaml'
2+
3+
# yaml bubble reader
4+
class BubbleReader
5+
attr_reader :hash
6+
7+
def read(path)
8+
@hash = YAML.load_file(path)
9+
end
10+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
require 'yaml'
2+
3+
# yaml bubble writer
4+
class BubbleWriter
5+
attr_reader :hash
6+
7+
def initialize(type:, data:)
8+
@hash = { type => data }
9+
end
10+
11+
def write(path)
12+
File.write(path, hash.to_yaml)
13+
end
14+
end
Lines changed: 9 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
require 'yaml'
1+
load_library :bubble_yaml
22

33
attr_reader :bubbles, :bubble_data
44

5-
MAX_BUBBLE = 10
6-
75
def settings
86
size(640, 360)
97
end
@@ -23,87 +21,27 @@ def draw
2321
end
2422

2523
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
24+
data = BubbleReader.new.read(data_path('data.yml')).fetch('emotions')
3225
@bubbles = data.map do |point|
33-
pos = point['position']
34-
Bubble.new(pos['x'], pos['y'], point['diameter'], point['label'])
26+
Bubble.new(point['position'], point['diameter'], point['label'])
3527
end
28+
@bubble_data = BubbleData.new(bubbles)
3629
end
3730

3831
def save_data
39-
# demonstrate how easy it is to create json object from a hash in ruby
40-
hash = bubble_data.to_hash
32+
# demonstrate how easy it is to create yaml object from a hash in ruby
33+
# you could easily store 'colors as well as 'emotions' bubbles
34+
writer = BubbleWriter.new(type: 'emotions', data: bubble_data.to_h)
4135
# overwite existing 'data.yml'
42-
open(data_path('data.yml'), 'w') { |file| file.write(hash.to_yaml) }
36+
writer.write(data_path('data.yml'))
4337
end
4438

4539
def mouse_pressed
4640
# create a new bubble instance, where mouse was clicked
4741
@bubble_data.add_bubble(
48-
Bubble.new(mouse_x, mouse_y, rand(40..80), 'new label')
42+
Bubble.new([mouse_x, mouse_y], rand(40..80), 'new label')
4943
)
5044
save_data
5145
# reload the yaml data from the freshly created file
5246
load_data
5347
end
54-
55-
# Bubble data holder
56-
class BubbleData
57-
attr_reader :name, :data
58-
def initialize(name = 'bubbles')
59-
@name = name
60-
@bubbles = []
61-
end
62-
63-
def add_bubble(bubble)
64-
bubbles << bubble
65-
bubbles.shift if bubbles.size > MAX_BUBBLE
66-
end
67-
68-
def extract_data(yaml)
69-
@bubbles = yaml[name]
70-
end
71-
72-
def to_hash
73-
{ name => bubbles.map(&:to_hash) }
74-
end
75-
end
76-
77-
# The Bubble class
78-
class Bubble
79-
attr_reader :x, :y, :diameter, :name, :over
80-
81-
def initialize(x, y, diameter, name)
82-
@x, @y, @diameter, @name = x, y, diameter, name
83-
@over = false
84-
end
85-
86-
def rollover(px, py)
87-
distance = dist px, py, x, y
88-
@over = (distance < diameter / 2.0)
89-
end
90-
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)
100-
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)