Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ../addons/eventscripts/_libs/python/es_menus.py
- # Python Native Imports
- import psyco
- psyco.full()
- # Local EventScripts Imports
- import es
- # Data Structures
- active_menus = {}
- # Error Classes
- class EsMenuError(Exception):
- pass
- # Private Classes
- class _Page(object):
- # Magic Class Methods
- def __init__(self):
- self.options = []
- self.text = ""
- self.valid_keys = "0"
- # Public Classes
- class Menu(object):
- # Magic Class Methods
- def __init__(self, callback=None):
- self._pages = [_Page()]
- self.callback = callback
- self.parent_menu = None
- self.title = ""
- self.description = ""
- # Private Class Methods
- def _build(self, page):
- total_pages = len(self._pages)
- page_instance = self._pages[page - 1]
- text = page_instance.text
- valid_keys = page_instance.valid_keys
- for header_line in (self.title, self.description,
- "Page [%s/%s]" %(page, total_pages)):
- text += header_line
- text += "\n"
- text += "------------------------------"
- text += "\n"
- number = 1
- for option in page_instance.options:
- if option.selectable:
- valid_keys += str(number)
- text += "->"
- text += "%s. %s" %(number, option.text)
- text += "\n"
- number += 1
- if number < 7:
- text += ("\n" * (7 - number))
- text += "------------------------------"
- text += "\n"
- if (page > 1 or self.parent_menu):
- valid_keys += "8"
- text += "->8. Back"
- text += " \n"
- if page < total_pages:
- valid_keys += "9"
- text += "->9. Next"
- text += " \n"
- text += "0. Exit"
- page_instance.text = text
- page_instance.valid_keys = valid_keys
- def _send(self, userid, page):
- page_instance = self._pages[page - 1]
- es.menu(0, userid, page_instance.text, page_instance.valid_keys)
- active_menus[userid] = (self, page)
- # Public Class Methods
- def append(option):
- if len(self._pages[-1].options) == 7:
- self._pages.append(_Page())
- self._pages[-1].options.append(option)
- option._menus.append((self, len(self._pages)))
- def send(userid, page=1):
- if not self._pages[page - 1].text:
- self._build(page)
- self._send(userid, page)
- return
- self._send(userid, page)
- def submenu(callback=None):
- menu = Menu(callback)
- menu.parent_menu = self
- return menu
- class Option(object):
- # Magic Class Methods
- def __init__(self, identifier, text, selectable=True):
- self._menus = []
- self.identifier = identifier
- self.text = text
- self.selectable = selectable
- def __setattr__(self, name, value):
- if name in self.__dict__:
- for menu, page in self._menus:
- page_instance = menu._pages[page - 1]
- page_instance.text = ""
- page_instance.valid_keys = "0"
- # Game Functions
- def player_disconnect(event_var):
- userid = int(event_var["userid"])
- if userid in active_menus:
- del active_menus[userid]
- es.addons.registerForEvent(__import__(__name__), "player_disconnect",
- player_disconnect)
- # Registered Filters
- def clientCommandFilter(userid, args):
- if not (args[0].lower() == "menuselect" or userid in active_menus or
- active_menus[userid]):
- return True
- menu, page = active_menus[userid]
- selection = args[1]
- if selection in (8, 9, 10):
- if selection == 8:
- if page > 1:
- menu.send(userid, page - 1)
- else:
- menu.previous_menu.send(userid)
- elif selection == 9:
- menu.send(userid, page + 1)
- else:
- active_menus[userid] = None
- return True
- callback = menu.callback
- if not callback:
- return True
- option = menu._pages[page - 1].options[selection - 1]
- callback({
- "identifier": option.identifier,
- "menu": menu,
- "number": selection,
- "option": option,
- "page": page})
- return True
- es.addons.registerClientCommandFilter(clientCommandFilter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement