comparison tools/editor/plugins/CameraEdit.py @ 378:64738befdf3b

bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 11 Jan 2010 23:34:52 +0000
parents
children
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 """ a tool for FIFEdit to edit camera attributes. It does not
25 currently support multiple cameras.
26 """
27
28 from fife import fife
29 from fife.extensions import pychan
30 import fife.extensions.pychan.widgets as widgets
31 from fife.extensions.pychan.tools import callbackWithArguments as cbwa
32
33 from fife.extensions.fife_timer import Timer
34
35 import scripts
36 import scripts.plugin as plugin
37 from scripts.events import *
38 from scripts.gui.action import Action
39
40
41 import os
42 try:
43 import xml.etree.cElementTree as ET
44 except:
45 import xml.etree.ElementTree as ET
46
47 import math
48
49
50 class CameraEdit(plugin.Plugin):
51
52 def __init__(self):
53 self._enabled = False
54
55 # Camera instance
56 self._camera = None
57
58 # Editor instance
59 self._editor = None
60
61 # Toolbar button to display Camera Editor
62 self._action_show = None
63
64 # GUI
65 self._container = None
66 self._ok_button = None
67 self._cancel_button = None
68
69 def enable(self):
70 """ plugin method """
71 if self._enabled is True:
72 return
73
74 self._editor = scripts.editor.getEditor()
75 #self._camera = self._editor.getActiveMapView().getCamera()
76 self._action_show = Action(u"Camera Editor", checkable=True)
77 scripts.gui.action.activated.connect(self.toggle, sender=self._action_show)
78 self._editor._tools_menu.addAction(self._action_show)
79
80 self._createGui()
81
82 self._enabled = True
83
84 def disable(self):
85 """ plugin method """
86 if self._enabled is False:
87 return
88
89 self._container.setDocked(False)
90 self._container.hide()
91
92 self._editor._tools_menu.removeAction(self._action_show)
93
94 self._enabled = False
95
96
97 def isEnabled(self):
98 """ plugin method """
99 return self._enabled;
100
101 def getName(self):
102 """ plugin method """
103 return "Camera Editor"
104
105 def toggle(self):
106 """ Toggles the cameratool visible / invisible and sets
107 dock status
108 """
109 if self._container.isVisible() or self._container.isDocked():
110 self._container.setDocked(False)
111 self._container.hide()
112
113 self._action_show.setChecked(False)
114 else:
115 self._container.show()
116 self.loadSettings()
117 self._action_show.setChecked(True)
118 self._adjustPosition()
119
120 def saveSettings(self):
121 engine = self._editor.getEngine()
122
123 id = self._container.collectData('idBox')
124 if id == '':
125 print 'Please enter a camera id.'
126 return
127
128 try:
129 map = engine.getModel().getMap(str(self._container.collectData('mapBox')))
130 except fife.Exception:
131 print 'Cannot find the specified map id.'
132 return
133
134 try:
135 layer = map.getLayer(str(self._container.collectData('layerBox')))
136 except fife.Exception:
137 print 'Cannot find the specified layer id.'
138 return
139
140 try:
141 vals = self._container.collectData('viewBox').split(',')
142 if len(vals) != 4:
143 raise ValueError
144
145 viewport = fife.Rect(*[int(c) for c in vals])
146 except ValueError:
147 print 'Please enter 4 comma (,) delimited values for viewport x,y,width,height.'
148 return
149
150 try:
151 refh = int(self._container.collectData('refhBox'))
152 refw = int(self._container.collectData('refwBox'))
153 except ValueError:
154 print 'Please enter positive integer values for reference width and height.'
155 return
156
157 try:
158 rot = int(self._container.collectData('rotBox'))
159 tilt = int(self._container.collectData('tiltBox'))
160 except ValueError:
161 print 'Please enter positive integer values for rotation and tilt.'
162 return
163
164 self._camera = self._editor.getActiveMapView().getCamera()
165 self._camera.setId(str(id))
166 self._camera.getLocation().setLayer(layer)
167 self._camera.setViewPort(viewport)
168 self._camera.setCellImageDimensions(refw, refh)
169 self._camera.setRotation(rot)
170 self._camera.setTilt(tilt)
171
172 self.toggle()
173
174 def loadSettings(self):
175 if self._editor.getActiveMapView() is None:
176 return
177 else:
178 self._camera = self._editor.getActiveMapView().getCamera()
179
180 map = self._editor.getActiveMapView().getMap().getId()
181 self._container.findChild(name="mapBox").text = unicode(str(map))
182
183 layer = self._camera.getLocation().getLayer().getId()
184 self._container.findChild(name="layerBox").text = unicode(layer)
185
186 vp = self._camera.getViewPort()
187 viewport_str = unicode(str(vp.x) + "," + str(vp.y) + "," + str(vp.w) + "," + str(vp.h))
188 self._container.findChild(name="viewBox").text = viewport_str
189
190 ref = self._camera.getCellImageDimensions()
191 refw_str = unicode(str(ref.x))
192 refh_str = unicode(str(ref.y))
193 self._container.findChild(name="refhBox").text = refh_str
194 self._container.findChild(name="refwBox").text = refw_str
195
196 self._container.findChild(name="idBox").text = unicode(str(self._camera.getId()))
197 self._container.findChild(name="rotBox").text = unicode(str(int(self._camera.getRotation())))
198 self._container.findChild(name="tiltBox").text = unicode(str(int(self._camera.getTilt())))
199
200 def _createGui(self):
201 """ Create the basic gui container """
202 self._container = pychan.loadXML('gui/cameradialog.xml')
203
204 self._ok_button = self._container.findChild(name="okButton")
205 self._cancel_button = self._container.findChild(name="cancelButton")
206
207 self._ok_button.capture(self.saveSettings)
208 self._ok_button.capture(cbwa(self._editor.getStatusBar().showTooltip, unicode("Save changes to the camera")), 'mouseEntered')
209 self._ok_button.capture(self._editor.getStatusBar().hideTooltip, 'mouseExited')
210
211 self._cancel_button.capture(self.toggle)
212 self._cancel_button.capture(cbwa(self._editor.getStatusBar().showTooltip, unicode("Discard any changes to the camera")), 'mouseEntered')
213 self._cancel_button.capture(self._editor.getStatusBar().hideTooltip, 'mouseExited')
214
215
216 def _adjustPosition(self):
217 """ Adjusts the position of the container - we don't want to
218 let the window appear at the center of the screen.
219 (new default position: left, beneath the tools window)
220 """
221 self._container.position = (50, 200)
222
223
224