]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - radiant/entity.cpp
cleaned up duplicated doom3 light_radius default value
[xonotic/netradiant.git] / radiant / entity.cpp
1 /*
2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include "entity.h"
23
24 #include "ientity.h"
25 #include "iselection.h"
26 #include "imodel.h"
27 #include "ifilesystem.h"
28 #include "iundo.h"
29 #include "editable.h"
30
31 #include "eclasslib.h"
32 #include "scenelib.h"
33 #include "os/path.h"
34 #include "os/file.h"
35 #include "stream/stringstream.h"
36 #include "stringio.h"
37
38 #include "gtkutil/filechooser.h"
39 #include "gtkmisc.h"
40 #include "select.h"
41 #include "map.h"
42 #include "preferences.h"
43 #include "gtkdlgs.h"
44 #include "mainframe.h"
45 #include "qe3.h"
46 #include "commands.h"
47
48 struct entity_globals_t
49 {
50   Vector3 color_entity;
51
52   entity_globals_t() :
53     color_entity(0.0f, 0.0f, 0.0f)
54   {
55   }
56 };
57
58 entity_globals_t g_entity_globals;
59
60 class EntitySetKeyValueSelected : public scene::Graph::Walker
61 {
62   const char* m_key;
63   const char* m_value;
64 public:
65   EntitySetKeyValueSelected(const char* key, const char* value)
66     : m_key(key), m_value(value)
67   {
68   }
69   bool pre(const scene::Path& path, scene::Instance& instance) const
70   {
71     return true;
72   }
73   void post(const scene::Path& path, scene::Instance& instance) const
74   {
75     Entity* entity = Node_getEntity(path.top());
76     if(entity != 0
77       && (instance.childSelected() || Instance_getSelectable(instance)->isSelected()))
78     {
79       entity->setKeyValue(m_key, m_value);
80     }
81   }
82 };
83
84 class EntitySetClassnameSelected : public scene::Graph::Walker
85 {
86   const char* m_classname;
87 public:
88   EntitySetClassnameSelected(const char* classname)
89     : m_classname(classname)
90   {
91   }
92   bool pre(const scene::Path& path, scene::Instance& instance) const
93   {
94     return true;
95   }
96   void post(const scene::Path& path, scene::Instance& instance) const
97   {
98     Entity* entity = Node_getEntity(path.top());
99     if(entity != 0
100       && (instance.childSelected() || Instance_getSelectable(instance)->isSelected()))
101     { 
102       NodeSmartReference node(GlobalEntityCreator().createEntity(GlobalEntityClassManager().findOrInsert(m_classname, node_is_group(path.top()))));
103
104       EntityCopyingVisitor visitor(*Node_getEntity(node));
105
106       entity->forEachKeyValue(visitor);
107
108       NodeSmartReference child(path.top().get());
109       NodeSmartReference parent(path.parent().get());
110       Node_getTraversable(parent)->erase(child);
111       if(Node_getTraversable(child) != 0
112         && Node_getTraversable(node) != 0
113         && node_is_group(node))
114       {
115         parentBrushes(child, node);
116       }
117       Node_getTraversable(parent)->insert(node);
118     }
119   }
120 };
121
122 void Scene_EntitySetKeyValue_Selected(const char* key, const char* value)
123 {
124   GlobalSceneGraph().traverse(EntitySetKeyValueSelected(key, value));
125 }
126
127 void Scene_EntitySetClassname_Selected(const char* classname)
128 {
129   GlobalSceneGraph().traverse(EntitySetClassnameSelected(classname));
130 }
131
132
133 class EntityUngroupVisitor : public SelectionSystem::Visitor
134 {
135   const scene::Path& m_parent;
136 public:
137   EntityUngroupVisitor(const scene::Path& parent) : m_parent(parent)
138   {
139   }
140   void visit(scene::Instance& instance) const
141   {
142     if(Node_getEntity(instance.path().top()) != 0
143       && node_is_group(instance.path().top()))
144     {
145       if(m_parent.top().get_pointer() != instance.path().top().get_pointer())
146       {
147         parentBrushes(instance.path().top(), m_parent.top());
148         Path_deleteTop(instance.path());
149       }
150     }
151   }
152 };
153
154 void Entity_ungroupSelected()
155 {
156   UndoableCommand undo("ungroupSelectedEntities");
157
158   scene::Path world_path(makeReference(GlobalSceneGraph().root()));
159   world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
160
161   GlobalSelectionSystem().foreachSelected(EntityUngroupVisitor(world_path));
162 }
163
164
165
166 void Entity_connectSelected()
167 {
168   if(GlobalSelectionSystem().countSelected() == 2)
169   {
170     GlobalEntityCreator().connectEntities(
171       GlobalSelectionSystem().penultimateSelected().path(),
172       GlobalSelectionSystem().ultimateSelected().path()
173     );
174   }
175   else
176   {
177     globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
178   }
179 }
180
181 AABB Doom3Light_getBounds(const AABB& workzone)
182 {
183   AABB aabb(workzone);
184
185   Vector3 defaultRadius(300, 300, 300);
186   if(!string_parse_vector3(EntityClass_valueForKey(*GlobalEntityClassManager().findOrInsert("light", false), "light_radius"), defaultRadius))
187   {
188     globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
189   }
190
191   if(aabb.extents[0] == 0)
192   {
193     aabb.extents[0] = defaultRadius[0];
194   }
195   if(aabb.extents[1] == 0)
196   {
197     aabb.extents[1] = defaultRadius[1];
198   }
199   if(aabb.extents[2] == 0)
200   {
201     aabb.extents[2] = defaultRadius[2];
202   }
203
204   if(aabb_valid(aabb))
205   {
206     return aabb;
207   }
208   return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
209 }
210
211 int g_iLastLightIntensity;
212
213 void Entity_createFromSelection(const char* name, const Vector3& origin)
214 {
215 #if 0
216   if(string_equal_nocase(name, "worldspawn"))
217   {
218     gtk_MessageBox(GTK_WIDGET(MainFrame_getWindow()), "Can't create an entity with worldspawn.", "info");
219     return;
220   }
221 #endif
222
223   EntityClass* entityClass = GlobalEntityClassManager().findOrInsert(name, true);
224
225   bool isModel = string_equal_nocase(name, "misc_model")
226     || string_equal_nocase(name, "misc_gamemodel")
227     || string_equal_nocase(name, "model_static")
228     || (GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase(name, "func_static"));
229
230   bool brushesSelected = Scene_countSelectedBrushes(GlobalSceneGraph()) != 0;
231
232   if(!(entityClass->fixedsize || isModel) && !brushesSelected)
233   {
234     globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
235     return;
236   }
237
238   AABB workzone(aabb_for_minmax(Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max));
239
240
241   NodeSmartReference node(GlobalEntityCreator().createEntity(entityClass));
242
243   Node_getTraversable(GlobalSceneGraph().root())->insert(node);
244
245   scene::Path entitypath(makeReference(GlobalSceneGraph().root()));
246   entitypath.push(makeReference(node.get()));
247   scene::Instance& instance = findInstance(entitypath);
248
249   if(entityClass->fixedsize)
250   {
251     Select_Delete();
252     
253     Transformable* transform = Instance_getTransformable(instance);
254     if(transform != 0)
255     {
256       transform->setType(TRANSFORM_PRIMITIVE);
257       transform->setTranslation(origin);
258       transform->freezeTransform();
259     }
260
261     GlobalSelectionSystem().setSelectedAll(false);
262
263     Instance_setSelected(instance, true);
264   }
265   else
266   {
267     Scene_parentSelectedBrushesToEntity(GlobalSceneGraph(), node);
268     Scene_forEachChildSelectable(SelectableSetSelected(true), instance.path());
269   }
270
271   // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
272   // see SF bug 105383
273
274   if (g_pGameDescription->mGameType == "hl")
275   {
276     // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
277     if (string_equal_nocase(name, "light")
278       || string_equal_nocase(name, "light_environment")
279       || string_equal_nocase(name, "light_spot"))
280     {
281       int intensity = g_iLastLightIntensity;
282
283       if (DoLightIntensityDlg (&intensity) == eIDOK)
284       {
285         g_iLastLightIntensity = intensity;
286         char buf[30];
287         sprintf( buf, "255 255 255 %d", intensity );
288         Node_getEntity(node)->setKeyValue("_light", buf);
289       }
290     }
291   }
292   else if(string_equal_nocase(name, "light"))
293   {
294     if(g_pGameDescription->mGameType != "doom3")
295     {
296       int intensity = g_iLastLightIntensity;
297
298       if (DoLightIntensityDlg (&intensity) == eIDOK)
299       {
300         g_iLastLightIntensity = intensity;
301         char buf[10];
302         sprintf( buf, "%d", intensity );
303         Node_getEntity(node)->setKeyValue("light", buf);
304       }
305     }
306     else if(brushesSelected) // use workzone to set light position/size for doom3 lights, if there are brushes selected
307     {
308       AABB bounds(Doom3Light_getBounds(workzone));
309       StringOutputStream key(64);
310       key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
311       Node_getEntity(node)->setKeyValue("origin", key.c_str());
312       key.clear();
313       key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
314       Node_getEntity(node)->setKeyValue("light_radius", key.c_str());
315     }
316   }
317
318   if(isModel)
319   {
320     const char* model = misc_model_dialog(GTK_WIDGET(MainFrame_getWindow()));
321     if(model != 0)
322     {
323       Node_getEntity(node)->setKeyValue("model", model);
324     }
325   }
326 }
327
328
329 bool DoNormalisedColor(Vector3& color)
330 {
331   if(!color_dialog(GTK_WIDGET(MainFrame_getWindow()), color))
332     return false;
333   /* 
334   ** scale colors so that at least one component is at 1.0F 
335   */
336
337   float largest = 0.0F;
338
339   if ( color[0] > largest )
340     largest = color[0];
341   if ( color[1] > largest )
342     largest = color[1];
343   if ( color[2] > largest )
344     largest = color[2];
345
346   if ( largest == 0.0F )
347   {
348     color[0] = 1.0F;
349     color[1] = 1.0F;
350     color[2] = 1.0F;
351   }
352   else
353   {
354     float scaler = 1.0F / largest;
355
356     color[0] *= scaler;
357     color[1] *= scaler;
358     color[2] *= scaler;
359   }
360
361   return true;
362 }
363
364 void Entity_setColour()
365 {
366   if(GlobalSelectionSystem().countSelected() != 0)
367   {
368     const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
369     Entity* entity = Node_getEntity(path.top());
370     if(entity != 0)
371     {
372       const char* strColor = entity->getKeyValue("_color");
373       if(!string_empty(strColor))
374       {
375         Vector3 rgb;
376         if (string_parse_vector3(strColor, rgb))
377         {
378           g_entity_globals.color_entity = rgb;
379         }
380       }
381
382       if(g_pGameDescription->mGameType == "doom3"
383         ? color_dialog(GTK_WIDGET(MainFrame_getWindow()), g_entity_globals.color_entity)
384         : DoNormalisedColor(g_entity_globals.color_entity))
385       {
386         char buffer[128];
387         sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
388                 g_entity_globals.color_entity[1],
389                 g_entity_globals.color_entity[2]);
390
391         Scene_EntitySetKeyValue_Selected("_color", buffer);
392       }
393     }
394   }
395 }
396
397 const char* misc_model_dialog(GtkWidget* parent)
398 {
399   StringOutputStream buffer(1024);
400
401   buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
402
403   if(!file_readable(buffer.c_str()))
404   {
405     // just go to fsmain
406     buffer.clear();
407     buffer << g_qeglobals.m_userGamePath.c_str() << "/";
408   }
409
410   const char *filename = file_dialog (parent, TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name());
411   if (filename != 0)
412   {
413     // use VFS to get the correct relative path
414     const char* relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
415     if(relative == filename)
416     {
417       globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
418     }
419     return relative;
420   }
421   return 0;
422 }
423
424 void LightRadiiImport(EntityCreator& self, bool value)
425 {
426   self.setLightRadii(value);
427 }
428 typedef ReferenceCaller1<EntityCreator, bool, LightRadiiImport> LightRadiiImportCaller;
429
430 void LightRadiiExport(EntityCreator& self, const BoolImportCallback& importer)
431 {
432   importer(self.getLightRadii());
433 }
434 typedef ReferenceCaller1<EntityCreator, const BoolImportCallback&, LightRadiiExport> LightRadiiExportCaller;
435
436 void Entity_constructPreferences(PreferencesPage& page)
437 {
438   page.appendCheckBox(
439     "Show", "Light Radii",
440     LightRadiiImportCaller(GlobalEntityCreator()),
441     LightRadiiExportCaller(GlobalEntityCreator())
442   );
443 }
444 void Entity_constructPage(PreferenceGroup& group)
445 {
446   PreferencesPage page(group.createPage("Entities", "Entity Display Preferences"));
447   Entity_constructPreferences(page);
448 }
449 void Entity_registerPreferencesPage()
450 {
451   PreferencesDialog_addDisplayPage(FreeCaller1<PreferenceGroup&, Entity_constructPage>());
452 }
453
454
455
456 void Entity_constructMenu(GtkMenu* menu)
457 {
458   create_menu_item_with_mnemonic(menu, "_Ungroup", "UngroupSelection");
459   create_menu_item_with_mnemonic(menu, "_Connect", "ConnectSelection");
460   create_menu_item_with_mnemonic(menu, "_Select Color...", "EntityColor");
461 }
462
463
464
465 #include "preferencesystem.h"
466 #include "stringio.h"
467
468 void Entity_Construct()
469 {
470   GlobalCommands_insert("EntityColor", FreeCaller<Entity_setColour>(), Accelerator('K'));
471   GlobalCommands_insert("ConnectSelection", FreeCaller<Entity_connectSelected>(), Accelerator('K', (GdkModifierType)GDK_CONTROL_MASK));
472   GlobalCommands_insert("UngroupSelection", FreeCaller<Entity_ungroupSelected>());
473
474   GlobalPreferenceSystem().registerPreference("SI_Colors5", Vector3ImportStringCaller(g_entity_globals.color_entity), Vector3ExportStringCaller(g_entity_globals.color_entity));
475   GlobalPreferenceSystem().registerPreference("LastLightIntensity", IntImportStringCaller(g_iLastLightIntensity), IntExportStringCaller(g_iLastLightIntensity));
476
477   Entity_registerPreferencesPage();
478 }
479
480 void Entity_Destroy()
481 {
482 }
483