annotate gui/containergui_base.py @ 189:4381be70e1bb tip

Fixed incompatibility with latest changes to bgrease.
author Beliar <KarstenBock@gmx.net>
date Sun, 08 Apr 2012 20:30:43 +0200
parents 2241722311bf
children
rev   line source
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
1 # This program is free software: you can redistribute it and/or modify
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
2 # it under the terms of the GNU General Public License as published by
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
3 # the Free Software Foundation, either version 3 of the License, or
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
4 # (at your option) any later version.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
5
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
6 # This program is distributed in the hope that it will be useful,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
9 # GNU General Public License for more details.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
10
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
11 # You should have received a copy of the GNU General Public License
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
2
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
13 from copy import deepcopy
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
14 from types import StringTypes
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
15
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
16 from fife import fife
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
17 from fife.extensions import pychan
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
18
2
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
19 from parpg import vfs
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
20 from parpg.gui import drag_drop_data as data_drag
148
2241722311bf Moved the action module into the entities package.
KarstenBock@gmx.net
parents: 142
diff changeset
21 from parpg.entities.action import ACTIONS
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
22 from parpg.entities import General
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
23
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
24 class ContainerGUIBase(object):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
25 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
26 Base class for windows that show the content of a container
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
27 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
28
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
29
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
30 def __init__(self, controller, gui_file):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
31 self.controller = controller
118
30dbbae653ad If gui_file, in the ContainerGUIBase constructor, is a widget self.gui will now be set to it.
KarstenBock@gmx.net
parents: 2
diff changeset
32 if isinstance(gui_file, pychan.Widget):
30dbbae653ad If gui_file, in the ContainerGUIBase constructor, is a widget self.gui will now be set to it.
KarstenBock@gmx.net
parents: 2
diff changeset
33 self.gui = gui_file
30dbbae653ad If gui_file, in the ContainerGUIBase constructor, is a widget self.gui will now be set to it.
KarstenBock@gmx.net
parents: 2
diff changeset
34 elif isinstance(gui_file, StringTypes):
2
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
35 xml_file = vfs.VFS.open(gui_file)
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
36 self.gui = pychan.loadXML(xml_file)
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
37 else:
06145a6ee387 Fixed resource path dependencies issue that caused PARPG to crash on start.
M. George Hansen <technopolitica@gmail.com>
parents: 0
diff changeset
38 self.gui = pychan.loadXML(gui_file)
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
39
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
40 def dragDrop(self, obj):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
41 """Decide whether to drag or drop the image.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
42 @type obj: string
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
43 @param obj: The name of the object within
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
44 the dictionary 'self.buttons'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
45 @return: None"""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
46 if(data_drag.dragging == True):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
47 self.dropObject(obj)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
48 elif(data_drag.dragging == False):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
49 self.dragObject(obj)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
50
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
51 def dragObject(self, obj):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
52 """Drag the selected object.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
53 @type obj: string
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
54 @param obj: The name of the object within
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
55 the dictionary 'self.buttons'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
56 @return: None"""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
57 pass
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
58
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
59 def dropObject(self, obj):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
60 """Drops the object being dropped
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
61 @type obj: string
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
62 @param obj: The name of the object within
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
63 the dictionary 'self.buttons'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
64 @return: None"""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
65 pass
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
66
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
67
142
c28e29056218 Removed source_container value from data_drag.
KarstenBock@gmx.net
parents: 135
diff changeset
68 def setDragData(self, drag_item, down_image, up_image):
121
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
69 """Set the dragging data"""
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
70 # set the mouse cursor to be the widget's image
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
71 self.controller.setMouseCursor(up_image.source, down_image.source)
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
72 data_drag.dragged_item = drag_item
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
73 data_drag.dragged_image = up_image.source
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
74 data_drag.dragging = True
4826c8cbd7d8 Added setDragData method.
KarstenBock@gmx.net
parents: 118
diff changeset
75
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
76 def createMenuItems(self, item, actions):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
77 """Creates context menu items for all classes based on ContainerGUI"""
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
78 assert(isinstance(actions, dict))
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
79 assert(isinstance(item, General))
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
80 menu_actions = []
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
81 item_name = item.description.view_name
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
82 if not actions.has_key("Look"):
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
83 look_action = actions["Look"] = {}
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
84 look_action["text"] = item.description.desc
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
85 if item.container and not actions.has_key("ExamineContents"):
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
86 actions["ExamineContents"] = {}
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
87 for action_name in actions:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
88 display_name = action_name
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
89 if action_name in ACTIONS:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
90 param_dict = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
91 param_dict["controller"] = self.controller
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
92 param_dict["commands"] = {}
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
93 if action_name == "Look":
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
94 param_dict["examine_name"] = item_name
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
95 param_dict["examine_desc"] = actions[action_name].\
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
96 pop("text")
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
97 if action_name == "Read":
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
98 param_dict["text_name"] = item_name
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
99 param_dict["text"] = ""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
100 if action_name == "Use":
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
101 param_dict["item"] = item
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
102 display_name = actions[action_name].pop("text")
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
103 if action_name == "ExamineContents":
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
104 param_dict["container"] = item
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
105 display_name = "Examine Contents"
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
106 if action_name == "BrewBeer":
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
107 param_dict["pot"] = item
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
108 display_name = "Brew beer"
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
109 if actions[action_name]:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
110 param_dict.update(actions[action_name])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
111 menu_actions.append([action_name,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
112 display_name,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
113 self.executeMenuItem,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
114 ACTIONS[action_name]\
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
115 (**param_dict)])
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
116 return menu_actions
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
117
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
118 def showContextMenu(self, event, widget):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
119 """Decide whether to drag or drop the image.
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
120 @type obj: string
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
121 @param obj: The name of the object within
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
122 the dictionary 'self.buttons'
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
123 @return: None"""
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
124 if event.getButton() == event.RIGHT:
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
125 item = widget.item
135
9e86c37f5506 Added context menu to the inventory.
KarstenBock@gmx.net
parents: 130
diff changeset
126 if item:
9e86c37f5506 Added context menu to the inventory.
KarstenBock@gmx.net
parents: 130
diff changeset
127 if not isinstance(item, General):
9e86c37f5506 Added context menu to the inventory.
KarstenBock@gmx.net
parents: 130
diff changeset
128 item = item.entity
130
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
129 actions = {}
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
130 if item.usable:
aea5a9229b4c Context menus in container guis now work with components.
KarstenBock@gmx.net
parents: 121
diff changeset
131 actions = deepcopy(item.usable.actions)
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
132 x_pos, y_pos = widget.getAbsolutePos()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
133 x_pos += event.getX()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
134 y_pos += event.getY()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
135 menu_actions = self.createMenuItems(item, actions)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
136 self.controller.view.hud.hideContextMenu()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
137 self.controller.view.hud.showContextMenu(menu_actions,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
138 (x_pos,
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
139 y_pos)
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
140 )
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
141
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
142 def executeMenuItem(self, action):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
143 """Executes the items action
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
144 @param action: The action to run
148
2241722311bf Moved the action module into the entities package.
KarstenBock@gmx.net
parents: 142
diff changeset
145 @type action: Class derived from parpg.entities.action.Action
0
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
146 """
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
147 action.execute()
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
148
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
149 def updateImages(self):
7a89ea5404b1 Initial commit of parpg-core.
M. George Hansen <technopolitica@gmail.com>
parents:
diff changeset
150 pass