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

Revision 312, 2.9 KB checked in by Kaydeth_parpg, 10 years ago (diff)

Ticket #2: Patch by Kaydeth. Updated the classes in console.py through engine.py (went through files alphabetically) to correct some obvious discrepancies with our coding standard. comment[s:trac, t:2]

  • 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
19import pychan
20
21class ContextMenu(object):
22    def __init__(self, engine, menu_items, pos):
23        """@type engine: engine.Engine
24           @param engine: An instance of the class Engine from engine.py
25           @type menu_items: list
26           @param menu_items: A list of items containing the name and
27                              text for the menu item and callback
28                              i.e. [["menu", "Some text",  Callback]
29           @type pos: (int, int)
30           @param pos: Screen position to use
31           @return: None"""
32
33        self.vbox = pychan.widgets.VBox(position=pos)
34        events_to_map = {}
35        for item in menu_items:
36            p = pychan.widgets.Button(name=item[0], text=unicode(item[1]))
37            self.vbox.addChild(p)
38            events_to_map [item[0]] = self.actionDecorator(*item[2:])
39        self.vbox.mapEvents(events_to_map)
40        self.show()
41   
42    def show(self):
43        """Shows the context menu"""
44        self.vbox.show()
45    def hide(self):
46        """Hides the context menu"""
47        self.vbox.hide()
48       
49    def actionDecorator (self,func, *args, **kwargs):
50        """This function is supposed to add some generic that should be
51        executed before and/or after an action is fired through the
52        context menu.
53       
54        @type func: Any callable
55        @param func: The original action function
56        @param args: Unpacked list of positional arguments
57        @param kwargs: Unpacked list of keyword arguments
58        @return: A wrapped version of func
59        """
60       
61        def decoratedFunc ():
62            """ This is the actual wrapped version of func, that is returned.
63            It takes no external arguments, so it can safely be passed around
64            as a callback."""
65            # some stuff that we do before the actual action is executed
66            self.hide()
67            # run the action function, and record the return value
68            ret_val = func (*args,**kwargs)
69            # we can eventually add some post-action code here, if needed (e.g. logging)
70            pass       
71            # return the value, as if the original function was called
72            return ret_val
73        return decoratedFunc
74       
Note: See TracBrowser for help on using the repository browser.