comparison clients/editor/plugins/CameraEdit.py @ 363:714673208050

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