Class Gruff::Scene
In: lib/gruff/scene.rb
Parent: Gruff::Base
StackedMixin StackedBar SideStackedBar StackedArea AccumulatorBar Base Scene Pie Area PhotoBar Bullet Spider SideBar Net Bar Dot Line Pie Observable Group SideBar StandardError IncorrectNumberOfDatasetsException Magick Bar Layer BarConversion lib/gruff/stacked_area.rb lib/gruff/scene.rb lib/gruff/spider.rb lib/gruff/pie.rb lib/gruff/bullet.rb lib/gruff/area.rb lib/gruff/net.rb lib/gruff/bar_conversion.rb lib/gruff/side_bar.rb lib/gruff/bar.rb lib/gruff/line.rb lib/gruff/stacked_bar.rb lib/gruff/dot.rb lib/gruff/side_stacked_bar.rb lib/gruff/photo_bar.rb lib/gruff/base.rb lib/gruff/accumulator_bar.rb lib/gruff/mini/bar.rb lib/gruff/mini/side_bar.rb lib/gruff/mini/pie.rb Legend Mini Deprecated Gruff dot/m_17_0.png

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.

Methods

add_group   draw   layers=   method_missing   new   set_input  

Attributes

layers  [R]  An array listing the foldernames that will be rendered, from back to front.
 g.layers = %w(sky clouds buildings street people)

Public Class methods

[Source]

    # File lib/gruff/scene.rb, line 50
50:   def initialize(target_width, base_dir)
51:     @base_dir = base_dir
52:     @groups = {}
53:     @layers = []    
54:     super target_width
55:   end

Public Instance methods

[Source]

    # File lib/gruff/scene.rb, line 57
57:   def draw
58:     # Join all the custom paths and filter out the empty ones
59:     image_paths = @layers.map { |layer| layer.path }.select { |path| !path.empty? }
60:     images = Magick::ImageList.new(*image_paths)
61:     @base_image = images.flatten_images
62:   end

[Source]

    # File lib/gruff/scene.rb, line 64
64:   def layers=(ordered_list)
65:     ordered_list.each do |layer_name|
66:       @layers << Gruff::Layer.new(@base_dir, layer_name)
67:     end
68:   end

Group layers to input values

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

Set input values

 g.weather = "cloudy"

[Source]

    # File lib/gruff/scene.rb, line 78
78:   def method_missing(method_name, *args)
79:     case method_name.to_s
80:     when /^(\w+)_group=$/
81:       add_group $1, *args
82:       return
83:     when /^(\w+)=$/
84:       set_input $1, args.first
85:       return
86:     end
87:     super
88:   end

Private Instance methods

[Source]

    # File lib/gruff/scene.rb, line 92
92:   def add_group(input_name, layer_names)
93:     @groups[input_name] = Gruff::Group.new(input_name, @layers.select { |layer| layer_names.include?(layer.name) })
94:   end

[Source]

     # File lib/gruff/scene.rb, line 96
 96:   def set_input(input_name, input_value)
 97:     if not @groups[input_name].nil?
 98:       @groups[input_name].send_updates(input_value)
 99:     else
100:       if chosen_layer = @layers.detect { |layer| layer.name == input_name }
101:         chosen_layer.update input_value
102:       end
103:     end
104:   end

[Validate]