### Home Classes Methods

### In Files

- lib/rubygame/queue.rb

### Methods

- ::new
- #deliver
- #list
- #push
- #setup
- #subscribe
- #subscribed?
- #unsubscribe

### Files

- CREDITS
- LICENSE
- NEWS
- README
- ROADMAP
- custom_sdl_load_paths.rdoc
- getting_started.rdoc
- keyboard_symbols.rdoc
- macosx_install.rdoc
- managing_framerate.rdoc
- surface_palettes.rdoc
- windows_install.rdoc

### Class/Module Index

Quicksearch

- Rubygame
- Rubygame::ActiveEvent
- Rubygame::Clock
- Rubygame::Color
- Rubygame::Color::ColorBase
- Rubygame::Color::ColorHSL
- Rubygame::Color::ColorHSV
- Rubygame::Color::ColorRGB
- Rubygame::Color::Palette
- Rubygame::Event
- Rubygame::EventActions
- Rubygame::EventActions::BlockAction
- Rubygame::EventActions::MethodAction
- Rubygame::EventActions::MultiAction
- Rubygame::EventHandler
- Rubygame::EventHandler::HasEventHandler
- Rubygame::EventHook
- Rubygame::EventQueue
- Rubygame::EventTriggers
- Rubygame::EventTriggers::AndTrigger
- Rubygame::EventTriggers::AttrTrigger
- Rubygame::EventTriggers::BlockTrigger
- Rubygame::EventTriggers::InstanceOfTrigger
- Rubygame::EventTriggers::KeyPressTrigger
- Rubygame::EventTriggers::KeyReleaseTrigger
- Rubygame::EventTriggers::KindOfTrigger
- Rubygame::EventTriggers::MouseMoveTrigger
- Rubygame::EventTriggers::MousePressTrigger
- Rubygame::EventTriggers::MouseReleaseTrigger
- Rubygame::EventTriggers::OrTrigger
- Rubygame::EventTriggers::TickTrigger
- Rubygame::EventTriggers::YesTrigger
- Rubygame::Events
- Rubygame::Events::ClockTicked
- Rubygame::Events::InputFocusGained
- Rubygame::Events::InputFocusLost
- Rubygame::Events::JoystickAxisMoved
- Rubygame::Events::JoystickBallMoved
- Rubygame::Events::JoystickButtonEvent
- Rubygame::Events::JoystickButtonPressed
- Rubygame::Events::JoystickButtonReleased
- Rubygame::Events::JoystickHatMoved
- Rubygame::Events::KeyPressed
- Rubygame::Events::KeyReleased
- Rubygame::Events::KeyboardEvent
- Rubygame::Events::MouseButtonEvent
- Rubygame::Events::MouseFocusGained
- Rubygame::Events::MouseFocusLost
- Rubygame::Events::MouseMoved
- Rubygame::Events::MousePressed
- Rubygame::Events::MouseReleased
- Rubygame::Events::QuitRequested
- Rubygame::Events::WindowExposed
- Rubygame::Events::WindowMinimized
- Rubygame::Events::WindowResized
- Rubygame::Events::WindowUnminimized
- Rubygame::ExposeEvent
- Rubygame::Ftor
- Rubygame::GL
- Rubygame::Hotspot
- Rubygame::JoyAxisEvent
- Rubygame::JoyBallEvent
- Rubygame::JoyDownEvent
- Rubygame::JoyHatEvent
- Rubygame::JoyUpEvent
- Rubygame::Joystick
- Rubygame::Key
- Rubygame::KeyDownEvent
- Rubygame::KeyUpEvent
- Rubygame::MailQueue
- Rubygame::MediaBag
- Rubygame::Mixer
- Rubygame::Mixer::Music
- Rubygame::Mixer::Sample
- Rubygame::Mouse
- Rubygame::MouseDownEvent
- Rubygame::MouseMotionEvent
- Rubygame::MouseUpEvent
- Rubygame::Music
- Rubygame::NamedResource
- Rubygame::NamedResource::NamedResourceClassMethods
- Rubygame::QuitEvent
- Rubygame::Rect
- Rubygame::ResizeEvent
- Rubygame::SDLError
- Rubygame::SFont
- Rubygame::Screen
- Rubygame::Sound
- Rubygame::Sprites
- Rubygame::Sprites::DepthSortGroup
- Rubygame::Sprites::Group
- Rubygame::Sprites::LimitGroup
- Rubygame::Sprites::Sprite
- Rubygame::Sprites::UpdateGroup
- Rubygame::Surface
- Rubygame::TTF

