Skip to content

Commit

Permalink
+ chained dialogues
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed May 9, 2011
1 parent c635e82 commit 0f4fc62
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 59 deletions.
1 change: 1 addition & 0 deletions lib/james.rb
Expand Up @@ -2,6 +2,7 @@ module James; end

require File.expand_path '../james/timer', __FILE__
require File.expand_path '../james/visitor', __FILE__
require File.expand_path '../james/visitors', __FILE__
require File.expand_path '../james/dialogues', __FILE__

require File.expand_path '../james/dialogue_api', __FILE__
Expand Down
25 changes: 0 additions & 25 deletions lib/james/builtin/main_dialogue.rb

This file was deleted.

30 changes: 18 additions & 12 deletions lib/james/controller.rb
Expand Up @@ -7,14 +7,18 @@
require File.expand_path '../outputs/audio', __FILE__
require File.expand_path '../outputs/terminal', __FILE__

require File.expand_path '../builtin/core_dialogue', __FILE__

module James

class Controller

attr_reader :visitor

def initialize
@visitor = initialize_dialogues.visitor
user_visitor = initialize_dialogues.visitor
system_visitor = Visitor.new CoreDialogue.new.state_for(:awake)
@visitor = Visitors.new system_visitor, user_visitor
end

def applicationDidFinishLaunching notification
Expand Down Expand Up @@ -80,29 +84,31 @@ def hear text
end
end
def expects
@visitor.expects
possibilities = @visitor.expects
puts "Possibilities:\n #{possibilities.join("\n ")}"
possibilities
end

def listen
app = NSApplication.sharedApplication
app.delegate = self

# window = NSWindow.alloc.initWithContentRect([200, 300, 300, 100],
# window = NSWindow.alloc.initWithContentRect([100, 300, 300, 100],
# styleMask:NSTitledWindowMask|NSClosableWindowMask|NSMiniaturizableWindowMask,
# backing:NSBackingStoreBuffered,
# defer:false)
# window.title = 'MacRuby: The Definitive Guide'
# window.title = 'James Debug/Info'
# window.level = 3
# window.delegate = app.delegate

# @button = NSButton.alloc.initWithFrame([10, 10, 400, 10])
# @button.bezelStyle = 4
# @button.title = ''
# @button.target = app.delegate
# @button.action = 'say_hello:'
#
# button = NSButton.alloc.initWithFrame([80, 10, 120, 80])
# button.bezelStyle = 4
# button.title = 'Hello World!'
# button.target = app.delegate
# button.action = 'say_hello:'
#
# window.contentView.addSubview(button)
#
# window.contentView.addSubview(@button)

# window.display
# window.orderFrontRegardless

Expand Down
2 changes: 1 addition & 1 deletion lib/james/dialogue_internals.rb
Expand Up @@ -11,7 +11,7 @@ module Dialogue

def self.included into
into.extend ClassMethods
Dialogues << into
Dialogues << into unless into == CoreDialogue # TODO Dirty as hell.
end

#
Expand Down
15 changes: 0 additions & 15 deletions lib/james/main_dialogue.rb

This file was deleted.

9 changes: 9 additions & 0 deletions lib/james/state_api.rb
Expand Up @@ -58,6 +58,15 @@ def exit &block
@exit_block = block
end

# By default, a state is not chainable.
#
def chainable?
@chainable
end
def chainable
@chainable = true
end

# Description of self using name and transitions.
#
def to_s
Expand Down
9 changes: 9 additions & 0 deletions lib/james/visitor.rb
Expand Up @@ -66,6 +66,15 @@ def hears? phrase
def expects
current.phrases
end
# Does the current state allow penetration into another dialogue?
#
def chainable?
current.chainable?
end

def to_s
"#{self.class.name}(#{initial}, current: #{current})"
end

end

Expand Down
22 changes: 16 additions & 6 deletions lib/james/visitors.rb
Expand Up @@ -28,23 +28,33 @@ def initialize *visitors
# After that, all remaining visitors are reset.
#
def hear phrase, &block
iterator = visitors.each
enumerator = visitors.dup

while visitor = iterator.next
while visitor = enumerator.shift
visitor.hear phrase, &block and break
end

while visitor = iterator.next
while visitor = enumerator.shift
visitor.reset
end
rescue StopIteration
# That's ok! Nothing to do here.
end

# Enter enters the first dialogue.
#
def enter
visitors.first.enter
end

# Simply returns the sum of what phrases all dialogues expect.
#
# Stops as soon as a visitor is not in a chainable state anymore.
#
def expects
visitors.inject([]) { |expects, visitor| expects + visitor.expects }
visitors.inject([]) do |expects, visitor|
total = expects + visitor.expects
break total unless visitor.chainable?
total
end
end

end
Expand Down

0 comments on commit 0f4fc62

Please sign in to comment.