From 69b304ef35bd2d680d4ab5ba67d6fb8815b887c1 Mon Sep 17 00:00:00 2001 From: Markus Meskanen Date: Wed, 29 Jun 2016 08:24:06 +0300 Subject: [PATCH 1/2] Added decorators for adding callbacks to menus This allows one to define the menu before its callbacks while keeping the code clean. --- .../packages/source-python/menus/base.py | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/addons/source-python/packages/source-python/menus/base.py b/addons/source-python/packages/source-python/menus/base.py index 16fc37169..74f0553ec 100644 --- a/addons/source-python/packages/source-python/menus/base.py +++ b/addons/source-python/packages/source-python/menus/base.py @@ -201,6 +201,35 @@ def _close(self, player_index): """ raise NotImplementedError + def register_select_callback(self, callback): + """Register a select callback for the menu. + + Can and should be used as a decorator. + + :param callable callback: A function that gets called + whenever a selection was made. + + The callback will recieve 3 parameters: + 1. The instance of this menu. + 2. The player's index who made the selection. + 3. The player's choice. + """ + self.select_callback = callback + + def register_build_callback(self, callback): + """Register a build callback for the menu. + + Can and should be used as a decorator. + + :param callable callback: A function that gets called + before a menu is displayed. + + The callback will recieve 2 parameters: + 1. The instance of this menu. + 2. The index of the player who will recieve this menu. + """ + self.build_callback = callback + class _MenuData(object): """Base class for menu data. From 342bed59725e22a938a9d78d5cbb55226644794d Mon Sep 17 00:00:00 2001 From: Markus Meskanen Date: Wed, 29 Jun 2016 08:26:31 +0300 Subject: [PATCH 2/2] Callback decorators now return the callback --- addons/source-python/packages/source-python/menus/base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/addons/source-python/packages/source-python/menus/base.py b/addons/source-python/packages/source-python/menus/base.py index 74f0553ec..7831f944b 100644 --- a/addons/source-python/packages/source-python/menus/base.py +++ b/addons/source-python/packages/source-python/menus/base.py @@ -215,6 +215,7 @@ def register_select_callback(self, callback): 3. The player's choice. """ self.select_callback = callback + return callback def register_build_callback(self, callback): """Register a build callback for the menu. @@ -229,6 +230,7 @@ def register_build_callback(self, callback): 2. The index of the player who will recieve this menu. """ self.build_callback = callback + return callback class _MenuData(object):