No matching classes.

# Rubygame::MailQueue

A mixin module to extend EventQueue with the ability to 'deliver' specific types of events to subscribed objects, a la a mailing list. Each object must subscribe for the classes of events it wishes to receive; when the MailQueue receives an event of that type, it will deliver it to the subscribers. See subscribe for more information.

Please note that if you extend an already-existing EventQueue object with this mixin module (rather than including it in a class), you must call setup before using the object. This will create the necessary internal variables for the MailQueue to work.

### Attributes

autodeliver[RW]

Whether to automatically deliver events as they are received. Enabled by default.

### Public Class Methods

new() click to toggle source

Create a new MailQueue object. Like EventQueue.new, this method will yield self if a block is given.

    # File lib/rubygame/queue.rb, line 204
    def initialize()
      setup()
      super
    end

### Public Instance Methods

deliver() click to toggle source

Deliver each pending event to all objects which are subscribed to that event class. Every client object MUST have a push method, or events can't be delivered to it, and it will become very lonely!

The queue will be cleared of all events after all deliveries are done.

    # File lib/rubygame/queue.rb, line 290
    def deliver()
      each() { |event|  deliver_event(event) }
      clear()
    end

list() click to toggle source

Returns an Array of all event classes which have at least one subscriber.

    # File lib/rubygame/queue.rb, line 216
    def list
      @subscribe.collect { |k, v|
        (v.length > 0) ? k : nil  rescue NoMethodError nil
      }.compact
    end

push(*args) click to toggle source

Append events to the queue. If @autodeliver is enabled, all events on the queue will be delivered to subscribed client objects immediately.

    # File lib/rubygame/queue.rb, line 297
    def push(*args)
      # Temporarily disable autofetch to avoid infinite loop
      a, @autofetch = @autofetch, false
      # Fetch once to emulate autofetch, if it was enabled before
      fetch_sdl_events() if a

      super
      deliver() if @autodeliver

      @autofetch = a
      return
    end

setup() click to toggle source

Create the necessary internal variables for the MailQueue.

    # File lib/rubygame/queue.rb, line 210
    def setup
      @subscribe = Hash.new
      @autodeliver = true
    end

subscribe(client,klass) click to toggle source

Subscribe `client` to receive events that match `klass`.

After the client object has been subscribed, the MailQueue will push along any event for which "klass === event" is true. This usually means that the event is an instance of klass or one of klass's child classes; however, note that klass may have changed its own #=== operator to have different behavior, so this is not always the case.

Important: the MailQueue uses the client's push method to deliver events! If the client does not have such a method, MailQueue will silently catch the error and move on to the next client.

A client object may be subscribed for many different types of events simultaneously, and more than one client object may be subscribed to any type of event (in which case each object will receive the event). A client may also be subscribed multiple times for the same type (in which case it will receive duplicate events). Likewise, the client will receive duplicates if it is subscribed to multiple classes which share ancestry, for example Numeric and Float.

If a client wishes to receive ALL types of events, it can subscribe to Object, which is a parent class of all objects.

If the queue's @autodeliver is true, it will deliver events to subscribers immediately after they are posted, rather than waiting for deliver to be called.

    # File lib/rubygame/queue.rb, line 248
    def subscribe(client,klass)
      @subscribe[klass] << client
    rescue NoMethodError
      @subscribe[klass] = [client] if @subscribe[klass].nil?
    end

subscribed?(client,klass) click to toggle source

Returns true if `client` is currently subscribed to receive events of type `klass`.

    # File lib/rubygame/queue.rb, line 256
    def subscribed?(client,klass)
      return true if @subscribe[klass].include?(client)  rescue NoMethodError
      return false
    end

unsubscribe(client,klass) click to toggle source

Unsubscribes the client to stop receiving events of type `klass`. It is safe (has no effect) to unsubscribe for an event type you are not subscribed to receive.

    # File lib/rubygame/queue.rb, line 264
    def unsubscribe(client,klass)
      @subscribe[klass] -= [client]  rescue NoMethodError
    ensure
      return
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.