comparison clients/editor/plugins/objectselector.py @ 194:9631a2958851

* Object selector can now be toggled to use either the old list format or show preview images. * Fixed a small bug in ScrollArea.removeChild() fife.ScrollArea.setContent(None) does not work, so the client would crash on calling that function. I now set an empty pychan.Container. There might be a better way to do this.
author nihathrael@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 01 Mar 2009 10:59:17 +0000
parents bec4b69ad83a
children 13e5be34484e
comparison
equal deleted inserted replaced
193:9661d611b346 194:9631a2958851
12 """ 12 """
13 def __init__(self, engine, map, selectNotify): 13 def __init__(self, engine, map, selectNotify):
14 self.engine = engine 14 self.engine = engine
15 self.map = map 15 self.map = map
16 self.notify = selectNotify 16 self.notify = selectNotify
17 self.mode = 'list' # Other mode is 'preview'
17 18
18 self.buildGui() 19 self.buildGui()
19 20
20 def buildGui(self): 21 def buildGui(self):
21 # Create the main Window 22 # Create the main Window
25 26
26 # Add the Scrollarea with list of namespaces 27 # Add the Scrollarea with list of namespaces
27 scrollArea = widgets.ScrollArea(size=(200,300)) 28 scrollArea = widgets.ScrollArea(size=(200,300))
28 hbox.addChild(scrollArea) 29 hbox.addChild(scrollArea)
29 self.namespaces = widgets.ListBox() 30 self.namespaces = widgets.ListBox()
30 self.namespaces.capture(self.fillList) 31 self.namespaces.capture(self.update)
31 self.namespaces.items = self.engine.getModel().getNamespaces() 32 self.namespaces.items = self.engine.getModel().getNamespaces()
32 self.namespaces.selected = 0 33 self.namespaces.selected = 0
33 scrollArea.addChild(self.namespaces) 34 scrollArea.addChild(self.namespaces)
34 35
35 # This Vbox is used to display the preview images 36 # This Vbox is used to display the preview images
37 self.mainScrollArea = widgets.ScrollArea(size=(200,300))
38 self.objects = None
39 if self.mode == 'list':
40 self.setTextList()
41 else: # Assuming self.mode is 'preview'
42 self.setImageList()
43 hbox.addChild(self.mainScrollArea)
44
45 # This is the preview area
36 scrollArea = widgets.ScrollArea(size=(200,300)) 46 scrollArea = widgets.ScrollArea(size=(200,300))
37 self.objects = widgets.VBox(size=(200,300))
38 scrollArea.addChild(self.objects)
39 hbox.addChild(scrollArea) 47 hbox.addChild(scrollArea)
48 self.preview = widgets.Icon()
49 scrollArea.addChild(self.preview)
40 50
41 # Add another Hbox to hold the close button 51 # Add another Hbox to hold the close button
42 hbox = widgets.HBox(parent=self.gui) 52 hbox = widgets.HBox(parent=self.gui)
43 self.gui.addChild(hbox) 53 self.gui.addChild(hbox)
44 hbox.addSpacer(widgets.Spacer()) 54 hbox.addSpacer(widgets.Spacer())
55 toggleButton = widgets.Button(text="Toggle Preview Mode")
56 toggleButton.capture(self.toggleMode)
57 hbox.addChild(toggleButton)
45 closeButton = widgets.Button(text="Close") 58 closeButton = widgets.Button(text="Close")
46 closeButton.capture(self.hide) 59 closeButton.capture(self.hide)
47 hbox.addChild(closeButton) 60 hbox.addChild(closeButton)
48 61
62 def toggleMode(self):
63 if self.mode == 'list':
64 self.setImageList()
65 self.mode = 'preview'
66 elif self.mode == 'preview':
67 self.setTextList()
68 self.mode = 'list'
69 self.update()
49 70
50 def update(self):
51 self.namespaces.items = self.engine.getModel().getNamespaces()
52 self.namespaces.selected = 0
53 self.fillList()
54 71
55 def fillList(self): 72 def setImageList(self):
73 """Sets the mainScrollArea to contain a Vbox that can be used to fill in
74 preview Images"""
75 if self.objects is not None:
76 self.mainScrollArea.removeChild(self.objects)
77 self.objects = widgets.VBox(name='list', size=(200,300))
78 self.mainScrollArea.addChild(self.objects)
79
80 def setTextList(self):
81 """Sets the mainScrollArea to contain a List that can be used to fill in
82 Object names/paths"""
83 if self.objects is not None:
84 self.mainScrollArea.removeChild(self.objects)
85 self.objects = widgets.ListBox(name='list')
86 self.objects.capture(self.listEntrySelected)
87 self.mainScrollArea.addChild(self.objects)
88
89 def fillTextList(self):
90 if self.namespaces.selected_item:
91 self.objects.items = [obj.getId() for obj in self.engine.getModel().getObjects(self.namespaces.selected_item)]
92 if not self.objects.selected_item:
93 self.objects.selected = 0
94
95 def listEntrySelected(self):
96 """This function is used as callback for the TextList."""
97 if self.objects.selected_item:
98 obj = self.engine.getModel().getObject(self.objects.selected_item, self.namespaces.selected_item)
99 self.objectSelected(obj)
100
101 def fillPreviewList(self):
56 for obj in self.engine.getModel().getObjects(self.namespaces.selected_item): 102 for obj in self.engine.getModel().getObjects(self.namespaces.selected_item):
57 # Find visual for the object 103 image = self._getImage(obj)
58 visual = None 104 if image is not None:
59 try:
60 visual = obj.get2dGfxVisual()
61 except:
62 print 'Visual Selection created for type without a visual?'
63 raise
64
65 # Try to find a usable image
66 index = visual.getStaticImageIndexByAngle(0)
67
68 # if no static image available, try default action
69 if index == -1:
70 action = obj.getDefaultAction()
71 if action:
72 animation_id = action.get2dGfxVisual().getAnimationIndexByAngle(0)
73 animation = self.engine.getAnimationPool().getAnimation(animation_id)
74 image = animation.getFrameByTimestamp(0)
75 index = image.getPoolId()
76
77 # Use the hopefully found image to set up the imagebutton
78 if index != -1:
79 image = fife.GuiImage(index, self.engine.getImagePool())
80 imagebutton = widgets.ImageButton(up_image=image, down_image=image, hover_image=image) 105 imagebutton = widgets.ImageButton(up_image=image, down_image=image, hover_image=image)
81 imagebutton.capture(tools.callbackWithArguments(self.objectSelected, obj)) 106 imagebutton.capture(tools.callbackWithArguments(self.objectSelected, obj))
82 self.objects.addChild(imagebutton) 107 self.objects.addChild(imagebutton)
83 self.objects._recursiveResizeToContent() 108 self.objects._recursiveResizeToContent()
84 self.gui.adaptLayout() 109 self.gui.adaptLayout()
87 112
88 def objectSelected(self, obj): 113 def objectSelected(self, obj):
89 """This is used as callback function to notify the editor that a new object has 114 """This is used as callback function to notify the editor that a new object has
90 been selected. 115 been selected.
91 @param obj: fife.Object instance""" 116 @param obj: fife.Object instance"""
117 self.preview.image = self._getImage(obj)
92 self.notify(obj) 118 self.notify(obj)
119
120 def update(self):
121 self.namespaces.items = self.engine.getModel().getNamespaces()
122 self.namespaces.selected = 0
123 if self.mode == 'list':
124 self.fillTextList()
125 elif self.mode == 'preview':
126 self.fillPreviewList()
127
128 self.mainScrollArea.resizeToContent()
129
130 def _getImage(self, obj):
131 """ Returs an image for the given object.
132 @param: fife.Object for which an image is to be returned
133 @return: fife.GuiImage"""
134 visual = None
135 try:
136 visual = obj.get2dGfxVisual()
137 except:
138 print 'Visual Selection created for type without a visual?'
139 raise
140
141 # Try to find a usable image
142 index = visual.getStaticImageIndexByAngle(0)
143 image = None
144 # if no static image available, try default action
145 if index == -1:
146 action = obj.getDefaultAction()
147 if action:
148 animation_id = action.get2dGfxVisual().getAnimationIndexByAngle(0)
149 animation = self.engine.getAnimationPool().getAnimation(animation_id)
150 image = animation.getFrameByTimestamp(0)
151 index = image.getPoolId()
152
153 # Construct the new GuiImage that is to be returned
154 if index != -1:
155 image = fife.GuiImage(index, self.engine.getImagePool())
156
157 return image
158
93 159
94 def show(self): 160 def show(self):
95 self.update() 161 self.update()
96 self.gui.show() 162 self.gui.show()
97 163