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
22 #include "entityinspector.h"
24 #include "debugging/debugging.h"
28 #include "ifilesystem.h"
30 #include "iscenegraph.h"
31 #include "iselection.h"
36 #include <gdk/gdkkeysyms.h>
37 #include <uilib/uilib.h>
41 #include "eclasslib.h"
43 #include "generic/callback.h"
45 #include "stream/stringstream.h"
46 #include "moduleobserver.h"
50 #include "gtkutil/accelerator.h"
51 #include "gtkutil/dialog.h"
52 #include "gtkutil/filechooser.h"
53 #include "gtkutil/messagebox.h"
54 #include "gtkutil/nonmodal.h"
55 #include "gtkutil/button.h"
56 #include "gtkutil/entry.h"
57 #include "gtkutil/container.h"
63 #include "mainframe.h"
64 #include "textureentry.h"
65 #include "groupdialog.h"
67 ui::Entry numeric_entry_new()
69 auto entry = ui::Entry(ui::New);
71 entry.dimensions(64, -1);
76 typedef std::map<CopiedString, CopiedString> KeyValues;
77 KeyValues g_selectedKeyValues;
78 KeyValues g_selectedDefaultKeyValues;
81 const char *SelectedEntity_getValueForKey(const char *key)
84 KeyValues::const_iterator i = g_selectedKeyValues.find(key);
85 if (i != g_selectedKeyValues.end()) {
86 return (*i).second.c_str();
90 KeyValues::const_iterator i = g_selectedDefaultKeyValues.find(key);
91 if (i != g_selectedDefaultKeyValues.end()) {
92 return (*i).second.c_str();
98 void Scene_EntitySetKeyValue_Selected_Undoable(const char *key, const char *value)
100 StringOutputStream command(256);
101 command << "entitySetKeyValue -key " << makeQuoted(key) << " -value " << makeQuoted(value);
102 UndoableCommand undo(command.c_str());
103 Scene_EntitySetKeyValue_Selected(key, value);
106 class EntityAttribute {
108 virtual ~EntityAttribute() = default;
110 virtual ui::Widget getWidget() const = 0;
112 virtual void update() = 0;
114 virtual void release() = 0;
117 class BooleanAttribute : public EntityAttribute {
119 ui::CheckButton m_check;
121 static gboolean toggled(ui::Widget widget, BooleanAttribute *self)
128 BooleanAttribute(const char *key) :
132 auto check = ui::CheckButton(ui::New);
137 guint handler = check.connect("toggled", G_CALLBACK(toggled), this);
138 g_object_set_data(G_OBJECT(check), "handler", gint_to_pointer(handler));
143 ui::Widget getWidget() const
155 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), m_check.active() ? "1" : "0");
158 typedef MemberCaller<BooleanAttribute, void(), &BooleanAttribute::apply> ApplyCaller;
162 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
163 if (!string_empty(value)) {
164 toggle_button_set_active_no_signal(m_check, atoi(value) != 0);
166 toggle_button_set_active_no_signal(m_check, false);
170 typedef MemberCaller<BooleanAttribute, void(), &BooleanAttribute::update> UpdateCaller;
174 class StringAttribute : public EntityAttribute {
177 NonModalEntry m_nonModal;
179 StringAttribute(const char *key) :
182 m_nonModal(ApplyCaller(*this), UpdateCaller(*this))
184 auto entry = ui::Entry(ui::New);
186 entry.dimensions(50, -1);
189 m_nonModal.connect(m_entry);
192 ui::Widget getWidget() const
197 ui::Entry getEntry() const
209 StringOutputStream value(64);
210 value << m_entry.text();
211 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), value.c_str());
214 typedef MemberCaller<StringAttribute, void(), &StringAttribute::apply> ApplyCaller;
218 StringOutputStream value(64);
219 value << SelectedEntity_getValueForKey(m_key.c_str());
220 m_entry.text(value.c_str());
223 typedef MemberCaller<StringAttribute, void(), &StringAttribute::update> UpdateCaller;
226 class ShaderAttribute : public StringAttribute {
228 ShaderAttribute(const char *key) : StringAttribute(key)
230 GlobalShaderEntryCompletion::instance().connect(StringAttribute::getEntry());
235 class ModelAttribute : public EntityAttribute {
237 BrowsedPathEntry m_entry;
238 NonModalEntry m_nonModal;
240 ModelAttribute(const char *key) :
242 m_entry(BrowseCaller(*this)),
243 m_nonModal(ApplyCaller(*this), UpdateCaller(*this))
245 m_nonModal.connect(m_entry.m_entry.m_entry);
253 ui::Widget getWidget() const
255 return m_entry.m_entry.m_frame;
260 StringOutputStream value(64);
261 value << m_entry.m_entry.m_entry.text();
262 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), value.c_str());
265 typedef MemberCaller<ModelAttribute, void(), &ModelAttribute::apply> ApplyCaller;
269 StringOutputStream value(64);
270 value << SelectedEntity_getValueForKey(m_key.c_str());
271 m_entry.m_entry.m_entry.text(value.c_str());
274 typedef MemberCaller<ModelAttribute, void(), &ModelAttribute::update> UpdateCaller;
276 void browse(const BrowsedPathEntry::SetPathCallback &setPath)
278 const char *filename = misc_model_dialog(m_entry.m_entry.m_frame.window());
286 typedef MemberCaller<ModelAttribute, void(
287 const BrowsedPathEntry::SetPathCallback &), &ModelAttribute::browse> BrowseCaller;
290 const char *browse_sound(ui::Widget parent)
292 StringOutputStream buffer(1024);
294 buffer << g_qeglobals.m_userGamePath.c_str() << "sound/";
296 if (!file_readable(buffer.c_str())) {
299 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
302 const char *filename = parent.file_dialog(TRUE, "Open Wav File", buffer.c_str(), "sound");
304 const char *relative = path_make_relative(filename, GlobalFileSystem().findRoot(filename));
305 if (relative == filename) {
306 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
313 class SoundAttribute : public EntityAttribute {
315 BrowsedPathEntry m_entry;
316 NonModalEntry m_nonModal;
318 SoundAttribute(const char *key) :
320 m_entry(BrowseCaller(*this)),
321 m_nonModal(ApplyCaller(*this), UpdateCaller(*this))
323 m_nonModal.connect(m_entry.m_entry.m_entry);
331 ui::Widget getWidget() const
333 return ui::Widget(m_entry.m_entry.m_frame);
338 StringOutputStream value(64);
339 value << m_entry.m_entry.m_entry.text();
340 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), value.c_str());
343 typedef MemberCaller<SoundAttribute, void(), &SoundAttribute::apply> ApplyCaller;
347 StringOutputStream value(64);
348 value << SelectedEntity_getValueForKey(m_key.c_str());
349 m_entry.m_entry.m_entry.text(value.c_str());
352 typedef MemberCaller<SoundAttribute, void(), &SoundAttribute::update> UpdateCaller;
354 void browse(const BrowsedPathEntry::SetPathCallback &setPath)
356 const char *filename = browse_sound(m_entry.m_entry.m_frame.window());
364 typedef MemberCaller<SoundAttribute, void(
365 const BrowsedPathEntry::SetPathCallback &), &SoundAttribute::browse> BrowseCaller;
368 inline double angle_normalised(double angle)
370 return float_mod(angle, 360.0);
373 class AngleAttribute : public EntityAttribute {
376 NonModalEntry m_nonModal;
378 AngleAttribute(const char *key) :
381 m_nonModal(ApplyCaller(*this), UpdateCaller(*this))
383 auto entry = numeric_entry_new();
385 m_nonModal.connect(m_entry);
393 ui::Widget getWidget() const
395 return ui::Widget(m_entry);
400 StringOutputStream angle(32);
401 angle << angle_normalised(entry_get_float(m_entry));
402 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), angle.c_str());
405 typedef MemberCaller<AngleAttribute, void(), &AngleAttribute::apply> ApplyCaller;
409 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
410 if (!string_empty(value)) {
411 StringOutputStream angle(32);
412 angle << angle_normalised(atof(value));
413 m_entry.text(angle.c_str());
419 typedef MemberCaller<AngleAttribute, void(), &AngleAttribute::update> UpdateCaller;
423 typedef const char *String;
424 const String buttons[] = {"up", "down", "z-axis"};
427 class DirectionAttribute : public EntityAttribute {
430 NonModalEntry m_nonModal;
432 NonModalRadio m_nonModalRadio;
433 ui::HBox m_hbox{ui::null};
435 DirectionAttribute(const char *key) :
438 m_nonModal(ApplyCaller(*this), UpdateCaller(*this)),
439 m_radio(RadioHBox_new(STRING_ARRAY_RANGE(buttons))),
440 m_nonModalRadio(ApplyRadioCaller(*this))
442 auto entry = numeric_entry_new();
444 m_nonModal.connect(m_entry);
446 m_nonModalRadio.connect(m_radio.m_radio);
448 m_hbox = ui::HBox(FALSE, 4);
451 m_hbox.pack_start(m_radio.m_hbox, TRUE, TRUE, 0);
452 m_hbox.pack_start(m_entry, TRUE, TRUE, 0);
460 ui::Widget getWidget() const
462 return ui::Widget(m_hbox);
467 StringOutputStream angle(32);
468 angle << angle_normalised(entry_get_float(m_entry));
469 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), angle.c_str());
472 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::apply> ApplyCaller;
476 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
477 if (!string_empty(value)) {
478 float f = float(atof(value));
480 gtk_widget_set_sensitive(m_entry, FALSE);
481 radio_button_set_active_no_signal(m_radio.m_radio, 0);
483 } else if (f == -2) {
484 gtk_widget_set_sensitive(m_entry, FALSE);
485 radio_button_set_active_no_signal(m_radio.m_radio, 1);
488 gtk_widget_set_sensitive(m_entry, TRUE);
489 radio_button_set_active_no_signal(m_radio.m_radio, 2);
490 StringOutputStream angle(32);
491 angle << angle_normalised(f);
492 m_entry.text(angle.c_str());
499 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::update> UpdateCaller;
503 int index = radio_button_get_active(m_radio.m_radio);
505 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), "-1");
506 } else if (index == 1) {
507 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), "-2");
508 } else if (index == 2) {
513 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::applyRadio> ApplyRadioCaller;
523 AnglesEntry() : m_roll(ui::null), m_pitch(ui::null), m_yaw(ui::null)
528 typedef BasicVector3<double> DoubleVector3;
530 class AnglesAttribute : public EntityAttribute {
532 AnglesEntry m_angles;
533 NonModalEntry m_nonModal;
536 AnglesAttribute(const char *key) :
538 m_nonModal(ApplyCaller(*this), UpdateCaller(*this)),
539 m_hbox(ui::HBox(TRUE, 4))
543 auto entry = numeric_entry_new();
544 m_hbox.pack_start(entry, TRUE, TRUE, 0);
545 m_angles.m_pitch = entry;
546 m_nonModal.connect(m_angles.m_pitch);
549 auto entry = numeric_entry_new();
550 m_hbox.pack_start(entry, TRUE, TRUE, 0);
551 m_angles.m_yaw = entry;
552 m_nonModal.connect(m_angles.m_yaw);
555 auto entry = numeric_entry_new();
556 m_hbox.pack_start(entry, TRUE, TRUE, 0);
557 m_angles.m_roll = entry;
558 m_nonModal.connect(m_angles.m_roll);
567 ui::Widget getWidget() const
569 return ui::Widget(m_hbox);
574 StringOutputStream angles(64);
575 angles << angle_normalised(entry_get_float(m_angles.m_pitch))
576 << " " << angle_normalised(entry_get_float(m_angles.m_yaw))
577 << " " << angle_normalised(entry_get_float(m_angles.m_roll));
578 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), angles.c_str());
581 typedef MemberCaller<AnglesAttribute, void(), &AnglesAttribute::apply> ApplyCaller;
585 StringOutputStream angle(32);
586 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
587 if (!string_empty(value)) {
588 DoubleVector3 pitch_yaw_roll;
589 if (!string_parse_vector3(value, pitch_yaw_roll)) {
590 pitch_yaw_roll = DoubleVector3(0, 0, 0);
593 angle << angle_normalised(pitch_yaw_roll.x());
594 m_angles.m_pitch.text(angle.c_str());
597 angle << angle_normalised(pitch_yaw_roll.y());
598 m_angles.m_yaw.text(angle.c_str());
601 angle << angle_normalised(pitch_yaw_roll.z());
602 m_angles.m_roll.text(angle.c_str());
605 m_angles.m_pitch.text("0");
606 m_angles.m_yaw.text("0");
607 m_angles.m_roll.text("0");
611 typedef MemberCaller<AnglesAttribute, void(), &AnglesAttribute::update> UpdateCaller;
620 Vector3Entry() : m_x(ui::null), m_y(ui::null), m_z(ui::null)
625 class Vector3Attribute : public EntityAttribute {
627 Vector3Entry m_vector3;
628 NonModalEntry m_nonModal;
629 ui::Box m_hbox{ui::null};
631 Vector3Attribute(const char *key) :
633 m_nonModal(ApplyCaller(*this), UpdateCaller(*this))
635 m_hbox = ui::HBox(TRUE, 4);
638 auto entry = numeric_entry_new();
639 m_hbox.pack_start(entry, TRUE, TRUE, 0);
640 m_vector3.m_x = entry;
641 m_nonModal.connect(m_vector3.m_x);
644 auto entry = numeric_entry_new();
645 m_hbox.pack_start(entry, TRUE, TRUE, 0);
646 m_vector3.m_y = entry;
647 m_nonModal.connect(m_vector3.m_y);
650 auto entry = numeric_entry_new();
651 m_hbox.pack_start(entry, TRUE, TRUE, 0);
652 m_vector3.m_z = entry;
653 m_nonModal.connect(m_vector3.m_z);
662 ui::Widget getWidget() const
664 return ui::Widget(m_hbox);
669 StringOutputStream vector3(64);
670 vector3 << entry_get_float(m_vector3.m_x)
671 << " " << entry_get_float(m_vector3.m_y)
672 << " " << entry_get_float(m_vector3.m_z);
673 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(), vector3.c_str());
676 typedef MemberCaller<Vector3Attribute, void(), &Vector3Attribute::apply> ApplyCaller;
680 StringOutputStream buffer(32);
681 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
682 if (!string_empty(value)) {
684 if (!string_parse_vector3(value, x_y_z)) {
685 x_y_z = DoubleVector3(0, 0, 0);
689 m_vector3.m_x.text(buffer.c_str());
693 m_vector3.m_y.text(buffer.c_str());
697 m_vector3.m_z.text(buffer.c_str());
700 m_vector3.m_x.text("0");
701 m_vector3.m_y.text("0");
702 m_vector3.m_z.text("0");
706 typedef MemberCaller<Vector3Attribute, void(), &Vector3Attribute::update> UpdateCaller;
709 class NonModalComboBox {
710 Callback<void()> m_changed;
711 guint m_changedHandler;
713 static gboolean changed(ui::ComboBox widget, NonModalComboBox *self)
720 NonModalComboBox(const Callback<void()> &changed) : m_changed(changed), m_changedHandler(0)
724 void connect(ui::ComboBox combo)
726 m_changedHandler = combo.connect("changed", G_CALLBACK(changed), this);
729 void setActive(ui::ComboBox combo, int value)
731 g_signal_handler_disconnect(G_OBJECT(combo), m_changedHandler);
732 gtk_combo_box_set_active(combo, value);
737 class ListAttribute : public EntityAttribute {
739 ui::ComboBox m_combo;
740 NonModalComboBox m_nonModal;
741 const ListAttributeType &m_type;
743 ListAttribute(const char *key, const ListAttributeType &type) :
746 m_nonModal(ApplyCaller(*this)),
749 auto combo = ui::ComboBoxText(ui::New);
751 for (ListAttributeType::const_iterator i = type.begin(); i != type.end(); ++i) {
752 gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(combo), (*i).first.c_str());
756 m_nonModal.connect(combo);
766 ui::Widget getWidget() const
768 return ui::Widget(m_combo);
773 Scene_EntitySetKeyValue_Selected_Undoable(m_key.c_str(),
774 m_type[gtk_combo_box_get_active(m_combo)].second.c_str());
777 typedef MemberCaller<ListAttribute, void(), &ListAttribute::apply> ApplyCaller;
781 const char *value = SelectedEntity_getValueForKey(m_key.c_str());
782 ListAttributeType::const_iterator i = m_type.findValue(value);
783 if (i != m_type.end()) {
784 m_nonModal.setActive(m_combo, static_cast<int>( std::distance(m_type.begin(), i)));
786 m_nonModal.setActive(m_combo, 0);
790 typedef MemberCaller<ListAttribute, void(), &ListAttribute::update> UpdateCaller;
795 ui::Widget g_entity_split1{ui::null};
796 ui::Widget g_entity_split2{ui::null};
797 int g_entitysplit1_position;
798 int g_entitysplit2_position;
800 bool g_entityInspector_windowConstructed = false;
802 ui::TreeView g_entityClassList{ui::null};
803 ui::TextView g_entityClassComment{ui::null};
805 GtkCheckButton *g_entitySpawnflagsCheck[MAX_FLAGS];
807 ui::Entry g_entityKeyEntry{ui::null};
808 ui::Entry g_entityValueEntry{ui::null};
810 ui::ListStore g_entlist_store{ui::null};
811 ui::ListStore g_entprops_store{ui::null};
812 const EntityClass *g_current_flags = 0;
813 const EntityClass *g_current_comment = 0;
814 const EntityClass *g_current_attributes = 0;
816 // the number of active spawnflags
817 int g_spawnflag_count;
818 // table: index, match spawnflag item to the spawnflag index (i.e. which bit)
819 int spawn_table[MAX_FLAGS];
820 // we change the layout depending on how many spawn flags we need to display
821 // the table is a 4x4 in which we need to put the comment box g_entityClassComment and the spawn flags..
822 ui::Table g_spawnflagsTable{ui::null};
824 ui::VBox g_attributeBox{ui::null};
825 typedef std::vector<EntityAttribute *> EntityAttributes;
826 EntityAttributes g_entityAttributes;
829 void GlobalEntityAttributes_clear()
831 for (EntityAttributes::iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i) {
834 g_entityAttributes.clear();
837 class GetKeyValueVisitor : public Entity::Visitor {
838 KeyValues &m_keyvalues;
840 GetKeyValueVisitor(KeyValues &keyvalues)
841 : m_keyvalues(keyvalues)
845 void visit(const char *key, const char *value)
847 m_keyvalues.insert(KeyValues::value_type(CopiedString(key), CopiedString(value)));
852 void Entity_GetKeyValues(const Entity &entity, KeyValues &keyvalues, KeyValues &defaultValues)
854 GetKeyValueVisitor visitor(keyvalues);
856 entity.forEachKeyValue(visitor);
858 const EntityClassAttributes &attributes = entity.getEntityClass().m_attributes;
860 for (EntityClassAttributes::const_iterator i = attributes.begin(); i != attributes.end(); ++i) {
861 defaultValues.insert(KeyValues::value_type((*i).first, (*i).second.m_value));
865 void Entity_GetKeyValues_Selected(KeyValues &keyvalues, KeyValues &defaultValues)
867 class EntityGetKeyValues : public SelectionSystem::Visitor {
868 KeyValues &m_keyvalues;
869 KeyValues &m_defaultValues;
870 mutable std::set<Entity *> m_visited;
872 EntityGetKeyValues(KeyValues &keyvalues, KeyValues &defaultValues)
873 : m_keyvalues(keyvalues), m_defaultValues(defaultValues)
877 void visit(scene::Instance &instance) const
879 Entity *entity = Node_getEntity(instance.path().top());
880 if (entity == 0 && instance.path().size() != 1) {
881 entity = Node_getEntity(instance.path().parent());
883 if (entity != 0 && m_visited.insert(entity).second) {
884 Entity_GetKeyValues(*entity, m_keyvalues, m_defaultValues);
887 } visitor(keyvalues, defaultValues);
888 GlobalSelectionSystem().foreachSelected(visitor);
891 const char *keyvalues_valueforkey(KeyValues &keyvalues, const char *key)
893 KeyValues::iterator i = keyvalues.find(CopiedString(key));
894 if (i != keyvalues.end()) {
895 return (*i).second.c_str();
900 class EntityClassListStoreAppend : public EntityClassVisitor {
903 EntityClassListStoreAppend(ui::ListStore store_) : store(store_)
907 void visit(EntityClass *e)
909 store.append(0, e->name(), 1, e);
913 void EntityClassList_fill()
915 EntityClassListStoreAppend append(g_entlist_store);
916 GlobalEntityClassManager().forEach(append);
919 void EntityClassList_clear()
921 g_entlist_store.clear();
924 void SetComment(EntityClass *eclass)
926 if (eclass == g_current_comment) {
930 g_current_comment = eclass;
932 g_entityClassComment.text(eclass->comments());
935 void SurfaceFlags_setEntityClass(EntityClass *eclass)
937 if (eclass == g_current_flags) {
941 g_current_flags = eclass;
943 unsigned int spawnflag_count = 0;
946 // do a first pass to count the spawn flags, don't touch the widgets, we don't know in what state they are
947 for (int i = 0; i < MAX_FLAGS; i++) {
948 if (eclass->flagnames[i] && eclass->flagnames[i][0] != 0 && strcmp(eclass->flagnames[i], "-")) {
949 spawn_table[spawnflag_count] = i;
955 // disable all remaining boxes
956 // NOTE: these boxes might not even be on display
958 for (int i = 0; i < g_spawnflag_count; ++i) {
959 auto widget = ui::CheckButton::from(g_entitySpawnflagsCheck[i]);
960 auto label = ui::Label::from(gtk_bin_get_child(GTK_BIN(widget)));
964 g_spawnflagsTable.remove(widget);
968 g_spawnflag_count = spawnflag_count;
971 for (unsigned int i = 0; (int) i < g_spawnflag_count; ++i) {
972 auto widget = ui::CheckButton::from(g_entitySpawnflagsCheck[i]);
975 StringOutputStream str(16);
976 str << LowerCase(eclass->flagnames[spawn_table[i]]);
978 g_spawnflagsTable.attach(widget, {i % 4, i % 4 + 1, i / 4, i / 4 + 1}, {GTK_FILL, GTK_FILL});
981 auto label = ui::Label::from(gtk_bin_get_child(GTK_BIN(widget)));
982 label.text(str.c_str());
987 void EntityClassList_selectEntityClass(EntityClass *eclass)
989 auto model = g_entlist_store;
991 for (gboolean good = gtk_tree_model_get_iter_first(model, &iter);
992 good != FALSE; good = gtk_tree_model_iter_next(model, &iter)) {
994 gtk_tree_model_get(model, &iter, 0, &text, -1);
995 if (strcmp(text, eclass->name()) == 0) {
996 auto view = ui::TreeView(g_entityClassList);
997 auto path = gtk_tree_model_get_path(model, &iter);
998 gtk_tree_selection_select_path(gtk_tree_view_get_selection(view), path);
999 if (gtk_widget_get_realized(view)) {
1000 gtk_tree_view_scroll_to_cell(view, path, 0, FALSE, 0, 0);
1002 gtk_tree_path_free(path);
1009 void EntityInspector_appendAttribute(const char *name, EntityAttribute &attribute)
1011 auto row = DialogRow_new(name, attribute.getWidget());
1012 DialogVBox_packRow(ui::VBox(g_attributeBox), row);
1016 template<typename Attribute>
1017 class StatelessAttributeCreator {
1019 static EntityAttribute *create(const char *name)
1021 return new Attribute(name);
1025 class EntityAttributeFactory {
1026 typedef EntityAttribute *( *CreateFunc )(const char *name);
1028 typedef std::map<const char *, CreateFunc, RawStringLess> Creators;
1029 Creators m_creators;
1031 EntityAttributeFactory()
1033 m_creators.insert(Creators::value_type("string", &StatelessAttributeCreator<StringAttribute>::create));
1034 m_creators.insert(Creators::value_type("color", &StatelessAttributeCreator<StringAttribute>::create));
1035 m_creators.insert(Creators::value_type("integer", &StatelessAttributeCreator<StringAttribute>::create));
1036 m_creators.insert(Creators::value_type("real", &StatelessAttributeCreator<StringAttribute>::create));
1037 m_creators.insert(Creators::value_type("shader", &StatelessAttributeCreator<ShaderAttribute>::create));
1038 m_creators.insert(Creators::value_type("boolean", &StatelessAttributeCreator<BooleanAttribute>::create));
1039 m_creators.insert(Creators::value_type("angle", &StatelessAttributeCreator<AngleAttribute>::create));
1040 m_creators.insert(Creators::value_type("direction", &StatelessAttributeCreator<DirectionAttribute>::create));
1041 m_creators.insert(Creators::value_type("angles", &StatelessAttributeCreator<AnglesAttribute>::create));
1042 m_creators.insert(Creators::value_type("model", &StatelessAttributeCreator<ModelAttribute>::create));
1043 m_creators.insert(Creators::value_type("sound", &StatelessAttributeCreator<SoundAttribute>::create));
1044 m_creators.insert(Creators::value_type("vector3", &StatelessAttributeCreator<Vector3Attribute>::create));
1045 m_creators.insert(Creators::value_type("real3", &StatelessAttributeCreator<Vector3Attribute>::create));
1048 EntityAttribute *create(const char *type, const char *name)
1050 Creators::iterator i = m_creators.find(type);
1051 if (i != m_creators.end()) {
1052 return (*i).second(name);
1054 const ListAttributeType *listType = GlobalEntityClassManager().findListType(type);
1055 if (listType != 0) {
1056 return new ListAttribute(name, *listType);
1062 typedef Static<EntityAttributeFactory> GlobalEntityAttributeFactory;
1064 void EntityInspector_setEntityClass(EntityClass *eclass)
1066 EntityClassList_selectEntityClass(eclass);
1067 SurfaceFlags_setEntityClass(eclass);
1069 if (eclass != g_current_attributes) {
1070 g_current_attributes = eclass;
1072 container_remove_all(g_attributeBox);
1073 GlobalEntityAttributes_clear();
1075 for (EntityClassAttributes::const_iterator i = eclass->m_attributes.begin();
1076 i != eclass->m_attributes.end(); ++i) {
1077 EntityAttribute *attribute = GlobalEntityAttributeFactory::instance().create((*i).second.m_type.c_str(),
1078 (*i).first.c_str());
1079 if (attribute != 0) {
1080 g_entityAttributes.push_back(attribute);
1081 EntityInspector_appendAttribute(EntityClassAttributePair_getName(*i), *g_entityAttributes.back());
1087 void EntityInspector_updateSpawnflags()
1090 int f = atoi(SelectedEntity_getValueForKey("spawnflags"));
1091 for (int i = 0; i < g_spawnflag_count; ++i) {
1092 int v = !!(f & (1 << spawn_table[i]));
1094 toggle_button_set_active_no_signal(ui::ToggleButton::from(g_entitySpawnflagsCheck[i]), v);
1098 // take care of the remaining ones
1099 for (int i = g_spawnflag_count; i < MAX_FLAGS; ++i) {
1100 toggle_button_set_active_no_signal(ui::ToggleButton::from(g_entitySpawnflagsCheck[i]), FALSE);
1105 void EntityInspector_applySpawnflags()
1111 for (i = 0; i < g_spawnflag_count; ++i) {
1112 v = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(g_entitySpawnflagsCheck[i]));
1113 f |= v << spawn_table[i];
1116 sprintf(sz, "%i", f);
1117 const char *value = (f == 0) ? "" : sz;
1120 StringOutputStream command;
1121 command << "entitySetFlags -flags " << f;
1122 UndoableCommand undo("entitySetSpawnflags");
1124 Scene_EntitySetKeyValue_Selected("spawnflags", value);
1129 void EntityInspector_updateKeyValues()
1131 g_selectedKeyValues.clear();
1132 g_selectedDefaultKeyValues.clear();
1133 Entity_GetKeyValues_Selected(g_selectedKeyValues, g_selectedDefaultKeyValues);
1135 EntityInspector_setEntityClass(
1136 GlobalEntityClassManager().findOrInsert(keyvalues_valueforkey(g_selectedKeyValues, "classname"), false));
1138 EntityInspector_updateSpawnflags();
1140 ui::ListStore store = g_entprops_store;
1142 // save current key/val pair around filling epair box
1143 // row_select wipes it and sets to first in list
1144 CopiedString strKey(g_entityKeyEntry.text());
1145 CopiedString strVal(g_entityValueEntry.text());
1148 // Walk through list and add pairs
1149 for (KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i) {
1150 StringOutputStream key(64);
1151 key << (*i).first.c_str();
1152 StringOutputStream value(64);
1153 value << (*i).second.c_str();
1154 store.append(0, key.c_str(), 1, value.c_str());
1157 g_entityKeyEntry.text(strKey.c_str());
1158 g_entityValueEntry.text(strVal.c_str());
1160 for (EntityAttributes::const_iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i) {
1165 class EntityInspectorDraw {
1166 IdleDraw m_idleDraw;
1168 EntityInspectorDraw() : m_idleDraw(makeCallbackF(EntityInspector_updateKeyValues))
1174 m_idleDraw.queueDraw();
1178 EntityInspectorDraw g_EntityInspectorDraw;
1181 void EntityInspector_keyValueChanged()
1183 g_EntityInspectorDraw.queueDraw();
1186 void EntityInspector_selectionChanged(const Selectable &)
1188 EntityInspector_keyValueChanged();
1191 // Creates a new entity based on the currently selected brush and entity type.
1193 void EntityClassList_createEntity()
1195 auto view = g_entityClassList;
1197 // find out what type of entity we are trying to create
1198 GtkTreeModel *model;
1200 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(g_entityClassList), &model, &iter) == FALSE) {
1201 ui::alert(view.window(), "You must have a selected class to create an entity", "info");
1206 gtk_tree_model_get(model, &iter, 0, &text, -1);
1209 StringOutputStream command;
1210 command << "entityCreate -class " << text;
1212 UndoableCommand undo(command.c_str());
1214 Entity_createFromSelection(text, g_vector3_identity);
1219 void EntityInspector_applyKeyValue()
1221 // Get current selection text
1222 StringOutputStream key(64);
1223 key << gtk_entry_get_text(g_entityKeyEntry);
1224 StringOutputStream value(64);
1225 value << gtk_entry_get_text(g_entityValueEntry);
1228 // TTimo: if you change the classname to worldspawn you won't merge back in the structural brushes but create a parasite entity
1229 if (!strcmp(key.c_str(), "classname") && !strcmp(value.c_str(), "worldspawn")) {
1230 ui::alert(g_entityKeyEntry.window(), "Cannot change \"classname\" key back to worldspawn.", 0,
1231 ui::alert_type::OK);
1236 // RR2DO2: we don't want spaces in entity keys
1237 if (strstr(key.c_str(), " ")) {
1238 ui::alert(g_entityKeyEntry.window(), "No spaces are allowed in entity keys.", 0, ui::alert_type::OK);
1242 if (strcmp(key.c_str(), "classname") == 0) {
1243 StringOutputStream command;
1244 command << "entitySetClass -class " << value.c_str();
1245 UndoableCommand undo(command.c_str());
1246 Scene_EntitySetClassname_Selected(value.c_str());
1248 Scene_EntitySetKeyValue_Selected_Undoable(key.c_str(), value.c_str());
1252 void EntityInspector_clearKeyValue()
1254 // Get current selection text
1255 StringOutputStream key(64);
1256 key << gtk_entry_get_text(g_entityKeyEntry);
1258 if (strcmp(key.c_str(), "classname") != 0) {
1259 StringOutputStream command;
1260 command << "entityDeleteKey -key " << key.c_str();
1261 UndoableCommand undo(command.c_str());
1262 Scene_EntitySetKeyValue_Selected(key.c_str(), "");
1266 void EntityInspector_clearAllKeyValues()
1268 UndoableCommand undo("entityClear");
1270 // remove all keys except classname
1271 for (KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i) {
1272 if (strcmp((*i).first.c_str(), "classname") != 0) {
1273 Scene_EntitySetKeyValue_Selected((*i).first.c_str(), "");
1278 // =============================================================================
1281 static void EntityClassList_selection_changed(ui::TreeSelection selection, gpointer data)
1283 GtkTreeModel *model;
1284 GtkTreeIter selected;
1285 if (gtk_tree_selection_get_selected(selection, &model, &selected)) {
1286 EntityClass *eclass;
1287 gtk_tree_model_get(model, &selected, 1, &eclass, -1);
1294 static gint EntityClassList_button_press(ui::Widget widget, GdkEventButton *event, gpointer data)
1296 if (event->type == GDK_2BUTTON_PRESS) {
1297 EntityClassList_createEntity();
1303 static gint EntityClassList_keypress(ui::Widget widget, GdkEventKey *event, gpointer data)
1305 unsigned int code = gdk_keyval_to_upper(event->keyval);
1307 if (event->keyval == GDK_KEY_Return) {
1308 EntityClassList_createEntity();
1312 // select the entity that starts with the key pressed
1313 if (code <= 'Z' && code >= 'A') {
1314 auto view = ui::TreeView(g_entityClassList);
1315 GtkTreeModel *model;
1317 if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(view), &model, &iter) == FALSE
1318 || gtk_tree_model_iter_next(model, &iter) == FALSE) {
1319 gtk_tree_model_get_iter_first(model, &iter);
1322 for (std::size_t count = gtk_tree_model_iter_n_children(model, 0); count > 0; --count) {
1324 gtk_tree_model_get(model, &iter, 0, &text, -1);
1326 if (toupper(text[0]) == (int) code) {
1327 auto path = gtk_tree_model_get_path(model, &iter);
1328 gtk_tree_selection_select_path(gtk_tree_view_get_selection(view), path);
1329 if (gtk_widget_get_realized(view)) {
1330 gtk_tree_view_scroll_to_cell(view, path, 0, FALSE, 0, 0);
1332 gtk_tree_path_free(path);
1338 if (gtk_tree_model_iter_next(model, &iter) == FALSE) {
1339 gtk_tree_model_get_iter_first(model, &iter);
1348 static void EntityProperties_selection_changed(ui::TreeSelection selection, gpointer data)
1350 // find out what type of entity we are trying to create
1351 GtkTreeModel *model;
1353 if (gtk_tree_selection_get_selected(selection, &model, &iter) == FALSE) {
1359 gtk_tree_model_get(model, &iter, 0, &key, 1, &val, -1);
1361 g_entityKeyEntry.text(key);
1362 g_entityValueEntry.text(val);
1368 static void SpawnflagCheck_toggled(ui::Widget widget, gpointer data)
1370 EntityInspector_applySpawnflags();
1373 static gint EntityEntry_keypress(ui::Entry widget, GdkEventKey *event, gpointer data)
1375 if (event->keyval == GDK_KEY_Return) {
1376 if (widget._handle == g_entityKeyEntry._handle) {
1377 g_entityValueEntry.text("");
1378 gtk_window_set_focus(widget.window(), g_entityValueEntry);
1380 EntityInspector_applyKeyValue();
1384 if (event->keyval == GDK_KEY_Escape) {
1385 gtk_window_set_focus(widget.window(), NULL);
1392 void EntityInspector_destroyWindow(ui::Widget widget, gpointer data)
1394 g_entitysplit1_position = gtk_paned_get_position(GTK_PANED(g_entity_split1));
1395 g_entitysplit2_position = gtk_paned_get_position(GTK_PANED(g_entity_split2));
1397 g_entityInspector_windowConstructed = false;
1398 GlobalEntityAttributes_clear();
1401 ui::Widget EntityInspector_constructWindow(ui::Window toplevel)
1403 auto vbox = ui::VBox(FALSE, 2);
1405 gtk_container_set_border_width(GTK_CONTAINER(vbox), 2);
1407 vbox.connect("destroy", G_CALLBACK(EntityInspector_destroyWindow), 0);
1410 auto split1 = ui::VPaned(ui::New);
1411 vbox.pack_start(split1, TRUE, TRUE, 0);
1414 g_entity_split1 = split1;
1417 ui::Widget split2 = ui::VPaned(ui::New);
1418 gtk_paned_add1(GTK_PANED(split1), split2);
1421 g_entity_split2 = split2;
1425 auto scr = ui::ScrolledWindow(ui::New);
1427 gtk_paned_add1(GTK_PANED(split2), scr);
1428 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1429 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1432 ui::ListStore store = ui::ListStore::from(gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER));
1434 auto view = ui::TreeView(ui::TreeModel::from(store._handle));
1435 gtk_tree_view_set_enable_search(view, FALSE);
1436 gtk_tree_view_set_headers_visible(view, FALSE);
1437 view.connect("button_press_event", G_CALLBACK(EntityClassList_button_press), 0);
1438 view.connect("key_press_event", G_CALLBACK(EntityClassList_keypress), 0);
1441 auto renderer = ui::CellRendererText(ui::New);
1442 auto column = ui::TreeViewColumn("Key", renderer, {{"text", 0}});
1443 gtk_tree_view_append_column(view, column);
1447 auto selection = ui::TreeSelection::from(gtk_tree_view_get_selection(view));
1448 selection.connect("changed", G_CALLBACK(EntityClassList_selection_changed), 0);
1456 g_entityClassList = view;
1457 g_entlist_store = store;
1462 auto scr = ui::ScrolledWindow(ui::New);
1464 gtk_paned_add2(GTK_PANED(split2), scr);
1465 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
1466 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1469 auto text = ui::TextView(ui::New);
1470 text.dimensions(0, -1); // allow shrinking
1471 gtk_text_view_set_wrap_mode(text, GTK_WRAP_WORD);
1472 gtk_text_view_set_editable(text, FALSE);
1475 g_entityClassComment = text;
1481 ui::Widget split2 = ui::VPaned(ui::New);
1482 gtk_paned_add2(GTK_PANED(split1), split2);
1486 auto vbox2 = ui::VBox(FALSE, 2);
1488 gtk_paned_pack1(GTK_PANED(split2), vbox2, FALSE, FALSE);
1491 // Spawnflags (4 colums wide max, or window gets too wide.)
1492 auto table = ui::Table(4, 4, FALSE);
1493 vbox2.pack_start(table, FALSE, TRUE, 0);
1496 g_spawnflagsTable = table;
1498 for (int i = 0; i < MAX_FLAGS; i++) {
1499 auto check = ui::CheckButton("");
1501 g_object_set_data(G_OBJECT(check), "handler", gint_to_pointer(
1502 check.connect("toggled", G_CALLBACK(SpawnflagCheck_toggled), 0)));
1503 g_entitySpawnflagsCheck[i] = check;
1509 auto scr = ui::ScrolledWindow(ui::New);
1511 vbox2.pack_start(scr, TRUE, TRUE, 0);
1512 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_AUTOMATIC,
1513 GTK_POLICY_AUTOMATIC);
1514 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scr), GTK_SHADOW_IN);
1517 ui::ListStore store = ui::ListStore::from(gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING));
1519 auto view = ui::TreeView(ui::TreeModel::from(store._handle));
1520 gtk_tree_view_set_enable_search(view, FALSE);
1521 gtk_tree_view_set_headers_visible(view, FALSE);
1524 auto renderer = ui::CellRendererText(ui::New);
1525 auto column = ui::TreeViewColumn("", renderer, {{"text", 0}});
1526 gtk_tree_view_append_column(view, column);
1530 auto renderer = ui::CellRendererText(ui::New);
1531 auto column = ui::TreeViewColumn("", renderer, {{"text", 1}});
1532 gtk_tree_view_append_column(view, column);
1536 auto selection = ui::TreeSelection::from(gtk_tree_view_get_selection(view));
1537 selection.connect("changed", G_CALLBACK(EntityProperties_selection_changed), 0);
1546 g_entprops_store = store;
1552 auto table = ui::Table(2, 2, FALSE);
1554 vbox2.pack_start(table, FALSE, TRUE, 0);
1555 gtk_table_set_row_spacings(table, 3);
1556 gtk_table_set_col_spacings(table, 5);
1559 auto entry = ui::Entry(ui::New);
1561 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1562 gtk_widget_set_events(entry, GDK_KEY_PRESS_MASK);
1563 entry.connect("key_press_event", G_CALLBACK(EntityEntry_keypress), 0);
1564 g_entityKeyEntry = entry;
1568 auto entry = ui::Entry(ui::New);
1570 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1571 gtk_widget_set_events(entry, GDK_KEY_PRESS_MASK);
1572 entry.connect("key_press_event", G_CALLBACK(EntityEntry_keypress), 0);
1573 g_entityValueEntry = entry;
1577 auto label = ui::Label("Value");
1579 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
1580 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1584 auto label = ui::Label("Key");
1586 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
1587 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1592 auto hbox = ui::HBox(TRUE, 4);
1594 vbox2.pack_start(hbox, FALSE, TRUE, 0);
1597 auto button = ui::Button("Clear All");
1599 button.connect("clicked", G_CALLBACK(EntityInspector_clearAllKeyValues), 0);
1600 hbox.pack_start(button, TRUE, TRUE, 0);
1603 auto button = ui::Button("Delete Key");
1605 button.connect("clicked", G_CALLBACK(EntityInspector_clearKeyValue), 0);
1606 hbox.pack_start(button, TRUE, TRUE, 0);
1612 auto scr = ui::ScrolledWindow(ui::New);
1614 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scr), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1616 auto viewport = ui::Container::from(gtk_viewport_new(0, 0));
1618 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1620 g_attributeBox = ui::VBox(FALSE, 2);
1621 g_attributeBox.show();
1623 viewport.add(g_attributeBox);
1625 gtk_paned_pack2(GTK_PANED(split2), scr, FALSE, FALSE);
1632 // show the sliders in any case
1633 if (g_entitysplit2_position > 22) {
1634 gtk_paned_set_position(GTK_PANED(g_entity_split2), g_entitysplit2_position);
1636 g_entitysplit2_position = 22;
1637 gtk_paned_set_position(GTK_PANED(g_entity_split2), 22);
1639 if ((g_entitysplit1_position - g_entitysplit2_position) > 27) {
1640 gtk_paned_set_position(GTK_PANED(g_entity_split1), g_entitysplit1_position);
1642 gtk_paned_set_position(GTK_PANED(g_entity_split1), g_entitysplit2_position + 27);
1646 g_entityInspector_windowConstructed = true;
1647 EntityClassList_fill();
1649 typedef FreeCaller<void(
1650 const Selectable &), EntityInspector_selectionChanged> EntityInspectorSelectionChangedCaller;
1651 GlobalSelectionSystem().addSelectionChangeCallback(EntityInspectorSelectionChangedCaller());
1652 GlobalEntityCreator().setKeyValueChangedFunc(EntityInspector_keyValueChanged);
1655 gtk_container_set_focus_chain(GTK_CONTAINER(vbox), NULL);
1660 class EntityInspector : public ModuleObserver {
1661 std::size_t m_unrealised;
1663 EntityInspector() : m_unrealised(1)
1669 if (--m_unrealised == 0) {
1670 if (g_entityInspector_windowConstructed) {
1671 //globalOutputStream() << "Entity Inspector: realise\n";
1672 EntityClassList_fill();
1679 if (++m_unrealised == 1) {
1680 if (g_entityInspector_windowConstructed) {
1681 //globalOutputStream() << "Entity Inspector: unrealise\n";
1682 EntityClassList_clear();
1688 EntityInspector g_EntityInspector;
1690 #include "preferencesystem.h"
1691 #include "stringio.h"
1693 void EntityInspector_construct()
1695 GlobalEntityClassManager().attach(g_EntityInspector);
1697 GlobalPreferenceSystem().registerPreference("EntitySplit1", make_property_string(g_entitysplit1_position));
1698 GlobalPreferenceSystem().registerPreference("EntitySplit2", make_property_string(g_entitysplit2_position));
1702 void EntityInspector_destroy()
1704 GlobalEntityClassManager().detach(g_EntityInspector);
1707 const char *EntityInspector_getCurrentKey()
1709 if (!GroupDialog_isShown()) {
1712 if (GroupDialog_getPage() != g_page_entity) {
1715 return gtk_entry_get_text(g_entityKeyEntry);