Popuplib.construct
|
[edit] Method: constructModule: popuplib [edit] Method Overview[edit] Table of Contents
[edit] DescriptionCreates a new easymenu type popup that is populated with items automatically based on the construct parameter, refined using flags. Popuplib comes with one construct type built-in, "players", which accepts all the same flags as playerlib.getPlayerList does, including custom registered filters. Selecting a menu item from this construct will have the selected userid returned as choice parameter on the menuselect function. More construct types can be registered by script addons, see below. The idea of construct popups is that you only need to create it once, usually in script loading method, and define which kind of content it will hold, for example "players". After that you can just simply send it to users and the contents will be automatically kept up to date with no further editing from the script's side needed. The code for players construct is below (edited for generality): def ConstructPlayerList(popupid, flags, menuselectfb): ''' Constructs a playerlist ''' def updatelisting(p): ''' Internal method that is called when the popup is displayed, the purpose is to update the menu contents, only if needed ''' playerlist = playerlib.getPlayerList(flags) useridlist = [int(x) for x in playerlist] if useridlist != p.construct_check: # player listing has changed since last update, redo the options: p.construct_check = useridlist langs = p.options.keys() mainopt = p.options[p.editlang] mainopt.clear() for player in playerlist: p.addoption(int(player), player.attributes['name']) for lang in langs: p.options[lang] = mainopt # return True as mark that the contents were changed return True else: # no modifications done, return False to let popuplib optimize speed return False # Creating the easymenu and returning it thenewmenu = popuplib.easymenu(popupid, None, menuselectfb) thenewmenu.cachemode = "construct" # IMPORTANT ! thenewmenu.construct = updatelisting # This is the function used to update thenewmenu.construct_check = None # This is the variable used to hold last state return thenewmenu # return the new menu to caller # register the construct to popuplib so it can be used from popuplib.construct: popuplib.registerConstruct('players', ConstructPlayerList)
[edit] Arguments
[edit] ExamplesThis example creates two player menus using construct, one with all players and another with only alive players. Anyone can call those menus up with !all or !alive in chat. import es import popuplib menus = {} def load(): menus['!all'] = popuplib.construct('all_players', 'players', '#all', player_selected) menus['!alive'] = popuplib.construct('alive_players', 'players', '#alive', player_selected) def es_player_chat(event_var): text = event_var['text'] if text in menus: menus[text].send(event_var['userid']) def player_selected(userid, choice, popupid): whodid = es.getplayername(userid) target = es.getplayername(choice) es.msg('%s selected %s from %s'%(whodid, target, popupid)) [edit] Notes
[edit] See Also |
|
