class Cura::Application

An application.

Attributes

cursor[R]

Get the text cursor.

@return [Cursor]

dispatcher[R]

Get the event dispatcher.

@return [Event::Dispatcher]

pencil[R]

Get the pencil used for drawing.

@return [Pencil]

Public Class Methods

new(attributes={}) click to toggle source
Calls superclass method Cura::Attributes::HasEvents::new
# File lib/cura/application.rb, line 48
def initialize(attributes={})
  super

  # setup_adapter

  @running = false
  @cursor = Cursor.new(application: self)
  @pencil = Pencil.new

  setup_adapter
  setup_dispatcher
end
run(attributes={}) click to toggle source
# File lib/cura/application.rb, line 38
def run(attributes={})
  new(attributes).run
end

Public Instance Methods

add_window(window) click to toggle source

Add a window to this application.

@param [Window] window @return [Window]

Calls superclass method Cura::Attributes::HasWindows#add_window
# File lib/cura/application.rb, line 170
def add_window(window)
  super

  window.application = self

  window
end
dispatch_event(event, options={}) click to toggle source

Dispatch an event.

@param [#to_sym] event The name of the event class to create an instance of or an event instance. @param [#to_hash, to_h] options @option options [#to_i] :target The optional target of the event. @return [Event::Base] The dispatched event.

# File lib/cura/application.rb, line 162
def dispatch_event(event, options={})
  @dispatcher.dispatch_event(event, options)
end
draw() click to toggle source

Draw all windows.

@return [Application]

# File lib/cura/application.rb, line 191
def draw
  draw_windows

  self
end
focus(component) click to toggle source

Set focus to a component.

There can only be one component focused at a time within an application, if any. All dispatched events are sent to the currently focused component, or the application if no component is focused.

@param [nil, Component::Base] component @return [Component::Base]

# File lib/cura/application.rb, line 146
def focus(component)
  raise TypeError, "component must be nil or be a Cura::Component::Base" unless component.nil? || component.is_a?(Cura::Component::Base)

  dispatch_event(:unfocus)
  @dispatcher.target = component
  dispatch_event(:focus)

  component
end
focused() click to toggle source

Get the currently focused component.

@return [Component::Base]

# File lib/cura/application.rb, line 135
def focused
  @dispatcher.target
end
inspect() click to toggle source

Instance inspection.

@return [String]

# File lib/cura/application.rb, line 200
def inspect
  "#<#{self.class}>"
end
run() click to toggle source

Run this application.

@return [Application] This application.

# File lib/cura/application.rb, line 93
def run
  run_event_loop

  self
ensure
  unless @cleaned
    @adapter.cleanup
    @cleaned = true
  end
end
running?() click to toggle source

Check if this application is running.

@return [Boolean]

# File lib/cura/application.rb, line 128
def running?
  @running
end
stop() click to toggle source

Stop the application after the current run cycle.

@return [Application] This application.

# File lib/cura/application.rb, line 107
def stop
  @running = false

  self
end
stop!() click to toggle source

Stop the application immediently.

@return [Application] This application.

# File lib/cura/application.rb, line 116
def stop!
  @running = false

  @adapter.cleanup
  @cleaned = true

  self
end
update() click to toggle source

Update all windows.

@return [Application]

# File lib/cura/application.rb, line 181
def update
  update_windows
  cursor.update

  self
end

Protected Instance Methods

run_event_loop() click to toggle source
# File lib/cura/application.rb, line 239
def run_event_loop
  @running = true

  while @running
    update
    draw
    dispatcher.run
  end
end
setup_adapter() click to toggle source
# File lib/cura/application.rb, line 206
def setup_adapter
  if @adapter.nil?
    adapter_class ||= Adapter.all.first
    raise Error::InvalidAdapter if adapter_class.nil?

    @adapter = adapter_class.new
  end

  # TODO: If a class is given, run .new on it first
  # TODO: Adapter.name(*arguments) which defaults to an underscore'd version of the class constant
  # TODO: Adapter.find_by_name
  # TODO: Use Adapter.find_by_name here to allow passing of a Symbol to #adapter=

  @adapter.setup
end
setup_dispatcher() click to toggle source
# File lib/cura/application.rb, line 222
def setup_dispatcher
  @dispatcher = Event::Dispatcher.new(application: self)

  @dispatcher.middleware << Event::Middleware::Aimer::MouseFocus.new
  @dispatcher.middleware << Event::Middleware::Aimer::TargetOption.new
  @dispatcher.middleware << Event::Middleware::Aimer::DispatcherTarget.new
  @dispatcher.middleware << Event::Middleware::Dispatch.new
  @dispatcher.middleware << Event::Middleware::Translator::MouseClick.new
end
validate_adapter(adapter) click to toggle source
# File lib/cura/application.rb, line 232
def validate_adapter(adapter)
  # TODO: Raise error if ever set more than once
  raise Error::InvalidAdapter unless adapter.is_a?(Cura::Adapter)

  adapter
end