source: trunk/game/scripts/context_menu.py @ 270

Revision 158, 2.9 KB checked in by bretzel_parpg, 10 years ago (diff)

Completed a few blank entrys for the documentation

  • Property svn:eol-style set to native
Line 
1#!/usr/bin/python
2
3#   This file is part of PARPG.
4
5#   PARPG is free software: you can redistribute it and/or modify
6#   it under the terms of the GNU General Public License as published by
7#   the Free Software Foundation, either version 3 of the License, or
8#   (at your option) any later version.
9
10#   PARPG is distributed in the hope that it will be useful,
11#   but WITHOUT ANY WARRANTY; without even the implied warranty of
12#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13#   GNU General Public License for more details.
14
15#   You should have received a copy of the GNU General Public License
16#   along with PARPG.  If not, see <http://www.gnu.org/licenses/>.
17
18import fife, pychan
19
20class ContextMenu(object):
21    def __init__(self, engine, menu_items, pos):
22        """@type engine: engine.Engine
23           @param engine: An instance of the class Engine from engine.py
24           @type menu_items: list
25           @param menu_items: A list of items containing the name and
26                              text for the menu item and callback
27                              i.e. [["menu", "Some text",  Callback]
28           @type pos: (int, int)
29           @param pos: Screen position to use
30           @return: None"""
31
32        self.vbox = pychan.widgets.VBox(position=pos)
33        events_to_map = {}
34        for item in menu_items:
35            p = pychan.widgets.Button(name=item[0], text=unicode(item[1]))
36            self.vbox.addChild(p)
37            events_to_map [item[0]] = self.action_decorator(*item[2:])
38        self.vbox.mapEvents(events_to_map)
39        self.show()
40   
41    def show(self):
42        """Shows the context menu"""
43        self.vbox.show()
44    def hide(self):
45        """Hides the context menu"""
46        self.vbox.hide()
47       
48    def action_decorator (self,func, *args, **kwargs):
49        """This function is supposed to add some generic that should be
50        executed before and/or after an action is fired through the
51        context menu.
52       
53        @type func: Any callable
54        @param func: The original action function
55        @param args: Unpacked list of positional arguments
56        @param kwargs: Unpacked list of keyword arguments
57        @return: A wrapped version of func
58        """
59       
60        def decorated_func ():
61            """ This is the actual wrapped version of func, that is returned.
62            It takes no external arguments, so it can safely be passed around
63            as a callback."""
64            # some stuff that we do before the actual action is executed
65            self.hide()
66            # run the action function, and record the return value
67            ret_val = func (*args,**kwargs)
68            # we can eventually add some post-action code here, if needed (e.g. logging)
69            pass       
70            # return the value, as if the original function was called
71            return ret_val
72        return decorated_func
73       
Note: See TracBrowser for help on using the repository browser.