From 848f3295136755e96f50d6da459361130ea1cc77 Mon Sep 17 00:00:00 2001 From: Ahmed Charles Date: Sat, 17 Oct 2015 20:54:40 +0000 Subject: [PATCH] Add a handle method to react to user input. --- evennia/utils/evmenu.py | 88 +++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/evennia/utils/evmenu.py b/evennia/utils/evmenu.py index dd057d918..c3d7ea7fe 100644 --- a/evennia/utils/evmenu.py +++ b/evennia/utils/evmenu.py @@ -181,64 +181,25 @@ class EvMenuError(RuntimeError): class CmdEvMenuNode(Command): """ Menu options. - """ - key = "look" - aliases = ["l", _CMD_NOMATCH, _CMD_NOINPUT] + key = _CMD_NOINPUT + aliases = [_CMD_NOMATCH] locks = "cmd:all()" help_category = "Menu" def func(self): """ Implement all menu commands. - """ caller = self.caller menu = caller.ndb._menutree if not menu: err = "Menu object not found as %s.ndb._menutree!" % (caller) - self.caller.msg(err) + caller.msg(err) raise EvMenuError(err) - # flags and data - raw_string = self.raw_string - cmd = raw_string.strip().lower() - options = menu.options - allow_quit = menu.allow_quit - cmd_on_quit = menu.cmd_on_quit - default = menu.default - - if cmd in options: - # this will overload the other commands - # if it has the same name! - goto, callback = options[cmd] - if callback: - menu.callback(callback, raw_string) - if goto: - menu.goto(goto, raw_string) - elif cmd in ("look", "l"): - caller.msg(menu.nodetext) - elif cmd in ("help", "h"): - caller.msg(menu.helptext) - elif allow_quit and cmd in ("quit", "q", "exit"): - menu.close_menu() - if cmd_on_quit is not None: - caller.execute_cmd(cmd_on_quit) - elif default: - goto, callback = default - if callback: - menu.callback(callback, raw_string) - if goto: - menu.goto(goto, raw_string) - else: - caller.msg(_HELP_NO_OPTION_MATCH) - - if not (options or default): - # no options - we are at the end of the menu. - menu.close_menu() - if cmd_on_quit is not None: - caller.execute_cmd(cmd_on_quit) + menu.handle(self.raw_string) class EvMenuCmdSet(CmdSet): @@ -492,6 +453,47 @@ class EvMenu(object): return nodetext, options + def handle(self, raw_string): + # flags and data + caller = self._caller + cmd = raw_string.strip().lower() + options = self.options + allow_quit = self.allow_quit + cmd_on_quit = self.cmd_on_quit + default = self.default + + if cmd in options: + # this will overload the other commands + # if it has the same name! + goto, callback = options[cmd] + if callback: + self.callback(callback, raw_string) + if goto: + self.goto(goto, raw_string) + elif cmd in ("look", "l"): + caller.msg(self.nodetext) + elif cmd in ("help", "h"): + caller.msg(self.helptext) + elif allow_quit and cmd in ("quit", "q", "exit"): + self.close_menu() + if cmd_on_quit is not None: + caller.execute_cmd(cmd_on_quit) + elif default: + goto, callback = default + if callback: + self.callback(callback, raw_string) + if goto: + self.goto(goto, raw_string) + else: + caller.msg(_HELP_NO_OPTION_MATCH) + + if not (options or default): + # no options - we are at the end of the menu. + self.close_menu() + if cmd_on_quit is not None: + caller.execute_cmd(cmd_on_quit) + + def callback(self, nodename, raw_string): """ Run a node as a callback. This makes no use of the return