Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. # ../addons/eventscripts/_libs/python/es_menus.py
  2.  
  3. # Python Native Imports
  4. import psyco
  5. psyco.full()
  6.  
  7. # Local EventScripts Imports
  8. import es
  9.  
  10. # Data Structures
  11. active_menus = {}
  12.  
  13. # Error Classes
  14. class EsMenuError(Exception):
  15.     pass
  16.    
  17. # Private Classes
  18. class _Page(object):
  19.  
  20.     # Magic Class Methods
  21.     def __init__(self):
  22.         self.options = []
  23.         self.text = ""
  24.         self.valid_keys = "0"
  25.    
  26. # Public Classes
  27. class Menu(object):
  28.  
  29.     # Magic Class Methods
  30.     def __init__(self, callback=None):
  31.         self._pages = [_Page()]
  32.         self.callback = callback
  33.         self.parent_menu = None
  34.         self.title = ""
  35.         self.description = ""
  36.    
  37.     # Private Class Methods
  38.     def _build(self, page):
  39.         total_pages = len(self._pages)
  40.         page_instance = self._pages[page - 1]
  41.         text = page_instance.text
  42.         valid_keys = page_instance.valid_keys
  43.         for header_line in (self.title, self.description,
  44.                             "Page [%s/%s]" %(page, total_pages)):
  45.             text += header_line
  46.             text += "\n"
  47.         text += "------------------------------"
  48.         text += "\n"
  49.         number = 1
  50.         for option in page_instance.options:
  51.             if option.selectable:
  52.                 valid_keys += str(number)
  53.                 text += "->"
  54.             text += "%s. %s" %(number, option.text)
  55.             text += "\n"
  56.             number += 1
  57.         if number < 7:
  58.             text += ("\n" * (7 - number))
  59.         text += "------------------------------"
  60.         text += "\n"
  61.         if (page > 1 or self.parent_menu):
  62.             valid_keys += "8"
  63.             text += "->8. Back"
  64.         text += " \n"
  65.         if page < total_pages:
  66.             valid_keys += "9"
  67.             text += "->9. Next"
  68.         text += " \n"
  69.         text += "0. Exit"
  70.         page_instance.text = text
  71.         page_instance.valid_keys = valid_keys
  72.        
  73.     def _send(self, userid, page):
  74.         page_instance = self._pages[page - 1]
  75.         es.menu(0, userid, page_instance.text, page_instance.valid_keys)
  76.         active_menus[userid] = (self, page)
  77.    
  78.     # Public Class Methods
  79.     def append(option):
  80.         if len(self._pages[-1].options) == 7:
  81.             self._pages.append(_Page())
  82.         self._pages[-1].options.append(option)
  83.         option._menus.append((self, len(self._pages)))
  84.        
  85.     def send(userid, page=1):
  86.         if not self._pages[page - 1].text:
  87.             self._build(page)
  88.             self._send(userid, page)
  89.             return
  90.         self._send(userid, page)
  91.        
  92.     def submenu(callback=None):
  93.         menu = Menu(callback)
  94.         menu.parent_menu = self
  95.         return menu
  96.        
  97. class Option(object):
  98.  
  99.     # Magic Class Methods
  100.     def __init__(self, identifier, text, selectable=True):
  101.         self._menus = []
  102.         self.identifier = identifier
  103.         self.text = text
  104.         self.selectable = selectable
  105.        
  106.     def __setattr__(self, name, value):
  107.         if name in self.__dict__:
  108.             for menu, page in self._menus:
  109.                 page_instance = menu._pages[page - 1]
  110.                 page_instance.text = ""
  111.                 page_instance.valid_keys = "0"
  112.                
  113. # Game Functions
  114. def player_disconnect(event_var):
  115.     userid = int(event_var["userid"])
  116.     if userid in active_menus:
  117.         del active_menus[userid]
  118.        
  119. es.addons.registerForEvent(__import__(__name__), "player_disconnect",
  120.                            player_disconnect)
  121.  
  122. # Registered Filters        
  123. def clientCommandFilter(userid, args):
  124.     if not (args[0].lower() == "menuselect" or userid in active_menus or
  125.             active_menus[userid]):
  126.         return True
  127.     menu, page = active_menus[userid]
  128.     selection = args[1]
  129.     if selection in (8, 9, 10):
  130.         if selection == 8:
  131.             if page > 1:
  132.                 menu.send(userid, page - 1)
  133.             else:
  134.                 menu.previous_menu.send(userid)
  135.         elif selection == 9:
  136.             menu.send(userid, page + 1)
  137.         else:
  138.             active_menus[userid] = None
  139.         return True
  140.     callback = menu.callback
  141.     if not callback:
  142.         return True
  143.     option = menu._pages[page - 1].options[selection - 1]
  144.     callback({
  145.         "identifier": option.identifier,
  146.         "menu": menu,
  147.         "number": selection,
  148.         "option": option,
  149.         "page": page})
  150.     return True
  151.    
  152. es.addons.registerClientCommandFilter(clientCommandFilter)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement