editor.rb
znmud-0.0.1/lib/olc/editor.rb · 3.1 KB
From the archive znmud-minimal-0.0.1.tar.gz
Download the archive « Back to the archive contents
# File: editor.rb
# Author: Craig Smith
# This source code copyright (C) 2009 Craig Smith
# All rights reserved.
#
# Released under the terms of the GNU Public License
# See COPYING file for additional information.
#
require 'utility/log'
require 'olc/olcmenu'
require 'olc/olcmenuoption'
require 'olc/olceditor'
# Multi-line method editor. Wrapper for OlcEditor
class Editor
logger 'DEBUG'
attr_accessor :ch, :classname, :method, :editstr, :title, :menu, :menu_stack
# Initializes an OLC Object
def initialize(id, classname, method)
self.title = "OLC Multi-Line Editor (#{method})" if not title
self.ch = get_object(id)
self.classname = classname
self.method = method
if not classname.respond_to?(method) or not classname.respond_to?("#{method}=")
ch.sendto("ERROR: Invalid method (#{method}) for class #{classname}")
exit_olc(true)
end
self.editstr = classname.send(method)
if not editstr
self.editstr = String.new
elsif not editstr.is_a? String
ch.sendto("Method #{method} is not a String.")
exit_olc(true)
end
self.menu = olc_basemenu(title,self,nil)
self.menu_stack = []
ch.mode = :olc
ch.sendroom("#{ch.name} closes #{ch.pos_pronoun} eyes and begins concentrating...")
show_menu
end
# Creates a basic menu with just a Quit button
# [+title+] title of the menu
# [+calling+] Calling object (one who controls this menu)
# [+init+] Init function that sets up this menu
# [+reset+] Optional reset
# [+return+] basic menu object
def olc_basemenu(title,calling,init,reset=nil)
if reset == "reset"
m = menu
else
m = OlcMenu.new(title,calling,init)
end
m.add_editor_option("E","Edit", editstr, self, :edit_str)
m.add_text_option("X","Abort", self, :abort_menu)
m.add_text_option("Q","Quit", self, :quit_menu)
m
end
# Main command parsing routine. Overwrite with your object editor
# [+m+] command to parse
def parse(m)
return if not menu
msg = menu.parse(m)
if msg
if msg.size > 0
ch.sendto(msg)
else
show_menu
end
else
show_menu
end
end
# Add a submenu
# [+menu+] A created OlcMenu object
# [+reset+] Optional. If set ignore
def submenu(newmenu, reset=nil)
if not reset == "reset"
self.menu_stack.push menu
self.menu = newmenu
end
show_menu
end
# Quit the current menu. If last menu, exit OLC
# [+m+] ignored
def quit_menu(m)
if menu_stack.size > 0
self.menu = menu_stack.pop
menu.reset
show_menu
else
exit_olc
end
end
# Abort the editing
def abort_menu(m)
ch.sendto("Aborting...")
exit_olc(true)
end
# Exits the OLC session
def exit_olc(cancel=false)
ch.mode = :playing
ch.olc = nil
self.menu = nil
if not cancel and editstr.size > 0
# Strip off ALL \r\n at the end of sentences
self.editstr.gsub!(/([\r\n]+)$/,"")
classname.send("#{method}=", editstr)
ch.sendto("Saved #{method}.")
end
ch.sendroom("#{ch.name} has finished creating.")
end
# Updates the edited string
def edit_str(str)
self.editstr = str
show_menu
editstr
end
# Display the menu to the user
def show_menu
ch.sendto(menu.to_s)
end
def get_object(oid)
Engine.instance.db.get(oid)
end
end