2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
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.
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.
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
25 #include "iselection.h"
27 #include "ifilesystem.h"
31 #include "eclasslib.h"
35 #include "stream/stringstream.h"
38 #include "gtkutil/filechooser.h"
42 #include "preferences.h"
44 #include "mainframe.h"
48 #include "uilib/uilib.h"
50 struct entity_globals_t {
54 color_entity(0.0f, 0.0f, 0.0f)
59 entity_globals_t g_entity_globals;
61 class EntitySetKeyValueSelected : public scene::Graph::Walker {
65 EntitySetKeyValueSelected(const char *key, const char *value)
66 : m_key(key), m_value(value)
70 bool pre(const scene::Path &path, scene::Instance &instance) const
75 void post(const scene::Path &path, scene::Instance &instance) const
77 Entity *entity = Node_getEntity(path.top());
79 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected())) {
80 entity->setKeyValue(m_key, m_value);
85 class EntitySetClassnameSelected : public scene::Graph::Walker {
86 const char *m_classname;
88 EntitySetClassnameSelected(const char *classname)
89 : m_classname(classname)
93 bool pre(const scene::Path &path, scene::Instance &instance) const
98 void post(const scene::Path &path, scene::Instance &instance) const
100 Entity *entity = Node_getEntity(path.top());
102 && (instance.childSelected() || Instance_getSelectable(instance)->isSelected())) {
103 NodeSmartReference node(GlobalEntityCreator().createEntity(
104 GlobalEntityClassManager().findOrInsert(m_classname, node_is_group(path.top()))));
106 EntityCopyingVisitor visitor(*Node_getEntity(node));
108 entity->forEachKeyValue(visitor);
110 NodeSmartReference child(path.top().get());
111 NodeSmartReference parent(path.parent().get());
112 Node_getTraversable(parent)->erase(child);
113 if (Node_getTraversable(child) != 0
114 && Node_getTraversable(node) != 0
115 && node_is_group(node)) {
116 parentBrushes(child, node);
118 Node_getTraversable(parent)->insert(node);
123 void Scene_EntitySetKeyValue_Selected(const char *key, const char *value)
125 GlobalSceneGraph().traverse(EntitySetKeyValueSelected(key, value));
128 void Scene_EntitySetClassname_Selected(const char *classname)
130 GlobalSceneGraph().traverse(EntitySetClassnameSelected(classname));
134 void Entity_ungroupSelected()
136 if (GlobalSelectionSystem().countSelected() < 1) {
140 UndoableCommand undo("ungroupSelectedEntities");
142 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
143 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
145 scene::Instance &instance = GlobalSelectionSystem().ultimateSelected();
146 scene::Path path = instance.path();
148 if (!Node_isEntity(path.top())) {
152 if (Node_getEntity(path.top()) != 0
153 && node_is_group(path.top())) {
154 if (world_path.top().get_pointer() != path.top().get_pointer()) {
155 parentBrushes(path.top(), world_path.top());
156 Path_deleteTop(path);
162 class EntityFindSelected : public scene::Graph::Walker {
164 mutable const scene::Path *groupPath;
165 mutable scene::Instance *groupInstance;
167 EntityFindSelected() : groupPath(0), groupInstance(0)
171 bool pre(const scene::Path &path, scene::Instance &instance) const
176 void post(const scene::Path &path, scene::Instance &instance) const
178 Entity *entity = Node_getEntity(path.top());
180 && Instance_getSelectable(instance)->isSelected()
181 && node_is_group(path.top())
184 groupInstance = &instance;
189 class EntityGroupSelected : public scene::Graph::Walker {
190 NodeSmartReference group, worldspawn;
191 //typedef std::pair<NodeSmartReference, NodeSmartReference> DeletionPair;
192 //Stack<DeletionPair> deleteme;
194 EntityGroupSelected(const scene::Path &p) : group(p.top().get()), worldspawn(Map_FindOrInsertWorldspawn(g_map))
198 bool pre(const scene::Path &path, scene::Instance &instance) const
203 void post(const scene::Path &path, scene::Instance &instance) const
205 Selectable *selectable = Instance_getSelectable(instance);
206 if (selectable && selectable->isSelected()) {
207 Entity *entity = Node_getEntity(path.top());
208 if (entity == 0 && Node_isPrimitive(path.top())) {
209 NodeSmartReference child(path.top().get());
210 NodeSmartReference parent(path.parent().get());
212 if (path.size() >= 3 && parent != worldspawn) {
213 NodeSmartReference parentparent(path[path.size() - 3].get());
215 Node_getTraversable(parent)->erase(child);
216 Node_getTraversable(group)->insert(child);
218 if (Node_getTraversable(parent)->empty()) {
219 //deleteme.push(DeletionPair(parentparent, parent));
220 Node_getTraversable(parentparent)->erase(parent);
223 Node_getTraversable(parent)->erase(child);
224 Node_getTraversable(group)->insert(child);
231 void Entity_groupSelected()
233 if (GlobalSelectionSystem().countSelected() < 1) {
237 UndoableCommand undo("groupSelectedEntities");
239 scene::Path world_path(makeReference(GlobalSceneGraph().root()));
240 world_path.push(makeReference(Map_FindOrInsertWorldspawn(g_map)));
242 EntityFindSelected fse;
243 GlobalSceneGraph().traverse(fse);
245 GlobalSceneGraph().traverse(EntityGroupSelected(*fse.groupPath));
247 GlobalSceneGraph().traverse(EntityGroupSelected(world_path));
252 void Entity_connectSelected()
254 if (GlobalSelectionSystem().countSelected() == 2) {
255 GlobalEntityCreator().connectEntities(
256 GlobalSelectionSystem().penultimateSelected().path(),
257 GlobalSelectionSystem().ultimateSelected().path(),
261 globalErrorStream() << "entityConnectSelected: exactly two instances must be selected\n";
265 void Entity_killconnectSelected()
267 if (GlobalSelectionSystem().countSelected() == 2) {
268 GlobalEntityCreator().connectEntities(
269 GlobalSelectionSystem().penultimateSelected().path(),
270 GlobalSelectionSystem().ultimateSelected().path(),
274 globalErrorStream() << "entityKillConnectSelected: exactly two instances must be selected\n";
278 AABB Doom3Light_getBounds(const AABB &workzone)
282 Vector3 defaultRadius(300, 300, 300);
283 if (!string_parse_vector3(
284 EntityClass_valueForKey(*GlobalEntityClassManager().findOrInsert("light", false), "light_radius"),
286 globalErrorStream() << "Doom3Light_getBounds: failed to parse default light radius\n";
289 if (aabb.extents[0] == 0) {
290 aabb.extents[0] = defaultRadius[0];
292 if (aabb.extents[1] == 0) {
293 aabb.extents[1] = defaultRadius[1];
295 if (aabb.extents[2] == 0) {
296 aabb.extents[2] = defaultRadius[2];
299 if (aabb_valid(aabb)) {
302 return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
305 int g_iLastLightIntensity;
307 void Entity_createFromSelection(const char *name, const Vector3 &origin)
310 if ( string_equal_nocase( name, "worldspawn" ) ) {
311 ui::alert( MainFrame_getWindow( ), "Can't create an entity with worldspawn.", "info" );
316 EntityClass *entityClass = GlobalEntityClassManager().findOrInsert(name, true);
318 bool isModel = (string_compare_nocase_n(name, "misc_", 5) == 0 &&
319 string_equal_nocase(name + string_length(name) - 5, "model")) // misc_*model (also misc_model)
320 || string_equal_nocase(name, "model_static")
321 || (GlobalSelectionSystem().countSelected() == 0 && string_equal_nocase(name, "func_static"));
323 bool brushesSelected = Scene_countSelectedBrushes(GlobalSceneGraph()) != 0;
325 if (!(entityClass->fixedsize || isModel) && !brushesSelected) {
326 globalErrorStream() << "failed to create a group entity - no brushes are selected\n";
330 AABB workzone(aabb_for_minmax(Select_getWorkZone().d_work_min, Select_getWorkZone().d_work_max));
333 NodeSmartReference node(GlobalEntityCreator().createEntity(entityClass));
335 Node_getTraversable(GlobalSceneGraph().root())->insert(node);
337 scene::Path entitypath(makeReference(GlobalSceneGraph().root()));
338 entitypath.push(makeReference(node.get()));
339 scene::Instance &instance = findInstance(entitypath);
341 if (entityClass->fixedsize || (isModel && !brushesSelected)) {
344 Transformable *transform = Instance_getTransformable(instance);
345 if (transform != 0) {
346 transform->setType(TRANSFORM_PRIMITIVE);
347 transform->setTranslation(origin);
348 transform->freezeTransform();
351 GlobalSelectionSystem().setSelectedAll(false);
353 Instance_setSelected(instance, true);
355 if (g_pGameDescription->mGameType == "doom3") {
356 Node_getEntity(node)->setKeyValue("model", Node_getEntity(node)->getKeyValue("name"));
359 Scene_parentSelectedBrushesToEntity(GlobalSceneGraph(), node);
360 Scene_forEachChildSelectable(SelectableSetSelected(true), instance.path());
363 // tweaking: when right clic dropping a light entity, ask for light value in a custom dialog box
366 if (g_pGameDescription->mGameType == "hl") {
367 // FIXME - Hydra: really we need a combined light AND color dialog for halflife.
368 if (string_equal_nocase(name, "light")
369 || string_equal_nocase(name, "light_environment")
370 || string_equal_nocase(name, "light_spot")) {
371 int intensity = g_iLastLightIntensity;
373 if (DoLightIntensityDlg(&intensity) == eIDOK) {
374 g_iLastLightIntensity = intensity;
376 sprintf(buf, "255 255 255 %d", intensity);
377 Node_getEntity(node)->setKeyValue("_light", buf);
380 } else if (string_equal_nocase(name, "light")) {
381 if (g_pGameDescription->mGameType != "doom3") {
382 int intensity = g_iLastLightIntensity;
384 if (DoLightIntensityDlg(&intensity) == eIDOK) {
385 g_iLastLightIntensity = intensity;
387 sprintf(buf, "%d", intensity);
388 Node_getEntity(node)->setKeyValue("light", buf);
390 } else if (brushesSelected) { // use workzone to set light position/size for doom3 lights, if there are brushes selected
391 AABB bounds(Doom3Light_getBounds(workzone));
392 StringOutputStream key(64);
393 key << bounds.origin[0] << " " << bounds.origin[1] << " " << bounds.origin[2];
394 Node_getEntity(node)->setKeyValue("origin", key.c_str());
396 key << bounds.extents[0] << " " << bounds.extents[1] << " " << bounds.extents[2];
397 Node_getEntity(node)->setKeyValue("light_radius", key.c_str());
402 const char *model = misc_model_dialog(MainFrame_getWindow());
404 Node_getEntity(node)->setKeyValue("model", model);
410 bool DoNormalisedColor( Vector3& color ){
411 if ( !color_dialog( MainFrame_getWindow( ), color ) ) {
415 ** scale colors so that at least one component is at 1.0F
418 float largest = 0.0F;
420 if ( color[0] > largest ) {
423 if ( color[1] > largest ) {
426 if ( color[2] > largest ) {
430 if ( largest == 0.0F ) {
437 float scaler = 1.0F / largest;
448 void NormalizeColor(Vector3 &color)
450 // scale colors so that at least one component is at 1.0F
452 float largest = 0.0F;
454 if (color[0] > largest) {
457 if (color[1] > largest) {
460 if (color[2] > largest) {
464 if (largest == 0.0F) {
469 float scaler = 1.0F / largest;
477 void Entity_normalizeColor()
479 if (GlobalSelectionSystem().countSelected() != 0) {
480 const scene::Path &path = GlobalSelectionSystem().ultimateSelected().path();
481 Entity *entity = Node_getEntity(path.top());
484 const char *strColor = entity->getKeyValue("_color");
485 if (!string_empty(strColor)) {
487 if (string_parse_vector3(strColor, rgb)) {
488 g_entity_globals.color_entity = rgb;
489 NormalizeColor(g_entity_globals.color_entity);
492 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
493 g_entity_globals.color_entity[1],
494 g_entity_globals.color_entity[2]);
496 Scene_EntitySetKeyValue_Selected("_color", buffer);
503 void Entity_setColour()
505 if (GlobalSelectionSystem().countSelected() != 0) {
506 bool normalize = false;
507 const scene::Path &path = GlobalSelectionSystem().ultimateSelected().path();
508 Entity *entity = Node_getEntity(path.top());
511 const char *strColor = entity->getKeyValue("_color");
512 if (!string_empty(strColor)) {
514 if (string_parse_vector3(strColor, rgb)) {
515 g_entity_globals.color_entity = rgb;
519 if (g_pGameDescription->mGameType == "doom3") {
523 if (color_dialog(MainFrame_getWindow(), g_entity_globals.color_entity)) {
525 NormalizeColor(g_entity_globals.color_entity);
529 sprintf(buffer, "%g %g %g", g_entity_globals.color_entity[0],
530 g_entity_globals.color_entity[1],
531 g_entity_globals.color_entity[2]);
533 Scene_EntitySetKeyValue_Selected("_color", buffer);
539 const char *misc_model_dialog(ui::Widget parent)
541 StringOutputStream buffer(1024);
543 buffer << g_qeglobals.m_userGamePath.c_str() << "models/";
545 if (!file_readable(buffer.c_str())) {
548 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
551 const char *filename = parent.file_dialog(TRUE, "Choose Model", buffer.c_str(), ModelLoader::Name());
553 // use VFS to get the correct relative path
554 const char *relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
555 if (relative == filename) {
556 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
564 static void Export(const EntityCreator &self, const Callback<void(bool)> &returnz)
566 returnz(self.getLightRadii());
569 static void Import(EntityCreator &self, bool value)
571 self.setLightRadii(value);
575 void Entity_constructPreferences(PreferencesPage &page)
578 "Show", "Light Radii",
579 make_property<LightRadii>(GlobalEntityCreator())
583 void Entity_constructPage(PreferenceGroup &group)
585 PreferencesPage page(group.createPage("Entities", "Entity Display Preferences"));
586 Entity_constructPreferences(page);
589 void Entity_registerPreferencesPage()
591 PreferencesDialog_addDisplayPage(makeCallbackF(Entity_constructPage));
595 void Entity_constructMenu(ui::Menu menu)
597 create_menu_item_with_mnemonic(menu, "_Regroup", "GroupSelection");
598 create_menu_item_with_mnemonic(menu, "_Ungroup", "UngroupSelection");
599 create_menu_item_with_mnemonic(menu, "_Connect", "ConnectSelection");
600 create_menu_item_with_mnemonic(menu, "_KillConnect", "KillConnectSelection");
601 create_menu_item_with_mnemonic(menu, "_Select Color...", "EntityColor");
602 create_menu_item_with_mnemonic(menu, "_Normalize Color...", "NormalizeColor");
606 #include "preferencesystem.h"
607 #include "stringio.h"
609 void Entity_Construct()
611 GlobalCommands_insert("EntityColor", makeCallbackF(Entity_setColour), Accelerator('K'));
612 GlobalCommands_insert("NormalizeColor", makeCallbackF(Entity_normalizeColor));
613 GlobalCommands_insert("ConnectSelection", makeCallbackF(Entity_connectSelected),
614 Accelerator('K', (GdkModifierType) GDK_CONTROL_MASK));
615 GlobalCommands_insert("KillConnectSelection", makeCallbackF(Entity_killconnectSelected),
616 Accelerator('K', (GdkModifierType) (GDK_SHIFT_MASK)));
617 GlobalCommands_insert("GroupSelection", makeCallbackF(Entity_groupSelected));
618 GlobalCommands_insert("UngroupSelection", makeCallbackF(Entity_ungroupSelected));
620 GlobalPreferenceSystem().registerPreference("SI_Colors5", make_property_string(g_entity_globals.color_entity));
621 GlobalPreferenceSystem().registerPreference("LastLightIntensity", make_property_string(g_iLastLightIntensity));
623 Entity_registerPreferencesPage();
626 void Entity_Destroy()