comparison clients/editor/plugins/LightEdit.py @ 307:22253b2c9b14

- added LightEdit editor plugin (needs light branch to work; deactivated if lighting renderer is not available) - added animation viewer to ObjectEdit - several bugfixes for ObjectEdit plugin FEATURES: - ObjectEdit - viewing and rotating animated instances (rotations are hardcoded for now, FIFE needs to expose available angles to python in order to make animation rotation work for every client) - LightEdit - test global light values
author chewie@33b003aa-7bff-0310-803a-e67f0ece8222
date Tue, 11 Aug 2009 15:32:54 +0000
parents
children dfd48d49c044
comparison
equal deleted inserted replaced
306:6177cdf72489 307:22253b2c9b14
1 #!/usr/bin/env python
2 # coding: utf-8
3 # ###################################################
4 # Copyright (C) 2008 The Zero-Projekt team
5 # http://zero-projekt.net
6 # info@zero-projekt.net
7 # This file is part of Zero "Was vom Morgen blieb"
8 #
9 # The Zero-Projekt codebase is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the
21 # Free Software Foundation, Inc.,
22 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 # ###################################################
24
25 """ a tool for FIFEdit to test global light """
26
27 import fife
28 import pychan
29 import pychan.widgets as widgets
30 from pychan.tools import callbackWithArguments as cbwa
31
32 from fife_timer import Timer
33
34 import scripts
35 import scripts.plugin as plugin
36 from scripts.events import *
37 from scripts.gui.action import Action
38
39 import random
40
41 DEFAULT_GLOBAL_LIGHT = {
42 "R" : 1.0,
43 "G" : 1.0,
44 "B" : 1.0,
45 "A" : 1.0,
46 }
47 DEFAULT_LIGHT_ID = "LightEdit"
48
49 class LightEdit(plugin.Plugin):
50 """ The B{LightEdit} module is a plugin for FIFedit and allows to change the
51 global light value
52
53 FEATURES:
54 - enable FIFE lighting renderer
55 - generate random light values
56 - test lightsetups by manipulating the color channels
57 - reset to default
58 """
59 def __init__(self):
60 self.active = False
61
62 self._renderer = None
63 self._camera = None
64 self._enabled = False
65 self._light = False
66
67 self.map_loaded = False
68
69 self._color = {}
70 self._color.update(DEFAULT_GLOBAL_LIGHT)
71
72 random.seed()
73
74 if "LightRenderer" in dir(fife):
75 self._renderer_available = True
76 else:
77 self._renderer_available = False
78
79 def _reset(self):
80 """ resets all dynamic vars """
81 pass
82
83 def enable(self):
84 """ plugin method """
85 if not self._renderer_available:
86 self._enabled = False
87 return
88 if self._enabled is True:
89 return
90
91 self._editor = scripts.editor.getEditor()
92 self.engine = self._editor.getEngine()
93
94 self._showAction = Action(unicode(self.getName(),"utf-8"), checkable=True)
95 scripts.gui.action.activated.connect(self.toggle_gui, sender=self._showAction)
96
97 self._editor._toolsMenu.addAction(self._showAction)
98
99 events.postMapShown.connect(self.update_renderer)
100 events.onMapChanged.connect(self.update_renderer)
101
102 self._reset()
103 self.create_gui()
104
105 def disable(self):
106 """ plugin method """
107 if self._enabled is False:
108 return
109
110 self._reset()
111 self.container.hide()
112 self.removeAllChildren()
113
114 self._editor._toolsMenu.removeAction(self._showAction)
115
116 events.postMapShown.disconnect(self.update_renderer)
117 events.onMapChanged.disconnect(self.update_renderer)
118
119 def isEnabled(self):
120 """ plugin method """
121 return self._enabled;
122
123 def getName(self):
124 """ plugin method """
125 return "Light editor"
126
127 def create_gui(self):
128 """ create gui container and setup callbacks """
129 self.container = pychan.loadXML('gui/lightedit.xml')
130 self.container.mapEvents({
131 "enable_global_light" : self.toggle_light,
132 "random_global_light" : self.random_color,
133 "reset_global_light" : self.reset_global_light,
134
135 "increase_R" : cbwa(self.increase_color, r=True),
136 "decrease_R" : cbwa(self.decrease_color, r=True),
137 "value_R/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, r=True),
138 "value_R/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, r=True),
139
140 "increase_G" : cbwa(self.increase_color, g=True),
141 "decrease_G" : cbwa(self.decrease_color, g=True),
142 "value_G/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, g=True),
143 "value_G/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, g=True),
144
145 "increase_B" : cbwa(self.increase_color, b=True),
146 "decrease_B" : cbwa(self.decrease_color, b=True),
147 "value_B/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, b=True),
148 "value_B/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, b=True),
149
150 "increase_A" : cbwa(self.increase_color, a=True),
151 "decrease_A" : cbwa(self.decrease_color, a=True),
152 "value_A/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, a=True),
153 "value_A/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, a=True),
154 })
155 self._widgets = {
156 "enable_global_light" : self.container.findChild(name="enable_global_light"),
157 "random_global_light" : self.container.findChild(name="random_global_light"),
158 "reset_global_light" : self.container.findChild(name="reset_global_light"),
159
160 "value_R" : self.container.findChild(name="value_R"),
161 "value_G" : self.container.findChild(name="value_G"),
162 "value_B" : self.container.findChild(name="value_B"),
163 "value_A" : self.container.findChild(name="value_A"),
164 }
165
166 def toggle_gui(self):
167 """ show / hide the gui """
168 if self.active:
169 self.active = False
170 if self.container.isVisible() or self.container.isDocked():
171 self.container.setDocked(False)
172 self.container.hide()
173 self._showAction.setChecked(False)
174 else:
175 self.active = True
176 self._showAction.setChecked(True)
177 self.container.show()
178
179 def toggle_light(self):
180 """ toggle light on / off """
181 if not self._renderer:
182 self._widgets['enable_global_light']._setToggled(False)
183 return
184
185 if self._light:
186 self._light = False
187 self.reset_global_light()
188 self._renderer.setEnabled(False)
189 else:
190 self._light = True
191 self._renderer.setEnabled(True)
192
193 def update_renderer(self):
194 """ sets current camera and renderer
195 bound to FIFedit core (updated on map change)
196 """
197 self._camera = self._editor.getActiveMapView().getCamera()
198 self._renderer = fife.LightRenderer.getInstance(self._camera)
199
200 def update_gui(self):
201 """ update gui widgets according to plugin data """
202 self._widgets["value_R"].text = unicode(str(self._color["R"]))
203 self._widgets["value_G"].text = unicode(str(self._color["G"]))
204 self._widgets["value_B"].text = unicode(str(self._color["B"]))
205 self._widgets["value_A"].text = unicode(str(self._color["A"]))
206
207 def reset_global_light(self):
208 """ reset global light to default values (1.0) """
209 if not self._renderer: return
210
211 self._color.update(DEFAULT_GLOBAL_LIGHT)
212 self.update_gui()
213 self.set_global_light()
214
215 def increase_color(self, step=0.1, r=None, g=None, b=None, a=None):
216 """ increase a given color value by step value
217
218 @type step float
219 @param step the step for changing the color channel
220 @type r bool
221 @param r flag to alter red color value
222 @type g bool
223 @param g flag to alter green color value
224 @type b bool
225 @param b flag to alter blue color value
226 @type a bool
227 @type a flag to alter alpha channel value (no effect atm)
228 """
229 if r:
230 self._color["R"] += step
231 if g:
232 self._color["G"] += step
233 if b:
234 self._color["B"] += step
235 if a:
236 self._color["A"] += step
237
238 self.update_gui()
239 self.set_global_light()
240
241 def decrease_color(self, step=0.1, r=None, g=None, b=None, a=None):
242 """ decrease a given color value by step value
243
244 @type step float
245 @param step the step for changing the color channel
246 @type r bool
247 @param r flag to alter red color value
248 @type g bool
249 @param g flag to alter green color value
250 @type b bool
251 @param b flag to alter blue color value
252 @type a bool
253 @type a flag to alter alpha channel value (no effect atm)
254 """
255 if r:
256 self._color["R"] -= step
257 if g:
258 self._color["G"] -= step
259 if b:
260 self._color["B"] -= step
261 if a:
262 self._color["A"] -= step
263
264 self.update_gui()
265 self.set_global_light()
266
267 def random_color(self):
268 """ generate random values for color channels """
269 if not self._renderer: return
270
271 self._color["R"] = random.uniform(0,2)
272 self._color["G"] = random.uniform(0,2)
273 self._color["B"] = random.uniform(0,2)
274 self._color["A"] = random.uniform(0,2)
275
276 self.update_gui()
277 self.set_global_light()
278
279 def set_global_light(self):
280 """ update the global light with the current set colors """
281 if not self._renderer: return
282
283 self._renderer.removeAll(DEFAULT_LIGHT_ID)
284 self._renderer.setglobalLight(
285 DEFAULT_LIGHT_ID,
286 1,
287 self._color["R"],
288 self._color["G"],
289 self._color["B"],
290 self._color["A"]
291 )
292