|
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 |
12 | 10 |
|
13 | 11 | 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 |
18 | 15 | end |
19 | 16 |
|
20 | 17 | def draw |
21 | 18 | 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) } |
23 | 43 | end |
24 | 44 |
|
25 | 45 | def mouse_pressed |
26 | 46 | # 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 |
28 | 53 | end |
29 | 54 |
|
30 | | -# Bubble class can create and display bubble data from a yaml file |
| 55 | +# Bubble data holder |
31 | 56 | 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 |
35 | 62 |
|
36 | | - MAX_BUBBLE = 10 |
| 63 | + def add_bubble(bubble) |
| 64 | + bubbles << bubble |
| 65 | + bubbles.shift if bubbles.size > MAX_BUBBLE |
| 66 | + end |
37 | 67 |
|
38 | | - attr_reader :key |
39 | | - def initialize(key) |
40 | | - @bubbles = [] |
41 | | - @key = key |
42 | | - super |
| 68 | + def extract_data(yaml) |
| 69 | + @bubbles = yaml[name] |
43 | 70 | end |
44 | 71 |
|
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) } |
49 | 74 | end |
| 75 | +end |
| 76 | + |
| 77 | +# The Bubble class |
| 78 | +class Bubble |
| 79 | + attr_reader :x, :y, :diameter, :name, :over |
50 | 80 |
|
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 |
56 | 84 | end |
57 | 85 |
|
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) |
85 | 89 | end |
86 | 90 |
|
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) |
89 | 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