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.

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::DEFAULT_MARGIN, Base::DEFAULT_TARGET_WIDTH, Base::LABEL_MARGIN, Base::LEGEND_MARGIN

Instance Attribute Summary collapse

Attributes inherited from Base

#bold_title, #bottom_margin, #center_labels_over_point, #colors, #font, #font_color, #has_left_labels, #hide_legend, #hide_line_markers, #hide_line_numbers, #hide_title, #label_max_size, #label_stagger_height, #label_truncation_style, #labels, #left_margin, #legend_at_bottom, #legend_box_size, #legend_font_size, #legend_margin, #marker_color, #marker_count, #marker_font_size, #marker_shadow_color, #maximum_value, #minimum_value, #no_data_message, #right_margin, #sort, #sorted_drawing, #title, #title_font, #title_font_size, #title_margin, #top_margin, #use_data_label, #x_axis_increment, #x_axis_label, #y_axis_increment, #y_axis_label

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(target_width, base_dir) ⇒ Scene

Returns a new instance of Scene.



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

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"


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/gruff/scene.rb', line 73

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

Instance Attribute Details

#layersObject

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

Examples:

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


43
44
45
# File 'lib/gruff/scene.rb', line 43

def layers
  @layers
end

Instance Method Details

#drawObject



52
53
54
55
56
57
# File 'lib/gruff/scene.rb', line 52

def draw
  # Join all the custom paths and filter out the empty ones
  image_paths = @layers.map(&:path).reject(&:empty?)
  images = Magick::ImageList.new(*image_paths)
  Gruff::Renderer.background_image = images.flatten_images
end