Debian Codegraph @

radiant/entityinspector.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 "entityinspector.h"
23
24 #include "debugging/debugging.h"
25 #include <gtk/gtk.h>
26
27 #include "ientity.h"
28 #include "ifilesystem.h"
29 #include "imodel.h"
30 #include "iscenegraph.h"
31 #include "iselection.h"
32 #include "iundo.h"
33
34 #include <map>
35 #include <set>
36 #include <gdk/gdkkeysyms.h>
37 #include <uilib/uilib.h>
38
39 #include "os/path.h"
40 #include "eclasslib.h"
41 #include "scenelib.h"
42 #include "generic/callback.h"
43 #include "os/file.h"
44 #include "stream/stringstream.h"
45 #include "moduleobserver.h"
46 #include "convert.h"
47 #include "stringio.h"
48
49 #include "gtkutil/accelerator.h"
50 #include "gtkutil/dialog.h"
51 #include "gtkutil/filechooser.h"
52 #include "gtkutil/messagebox.h"
53 #include "gtkutil/nonmodal.h"
54 #include "gtkutil/button.h"
55 #include "gtkutil/entry.h"
56 #include "gtkutil/container.h"
57
58 #include "qe3.h"
59 #include "gtkmisc.h"
60 #include "gtkdlgs.h"
61 #include "entity.h"
62 #include "mainframe.h"
63 #include "modelbrowser.h"
64 #include "textureentry.h"
65 #include "groupdialog.h"
66
67 #include "select.h"
68
69 ui::Entry numeric_entry_new(){
70 auto entry = ui::Entry(ui::New);
71 entry.show();
72 entry.dimensions(64, -1);
73 return entry;
74 }
75
76 namespace
77 {
78 typedef std::map<CopiedString, CopiedString> KeyValues;
79 KeyValues g_selectedKeyValues;
80 KeyValues g_selectedDefaultKeyValues;
81 }
82
83 const char* SelectedEntity_getValueForKey( const char* key ){
84 {
85 KeyValues::const_iterator i = g_selectedKeyValues.find( key );
86 if ( i != g_selectedKeyValues.end() ) {
87 return ( *i ).second.c_str();
88 }
89 }
90 {
91 KeyValues::const_iterator i = g_selectedDefaultKeyValues.find( key );
92 if ( i != g_selectedDefaultKeyValues.end() ) {
93 return ( *i ).second.c_str();
94 }
95 }
96 return "";
97 }
98
99 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 );
104 }
105
106 class EntityAttribute
107 {
108 public:
109 virtual ~EntityAttribute() = default;
110 virtual ui::Widget getWidget() const = 0;
111 virtual void update() = 0;
112 virtual void release() = 0;
113 };
114
115 class BooleanAttribute : public EntityAttribute
116 {
117 CopiedString m_key;
118 ui::CheckButton m_check;
119
120 static gboolean toggled( ui::Widget widget, BooleanAttribute* self ){
121 self->apply();
122 return FALSE;
123 }
124 public:
125 BooleanAttribute( const char* key ) :
126 m_key( key ),
127 m_check( ui::null ){
128 auto check = ui::CheckButton(ui::New);
129 check.show();
130
131 m_check = check;
132
133 guint handler = check.connect( "toggled", G_CALLBACK( toggled ), this );
134 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( handler ) );
135
136 update();
137 }
138 ui::Widget getWidget() const {
139 return m_check;
140 }
141 void release(){
142 delete this;
143 }
144 void apply(){
145 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_check.active() ? "1" : "" );
146 }
147 typedef MemberCaller<BooleanAttribute, void(), &BooleanAttribute::apply> ApplyCaller;
148
149 void update(){
150 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
151 if ( !string_empty( value ) ) {
152 toggle_button_set_active_no_signal( m_check, atoi( value ) != 0 );
153 }
154 else
155 {
156 toggle_button_set_active_no_signal( m_check, false );
157 }
158 }
159 typedef MemberCaller<BooleanAttribute, void(), &BooleanAttribute::update> UpdateCaller;
160 };
161
162
163 class StringAttribute : public EntityAttribute
164 {
165 CopiedString m_key;
166 ui::Entry m_entry;
167 NonModalEntry m_nonModal;
168 public:
169 StringAttribute( const char* key ) :
170 m_key( key ),
171 m_entry( ui::null ),
172 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
173 auto entry = ui::Entry(ui::New);
174 entry.show();
175 entry.dimensions(50, -1);
176
177 m_entry = entry;
178 m_nonModal.connect( m_entry );
179 }
180 ui::Widget getWidget() const {
181 return m_entry;
182 }
183 ui::Entry getEntry() const {
184 return m_entry;
185 }
186
187 void release(){
188 delete this;
189 }
190 void apply(){
191 StringOutputStream value( 64 );
192 value << m_entry.text();
193 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
194 }
195 typedef MemberCaller<StringAttribute, void(), &StringAttribute::apply> ApplyCaller;
196
197 void update(){
198 StringOutputStream value( 64 );
199 value << SelectedEntity_getValueForKey( m_key.c_str() );
200 m_entry.text(value.c_str());
201 }
202 typedef MemberCaller<StringAttribute, void(), &StringAttribute::update> UpdateCaller;
203 };
204
205 class ShaderAttribute : public StringAttribute
206 {
207 public:
208 ShaderAttribute( const char* key ) : StringAttribute( key ){
209 GlobalShaderEntryCompletion::instance().connect( StringAttribute::getEntry() );
210 }
211 };
212
213
214 class ColorAttribute : public EntityAttribute
215 {
216 CopiedString m_key;
217 BrowsedPathEntry m_entry;
218 NonModalEntry m_nonModal;
219 public:
220 ColorAttribute( const char* key ) :
221 m_key( key ),
222 m_entry( BrowseCaller( *this ) ),
223 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
224 m_nonModal.connect( m_entry.m_entry.m_entry );
225 }
226 void release(){
227 delete this;
228 }
229 ui::Widget getWidget() const {
230 return ui::Widget( m_entry.m_entry.m_frame );
231 }
232 void apply(){
233 StringOutputStream value( 64 );
234 value << gtk_entry_get_text( GTK_ENTRY( m_entry.m_entry.m_entry ) );
235 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
236 }
237 typedef MemberCaller<ColorAttribute, void(), &ColorAttribute::apply> ApplyCaller;
238 void update(){
239 StringOutputStream value( 64 );
240 value << SelectedEntity_getValueForKey( m_key.c_str() );
241 gtk_entry_set_text( GTK_ENTRY( m_entry.m_entry.m_entry ), value.c_str() );
242 }
243 typedef MemberCaller<ColorAttribute, void(), &ColorAttribute::update> UpdateCaller;
244 void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
245 //const char *filename = misc_model_dialog( gtk_widget_get_toplevel( GTK_WIDGET( m_entry.m_entry.m_frame ) ) );
246
247 /* hijack BrowsedPathEntry to call colour chooser */
248 Entity_setColour();
249
250 // if ( filename != 0 ) {
251 // setPath( filename );
252 // apply();
253 // }
254 update();
255 }
256 typedef MemberCaller<ColorAttribute, void(const BrowsedPathEntry::SetPathCallback&), &ColorAttribute::browse> BrowseCaller;
257 };
258
259
260 class ModelAttribute : public EntityAttribute
261 {
262 CopiedString m_key;
263 BrowsedPathEntry m_entry;
264 NonModalEntry m_nonModal;
265 public:
266 ModelAttribute( const char* key ) :
267 m_key( key ),
268 m_entry( BrowseCaller( *this ) ),
269 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
270 m_nonModal.connect( m_entry.m_entry.m_entry );
271 }
272 void release(){
273 delete this;
274 }
275 ui::Widget getWidget() const {
276 return m_entry.m_entry.m_frame;
277 }
278 void apply(){
279 StringOutputStream value( 64 );
280 value << m_entry.m_entry.m_entry.text();
281 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
282 }
283 typedef MemberCaller<ModelAttribute, void(), &ModelAttribute::apply> ApplyCaller;
284 void update(){
285 StringOutputStream value( 64 );
286 value << SelectedEntity_getValueForKey( m_key.c_str() );
287 m_entry.m_entry.m_entry.text(value.c_str());
288 }
289 typedef MemberCaller<ModelAttribute, void(), &ModelAttribute::update> UpdateCaller;
290 void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
291 const char *filename = ModelBrowser_showChooser( m_entry.m_entry.m_frame.window() );
292
293 if ( filename != 0 ) {
294 setPath( filename );
295 apply();
296 }
297 }
298 typedef MemberCaller<ModelAttribute, void(const BrowsedPathEntry::SetPathCallback&), &ModelAttribute::browse> BrowseCaller;
299 };
300
301 const char* browse_sound( ui::Widget parent ){
302 StringOutputStream buffer( 1024 );
303
304 buffer << g_qeglobals.m_userGamePath.c_str() << "sound/";
305
306 if ( !file_readable( buffer.c_str() ) ) {
307 // just go to fsmain
308 buffer.clear();
309 buffer << g_qeglobals.m_userGamePath.c_str() << "/";
310 }
311
312 const char* filename = parent.file_dialog(TRUE, "Open Wav File", buffer.c_str(), "sound" );
313 if ( filename != 0 ) {
314 const char* relative = path_make_relative( filename, GlobalFileSystem().findRoot( filename ) );
315 if ( relative == filename ) {
316 globalOutputStream() << "WARNING: could not extract the relative path, using full path instead\n";
317 }
318 return relative;
319 }
320 return filename;
321 }
322
323 class SoundAttribute : public EntityAttribute
324 {
325 CopiedString m_key;
326 BrowsedPathEntry m_entry;
327 NonModalEntry m_nonModal;
328 public:
329 SoundAttribute( const char* key ) :
330 m_key( key ),
331 m_entry( BrowseCaller( *this ) ),
332 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
333 m_nonModal.connect( m_entry.m_entry.m_entry );
334 }
335 void release(){
336 delete this;
337 }
338 ui::Widget getWidget() const {
339 return ui::Widget(m_entry.m_entry.m_frame );
340 }
341 void apply(){
342 StringOutputStream value( 64 );
343 value << m_entry.m_entry.m_entry.text();
344 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), value.c_str() );
345 }
346 typedef MemberCaller<SoundAttribute, void(), &SoundAttribute::apply> ApplyCaller;
347 void update(){
348 StringOutputStream value( 64 );
349 value << SelectedEntity_getValueForKey( m_key.c_str() );
350 m_entry.m_entry.m_entry.text(value.c_str());
351 }
352 typedef MemberCaller<SoundAttribute, void(), &SoundAttribute::update> UpdateCaller;
353 void browse( const BrowsedPathEntry::SetPathCallback& setPath ){
354 const char *filename = browse_sound( m_entry.m_entry.m_frame.window() );
355
356 if ( filename != 0 ) {
357 setPath( filename );
358 apply();
359 }
360 }
361 typedef MemberCaller<SoundAttribute, void(const BrowsedPathEntry::SetPathCallback&), &SoundAttribute::browse> BrowseCaller;
362 };
363
364 inline double angle_normalised( double angle ){
365 return float_mod( angle, 360.0 );
366 }
367
368 class AngleAttribute : public EntityAttribute
369 {
370 CopiedString m_key;
371 ui::Entry m_entry;
372 NonModalEntry m_nonModal;
373 public:
374 AngleAttribute( const char* key ) :
375 m_key( key ),
376 m_entry( ui::null ),
377 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
378 auto entry = numeric_entry_new();
379 m_entry = entry;
380 m_nonModal.connect( m_entry );
381 }
382 void release(){
383 delete this;
384 }
385 ui::Widget getWidget() const {
386 return ui::Widget(m_entry );
387 }
388 void apply(){
389 StringOutputStream angle( 32 );
390 angle << angle_normalised( entry_get_float( m_entry ) );
391 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angle.c_str() );
392 }
393 typedef MemberCaller<AngleAttribute, void(), &AngleAttribute::apply> ApplyCaller;
394
395 void update(){
396 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
397 if ( !string_empty( value ) ) {
398 StringOutputStream angle( 32 );
399 angle << angle_normalised( atof( value ) );
400 m_entry.text(angle.c_str());
401 }
402 else
403 {
404 m_entry.text("0");
405 }
406 }
407 typedef MemberCaller<AngleAttribute, void(), &AngleAttribute::update> UpdateCaller;
408 };
409
410 namespace
411 {
412 typedef const char* String;
413 const String buttons[] = { "up", "down", "z-axis" };
414 }
415
416 class DirectionAttribute : public EntityAttribute
417 {
418 CopiedString m_key;
419 ui::Entry m_entry;
420 NonModalEntry m_nonModal;
421 RadioHBox m_radio;
422 NonModalRadio m_nonModalRadio;
423 ui::HBox m_hbox{ui::null};
424 public:
425 DirectionAttribute( const char* key ) :
426 m_key( key ),
427 m_entry( ui::null ),
428 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ),
429 m_radio( RadioHBox_new( STRING_ARRAY_RANGE( buttons ) ) ),
430 m_nonModalRadio( ApplyRadioCaller( *this ) ){
431 auto entry = numeric_entry_new();
432 m_entry = entry;
433 m_nonModal.connect( m_entry );
434
435 m_nonModalRadio.connect( m_radio.m_radio );
436
437 m_hbox = ui::HBox( FALSE, 4 );
438 m_hbox.show();
439
440 m_hbox.pack_start( m_radio.m_hbox, TRUE, TRUE, 0 );
441 m_hbox.pack_start( m_entry, TRUE, TRUE, 0 );
442 }
443 void release(){
444 delete this;
445 }
446 ui::Widget getWidget() const {
447 return ui::Widget(m_hbox );
448 }
449 void apply(){
450 StringOutputStream angle( 32 );
451 angle << angle_normalised( entry_get_float( m_entry ) );
452 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angle.c_str() );
453 }
454 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::apply> ApplyCaller;
455
456 void update(){
457 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
458 if ( !string_empty( value ) ) {
459 float f = float(atof( value ) );
460 if ( f == -1 ) {
461 gtk_widget_set_sensitive( m_entry , FALSE );
462 radio_button_set_active_no_signal( m_radio.m_radio, 0 );
463 m_entry.text("");
464 }
465 else if ( f == -2 ) {
466 gtk_widget_set_sensitive( m_entry , FALSE );
467 radio_button_set_active_no_signal( m_radio.m_radio, 1 );
468 m_entry.text("");
469 }
470 else
471 {
472 gtk_widget_set_sensitive( m_entry , TRUE );
473 radio_button_set_active_no_signal( m_radio.m_radio, 2 );
474 StringOutputStream angle( 32 );
475 angle << angle_normalised( f );
476 m_entry.text(angle.c_str());
477 }
478 }
479 else
480 {
481 m_entry.text("0");
482 }
483 }
484 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::update> UpdateCaller;
485
486 void applyRadio(){
487 int index = radio_button_get_active( m_radio.m_radio );
488 if ( index == 0 ) {
489 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), "-1" );
490 }
491 else if ( index == 1 ) {
492 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), "-2" );
493 }
494 else if ( index == 2 ) {
495 apply();
496 }
497 }
498 typedef MemberCaller<DirectionAttribute, void(), &DirectionAttribute::applyRadio> ApplyRadioCaller;
499 };
500
501
502 class AnglesEntry
503 {
504 public:
505 ui::Entry m_roll;
506 ui::Entry m_pitch;
507 ui::Entry m_yaw;
508 AnglesEntry() : m_roll( ui::null ), m_pitch( ui::null ), m_yaw( ui::null ){
509 }
510 };
511
512 typedef BasicVector3<double> DoubleVector3;
513
514 class AnglesAttribute : public EntityAttribute
515 {
516 CopiedString m_key;
517 AnglesEntry m_angles;
518 NonModalEntry m_nonModal;
519 ui::HBox m_hbox;
520 public:
521 AnglesAttribute( const char* key ) :
522 m_key( key ),
523 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ),
524 m_hbox(ui::HBox( TRUE, 4 ))
525 {
526 m_hbox.show();
527 {
528 auto entry = numeric_entry_new();
529 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
530 m_angles.m_pitch = entry;
531 m_nonModal.connect( m_angles.m_pitch );
532 }
533 {
534 auto entry = numeric_entry_new();
535 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
536 m_angles.m_yaw = entry;
537 m_nonModal.connect( m_angles.m_yaw );
538 }
539 {
540 auto entry = numeric_entry_new();
541 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
542 m_angles.m_roll = entry;
543 m_nonModal.connect( m_angles.m_roll );
544 }
545 }
546 void release(){
547 delete this;
548 }
549 ui::Widget getWidget() const {
550 return ui::Widget(m_hbox );
551 }
552 void apply(){
553 StringOutputStream angles( 64 );
554 angles << angle_normalised( entry_get_float( m_angles.m_pitch ) )
555 << " " << angle_normalised( entry_get_float( m_angles.m_yaw ) )
556 << " " << angle_normalised( entry_get_float( m_angles.m_roll ) );
557 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), angles.c_str() );
558 }
559 typedef MemberCaller<AnglesAttribute, void(), &AnglesAttribute::apply> ApplyCaller;
560
561 void update(){
562 StringOutputStream angle( 32 );
563 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
564 if ( !string_empty( value ) ) {
565 DoubleVector3 pitch_yaw_roll;
566 if ( !string_parse_vector3( value, pitch_yaw_roll ) ) {
567 pitch_yaw_roll = DoubleVector3( 0, 0, 0 );
568 }
569
570 angle << angle_normalised( pitch_yaw_roll.x() );
571 m_angles.m_pitch.text(angle.c_str());
572 angle.clear();
573
574 angle << angle_normalised( pitch_yaw_roll.y() );
575 m_angles.m_yaw.text(angle.c_str());
576 angle.clear();
577
578 angle << angle_normalised( pitch_yaw_roll.z() );
579 m_angles.m_roll.text(angle.c_str());
580 angle.clear();
581 }
582 else
583 {
584 m_angles.m_pitch.text("0");
585 m_angles.m_yaw.text("0");
586 m_angles.m_roll.text("0");
587 }
588 }
589 typedef MemberCaller<AnglesAttribute, void(), &AnglesAttribute::update> UpdateCaller;
590 };
591
592 class Vector3Entry
593 {
594 public:
595 ui::Entry m_x;
596 ui::Entry m_y;
597 ui::Entry m_z;
598 Vector3Entry() : m_x( ui::null ), m_y( ui::null ), m_z( ui::null ){
599 }
600 };
601
602 class Vector3Attribute : public EntityAttribute
603 {
604 CopiedString m_key;
605 Vector3Entry m_vector3;
606 NonModalEntry m_nonModal;
607 ui::Box m_hbox{ui::null};
608 public:
609 Vector3Attribute( const char* key ) :
610 m_key( key ),
611 m_nonModal( ApplyCaller( *this ), UpdateCaller( *this ) ){
612 m_hbox = ui::HBox( TRUE, 4 );
613 m_hbox.show();
614 {
615 auto entry = numeric_entry_new();
616 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
617 m_vector3.m_x = entry;
618 m_nonModal.connect( m_vector3.m_x );
619 }
620 {
621 auto entry = numeric_entry_new();
622 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
623 m_vector3.m_y = entry;
624 m_nonModal.connect( m_vector3.m_y );
625 }
626 {
627 auto entry = numeric_entry_new();
628 m_hbox.pack_start( entry, TRUE, TRUE, 0 );
629 m_vector3.m_z = entry;
630 m_nonModal.connect( m_vector3.m_z );
631 }
632 }
633 void release(){
634 delete this;
635 }
636 ui::Widget getWidget() const {
637 return ui::Widget(m_hbox );
638 }
639 void apply(){
640 StringOutputStream vector3( 64 );
641 vector3 << entry_get_float( m_vector3.m_x )
642 << " " << entry_get_float( m_vector3.m_y )
643 << " " << entry_get_float( m_vector3.m_z );
644 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), vector3.c_str() );
645 }
646 typedef MemberCaller<Vector3Attribute, void(), &Vector3Attribute::apply> ApplyCaller;
647
648 void update(){
649 StringOutputStream buffer( 32 );
650 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
651 if ( !string_empty( value ) ) {
652 DoubleVector3 x_y_z;
653 if ( !string_parse_vector3( value, x_y_z ) ) {
654 x_y_z = DoubleVector3( 0, 0, 0 );
655 }
656
657 buffer << x_y_z.x();
658 m_vector3.m_x.text(buffer.c_str());
659 buffer.clear();
660
661 buffer << x_y_z.y();
662 m_vector3.m_y.text(buffer.c_str());
663 buffer.clear();
664
665 buffer << x_y_z.z();
666 m_vector3.m_z.text(buffer.c_str());
667 buffer.clear();
668 }
669 else
670 {
671 m_vector3.m_x.text("0");
672 m_vector3.m_y.text("0");
673 m_vector3.m_z.text("0");
674 }
675 }
676 typedef MemberCaller<Vector3Attribute, void(), &Vector3Attribute::update> UpdateCaller;
677 };
678
679 class NonModalComboBox
680 {
681 Callback<void()> m_changed;
682 guint m_changedHandler;
683
684 static gboolean changed( ui::ComboBox widget, NonModalComboBox* self ){
685 self->m_changed();
686 return FALSE;
687 }
688
689 public:
690 NonModalComboBox( const Callback<void()>& changed ) : m_changed( changed ), m_changedHandler( 0 ){
691 }
692 void connect( ui::ComboBox combo ){
693 m_changedHandler = combo.connect( "changed", G_CALLBACK( changed ), this );
694 }
695 void setActive( ui::ComboBox combo, int value ){
696 g_signal_handler_disconnect( G_OBJECT( combo ), m_changedHandler );
697 gtk_combo_box_set_active( combo, value );
698 connect( combo );
699 }
700 };
701
702 class ListAttribute : public EntityAttribute
703 {
704 CopiedString m_key;
705 ui::ComboBox m_combo;
706 NonModalComboBox m_nonModal;
707 const ListAttributeType& m_type;
708 public:
709 ListAttribute( const char* key, const ListAttributeType& type ) :
710 m_key( key ),
711 m_combo( ui::null ),
712 m_nonModal( ApplyCaller( *this ) ),
713 m_type( type ){
714 auto combo = ui::ComboBoxText(ui::New);
715
716 for ( ListAttributeType::const_iterator i = type.begin(); i != type.end(); ++i )
717 {
718 gtk_combo_box_text_append_text( GTK_COMBO_BOX_TEXT( combo ), ( *i ).first.c_str() );
719 }
720
721 combo.show();
722 m_nonModal.connect( combo );
723
724 m_combo = combo;
725 }
726 void release(){
727 delete this;
728 }
729 ui::Widget getWidget() const {
730 return ui::Widget(m_combo );
731 }
732 void apply(){
733 Scene_EntitySetKeyValue_Selected_Undoable( m_key.c_str(), m_type[gtk_combo_box_get_active( m_combo )].second.c_str() );
734 }
735 typedef MemberCaller<ListAttribute, void(), &ListAttribute::apply> ApplyCaller;
736
737 void update(){
738 const char* value = SelectedEntity_getValueForKey( m_key.c_str() );
739 ListAttributeType::const_iterator i = m_type.findValue( value );
740 if ( i != m_type.end() ) {
741 m_nonModal.setActive( m_combo, static_cast<int>( std::distance( m_type.begin(), i ) ) );
742 }
743 else
744 {
745 m_nonModal.setActive( m_combo, 0 );
746 }
747 }
748 typedef MemberCaller<ListAttribute, void(), &ListAttribute::update> UpdateCaller;
749 };
750
751
752 namespace
753 {
754 GtkWidget* g_entity_split0 = 0;
755 ui::Widget g_entity_split1{ui::null};
756 ui::Widget g_entity_split2{ui::null};
757 int g_entitysplit0_position;
758 int g_entitysplit1_position;
759 int g_entitysplit2_position;
760
761 bool g_entityInspector_windowConstructed = false;
762
763 ui::TreeView g_entityClassList{ui::null};
764 ui::TextView g_entityClassComment{ui::null};
765
766 GtkCheckButton* g_entitySpawnflagsCheck[MAX_FLAGS];
767
768 ui::Entry g_entityKeyEntry{ui::null};
769 ui::Entry g_entityValueEntry{ui::null};
770
771 GtkToggleButton* g_focusToggleButton;
772
773 ui::ListStore g_entlist_store{ui::null};
774 ui::ListStore g_entprops_store{ui::null};
775 const EntityClass* g_current_flags = 0;
776 const EntityClass* g_current_comment = 0;
777 const EntityClass* g_current_attributes = 0;
778
779 // the number of active spawnflags
780 int g_spawnflag_count;
781 // table: index, match spawnflag item to the spawnflag index (i.e. which bit)
782 int spawn_table[MAX_FLAGS];
783 // we change the layout depending on how many spawn flags we need to display
784 // the table is a 4x4 in which we need to put the comment box g_entityClassComment and the spawn flags..
785 ui::Table g_spawnflagsTable{ui::null};
786
787 ui::VBox g_attributeBox{ui::null};
788 typedef std::vector<EntityAttribute*> EntityAttributes;
789 EntityAttributes g_entityAttributes;
790 }
791
792 void GlobalEntityAttributes_clear(){
793 for ( EntityAttributes::iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
794 {
795 ( *i )->release();
796 }
797 g_entityAttributes.clear();
798 }
799
800 class GetKeyValueVisitor : public Entity::Visitor
801 {
802 KeyValues& m_keyvalues;
803 public:
804 GetKeyValueVisitor( KeyValues& keyvalues )
805 : m_keyvalues( keyvalues ){
806 }
807
808 void visit( const char* key, const char* value ){
809 m_keyvalues.insert( KeyValues::value_type( CopiedString( key ), CopiedString( value ) ) );
810 }
811
812 };
813
814 void Entity_GetKeyValues( const Entity& entity, KeyValues& keyvalues, KeyValues& defaultValues ){
815 GetKeyValueVisitor visitor( keyvalues );
816
817 entity.forEachKeyValue( visitor );
818
819 const EntityClassAttributes& attributes = entity.getEntityClass().m_attributes;
820
821 for ( EntityClassAttributes::const_iterator i = attributes.begin(); i != attributes.end(); ++i )
822 {
823 defaultValues.insert( KeyValues::value_type( ( *i ).first, ( *i ).second.m_value ) );
824 }
825 }
826
827 void Entity_GetKeyValues_Selected( KeyValues& keyvalues, KeyValues& defaultValues ){
828 class EntityGetKeyValues : public SelectionSystem::Visitor
829 {
830 KeyValues& m_keyvalues;
831 KeyValues& m_defaultValues;
832 mutable std::set<Entity*> m_visited;
833 public:
834 EntityGetKeyValues( KeyValues& keyvalues, KeyValues& defaultValues )
835 : m_keyvalues( keyvalues ), m_defaultValues( defaultValues ){
836 }
837 void visit( scene::Instance& instance ) const {
838 Entity* entity = Node_getEntity( instance.path().top() );
839 if ( entity == 0 && instance.path().size() != 1 ) {
840 entity = Node_getEntity( instance.path().parent() );
841 }
842 if ( entity != 0 && m_visited.insert( entity ).second ) {
843 Entity_GetKeyValues( *entity, m_keyvalues, m_defaultValues );
844 }
845 }
846 } visitor( keyvalues, defaultValues );
847 GlobalSelectionSystem().foreachSelected( visitor );
848 }
849
850 const char* keyvalues_valueforkey( KeyValues& keyvalues, const char* key ){
851 KeyValues::iterator i = keyvalues.find( CopiedString( key ) );
852 if ( i != keyvalues.end() ) {
853 return ( *i ).second.c_str();
854 }
855 return "";
856 }
857
858 class EntityClassListStoreAppend : public EntityClassVisitor
859 {
860 ui::ListStore store;
861 public:
862 EntityClassListStoreAppend( ui::ListStore store_ ) : store( store_ ){
863 }
864 void visit( EntityClass* e ){
865 store.append(0, e->name(), 1, e);
866 }
867 };
868
869 void EntityClassList_fill(){
870 EntityClassListStoreAppend append( g_entlist_store );
871 GlobalEntityClassManager().forEach( append );
872 }
873
874 void EntityClassList_clear(){
875 g_entlist_store.clear();
876 }
877
878 void SetComment( EntityClass* eclass ){
879 if ( eclass == g_current_comment ) {
880 return;
881 }
882
883 g_current_comment = eclass;
884
885 g_entityClassComment.text(eclass->comments());
886
887 GtkTextBuffer* buffer = gtk_text_view_get_buffer( g_entityClassComment );
888 //gtk_text_buffer_set_text( buffer, eclass->comments(), -1 );
889 const char* comment = eclass->comments(), *c;
890 int offset = 0, pattern_start = -1, spaces = 0;
891
892 gtk_text_buffer_set_text( buffer, comment, -1 );
893
894 // Catch patterns like "\nstuff :" used to describe keys and spawnflags, and make them bold for readability.
895
896 for( c = comment; *c; ++c, ++offset ) {
897 if( *c == '\n' ) {
898 pattern_start = offset;
899 spaces = 0;
900 }
901 else if( pattern_start >= 0 && ( *c < 'a' || *c > 'z' ) && ( *c < 'A' || *c > 'Z' ) && ( *c < '0' || *c > '9' ) && ( *c != '_' ) ) {
902 if( *c == ':' && spaces <= 1 ) {
903 GtkTextIter iter_start, iter_end;
904
905 gtk_text_buffer_get_iter_at_offset( buffer, &iter_start, pattern_start );
906 gtk_text_buffer_get_iter_at_offset( buffer, &iter_end, offset );
907 gtk_text_buffer_apply_tag_by_name( buffer, "bold", &iter_start, &iter_end );
908 }
909
910 if( *c == ' ' )
911 ++spaces;
912 else
913 pattern_start = -1;
914 }
915 }
916 }
917
918 void SurfaceFlags_setEntityClass( EntityClass* eclass ){
919 if ( eclass == g_current_flags ) {
920 return;
921 }
922
923 g_current_flags = eclass;
924
925 unsigned int spawnflag_count = 0;
926
927 {
928 // do a first pass to count the spawn flags, don't touch the widgets, we don't know in what state they are
929 for ( int i = 0 ; i < MAX_FLAGS ; i++ )
930 {
931 if ( eclass->flagnames[i][0] != 0 && strcmp( eclass->flagnames[i],"-" ) ) {
932 spawn_table[spawnflag_count] = i;
933 spawnflag_count++;
934 }
935 }
936 }
937
938 // disable all remaining boxes
939 // NOTE: these boxes might not even be on display
940 {
941 for ( int i = 0; i < g_spawnflag_count; ++i )
942 {
943 auto widget = ui::CheckButton::from(g_entitySpawnflagsCheck[i]);
944 auto label = ui::Label::from(gtk_bin_get_child(GTK_BIN(widget)));
945 label.text(" ");
946 widget.hide();
947 widget.ref();
948 g_spawnflagsTable.remove(widget);
949 }
950 }
951
952 g_spawnflag_count = spawnflag_count;
953
954 {
955 for (unsigned int i = 0; (int) i < g_spawnflag_count; ++i)
956 {
957 auto widget = ui::CheckButton::from(g_entitySpawnflagsCheck[i] );
958 widget.show();
959
960 StringOutputStream str( 16 );
961 str << LowerCase( eclass->flagnames[spawn_table[i]] );
962
963 g_spawnflagsTable.attach(widget, {i % 4, i % 4 + 1, i / 4, i / 4 + 1}, {GTK_FILL, GTK_FILL});
964 widget.unref();
965
966 auto label = ui::Label::from(gtk_bin_get_child(GTK_BIN(widget)) );
967 label.text(str.c_str());
968 }
969 }
970 }
971
972 void EntityClassList_selectEntityClass( EntityClass* eclass ){
973 auto model = g_entlist_store;
974 GtkTreeIter iter;
975 for ( gboolean good = gtk_tree_model_get_iter_first( model, &iter ); good != FALSE; good = gtk_tree_model_iter_next( model, &iter ) )
976 {
977 char* text;
978 gtk_tree_model_get( model, &iter, 0, &text, -1 );
979 if ( strcmp( text, eclass->name() ) == 0 ) {
980 auto view = ui::TreeView(g_entityClassList);
981 auto path = gtk_tree_model_get_path( model, &iter );
982 gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
983 if ( gtk_widget_get_realized( view ) ) {
984 gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
985 }
986 gtk_tree_path_free( path );
987 good = FALSE;
988 }
989 g_free( text );
990 }
991 }
992
993 void EntityInspector_appendAttribute( const char* name, EntityAttribute& attribute ){
994 auto row = DialogRow_new( name, attribute.getWidget() );
995 DialogVBox_packRow( ui::VBox(g_attributeBox), row );
996 }
997
998
999 template<typename Attribute>
1000 class StatelessAttributeCreator
1001 {
1002 public:
1003 static EntityAttribute* create( const char* name ){
1004 return new Attribute( name );
1005 }
1006 };
1007
1008 class EntityAttributeFactory
1009 {
1010 typedef EntityAttribute* ( *CreateFunc )( const char* name );
1011 typedef std::map<const char*, CreateFunc, RawStringLess> Creators;
1012 Creators m_creators;
1013 public:
1014 EntityAttributeFactory(){
1015 m_creators.insert( Creators::value_type( "string", &StatelessAttributeCreator<StringAttribute>::create ) );
1016 m_creators.insert( Creators::value_type( "color", &StatelessAttributeCreator<ColorAttribute>::create ) );
1017 m_creators.insert( Creators::value_type( "integer", &StatelessAttributeCreator<StringAttribute>::create ) );
1018 m_creators.insert( Creators::value_type( "real", &StatelessAttributeCreator<StringAttribute>::create ) );
1019 m_creators.insert( Creators::value_type( "shader", &StatelessAttributeCreator<ShaderAttribute>::create ) );
1020 m_creators.insert( Creators::value_type( "boolean", &StatelessAttributeCreator<BooleanAttribute>::create ) );
1021 m_creators.insert( Creators::value_type( "angle", &StatelessAttributeCreator<AngleAttribute>::create ) );
1022 m_creators.insert( Creators::value_type( "direction", &StatelessAttributeCreator<DirectionAttribute>::create ) );
1023 m_creators.insert( Creators::value_type( "angles", &StatelessAttributeCreator<AnglesAttribute>::create ) );
1024 m_creators.insert( Creators::value_type( "model", &StatelessAttributeCreator<ModelAttribute>::create ) );
1025 m_creators.insert( Creators::value_type( "sound", &StatelessAttributeCreator<SoundAttribute>::create ) );
1026 m_creators.insert( Creators::value_type( "vector3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
1027 m_creators.insert( Creators::value_type( "real3", &StatelessAttributeCreator<Vector3Attribute>::create ) );
1028 }
1029 EntityAttribute* create( const char* type, const char* name ){
1030 Creators::iterator i = m_creators.find( type );
1031 if ( i != m_creators.end() ) {
1032 return ( *i ).second( name );
1033 }
1034 const ListAttributeType* listType = GlobalEntityClassManager().findListType( type );
1035 if ( listType != 0 ) {
1036 return new ListAttribute( name, *listType );
1037 }
1038 return 0;
1039 }
1040 };
1041
1042 typedef Static<EntityAttributeFactory> GlobalEntityAttributeFactory;
1043
1044 void EntityInspector_setEntityClass( EntityClass *eclass ){
1045 EntityClassList_selectEntityClass( eclass );
1046 SurfaceFlags_setEntityClass( eclass );
1047
1048 if ( eclass != g_current_attributes ) {
1049 g_current_attributes = eclass;
1050
1051 container_remove_all( g_attributeBox );
1052 GlobalEntityAttributes_clear();
1053
1054 for ( EntityClassAttributes::const_iterator i = eclass->m_attributes.begin(); i != eclass->m_attributes.end(); ++i )
1055 {
1056 EntityAttribute* attribute = GlobalEntityAttributeFactory::instance().create( ( *i ).second.m_type.c_str(), ( *i ).first.c_str() );
1057 if ( attribute != 0 ) {
1058 g_entityAttributes.push_back( attribute );
1059 EntityInspector_appendAttribute( EntityClassAttributePair_getName( *i ), *g_entityAttributes.back() );
1060 }
1061 }
1062 }
1063 }
1064
1065 void EntityInspector_updateSpawnflags(){
1066 {
1067 int f = atoi( SelectedEntity_getValueForKey( "spawnflags" ) );
1068 for ( int i = 0; i < g_spawnflag_count; ++i )
1069 {
1070 int v = !!( f & ( 1 << spawn_table[i] ) );
1071
1072 toggle_button_set_active_no_signal( ui::ToggleButton::from( g_entitySpawnflagsCheck[i] ), v );
1073 }
1074 }
1075 {
1076 // take care of the remaining ones
1077 for ( int i = g_spawnflag_count; i < MAX_FLAGS; ++i )
1078 {
1079 toggle_button_set_active_no_signal( ui::ToggleButton::from( g_entitySpawnflagsCheck[i] ), FALSE );
1080 }
1081 }
1082 }
1083
1084 void EntityInspector_applySpawnflags(){
1085 int f, i, v;
1086 char sz[32];
1087
1088 f = 0;
1089 for ( i = 0; i < g_spawnflag_count; ++i )
1090 {
1091 v = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( g_entitySpawnflagsCheck[i] ) );
1092 f |= v << spawn_table[i];
1093 }
1094
1095 sprintf( sz, "%i", f );
1096 const char* value = ( f == 0 ) ? "" : sz;
1097
1098 {
1099 StringOutputStream command;
1100 command << "entitySetFlags -flags " << f;
1101 UndoableCommand undo( "entitySetSpawnflags" );
1102
1103 Scene_EntitySetKeyValue_Selected( "spawnflags", value );
1104 }
1105 }
1106
1107
1108 void EntityInspector_updateKeyValues(){
1109 g_selectedKeyValues.clear();
1110 g_selectedDefaultKeyValues.clear();
1111 Entity_GetKeyValues_Selected( g_selectedKeyValues, g_selectedDefaultKeyValues );
1112
1113 EntityInspector_setEntityClass( GlobalEntityClassManager().findOrInsert( keyvalues_valueforkey( g_selectedKeyValues, "classname" ), false ) );
1114
1115 EntityInspector_updateSpawnflags();
1116
1117 ui::ListStore store = g_entprops_store;
1118
1119 // save current key/val pair around filling epair box
1120 // row_select wipes it and sets to first in list
1121 CopiedString strKey( g_entityKeyEntry.text() );
1122 CopiedString strVal( g_entityValueEntry.text() );
1123
1124 store.clear();
1125 // Walk through list and add pairs
1126 for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1127 {
1128 StringOutputStream key( 64 );
1129 key << ( *i ).first.c_str();
1130 StringOutputStream value( 64 );
1131 value << ( *i ).second.c_str();
1132 store.append(0, key.c_str(), 1, value.c_str());
1133 }
1134
1135 g_entityKeyEntry.text( strKey.c_str() );
1136 g_entityValueEntry.text( strVal.c_str() );
1137
1138 for ( EntityAttributes::const_iterator i = g_entityAttributes.begin(); i != g_entityAttributes.end(); ++i )
1139 {
1140 ( *i )->update();
1141 }
1142 }
1143
1144 class EntityInspectorDraw
1145 {
1146 IdleDraw m_idleDraw;
1147 public:
1148 EntityInspectorDraw() : m_idleDraw( makeCallbackF(EntityInspector_updateKeyValues) ){
1149 }
1150 void queueDraw(){
1151 m_idleDraw.queueDraw();
1152 }
1153 };
1154
1155 EntityInspectorDraw g_EntityInspectorDraw;
1156
1157
1158 void EntityInspector_keyValueChanged(){
1159 g_EntityInspectorDraw.queueDraw();
1160 }
1161 void EntityInspector_selectionChanged( const Selectable& ){
1162 EntityInspector_keyValueChanged();
1163 }
1164
1165 // Creates a new entity based on the currently selected brush and entity type.
1166 //
1167 void EntityClassList_createEntity(){
1168 auto view = g_entityClassList;
1169
1170 // find out what type of entity we are trying to create
1171 GtkTreeModel* model;
1172 GtkTreeIter iter;
1173 if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( g_entityClassList ), &model, &iter ) == FALSE ) {
1174 ui::alert( view.window(), "You must have a selected class to create an entity", "info" );
1175 return;
1176 }
1177
1178 char* text;
1179 gtk_tree_model_get( model, &iter, 0, &text, -1 );
1180
1181 {
1182 StringOutputStream command;
1183 command << "entityCreate -class " << text;
1184
1185 UndoableCommand undo( command.c_str() );
1186
1187 Entity_createFromSelection( text, g_vector3_identity );
1188 }
1189 g_free( text );
1190 }
1191
1192 void EntityInspector_applyKeyValue(){
1193 // Get current selection text
1194 StringOutputStream key( 64 );
1195 key << gtk_entry_get_text( g_entityKeyEntry );
1196 StringOutputStream value( 64 );
1197 value << gtk_entry_get_text( g_entityValueEntry );
1198
1199
1200 // TTimo: if you change the classname to worldspawn you won't merge back in the structural brushes but create a parasite entity
1201 if ( !strcmp( key.c_str(), "classname" ) && !strcmp( value.c_str(), "worldspawn" ) ) {
1202 ui::alert( g_entityKeyEntry.window(), "Cannot change \"classname\" key back to worldspawn.", 0, ui::alert_type::OK );
1203 return;
1204 }
1205
1206
1207 // RR2DO2: we don't want spaces in entity keys
1208 if ( strstr( key.c_str(), " " ) ) {
1209 ui::alert( g_entityKeyEntry.window(), "No spaces are allowed in entity keys.", 0, ui::alert_type::OK );
1210 return;
1211 }
1212
1213 if ( strcmp( key.c_str(), "classname" ) == 0 ) {
1214 StringOutputStream command;
1215 command << "entitySetClass -class " << value.c_str();
1216 UndoableCommand undo( command.c_str() );
1217 Scene_EntitySetClassname_Selected( value.c_str() );
1218 }
1219 else
1220 {
1221 Scene_EntitySetKeyValue_Selected_Undoable( key.c_str(), value.c_str() );
1222 }
1223 }
1224
1225 void EntityInspector_clearKeyValue(){
1226 // Get current selection text
1227 StringOutputStream key( 64 );
1228 key << gtk_entry_get_text( g_entityKeyEntry );
1229
1230 if ( strcmp( key.c_str(), "classname" ) != 0 ) {
1231 StringOutputStream command;
1232 command << "entityDeleteKey -key " << key.c_str();
1233 UndoableCommand undo( command.c_str() );
1234 Scene_EntitySetKeyValue_Selected( key.c_str(), "" );
1235 }
1236 }
1237
1238 static gint EntityInspector_clearKeyValueKB( GtkEntry* widget, GdkEventKey* event, gpointer data ){
1239 if ( event->keyval == GDK_KEY_Delete ) {
1240 EntityInspector_clearKeyValue();
1241 return TRUE;
1242 }
1243 return FALSE;
1244 }
1245
1246 void EntityInspector_clearAllKeyValues(){
1247 UndoableCommand undo( "entityClear" );
1248
1249 // remove all keys except classname
1250 for ( KeyValues::iterator i = g_selectedKeyValues.begin(); i != g_selectedKeyValues.end(); ++i )
1251 {
1252 if ( strcmp( ( *i ).first.c_str(), "classname" ) != 0 ) {
1253 Scene_EntitySetKeyValue_Selected( ( *i ).first.c_str(), "" );
1254 }
1255 }
1256 }
1257
1258 // =============================================================================
1259 // callbacks
1260
1261 static void EntityClassList_selection_changed( ui::TreeSelection selection, gpointer data ){
1262 GtkTreeModel* model;
1263 GtkTreeIter selected;
1264 if ( gtk_tree_selection_get_selected( selection, &model, &selected ) ) {
1265 EntityClass* eclass;
1266 gtk_tree_model_get( model, &selected, 1, &eclass, -1 );
1267 if ( eclass != 0 ) {
1268 SetComment( eclass );
1269 }
1270 }
1271 }
1272
1273 static gint EntityClassList_button_press( ui::Widget widget, GdkEventButton *event, gpointer data ){
1274 if ( event->type == GDK_2BUTTON_PRESS ) {
1275 EntityClassList_createEntity();
1276 return TRUE;
1277 }
1278 return FALSE;
1279 }
1280
1281 static gint EntityClassList_keypress( ui::Widget widget, GdkEventKey* event, gpointer data ){
1282 if ( event->keyval == GDK_KEY_Return ) {
1283 EntityClassList_createEntity();
1284 return TRUE;
1285 }
1286
1287 // select the entity that starts with the key pressed
1288 /*
1289 unsigned int code = gdk_keyval_to_upper( event->keyval );
1290 if ( code <= 'Z' && code >= 'A' && event->state == 0 ) {
1291 auto view = ui::TreeView(g_entityClassList);
1292 GtkTreeModel* model;
1293 GtkTreeIter iter;
1294 if ( gtk_tree_selection_get_selected( gtk_tree_view_get_selection( view ), &model, &iter ) == FALSE
1295 || gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1296 gtk_tree_model_get_iter_first( model, &iter );
1297 }
1298
1299 for ( std::size_t count = gtk_tree_model_iter_n_children( model, 0 ); count > 0; --count )
1300 {
1301 char* text;
1302 gtk_tree_model_get( model, &iter, 0, &text, -1 );
1303
1304 if ( toupper( text[0] ) == (int)code ) {
1305 auto path = gtk_tree_model_get_path( model, &iter );
1306 gtk_tree_selection_select_path( gtk_tree_view_get_selection( view ), path );
1307 if ( gtk_widget_get_realized( view ) ) {
1308 gtk_tree_view_scroll_to_cell( view, path, 0, FALSE, 0, 0 );
1309 }
1310 gtk_tree_path_free( path );
1311 count = 1;
1312 }
1313
1314 g_free( text );
1315
1316 if ( gtk_tree_model_iter_next( model, &iter ) == FALSE ) {
1317 gtk_tree_model_get_iter_first( model, &iter );
1318 }
1319 }
1320
1321 return TRUE;
1322 }
1323 */
1324 return FALSE;
1325 }
1326
1327 static void EntityProperties_selection_changed( ui::TreeSelection selection, gpointer data ){
1328 // find out what type of entity we are trying to create
1329 GtkTreeModel* model;
1330 GtkTreeIter iter;
1331 if ( gtk_tree_selection_get_selected( selection, &model, &iter ) == FALSE ) {
1332 return;
1333 }
1334
1335 char* key;
1336 char* val;
1337 gtk_tree_model_get( model, &iter, 0, &key, 1, &val, -1 );
1338
1339 g_entityKeyEntry.text( key );
1340 g_entityValueEntry.text( val );
1341
1342 g_free( key );
1343 g_free( val );
1344 }
1345
1346 static void SpawnflagCheck_toggled( ui::Widget widget, gpointer data ){
1347 EntityInspector_applySpawnflags();
1348 }
1349
1350 static gint EntityEntry_keypress( ui::Entry widget, GdkEventKey* event, gpointer data ){
1351 if ( event->keyval == GDK_KEY_Return ) {
1352 if ( widget._handle == g_entityKeyEntry._handle ) {
1353 // g_entityValueEntry.text( "" );
1354 gtk_window_set_focus( widget.window(), g_entityValueEntry );
1355 }
1356 else
1357 {
1358 EntityInspector_applyKeyValue();
1359 }
1360 return TRUE;
1361 }
1362 if ( event->keyval == GDK_KEY_Tab ) {
1363 if ( widget._handle == g_entityKeyEntry._handle ) {
1364 gtk_window_set_focus( widget.window(), g_entityValueEntry );
1365 }
1366 else
1367 {
1368 gtk_window_set_focus( widget.window(), g_entityKeyEntry );
1369 }
1370 return TRUE;
1371 }
1372 if ( event->keyval == GDK_KEY_Escape ) {
1373 // gtk_window_set_focus( widget.window(), NULL );
1374 return TRUE;
1375 }
1376
1377 return FALSE;
1378 }
1379
1380 void EntityInspector_destroyWindow( ui::Widget widget, gpointer data ){
1381 g_entitysplit0_position = gtk_paned_get_position( GTK_PANED( g_entity_split0 ) );
1382 g_entitysplit1_position = gtk_paned_get_position( GTK_PANED( g_entity_split1 ) );
1383 g_entitysplit2_position = gtk_paned_get_position( GTK_PANED( g_entity_split2 ) );
1384 g_entityInspector_windowConstructed = false;
1385 GlobalEntityAttributes_clear();
1386 }
1387
1388 static gint EntityInspector_hideWindowKB( GtkWidget* widget, GdkEventKey* event, gpointer data ){
1389 //if ( event->keyval == GDK_KEY_Escape && GTK_WIDGET_VISIBLE( GTK_WIDGET( widget ) ) ) {
1390 if ( event->keyval == GDK_KEY_Escape ) {
1391 //GroupDialog_showPage( g_page_entity );
1392 gtk_widget_hide( GTK_WIDGET( GroupDialog_getWindow() ) );
1393 return TRUE;
1394 }
1395 /* this doesn't work, if tab is bound (func is not called then) */
1396 if ( event->keyval == GDK_KEY_Tab ) {
1397 gtk_window_set_focus( GTK_WINDOW( gtk_widget_get_toplevel( GTK_WIDGET( widget ) ) ), GTK_WIDGET( g_entityKeyEntry ) );
1398 return TRUE;
1399 }
1400 return FALSE;
1401 }
1402
1403 void EntityInspector_selectTargeting( GtkButton *button, gpointer user_data ){
1404 bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
1405 Select_ConnectedEntities( true, false, focus );
1406 }
1407
1408 void EntityInspector_selectTargets( GtkButton *button, gpointer user_data ){
1409 bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
1410 Select_ConnectedEntities( false, true, focus );
1411 }
1412
1413 void EntityInspector_selectConnected( GtkButton *button, gpointer user_data ){
1414 bool focus = gtk_toggle_button_get_active( g_focusToggleButton );
1415 Select_ConnectedEntities( true, true, focus );
1416 }
1417
1418 ui::Widget EntityInspector_constructWindow( ui::Window toplevel ){
1419 auto vbox = ui::VBox( FALSE, 2 );
1420 vbox.show();
1421 gtk_container_set_border_width( GTK_CONTAINER( vbox ), 2 );
1422
1423 g_signal_connect( G_OBJECT( toplevel ), "key_press_event", G_CALLBACK( EntityInspector_hideWindowKB ), 0 );
1424 vbox.connect( "destroy", G_CALLBACK( EntityInspector_destroyWindow ), 0 );
1425
1426 {
1427 auto split1 = ui::VPaned(ui::New);
1428 vbox.pack_start( split1, TRUE, TRUE, 0 );
1429 split1.show();
1430
1431 g_entity_split1 = split1;
1432
1433 {
1434 ui::Widget split2 = ui::VPaned(ui::New);
1435 //gtk_paned_add1( GTK_PANED( split1 ), split2 );
1436 gtk_paned_pack1( GTK_PANED( split1 ), split2, FALSE, FALSE );
1437 split2.show();
1438
1439 g_entity_split2 = split2;
1440
1441 {
1442 // class list
1443 auto scr = ui::ScrolledWindow(ui::New);
1444 scr.show();
1445 //gtk_paned_add1( GTK_PANED( split2 ), scr );
1446 gtk_paned_pack1( GTK_PANED( split2 ), scr, FALSE, FALSE );
1447 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1448 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1449
1450 {
1451 ui::ListStore store = ui::ListStore::from(gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_POINTER ));
1452
1453 auto view = ui::TreeView( ui::TreeModel::from( store._handle ));
1454 // gtk_tree_view_set_enable_search(view, FALSE );
1455 gtk_tree_view_set_headers_visible( view, FALSE );
1456 view.connect( "button_press_event", G_CALLBACK( EntityClassList_button_press ), 0 );
1457 view.connect( "key_press_event", G_CALLBACK( EntityClassList_keypress ), 0 );
1458
1459 {
1460 auto renderer = ui::CellRendererText(ui::New);
1461 auto column = ui::TreeViewColumn( "Key", renderer, {{"text", 0}} );
1462 gtk_tree_view_append_column( view, column );
1463 }
1464
1465 {
1466 auto selection = ui::TreeSelection::from(gtk_tree_view_get_selection( view ));
1467 selection.connect( "changed", G_CALLBACK( EntityClassList_selection_changed ), 0 );
1468 }
1469
1470 view.show();
1471
1472 scr.add(view);
1473
1474 store.unref();
1475 g_entityClassList = view;
1476 g_entlist_store = store;
1477 }
1478 }
1479
1480 {
1481 auto scr = ui::ScrolledWindow(ui::New);
1482 scr.show();
1483 //gtk_paned_add2( GTK_PANED( split2 ), scr );
1484 gtk_paned_pack2( GTK_PANED( split2 ), scr, FALSE, FALSE );
1485 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_ALWAYS );
1486 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1487
1488 {
1489 auto text = ui::TextView(ui::New);
1490 text.dimensions(0, -1); // allow shrinking
1491 gtk_text_view_set_wrap_mode( text, GTK_WRAP_WORD );
1492 gtk_text_view_set_editable( text, FALSE );
1493 text.show();
1494 scr.add(text);
1495 g_entityClassComment = text;
1496 {
1497 GtkTextBuffer *buffer = gtk_text_view_get_buffer( text );
1498 gtk_text_buffer_create_tag( buffer, "bold", "weight", PANGO_WEIGHT_BOLD, NULL );
1499 }
1500 }
1501 }
1502 }
1503
1504 {
1505 ui::Widget split0 = ui::VPaned(ui::New);
1506 //gtk_paned_add2( GTK_PANED( split1 ), split0 );
1507 gtk_paned_pack2( GTK_PANED( split1 ), split0, FALSE, FALSE );
1508 split0.show();
1509 g_entity_split0 = split0;
1510
1511 {
1512 auto vbox2 = ui::VBox( FALSE, 2 );
1513 vbox2.show();
1514 gtk_paned_pack1( GTK_PANED( split0 ), vbox2, FALSE, FALSE );
1515
1516 {
1517 // Spawnflags (4 colums wide max, or window gets too wide.)
1518 auto table = ui::Table( 4, 4, FALSE );
1519 vbox2.pack_start( table, FALSE, TRUE, 0 );
1520 table.show();
1521
1522 g_spawnflagsTable = table;
1523
1524 for ( int i = 0; i < MAX_FLAGS; i++ )
1525 {
1526 auto check = ui::CheckButton( "" );
1527 check.ref();
1528 g_object_set_data( G_OBJECT( check ), "handler", gint_to_pointer( check.connect( "toggled", G_CALLBACK( SpawnflagCheck_toggled ), 0 ) ) );
1529 g_entitySpawnflagsCheck[i] = check;
1530 }
1531 }
1532
1533 {
1534 // key/value list
1535 auto scr = ui::ScrolledWindow(ui::New);
1536 scr.show();
1537 vbox2.pack_start( scr, TRUE, TRUE, 0 );
1538 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
1539 gtk_scrolled_window_set_shadow_type( GTK_SCROLLED_WINDOW( scr ), GTK_SHADOW_IN );
1540
1541 {
1542 ui::ListStore store = ui::ListStore::from(gtk_list_store_new( 2, G_TYPE_STRING, G_TYPE_STRING ));
1543
1544 auto view = ui::TreeView(ui::TreeModel::from(store._handle));
1545 gtk_tree_view_set_enable_search(view, FALSE );
1546 gtk_tree_view_set_headers_visible(view, FALSE );
1547 g_signal_connect( G_OBJECT( view ), "key_press_event", G_CALLBACK( EntityInspector_clearKeyValueKB ), 0 );
1548
1549 {
1550 auto renderer = ui::CellRendererText(ui::New);
1551 auto column = ui::TreeViewColumn( "", renderer, {{"text", 0}} );
1552 gtk_tree_view_append_column(view, column );
1553 }
1554
1555 {
1556 auto renderer = ui::CellRendererText(ui::New);
1557 auto column = ui::TreeViewColumn( "", renderer, {{"text", 1}} );
1558 gtk_tree_view_append_column(view, column );
1559 }
1560
1561 {
1562 auto selection = ui::TreeSelection::from(gtk_tree_view_get_selection(view));
1563 selection.connect( "changed", G_CALLBACK( EntityProperties_selection_changed ), 0 );
1564 }
1565
1566 view.show();
1567
1568 scr.add(view);
1569
1570 store.unref();
1571
1572 g_entprops_store = store;
1573 }
1574 }
1575
1576 {
1577 // key/value entry
1578 auto table = ui::Table( 2, 2, FALSE );
1579 table.show();
1580 vbox2.pack_start( table, FALSE, TRUE, 0 );
1581 gtk_table_set_row_spacings( table, 3 );
1582 gtk_table_set_col_spacings( table, 5 );
1583
1584 {
1585 auto entry = ui::Entry(ui::New);
1586 entry.show();
1587 table.attach(entry, {1, 2, 0, 1}, {GTK_EXPAND | GTK_FILL, 0});
1588 gtk_widget_set_events( entry , GDK_KEY_PRESS_MASK );
1589 entry.connect( "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1590 g_entityKeyEntry = entry;
1591 }
1592
1593 {
1594 auto entry = ui::Entry(ui::New);
1595 entry.show();
1596 table.attach(entry, {1, 2, 1, 2}, {GTK_EXPAND | GTK_FILL, 0});
1597 gtk_widget_set_events( entry , GDK_KEY_PRESS_MASK );
1598 entry.connect( "key_press_event", G_CALLBACK( EntityEntry_keypress ), 0 );
1599 g_entityValueEntry = entry;
1600 }
1601
1602 {
1603 auto label = ui::Label( "Value" );
1604 label.show();
1605 table.attach(label, {0, 1, 1, 2}, {GTK_FILL, 0});
1606 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1607 }
1608
1609 {
1610 auto label = ui::Label( "Key" );
1611 label.show();
1612 table.attach(label, {0, 1, 0, 1}, {GTK_FILL, 0});
1613 gtk_misc_set_alignment( GTK_MISC( label ), 0, 0.5 );
1614 }
1615 }
1616
1617 {
1618 auto hbox = ui::HBox( FALSE, 4 );
1619 hbox.show();
1620 vbox2.pack_start( hbox, FALSE, TRUE, 0 );
1621
1622 {
1623 auto button = ui::Button( "Clear All" );
1624 #define GARUX_DISABLE_BUTTON_NOFOCUS
1625 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1626 GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
1627 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1628 button.show();
1629 button.connect( "clicked", G_CALLBACK( EntityInspector_clearAllKeyValues ), 0 );
1630 hbox.pack_start( button, TRUE, TRUE, 0 );
1631 }
1632 {
1633 auto button = ui::Button( "Delete Key" );
1634 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1635 GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
1636 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1637 button.show();
1638 button.connect( "clicked", G_CALLBACK( EntityInspector_clearKeyValue ), 0 );
1639 hbox.pack_start( button, TRUE, TRUE, 0 );
1640 }
1641 {
1642 GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "<" ) );
1643 gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select targeting entities" );
1644 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1645 GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
1646 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1647 gtk_widget_show( GTK_WIDGET( button ) );
1648 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectTargeting ), 0 );
1649 gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
1650 }
1651 {
1652 GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( ">" ) );
1653 gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select targets" );
1654 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1655 GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
1656 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1657 gtk_widget_show( GTK_WIDGET( button ) );
1658 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectTargets ), 0 );
1659 gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
1660 }
1661 {
1662 GtkButton* button = GTK_BUTTON( gtk_button_new_with_label( "<->" ) );
1663 gtk_widget_set_tooltip_text( GTK_WIDGET( button ), "Select connected entities" );
1664 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1665 GTK_WIDGET_UNSET_FLAGS( GTK_WIDGET( button ), GTK_CAN_FOCUS );
1666 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1667 gtk_widget_show( GTK_WIDGET( button ) );
1668 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( EntityInspector_selectConnected ), 0 );
1669 gtk_box_pack_start( hbox, GTK_WIDGET( button ), FALSE, FALSE, 0 );
1670 }
1671 {
1672 GtkWidget* button = gtk_toggle_button_new();
1673 GtkImage* image = GTK_IMAGE( gtk_image_new_from_stock( GTK_STOCK_ZOOM_IN, GTK_ICON_SIZE_SMALL_TOOLBAR ) );
1674 gtk_widget_show( GTK_WIDGET( image ) );
1675 gtk_container_add( GTK_CONTAINER( button ), GTK_WIDGET( image ) );
1676 gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
1677 #ifndef GARUX_DISABLE_BUTTON_NOFOCUS
1678 GTK_WIDGET_UNSET_FLAGS( button, GTK_CAN_FOCUS );
1679 #endif // GARUX_DISABLE_BUTTON_NOFOCUS
1680 gtk_box_pack_start( hbox, button, FALSE, FALSE, 0 );
1681 gtk_widget_set_tooltip_text( button, "Focus on Selected" );
1682 gtk_widget_show( button );
1683 g_focusToggleButton = GTK_TOGGLE_BUTTON( button );
1684 }
1685 }
1686 }
1687
1688 {
1689 auto scr = ui::ScrolledWindow(ui::New);
1690 scr.show();
1691 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW( scr ), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC );
1692
1693 auto viewport = ui::Container::from(gtk_viewport_new( 0, 0 ));
1694 viewport.show();
1695 gtk_viewport_set_shadow_type( GTK_VIEWPORT( viewport ), GTK_SHADOW_NONE );
1696
1697 g_attributeBox = ui::VBox( FALSE, 2 );
1698 g_attributeBox.show();
1699
1700 viewport.add(g_attributeBox);
1701 scr.add(viewport);
1702 gtk_paned_pack2( GTK_PANED( split0 ), scr, FALSE, FALSE );
1703 }
1704 }
1705 }
1706
1707
1708 {
1709 // show the sliders in any case //no need, gtk can care
1710 /*if ( g_entitysplit2_position < 22 ) {
1711 g_entitysplit2_position = 22;
1712 }*/
1713 gtk_paned_set_position( GTK_PANED( g_entity_split2 ), g_entitysplit2_position );
1714 /*if ( ( g_entitysplit1_position - g_entitysplit2_position ) < 27 ) {
1715 g_entitysplit1_position = g_entitysplit2_position + 27;
1716 }*/
1717 gtk_paned_set_position( GTK_PANED( g_entity_split1 ), g_entitysplit1_position );
1718 gtk_paned_set_position( GTK_PANED( g_entity_split0 ), g_entitysplit0_position );
1719 }
1720
1721 g_entityInspector_windowConstructed = true;
1722 EntityClassList_fill();
1723
1724 typedef FreeCaller<void(const Selectable&), EntityInspector_selectionChanged> EntityInspectorSelectionChangedCaller;
1725 GlobalSelectionSystem().addSelectionChangeCallback( EntityInspectorSelectionChangedCaller() );
1726 GlobalEntityCreator().setKeyValueChangedFunc( EntityInspector_keyValueChanged );
1727
1728 // hack
1729 gtk_container_set_focus_chain( GTK_CONTAINER( vbox ), NULL );
1730
1731 return vbox;
1732 }
1733
1734 class EntityInspector : public ModuleObserver
1735 {
1736 std::size_t m_unrealised;
1737 public:
1738 EntityInspector() : m_unrealised( 1 ){
1739 }
1740 void realise(){
1741 if ( --m_unrealised == 0 ) {
1742 if ( g_entityInspector_windowConstructed ) {
1743 //globalOutputStream() << "Entity Inspector: realise\n";
1744 EntityClassList_fill();
1745 }
1746 }
1747 }
1748 void unrealise(){
1749 if ( ++m_unrealised == 1 ) {
1750 if ( g_entityInspector_windowConstructed ) {
1751 //globalOutputStream() << "Entity Inspector: unrealise\n";
1752 EntityClassList_clear();
1753 }
1754 }
1755 }
1756 };
1757
1758 EntityInspector g_EntityInspector;
1759
1760 #include "preferencesystem.h"
1761 #include "stringio.h"
1762
1763 void EntityInspector_construct(){
1764 GlobalEntityClassManager().attach( g_EntityInspector );
1765
1766 GlobalPreferenceSystem().registerPreference( "EntitySplit0", make_property_string( g_entitysplit0_position ) );
1767 GlobalPreferenceSystem().registerPreference( "EntitySplit1", make_property_string( g_entitysplit1_position ) );
1768 GlobalPreferenceSystem().registerPreference( "EntitySplit2", make_property_string( g_entitysplit2_position ) );
1769
1770 }
1771
1772 void EntityInspector_destroy(){
1773 GlobalEntityClassManager().detach( g_EntityInspector );
1774 }
1775
1776 const char *EntityInspector_getCurrentKey(){
1777 if ( !GroupDialog_isShown() ) {
1778 return 0;
1779 }
1780 if ( GroupDialog_getPage() != g_page_entity ) {
1781 return 0;
1782 }
1783 return gtk_entry_get_text( g_entityKeyEntry );
1784 }