Debian Codegraph @

radiant/entity.cpp

raw symbols SCIP
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 "preferencesystem.h"
44 #include "stringio.h"
45 #include "gtkdlgs.h"
46 #include "mainframe.h"
47 #include "modelbrowser.h"
48 #include "qe3.h"
49 #include "commands.h"
50
51 #include "brushmanip.h"
52 #include "patchmanip.h"
53
54 #include "uilib/uilib.h"
55
56 struct entity_globals_t
57 {
58 Vector3 color_entity;
59
60 entity_globals_t() :
61 color_entity( 0.0f, 0.0f, 0.0f ){
62 }
63 };
64
65 entity_globals_t g_entity_globals;
66
67 class EntitySetKeyValueSelected : public scene::Graph::Walker
68 {
69 const char* m_key;
70 const char* m_value;
71 public:
72 EntitySetKeyValueSelected( const char* key, const char* value )
73 : m_key( key ), m_value( value ){
74 }
75
76 bool pre( const scene::Path& path, scene::Instance& instance ) const {
77 return true;
78 }
79
80 void post( const scene::Path& path, scene::Instance& instance ) const {
81 Entity* entity = Node_getEntity( path.top() );
82 if ( entity != 0
83 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
84 entity->setKeyValue( m_key, m_value );
85 }
86 }
87 };
88
89 class EntitySetClassnameSelected : public scene::Graph::Walker
90 {
91 const char* m_classname;
92 public:
93 EntitySetClassnameSelected( const char* classname )
94 : m_classname( classname ){
95 }
96 bool pre( const scene::Path& path, scene::Instance& instance ) const {
97 return true;
98 }
99 void post( const scene::Path& path, scene::Instance& instance ) const {
100 Entity* entity = Node_getEntity( path.top() );
101 if ( entity != 0
102 && ( instance.childSelected() || Instance_getSelectable( instance )->isSelected() ) ) {
103 NodeSmartReference node( GlobalEntityCreator().createEntity( GlobalEntityClassManager().findOrInsert( m_classname, node_is_group( path.top() ) ) ) );
104
105 EntityCopyingVisitor visitor( *Node_getEntity( node ) );
106
107 //entity->forEachKeyValue( visitor );
108
109 NodeSmartReference child( path.top().get() );
110 NodeSmartReference parent( path.parent().get() );
111 //Node_getTraversable( parent )->erase( child );
112 if ( Node_getTraversable( child ) != 0
113 && Node_getTraversable( node ) != 0
114 && node_is_group( node ) ) {
115 parentBrushes( child, node );
116 }
117 Node_getTraversable( parent )->insert( node );
118 /* must do this after inserting node, otherwise problem: targeted + having model + not loaded b4 new entities aren't selectable normally + rendered only while 0 0 0 is rendered */
119 entity->forEachKeyValue( visitor );
120 Node_getTraversable( parent )->erase( child );
121 }
122 }
123 };
124
125 void Scene_EntitySetKeyValue_Selected( const char* key, const char* value ){
126 GlobalSceneGraph().traverse( EntitySetKeyValueSelected( key, value ) );
127 }
128
129 void Scene_EntitySetClassname_Selected( const char* classname ){
130 GlobalSceneGraph().traverse( EntitySetClassnameSelected( classname ) );
131 }
132
133
134 void Entity_ungroupSelected(){
135 if ( GlobalSelectionSystem().countSelected() < 1 ) {
136 return;
137 }
138
139 UndoableCommand undo( "ungroupSelectedEntities" );
140
141 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
142 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
143
144 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
145 scene::Path path = instance.path();
146
147 if ( !Node_isEntity( path.top() ) ) {
148 path.pop();
149 }
150
151 if ( Node_getEntity( path.top() ) != 0
152 && node_is_group( path.top() ) ) {
153 if ( world_path.top().get_pointer() != path.top().get_pointer() ) {
154 parentBrushes( path.top(), world_path.top() );
155 Path_deleteTop( path );
156 }
157 }
158 }
159
160
161 class EntityFindSelected : public scene::Graph::Walker
162 {
163 public:
164 mutable const scene::Path *groupPath;
165 mutable scene::Instance *groupInstance;
166
167 EntityFindSelected() : groupPath( 0 ), groupInstance( 0 ){
168 }
169
170 bool pre( const scene::Path& path, scene::Instance& instance ) const {
171 return true;
172 }
173
174 void post( const scene::Path& path, scene::Instance& instance ) const {
175 Entity* entity = Node_getEntity( path.top() );
176 if ( entity != 0
177 && Instance_getSelectable( instance )->isSelected()
178 && node_is_group( path.top() )
179 && !groupPath ) {
180 groupPath = &path;
181 groupInstance = &instance;
182 }
183 }
184 };
185
186 class EntityGroupSelected : public scene::Graph::Walker
187 {
188 NodeSmartReference group, worldspawn;
189 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
190 //Stack<DeletionPair> deleteme;
191 public:
192 EntityGroupSelected( const scene::Path &p ) : group( p.top().get() ), worldspawn( Map_FindOrInsertWorldspawn( g_map ) ){
193 }
194
195 bool pre( const scene::Path& path, scene::Instance& instance ) const {
196 return true;
197 }
198
199 void post( const scene::Path& path, scene::Instance& instance ) const {
200 Selectable *selectable = Instance_getSelectable( instance );
201 if ( selectable && selectable->isSelected() ) {
202 Entity* entity = Node_getEntity( path.top() );
203 if ( entity == 0 && Node_isPrimitive( path.top() ) ) {
204 NodeSmartReference child( path.top().get() );
205 NodeSmartReference parent( path.parent().get() );
206
207 if ( path.size() >= 3 && parent != worldspawn ) {
208 NodeSmartReference parentparent( path[path.size() - 3].get() );
209
210 Node_getTraversable( parent )->erase( child );
211 Node_getTraversable( group )->insert( child );
212
213 if ( Node_getTraversable( parent )->empty() ) {
214 //deleteme.push(DeletionPair(parentparent, parent));
215 Node_getTraversable( parentparent )->erase( parent );
216 }
217 }
218 else
219 {
220 Node_getTraversable( parent )->erase( child );
221 Node_getTraversable( group )->insert( child );
222 }
223 }
224 }
225 }
226 };
227
228 void Entity_groupSelected(){
229 if ( GlobalSelectionSystem().countSelected() < 1 ) {
230 return;
231 }
232
233 UndoableCommand undo( "groupSelectedEntities" );
234
235 scene::Path world_path( makeReference( GlobalSceneGraph().root() ) );
236 world_path.push( makeReference( Map_FindOrInsertWorldspawn( g_map ) ) );
237
238 EntityFindSelected fse;
239 GlobalSceneGraph().traverse( fse );
240 if ( fse.groupPath ) {
241 GlobalSceneGraph().traverse( EntityGroupSelected( *fse.groupPath ) );
242 }
243 else
244 {
245 GlobalSceneGraph().traverse( EntityGroupSelected( world_path ) );
246 }
247 }
248
249
250 void Entity_connectSelected(){
251 if ( GlobalSelectionSystem().countSelected() == 2 ) {
252 GlobalEntityCreator().connectEntities(
253 GlobalSelectionSystem().penultimateSelected().path(),
254 GlobalSelectionSystem().ultimateSelected().path(),
255 0
256 );
257 }
258 else
259 {
260 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
261 }
262 }
263
264 void Entity_killconnectSelected(){
265 if ( GlobalSelectionSystem().countSelected() == 2 ) {
266 GlobalEntityCreator().connectEntities(
267 GlobalSelectionSystem().penultimateSelected().path(),
268 GlobalSelectionSystem().ultimateSelected().path(),
269 1
270 );
271 }
272 else
273 {
274 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
275 }
276 }
277
278 AABB Doom3Light_getBounds( const AABB& workzone ){
279 AABB aabb( workzone );
280
281 Vector3 defaultRadius( 300, 300, 300 );
282 if ( !string_parse_vector3( EntityClass_valueForKey( *GlobalEntityClassManager().findOrInsert( "light", false ), "light_radius" ), defaultRadius ) ) {
283 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
284 }
285
286 if ( aabb.extents[0] == 0 ) {
287 aabb.extents[0] = defaultRadius[0];
288 }
289 if ( aabb.extents[1] == 0 ) {
290 aabb.extents[1] = defaultRadius[1];
291 }
292 if ( aabb.extents[2] == 0 ) {
293 aabb.extents[2] = defaultRadius[2];
294 }
295
296 if ( aabb_valid( aabb ) ) {
297 return aabb;
298 }
299 return AABB( Vector3( 0, 0, 0 ), Vector3( 64, 64, 64 ) );
300 }
301
302 int g_iLastLightIntensity;
303
304 void Entity_createFromSelection( const char* name, const Vector3& origin ){
305 #if 0
306 if ( string_equal_nocase( name, "worldspawn" ) ) {
307 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
308 return;
309 }
310 #endif
311
312 EntityClass* entityClass = GlobalEntityClassManager().findOrInsert( name, true );
313
314 bool isModel = ( string_compare_nocase_n( name, "misc_", 5 ) == 0 && string_equal_nocase( name + string_length( name ) - 5, "model" ) ) // misc_*model (also misc_model)
315 || string_equal_nocase( name, "model_static" )
316 || ( GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase( name, "func_static" ) && g_pGameDescription->mGameType == "doom3" );
317
318 bool brushesSelected = Scene_countSelectedBrushes( GlobalSceneGraph() ) != 0;
319
320 //is important to have retexturing here; if doing in the end, undo doesn't succeed;
321 if ( string_compare_nocase_n( name, "trigger_", 8 ) == 0 && brushesSelected ){
322 const char* shader = g_pGameDescription->getKeyValue( "shader_trigger" );
323 if ( shader && *shader ){
324 Scene_PatchSetShader_Selected( GlobalSceneGraph(), shader );
325 Scene_BrushSetShader_Selected( GlobalSceneGraph(), shader );
326 }
327 else{
328 Scene_PatchSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
329 Scene_BrushSetShader_Selected( GlobalSceneGraph(), "textures/common/trigger" );
330 }
331 }
332
333 if ( !( entityClass->fixedsize || isModel ) && !brushesSelected ) {
334 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
335 return;
336 }
337
338 AABB workzone( aabb_for_minmax( Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max ) );
339
340
341 NodeSmartReference node( GlobalEntityCreator().createEntity( entityClass ) );
342
343 Node_getTraversable( GlobalSceneGraph().root() )->insert( node );
344
345 scene::Path entitypath( makeReference( GlobalSceneGraph().root() ) );
346 entitypath.push( makeReference( node.get() ) );
347 scene::Instance& instance = findInstance( entitypath );
348
349 if ( entityClass->fixedsize || ( isModel && !brushesSelected ) ) {
350 Select_Delete();
351
352 Transformable* transform = Instance_getTransformable( instance );
353 if ( transform != 0 ) {
354 transform->setType( TRANSFORM_PRIMITIVE );
355 transform->setTranslation( origin );
356 transform->freezeTransform();
357 }
358
359 GlobalSelectionSystem().setSelectedAll( false );
360
361 Instance_setSelected( instance, true );
362 }
363 else
364 {
365 if ( g_pGameDescription->mGameType == "doom3" ) {
366 Node_getEntity( node )->setKeyValue( "model", Node_getEntity( node )->getKeyValue( "name" ) );
367 }
368
369 Scene_parentSelectedBrushesToEntity( GlobalSceneGraph(), node );
370 Scene_forEachChildSelectable( SelectableSetSelected( true ), instance.path() );
371 }
372
373 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
374 // see SF bug 105383
375
376 if ( g_pGameDescription->mGameType == "hl" ) {
377 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
378 if ( string_equal_nocase( name, "light" )
379 || string_equal_nocase( name, "light_environment" )
380 || string_equal_nocase( name, "light_spot" ) ) {
381 int intensity = g_iLastLightIntensity;
382
383 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
384 g_iLastLightIntensity = intensity;
385 char buf[30];
386 sprintf( buf, "255 255 255 %d", intensity );
387 Node_getEntity( node )->setKeyValue( "_light", buf );
388 }
389 }
390 }
391 else if ( string_equal_nocase( name, "light" ) ) {
392 if ( g_pGameDescription->mGameType != "doom3" ) {
393 int intensity = g_iLastLightIntensity;
394
395 if ( DoLightIntensityDlg( &intensity ) == eIDOK ) {
396 g_iLastLightIntensity = intensity;
397 char buf[10];
398 sprintf( buf, "%d", intensity );
399 Node_getEntity( node )->setKeyValue( "light", buf );
400 }
401 }
402 else if ( brushesSelected ) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
403 AABB bounds( Doom3Light_getBounds( workzone ) );
404 StringOutputStream key( 64 );
405 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
406 Node_getEntity( node )->setKeyValue( "origin", key.c_str() );
407 key.clear();
408 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
409 Node_getEntity( node )->setKeyValue( "light_radius", key.c_str() );
410 }
411 }
412
413 if ( isModel ) {
414 const char* model = misc_model_dialog(MainFrame_getWindow());
415 if ( model != 0 ) {
416 Node_getEntity( node )->setKeyValue( "model", model );
417 }
418 }
419 }
420
421 #if 0
422 bool DoNormalisedColor( Vector3& color ){
423 if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
424 return false;
425 }
426 /*
427 ** scale colors so that at least one component is at 1.0F
428 */
429
430 float largest = 0.0F;
431
432 if ( color[0] > largest ) {
433 largest = color[0];
434 }
435 if ( color[1] > largest ) {
436 largest = color[1];
437 }
438 if ( color[2] > largest ) {
439 largest = color[2];
440 }
441
442 if ( largest == 0.0F ) {
443 color[0] = 1.0F;
444 color[1] = 1.0F;
445 color[2] = 1.0F;
446 }
447 else
448 {
449 float scaler = 1.0F / largest;
450
451 color[0] *= scaler;
452 color[1] *= scaler;
453 color[2] *= scaler;
454 }
455
456 return true;
457 }
458 #endif
459
460 void NormalizeColor( Vector3& color ){
461 // scale colors so that at least one component is at 1.0F
462
463 float largest = 0.0F;
464
465 if ( color[0] > largest ) {
466 largest = color[0];
467 }
468 if ( color[1] > largest ) {
469 largest = color[1];
470 }
471 if ( color[2] > largest ) {
472 largest = color[2];
473 }
474
475 if ( largest == 0.0F ) {
476 color[0] = 1.0F;
477 color[1] = 1.0F;
478 color[2] = 1.0F;
479 }
480 else
481 {
482 float scaler = 1.0F / largest;
483
484 color[0] *= scaler;
485 color[1] *= scaler;
486 color[2] *= scaler;
487 }
488 }
489
490 void Entity_normalizeColor(){
491 if ( GlobalSelectionSystem().countSelected() != 0 ) {
492 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
493 Entity* entity = Node_getEntity( path.top() );
494
495 if( entity == 0 && path.size() == 3 ){
496 entity = Node_getEntity( path.parent() );
497 }
498
499 if ( entity != 0 ) {
500 const char* strColor = entity->getKeyValue( "_color" );
501 if ( !string_empty( strColor ) ) {
502 Vector3 rgb;
503 if ( string_parse_vector3( strColor, rgb ) ) {
504 g_entity_globals.color_entity = rgb;
505 NormalizeColor( g_entity_globals.color_entity );
506
507 char buffer[128];
508 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
509 g_entity_globals.color_entity[1],
510 g_entity_globals.color_entity[2] );
511
512 StringOutputStream command( 256 );
513 command << "entityNormalizeColour " << buffer;
514 UndoableCommand undo( command.c_str() );
515 Scene_EntitySetKeyValue_Selected( "_color", buffer );
516 }
517 }
518 }
519 }
520 }
521
522 void Entity_setColour(){
523 if ( GlobalSelectionSystem().countSelected() != 0 ) {
524 const scene::Path& path = GlobalSelectionSystem().ultimateSelected().path();
525 Entity* entity = Node_getEntity( path.top() );
526
527 if( entity == 0 && path.size() == 3 ){
528 entity = Node_getEntity( path.parent() );
529 }
530
531 if ( entity != 0 ) {
532 const char* strColor = entity->getKeyValue( "_color" );
533 if ( !string_empty( strColor ) ) {
534 Vector3 rgb;
535 if ( string_parse_vector3( strColor, rgb ) ) {
536 g_entity_globals.color_entity = rgb;
537 }
538 }
539 if ( color_dialog( MainFrame_getWindow(), g_entity_globals.color_entity ) ) {
540 char buffer[128];
541 sprintf( buffer, "%g %g %g", g_entity_globals.color_entity[0],
542 g_entity_globals.color_entity[1],
543 g_entity_globals.color_entity[2] );
544
545 StringOutputStream command( 256 );
546 command << "entitySetColour " << buffer;
547 UndoableCommand undo( command.c_str() );
548 Scene_EntitySetKeyValue_Selected( "_color", buffer );
549 }
550 }
551 }
552 }
553
554 CopiedString g_strLastModelFolder = "";
555
556 const char *getLastModelFolderPath(){
557 if ( g_strLastModelFolder.empty() ) {
558 GlobalPreferenceSystem().registerPreference( "LastModelFolder", make_property_string( g_strLastModelFolder ) );
559 if ( g_strLastModelFolder.empty() ) {
560 StringOutputStream buffer( 1024 );
561 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
562 if ( !file_readable( buffer.c_str() ) ) {
563 // just go to fsmain
564 buffer.clear();
565 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
566 }
567 g_strLastModelFolder = buffer.c_str();
568 }
569 }
570 return g_strLastModelFolder.c_str();
571 }
572
573 const char *misc_model_dialog( ui::Widget parent ){
574 return ModelBrowser_showChooser( parent );
575 }
576 /*
577 struct LightRadii {
578 static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz) {
579 returnz(self.getLightRadii());
580 }
581
582 static void Import(EntityCreator &self, bool value) {
583 self.setLightRadii(value);
584 }
585 };
586
587 void Entity_constructPreferences( PreferencesPage& page ){
588 page.appendCheckBox(
589 "Show", "Light Radii",
590 make_property<LightRadii>(GlobalEntityCreator())
591 );
592 }
593 void Entity_constructPage( PreferenceGroup& group ){
594 PreferencesPage page( group.createPage( "Entities", "Entity Display Preferences" ) );
595 Entity_constructPreferences( page );
596 }
597
598 void Entity_registerPreferencesPage(){
599 PreferencesDialog_addDisplayPage( makeCallbackF(Entity_constructPage) );
600 }
601 */
602
603 void ShowLightRadiiToggle(){
604 GlobalEntityCreator().setLightRadii( !GlobalEntityCreator().getLightRadii() );
605 UpdateAllWindows();
606 }
607 typedef FreeCaller<void(), ShowLightRadiiToggle> ShowLightRadiiToggleCaller;
608 void ShowLightRadiiExport( const Callback<void(bool)> & importer ){
609 GlobalEntityCreator().getLightRadii();
610 }
611 typedef FreeCaller<void(const Callback<void(bool)> &), ShowLightRadiiExport> ShowLightRadiiExportCaller;
612
613 ShowLightRadiiExportCaller g_show_lightradii_caller;
614 Callback<void(const Callback<void(bool)> &)> g_show_lightradii_callback( g_show_lightradii_caller );
615 ToggleItem g_show_lightradii( g_show_lightradii_callback );
616
617 void Entity_constructMenu( ui::Menu menu ){
618 create_menu_item_with_mnemonic( menu, "_Regroup", "GroupSelection" );
619 create_menu_item_with_mnemonic( menu, "_Ungroup", "UngroupSelection" );
620 create_menu_item_with_mnemonic( menu, "_Connect", "ConnectSelection" );
621 if ( g_pGameDescription->mGameType == "nexuiz" ) {
622 create_menu_item_with_mnemonic( menu, "_KillConnect", "KillConnectSelection" );
623 }
624 create_menu_item_with_mnemonic( menu, "_Select Color...", "EntityColor" );
625 create_menu_item_with_mnemonic( menu, "_Normalize Color", "NormalizeColor" );
626 }
627
628
629 void Entity_Construct(){
630 GlobalCommands_insert( "EntityColor", makeCallbackF(Entity_setColour), Accelerator( 'K' ) );
631 GlobalCommands_insert( "NormalizeColor", makeCallbackF(Entity_normalizeColor) );
632 GlobalCommands_insert( "ConnectSelection", makeCallbackF(Entity_connectSelected), Accelerator( 'K', (GdkModifierType)GDK_CONTROL_MASK ) );
633 GlobalCommands_insert( "KillConnectSelection", makeCallbackF(Entity_killconnectSelected), Accelerator( 'K', (GdkModifierType)( GDK_SHIFT_MASK ) ) );
634 GlobalCommands_insert( "GroupSelection", makeCallbackF(Entity_groupSelected) );
635 GlobalCommands_insert( "UngroupSelection", makeCallbackF(Entity_ungroupSelected) );
636
637 GlobalToggles_insert( "ShowLightRadiuses", makeCallbackF( ShowLightRadiiToggle ), ToggleItem::AddCallbackCaller( g_show_lightradii ) );
638
639 GlobalPreferenceSystem().registerPreference( "SI_Colors5", make_property_string( g_entity_globals.color_entity ) );
640 GlobalPreferenceSystem().registerPreference( "LastLightIntensity", make_property_string( g_iLastLightIntensity ) );
641
642 // Entity_registerPreferencesPage();
643 }
644
645 void Entity_Destroy(){
646 }