comparison clients/editor/plugins/objectselector.py @ 192:bec4b69ad83a

* Redid the editor's ObjectSelector to display previews for all objects instead of only 1 preview + paths/filenames * ImageButton now takes GuiImage's as argument for up/down/hover image, like icon does. * Tab cleanup
author nihathrael@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 28 Feb 2009 22:33:21 +0000
parents afdca61c314b
children 9631a2958851
comparison
equal deleted inserted replaced
191:c725ecb2ce4c 192:bec4b69ad83a
1 # coding: utf-8 1 # coding: utf-8
2 2
3 from pychan import widgets 3 from pychan import widgets, tools
4 import fife 4 import fife
5 5
6 class ObjectSelector(object): 6 class ObjectSelector(object):
7 def __init__(self,engine,map,selectNotify): 7 """The ObjectSelector class offers a gui Widget that let's you select the object you
8 wish to use to in the editor.
9 @param engine: fife instance
10 @param map: fife.Map instance containing your map
11 @param selectNotify: callback function used to tell the editor you selected an object.
12 """
13 def __init__(self, engine, map, selectNotify):
8 self.engine = engine 14 self.engine = engine
9 self.map = map 15 self.map = map
10 self.notify = selectNotify 16 self.notify = selectNotify
11 17
12 self.buildGui() 18 self.buildGui()
13 19
14 def buildGui(self): 20 def buildGui(self):
21 # Create the main Window
15 self.gui = widgets.Window(title="Object selector") 22 self.gui = widgets.Window(title="Object selector")
23 hbox = widgets.HBox()
24 self.gui.addChild(hbox)
25
26 # Add the Scrollarea with list of namespaces
27 scrollArea = widgets.ScrollArea(size=(200,300))
28 hbox.addChild(scrollArea)
29 self.namespaces = widgets.ListBox()
30 self.namespaces.capture(self.fillList)
31 self.namespaces.items = self.engine.getModel().getNamespaces()
32 self.namespaces.selected = 0
33 scrollArea.addChild(self.namespaces)
34
35 # This Vbox is used to display the preview images
36 scrollArea = widgets.ScrollArea(size=(200,300))
37 self.objects = widgets.VBox(size=(200,300))
38 scrollArea.addChild(self.objects)
39 hbox.addChild(scrollArea)
40
41 # Add another Hbox to hold the close button
16 hbox = widgets.HBox(parent=self.gui) 42 hbox = widgets.HBox(parent=self.gui)
17 self.gui.addChild(hbox) 43 self.gui.addChild(hbox)
18 scrollArea = widgets.ScrollArea(parent=hbox,size=(200,300)) 44 hbox.addSpacer(widgets.Spacer())
19 hbox.addChild(scrollArea) 45 closeButton = widgets.Button(text="Close")
20 self.namespaces = widgets.ListBox(parent=scrollArea) 46 closeButton.capture(self.hide)
21 scrollArea.content = self.namespaces 47 hbox.addChild(closeButton)
22 scrollArea = widgets.ScrollArea(parent=hbox,size=(200,300))
23 hbox.addChild(scrollArea)
24 self.objects = widgets.ListBox(parent=scrollArea)
25 scrollArea.content = self.objects
26 scrollArea = widgets.ScrollArea(parent=hbox, size=(200,300))
27 hbox.addChild(scrollArea)
28 self.preview = widgets.Icon()
29 scrollArea.content = self.preview
30 48
31 hbox = widgets.HBox(parent=self.gui)
32 self.gui.addChild(hbox)
33 hbox.addSpacer( widgets.Spacer() )
34 closeButton = widgets.Button(parent=hbox,text="Close")
35 hbox.addChild( closeButton )
36 closeButton.capture(self.hide)
37
38 self.namespaces.capture(self.updateObjects)
39 self.namespaces.items = self.engine.getModel().getNamespaces()
40 self.namespaces.selected = 0
41 self.updateObjects()
42
43 self.objects.capture(self.objectSelected)
44 49
45 def update(self): 50 def update(self):
46 self.namespaces.items = self.engine.getModel().getNamespaces() 51 self.namespaces.items = self.engine.getModel().getNamespaces()
47 self.namespaces.selected = 0 52 self.namespaces.selected = 0
48 self.updateObjects() 53 self.fillList()
49
50 def updateObjects(self):
51 if not self.namespaces.selected_item: return
52 54
53 self.objects.items = [obj.getId() for obj in self.engine.getModel().getObjects(self.namespaces.selected_item)] 55 def fillList(self):
54 if not self.objects.selected_item: 56 for obj in self.engine.getModel().getObjects(self.namespaces.selected_item):
55 self.objects.selected = 0 57 # Find visual for the object
56 self.objectSelected() 58 visual = None
59 try:
60 visual = obj.get2dGfxVisual()
61 except:
62 print 'Visual Selection created for type without a visual?'
63 raise
57 64
58 def objectSelected(self): 65 # Try to find a usable image
59 if not self.objects.selected_item: return 66 index = visual.getStaticImageIndexByAngle(0)
60 object = self.engine.getModel().getObject(self.objects.selected_item, self.namespaces.selected_item)
61 self.notify(object)
62 self._refreshPreview(object)
63 67
64 def _refreshPreview(self, object): 68 # if no static image available, try default action
65 visual = None 69 if index == -1:
66 70 action = obj.getDefaultAction()
67 try: 71 if action:
68 visual = object.get2dGfxVisual() 72 animation_id = action.get2dGfxVisual().getAnimationIndexByAngle(0)
69 except: 73 animation = self.engine.getAnimationPool().getAnimation(animation_id)
70 print 'Visual Selection created for type without a visual?' 74 image = animation.getFrameByTimestamp(0)
71 raise 75 index = image.getPoolId()
72 76
73 index = visual.getStaticImageIndexByAngle(0) 77 # Use the hopefully found image to set up the imagebutton
74 if index == -1: 78 if index != -1:
75 # no static image available, try default action 79 image = fife.GuiImage(index, self.engine.getImagePool())
76 action = object.getDefaultAction() 80 imagebutton = widgets.ImageButton(up_image=image, down_image=image, hover_image=image)
77 if action: 81 imagebutton.capture(tools.callbackWithArguments(self.objectSelected, obj))
78 animation_id = action.get2dGfxVisual().getAnimationIndexByAngle(0) 82 self.objects.addChild(imagebutton)
79 animation = self.engine.getAnimationPool().getAnimation(animation_id) 83 self.objects._recursiveResizeToContent()
80 image = animation.getFrameByTimestamp(0) 84 self.gui.adaptLayout()
81 index = image.getPoolId() 85 else:
82 86 print 'No image available for selected object'
83 if index != -1: 87
84 image = fife.GuiImage(index, self.engine.getImagePool()) 88 def objectSelected(self, obj):
85 self.preview.image = image 89 """This is used as callback function to notify the editor that a new object has
86 self.gui.adaptLayout() 90 been selected.
87 else: 91 @param obj: fife.Object instance"""
88 print 'No image available for selected object' 92 self.notify(obj)
89 93
90
91 def show(self): 94 def show(self):
92 self.update() 95 self.update()
93 self.gui.show() 96 self.gui.show()
97
94 def hide(self): 98 def hide(self):
95 self.gui.hide() 99 self.gui.hide()