Mercurial > fife-parpg
comparison tools/editor/plugins/LightEdit.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 # 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 test global light """ | |
25 | |
26 from fife import fife | |
27 from fife.extensions import pychan | |
28 import fife.extensions.pychan.widgets as widgets | |
29 from fife.extensions.pychan.tools import callbackWithArguments as cbwa | |
30 | |
31 from fife.extensions.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 import random | |
39 | |
40 DEFAULT_GLOBAL_LIGHT = { | |
41 "R" : 1.0, | |
42 "G" : 1.0, | |
43 "B" : 1.0, | |
44 "A" : 1.0, | |
45 } | |
46 DEFAULT_LIGHT_ID = "LightEdit" | |
47 | |
48 class LightEdit(plugin.Plugin): | |
49 """ The B{LightEdit} module is a plugin for FIFedit and allows to change the | |
50 global light value | |
51 | |
52 FEATURES: | |
53 - enable FIFE lighting renderer | |
54 - generate random light values | |
55 - test lightsetups by manipulating the color channels | |
56 - reset to default | |
57 """ | |
58 def __init__(self): | |
59 self.active = False | |
60 | |
61 self._renderer = None | |
62 self._camera = None | |
63 self._enabled = False | |
64 self._light = False | |
65 | |
66 self.map_loaded = False | |
67 | |
68 self._color = {} | |
69 self._color.update(DEFAULT_GLOBAL_LIGHT) | |
70 | |
71 random.seed() | |
72 | |
73 if "LightRenderer" in dir(fife): | |
74 self._renderer_available = True | |
75 else: | |
76 self._renderer_available = False | |
77 | |
78 def _reset(self): | |
79 """ resets all dynamic vars """ | |
80 pass | |
81 | |
82 def enable(self): | |
83 """ plugin method """ | |
84 if not self._renderer_available: | |
85 self._enabled = False | |
86 return | |
87 if self._enabled is True: | |
88 return | |
89 | |
90 self._editor = scripts.editor.getEditor() | |
91 self.engine = self._editor.getEngine() | |
92 | |
93 self._showAction = Action(unicode(self.getName(),"utf-8"), checkable=True) | |
94 scripts.gui.action.activated.connect(self.toggle_gui, sender=self._showAction) | |
95 | |
96 self._editor._tools_menu.addAction(self._showAction) | |
97 | |
98 events.postMapShown.connect(self.update_renderer) | |
99 events.onMapChanged.connect(self.update_renderer) | |
100 | |
101 self._reset() | |
102 self.create_gui() | |
103 | |
104 def disable(self): | |
105 """ plugin method """ | |
106 if self._enabled is False: | |
107 return | |
108 | |
109 self._reset() | |
110 self.container.hide() | |
111 self.removeAllChildren() | |
112 | |
113 self._editor._tools_menu.removeAction(self._showAction) | |
114 | |
115 events.postMapShown.disconnect(self.update_renderer) | |
116 events.onMapChanged.disconnect(self.update_renderer) | |
117 | |
118 def isEnabled(self): | |
119 """ plugin method """ | |
120 return self._enabled; | |
121 | |
122 def getName(self): | |
123 """ plugin method """ | |
124 return "Light editor" | |
125 | |
126 def create_gui(self): | |
127 """ create gui container and setup callbacks """ | |
128 self.container = pychan.loadXML('gui/lightedit.xml') | |
129 self.container.mapEvents({ | |
130 "enable_global_light" : self.toggle_light, | |
131 "random_global_light" : self.random_color, | |
132 "reset_global_light" : self.reset_global_light, | |
133 | |
134 "increase_R" : cbwa(self.increase_color, r=True), | |
135 "decrease_R" : cbwa(self.decrease_color, r=True), | |
136 "value_R/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, r=True), | |
137 "value_R/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, r=True), | |
138 | |
139 "increase_G" : cbwa(self.increase_color, g=True), | |
140 "decrease_G" : cbwa(self.decrease_color, g=True), | |
141 "value_G/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, g=True), | |
142 "value_G/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, g=True), | |
143 | |
144 "increase_B" : cbwa(self.increase_color, b=True), | |
145 "decrease_B" : cbwa(self.decrease_color, b=True), | |
146 "value_B/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, b=True), | |
147 "value_B/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, b=True), | |
148 | |
149 "increase_A" : cbwa(self.increase_color, a=True), | |
150 "decrease_A" : cbwa(self.decrease_color, a=True), | |
151 "value_A/mouseWheelMovedUp" : cbwa(self.increase_color, step=0.2, a=True), | |
152 "value_A/mouseWheelMovedDown" : cbwa(self.decrease_color, step=0.2, a=True), | |
153 }) | |
154 self._widgets = { | |
155 "enable_global_light" : self.container.findChild(name="enable_global_light"), | |
156 "random_global_light" : self.container.findChild(name="random_global_light"), | |
157 "reset_global_light" : self.container.findChild(name="reset_global_light"), | |
158 | |
159 "value_R" : self.container.findChild(name="value_R"), | |
160 "value_G" : self.container.findChild(name="value_G"), | |
161 "value_B" : self.container.findChild(name="value_B"), | |
162 "value_A" : self.container.findChild(name="value_A"), | |
163 } | |
164 | |
165 def toggle_gui(self): | |
166 """ show / hide the gui """ | |
167 if self.active: | |
168 self.active = False | |
169 if self.container.isVisible() or self.container.isDocked(): | |
170 self.container.setDocked(False) | |
171 self.container.hide() | |
172 self._showAction.setChecked(False) | |
173 else: | |
174 self.active = True | |
175 self._showAction.setChecked(True) | |
176 self.container.show() | |
177 | |
178 def toggle_light(self): | |
179 """ toggle light on / off """ | |
180 if not self._renderer: | |
181 self._widgets['enable_global_light']._setToggled(False) | |
182 return | |
183 | |
184 if self._light: | |
185 self._light = False | |
186 self.reset_global_light() | |
187 self._renderer.setEnabled(False) | |
188 else: | |
189 self._light = True | |
190 self._renderer.setEnabled(True) | |
191 | |
192 def update_renderer(self): | |
193 """ sets current camera and renderer | |
194 bound to FIFedit core (updated on map change) | |
195 """ | |
196 self._camera = self._editor.getActiveMapView().getCamera() | |
197 self._renderer = fife.LightRenderer.getInstance(self._camera) | |
198 | |
199 def update_gui(self): | |
200 """ update gui widgets according to plugin data """ | |
201 self._widgets["value_R"].text = unicode(str(self._color["R"])) | |
202 self._widgets["value_G"].text = unicode(str(self._color["G"])) | |
203 self._widgets["value_B"].text = unicode(str(self._color["B"])) | |
204 self._widgets["value_A"].text = unicode(str(self._color["A"])) | |
205 | |
206 def reset_global_light(self): | |
207 """ reset global light to default values (1.0) """ | |
208 if not self._renderer: return | |
209 | |
210 self._color.update(DEFAULT_GLOBAL_LIGHT) | |
211 self.update_gui() | |
212 self.set_global_light() | |
213 | |
214 def increase_color(self, step=0.1, r=None, g=None, b=None, a=None): | |
215 """ increase a given color value by step value | |
216 | |
217 @type step float | |
218 @param step the step for changing the color channel | |
219 @type r bool | |
220 @param r flag to alter red color value | |
221 @type g bool | |
222 @param g flag to alter green color value | |
223 @type b bool | |
224 @param b flag to alter blue color value | |
225 @type a bool | |
226 @type a flag to alter alpha channel value (no effect atm) | |
227 """ | |
228 if r: | |
229 self._color["R"] += step | |
230 if g: | |
231 self._color["G"] += step | |
232 if b: | |
233 self._color["B"] += step | |
234 if a: | |
235 self._color["A"] += step | |
236 | |
237 self.update_gui() | |
238 self.set_global_light() | |
239 | |
240 def decrease_color(self, step=0.1, r=None, g=None, b=None, a=None): | |
241 """ decrease a given color value by step value | |
242 | |
243 @type step float | |
244 @param step the step for changing the color channel | |
245 @type r bool | |
246 @param r flag to alter red color value | |
247 @type g bool | |
248 @param g flag to alter green color value | |
249 @type b bool | |
250 @param b flag to alter blue color value | |
251 @type a bool | |
252 @type a flag to alter alpha channel value (no effect atm) | |
253 """ | |
254 if r: | |
255 self._color["R"] -= step | |
256 if g: | |
257 self._color["G"] -= step | |
258 if b: | |
259 self._color["B"] -= step | |
260 if a: | |
261 self._color["A"] -= step | |
262 | |
263 self.update_gui() | |
264 self.set_global_light() | |
265 | |
266 def random_color(self): | |
267 """ generate random values for color channels """ | |
268 if not self._renderer: return | |
269 | |
270 self._color["R"] = random.uniform(0,2) | |
271 self._color["G"] = random.uniform(0,2) | |
272 self._color["B"] = random.uniform(0,2) | |
273 self._color["A"] = random.uniform(0,2) | |
274 | |
275 self.update_gui() | |
276 self.set_global_light() | |
277 | |
278 def set_global_light(self): | |
279 """ update the global light with the current set colors """ | |
280 if not self._renderer: return | |
281 | |
282 self._renderer.removeAll(DEFAULT_LIGHT_ID) | |
283 self._renderer.setglobalLight( | |
284 DEFAULT_LIGHT_ID, | |
285 1, | |
286 self._color["R"], | |
287 self._color["G"], | |
288 self._color["B"], | |
289 self._color["A"] | |
290 ) | |
291 |