Class: Gruff::Scene

Inherits:
Base
  • Object
show all
Defined in:
lib/gruff/scene.rb

Overview

A scene is a non-linear graph that assembles layers together to tell a story. Layers are folders with appropriately named files (see below). You can group layers and control them together or just set their values individually.

Examples:

  • A city scene that changes with the time of day and the weather conditions.

  • A traffic map that shows red lines on streets that are crowded and green on free-flowing ones.

Usage:

g = Gruff::Scene.new("500x100", "path/to/city_scene_directory")

# Define order of layers, back to front
g.layers = %w(background haze sky clouds)

# Define groups that will be controlled by the same input
g.weather_group = %w(clouds)
g.time_group = %w(background sky)

# Set values for the layers or groups
g.weather = "cloudy"
g.time = Time.now
g.haze = true

# Write the final graph to disk
g.write "hazy_daytime_city_scene.png"

There are several rules that will magically select a layer when possible.

  • Numbered files will be selected according to the closest value that is less than the input value.

  • ‘true.png’ and ‘false.png’ will be used as booleans.

  • Other named files will be used if the input matches the filename (without the filetype extension).

  • If there is a file named ‘default.png’, it will be used unless other input values are set for the corresponding layer.

Constant Summary

Constants inherited from Base

Base::DATA_COLOR_INDEX, Base::DATA_LABEL_INDEX, Base::DATA_VALUES_INDEX, Base::DEBUG, Base::DEFAULT_TARGET_WIDTH, Base::LEGEND_MARGIN

Instance Attribute Summary collapse

Attributes inherited from Base

#additional_line_values, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #labels, #left_margin, #legend_box_size, #legend_font_size, #marker_color, #marker_count, #marker_font_size, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #stacked, #title, #title_font_size, #top_margin, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

#add_color, #data, #initialize_ivars, #margins=, #replace_colors, #theme=, #theme_37signals, #theme_greyscale, #theme_keynote, #theme_odeo, #theme_pastel, #theme_rails_keynote, #to_blob, #write

Methods included from Deprecated

#graph_height, #graph_left, #graph_top, #graph_width, #scale_measurements, #total_height

Constructor Details

#initialize(target_width, base_dir) ⇒ Scene

Returns a new instance of Scene.



50
51
52
53
54
55
# File 'lib/gruff/scene.rb', line 50

def initialize(target_width, base_dir)
  @base_dir = base_dir
  @groups = {}
  @layers = []    
  super target_width
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Group layers to input values

g.weather_group = ["sky", "sea", "clouds"]

Set input values

g.weather = "cloudy"


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gruff/scene.rb', line 78

def method_missing(method_name, *args)
  case method_name.to_s
  when /^(\w+)_group=$/
    add_group $1, *args
    return
  when /^(\w+)=$/
    set_input $1, args.first
    return
  end
  super
end

Instance Attribute Details

#layersObject

An array listing the foldernames that will be rendered, from back to front.

g.layers = %w(sky clouds buildings street people)


48
49
50
# File 'lib/gruff/scene.rb', line 48

def layers
  @layers
end

Instance Method Details

#drawObject



57
58
59
60
61
62
# File 'lib/gruff/scene.rb', line 57

def draw
  # Join all the custom paths and filter out the empty ones
  image_paths = @layers.map { |layer| layer.path }.select { |path| !path.empty? }
  images = Magick::ImageList.new(*image_paths)
  @base_image = images.flatten_images
end