Sha256: cb55a3f019e2fe03c4c56996162893d4ffcb6e83cc284d3842db64321417a7ae

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

class Example
  attr_accessor :caption
  attr_reader :width, :height
  
  def initialize(width, height, *options)
    @width, @height = width, height
  end
  
  def draw
  end
  
  def update
  end
  
  def button_down(id)
  end
  
  def button_up(id)
  end
  
  def close
    # no-op, examples cannot close the containing window.
  end

  def self.current_source_file
    @current_source_file
  end
  
  def self.current_source_file=(current_source_file)
    @current_source_file = current_source_file
  end
  
  def self.inherited(subclass)
    @@examples ||= {}
    @@examples[subclass] = self.current_source_file
  end
  
  def self.examples
    @@examples.keys
  end
  
  def self.source_file
    @@examples[self]
  end
  
  def self.load_examples(pattern)
    Dir.glob(pattern) do |file| 
      begin
        # Remember which file we are loading.
        Example.current_source_file = File.expand_path(file)

        # Load the example in a sandbox module (second parameter to load()). This way, examples can
        # define classes and constants with the same names, and they will not collide.
        # 
        # load() does not let us refer to the anonymous module it creates, but we can enumerate all
        # loaded examples using Example.examples thanks to the "inherited" callback above.
        load file, true
      rescue Exception => e
        puts "*** Cannot load #{file}:"
        puts e
        puts
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gosu-examples-1.0.4 lib/gosu-examples/example.rb