ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/deliantra
Revision: 1.59
Committed: Tue Aug 19 17:38:30 2008 UTC (15 years, 9 months ago) by root
Branch: MAIN
Changes since 1.58: +108 -101 lines
Log Message:
os x port firts rough hack^Wcut

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3 root 1.7 if ($ENV{DELIANTRA_CORO_DEBUG}) {
4     eval '
5     use Coro;
6     use Coro::EV;
7     use Coro::Debug;
8     our $debug = new_unix_server Coro::Debug "/tmp/dc";
9     ';
10     }
11    
12 root 1.1 # do splash-screen thingy on win32
13     my $startup_done = sub { };
14     BEGIN {
15     if (%PAR::LibCache && $^O eq "MSWin32") {
16     while (my ($filename, $zip) = each %PAR::LibCache) {
17     $zip->extractMember ("SPLASH.bmp", "$ENV{PAR_TEMP}/SPLASH.bmp");
18     }
19    
20     require Win32::GUI::SplashScreen;
21    
22     Win32::GUI::SplashScreen::Show (
23     -file => "$ENV{PAR_TEMP}/SPLASH.bmp",
24     );
25    
26     $startup_done = sub {
27     Win32::GUI::SplashScreen::Done (1);
28     };
29     }
30     }
31    
32     use strict;
33     use utf8;
34    
35     use Carp 'verbose';
36    
37     # do things only needed for single-binary version (par)
38     BEGIN {
39     if (%PAR::LibCache) {
40     @INC = grep ref, @INC; # weed out all paths except pars loader refs
41    
42     my $root = $ENV{PAR_TEMP};
43    
44     while (my ($filename, $zip) = each %PAR::LibCache) {
45     for ($zip->memberNames) {
46     next unless /^root\/(.*)/;
47     $zip->extractMember ($_, "$root/$1")
48     unless -e "$root/$1";
49     }
50     }
51    
52     if ($^O eq "MSWin32") {
53     # pango is relocatable on win32
54     } else {
55     open my $fh, "<:perlio", "$root/pangoversion"
56     or die "pangoversion: $!";
57     my $PANGO = <$fh>;
58     # unix, need to patch pango rc file
59     open my $fh, "<:perlio", "$root/usr/lib/pango/$PANGO/module-files.d/libpango1.0-0.modules"
60     or die "$root/usr/lib/$PANGO/module-files.d/libpango1.0-0.modules: $!";
61     local $/;
62     my $rc = <$fh>;
63     $rc =~ s/^\//$root\//gm; # replace abs paths by relative ones
64    
65     mkdir "$root/pango-modules";
66     open my $fh, ">:perlio", "$root/pango-modules/pango.modules"
67     or die "$root/pango-modules/pango.modules: $!";
68     print $fh $rc;
69    
70     $ENV{PANGO_RC_FILE} = "$root/pango.rc";
71     open my $fh, ">:perlio", $ENV{PANGO_RC_FILE}
72     or die "$ENV{PANGO_RC_FILE}: $!";
73     print $fh "[Pango]\nModuleFiles = $root/pango-modules\n";
74     }
75    
76     unshift @INC, $root;
77     }
78     }
79    
80 root 1.17 # prepend private library directory
81     BEGIN {
82     for (grep !ref, @INC) {
83 root 1.24 my $path = "$_/Deliantra/Client/private";
84 root 1.17 if (-d $path) {
85     unshift @INC, $path;
86     last;
87     }
88     }
89     }
90    
91 root 1.1 # need to do it again because that pile of garbage called PAR nukes it before main
92     unshift @INC, $ENV{PAR_TEMP}
93     if %PAR::LibCache;
94    
95     use Time::HiRes 'time';
96 root 1.4 use EV;
97 root 1.1 use List::Util qw(max min);
98    
99 root 1.14 use Deliantra;
100     use Deliantra::Protocol::Constants;
101 root 1.1
102 root 1.46 use AnyEvent::DNS;
103 root 1.51 use AnyEvent::Socket ();
104 root 1.46
105 root 1.1 use Compress::LZF;
106    
107 root 1.44 use DC;
108 root 1.52 BEGIN { $SIG{__DIE__} = sub { DC::fatal Carp::longmess "$_[0]" unless $^S } }
109 root 1.18 use DC::OpenGL ();
110     use DC::Protocol;
111     use DC::DB;
112     use DC::UI;
113     use DC::UI::Canvas;
114     use DC::UI::Inventory;
115     use DC::UI::SpellList;
116     use DC::UI::Dockable;
117 elmex 1.23 use DC::UI::Dockbar;
118 root 1.18 use DC::UI::MessageWindow;
119     use DC::UI::ChatView;
120 elmex 1.23 use DC::MessageDistributor;
121 root 1.18 use DC::Pod;
122     use DC::MapWidget;
123     use DC::Macro;
124 root 1.1
125     $SIG{QUIT} = sub { Carp::cluck "QUIT" };
126     $SIG{PIPE} = 'IGNORE';
127    
128 root 1.4 $EV::DIED = sub {
129 root 1.18 DC::fatal Carp::longmess $@;
130 root 1.1 };
131    
132     my $MAX_FPS = 60;
133    
134     our $META_SERVER = "http://metaserver.schmorp.de/current.json";
135    
136     our $LAST_REFRESH;
137     our $NOW;
138    
139     our $CFG;
140     our $CONN;
141     our $PROFILE; # current profile
142     our $FAST; # fast, low-quality mode, possibly useful for software-rendering
143    
144     our $WANT_REFRESH;
145    
146 root 1.53 our $MODE_SLIDER;
147     our $CAVEAT_LABEL;
148    
149 root 1.1 our @SDL_MODES;
150 root 1.52 our $SDL_REINIT = 1;
151 root 1.1 our $WIDTH;
152     our $HEIGHT;
153     our $FULLSCREEN;
154     our $FONTSIZE;
155    
156     our $FONT_PROP;
157     our $FONT_FIXED;
158    
159     our $MAP;
160     our $MAPMAP;
161     our $MAPWIDGET;
162     our $COMPLETER;
163     our $BUTTONBAR;
164     our $METASERVER;
165     our $LOGIN_BUTTON;
166     our $QUIT_DIALOG;
167     our $HOST_ENTRY;
168     our $FULLSCREEN_ENABLE;
169     our $PICKUP_ENABLE;
170     our $SERVER_INFO;
171    
172     our $SETUP_DIALOG;
173     our $SETUP_NOTEBOOK;
174     our $SETUP_SERVER;
175     our $SETUP_LOGIN;
176     our $SETUP_KEYBOARD;
177    
178     our $PL_NOTEBOOK;
179     our $PL_WINDOW;
180    
181     our $MUSIC_PLAYING_WIDGET;
182     our $LICENSE_WIDGET;
183    
184     our $PICKUP_PAGE;
185     our $INVENTORY_PAGE;
186     our $STATS_PAGE;
187     our $SKILL_PAGE;
188     our $SPELL_PAGE;
189     our $SPELL_LIST;
190    
191     our $HELP_WINDOW;
192     our $MESSAGE_WINDOW;
193 elmex 1.23 our $MESSAGE_DIST;
194 root 1.1 our $FLOORBOX;
195     our $GAUGES;
196     our $STATWIDS;
197    
198     our $SDL_ACTIVE;
199     our %SDL_CB;
200    
201     our $ALT_ENTER_MESSAGE;
202     our $STATUSBOX;
203 root 1.29 our $MODBOX;
204 root 1.1 our $DEBUG_STATUS;
205    
206     our $INV;
207     our $INVR;
208     our $INVR_HB;
209    
210     #############################################################################
211    
212     sub status {
213 root 1.18 $STATUSBOX->add (DC::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
214 root 1.1 }
215    
216     sub debug {
217     $DEBUG_STATUS->set_text ($_[0]);
218     }
219    
220     sub message {
221 elmex 1.23 $MESSAGE_DIST->message (@_);
222 root 1.1 }
223    
224 root 1.29 sub update_modbox {
225     my $mod = DC::SDL_GetModState;
226    
227     my $markup;
228    
229     $markup .= $mod & DC::KMOD_CTRL
230     ? ($MAPWIDGET->{ctrl} ? "[REPEAT]" : "[<span foreground='#888'>REPEAT</span>]")
231     : "[<span foreground='#888'> once </span>]";
232    
233     $markup .= $mod & DC::KMOD_SHIFT
234     ? ($MAPWIDGET->{shft} ? "[FIRE]" : "[<span foreground='#888'>FIRE</span>]")
235     : "[<span foreground='#888'>move</span>]";
236    
237     $markup .= $mod & (DC::KMOD_ALT | DC::KMOD_META)
238     ? "[ALT]"
239     : "[<span foreground='#888'>alt</span>]";
240    
241     $markup .= $mod & DC::KMOD_NUM
242     ? "[NUM]"
243     : "[<span foreground='#888'>num</span>]";
244    
245 root 1.30 # <tt> around next statement works around some bug that keeps the
246     # "font =>" from being used on windows
247     $MODBOX->set_markup ("<tt>$markup</tt>");
248 root 1.29 }
249    
250 root 1.1 #############################################################################
251     #TODO: maybe move into own audio module...
252    
253     our $SDL_MIXER;
254    
255     our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
256     our $MUSIC_WANT; # arryref of ambient music we want to play
257     our @MUSIC_HAVE; # ambient music we have on disk
258     our $MUSIC_START;
259     our @MUSIC_JINGLE; # which jingles to play next
260     our $MUSIC_PLAYING_DATA;
261     our $MUSIC_PLAYING_META;
262     our $MUSIC_PLAYER;
263     our $MUSIC_RESUME = 30; # resume music when played less than these many seconds before
264    
265     our %AUDIO_CHUNK; # audio "files"
266     our %AUDIO_PLAY; # which audio faces should be played
267    
268     sub audio_channel_finished {
269     my ($channel) = @_;
270    
271     # warn "channel $channel finished\n";#d#
272     }
273    
274     sub audio_sound_push($) {
275     my ($face) = @_;
276    
277     $CFG->{effects_enable}
278     or return;
279    
280     $AUDIO_PLAY{$face}
281     or return;
282    
283     if (my $chunk = $AUDIO_CHUNK{$face}) {
284 root 1.4 for (grep $_->[0] >= EV::now, @{(delete $AUDIO_PLAY{$face}) || []}) {
285 root 1.1 my (undef, $dx, $dy, $vol) = @$_;
286    
287 root 1.18 my $channel = DC::Channel::find;
288 root 1.1 $channel->volume ($vol * $CFG->{effects_volume} * 128 / 255);
289     $channel->set_position_r ($dx, $dy, 20);
290     $chunk->play ($channel);
291     }
292     } else {
293     # sound_meta not set means data is in flight either way
294     my $meta = $CONN->{face}[$face]
295     or return;
296    
297     $meta->{data}
298     or return;
299    
300     # if its a jingle, play it as ambient music
301     if ($meta->{data}{jingle}) {
302     if (delete $AUDIO_PLAY{$face}) { # take the jingle out of the sound queue
303     push @MUSIC_JINGLE, $meta; # push it oto the music/jingle queue
304     &audio_music_push ($face);
305     }
306     } else {
307     # fetch from database
308 root 1.18 DC::DB::get res_data => $meta->{name}, sub {
309     my $rwops = new DC::RW $_[0];
310     my $chunk = new DC::MixChunk $rwops
311     or Carp::confess "sound face " . (JSON::XS::encode_json $meta) . " unloadable: " . DC::Mix_GetError;
312 root 1.1 $chunk->volume (($meta->{data}{volume} || 1) * 128);
313     $AUDIO_CHUNK{$face} = $chunk;
314    
315     audio_sound_push ($face);
316     };
317     }
318     }
319     }
320    
321     sub audio_sound_play {
322     my ($face, $dx, $dy, $vol) = @_;
323    
324     $SDL_MIXER
325     or return;
326     $CFG->{effects_enable}
327     or return;
328    
329     my $queue = $AUDIO_PLAY{$face} ||= [];
330 root 1.4 push @$queue, [EV::now + 0.6, $dx, $dy, $vol]; # do not play sound for outdated events
331 root 1.1 audio_sound_push $face
332     unless @$queue > 1;
333     }
334    
335     sub audio_music_set_meta {
336     my ($meta) = @_;
337    
338     $MUSIC_PLAYING_META = $meta;
339     $MUSIC_PLAYING_WIDGET->set_markup (
340 root 1.18 "<b>Name</b>: " . (DC::asxml $meta->{data}{name}) . "\n"
341     . "<b>Author</b>: " . (DC::asxml $meta->{data}{author}) . "\n"
342     . "<b>Source</b>: " . (DC::asxml $meta->{data}{source}) . "\n"
343     . "<b>License</b>: " . (DC::asxml $meta->{data}{license})
344 root 1.1 );
345     }
346    
347     sub audio_music_update_volume {
348     return unless $MUSIC_PLAYING_META;
349     my $volume = $MUSIC_PLAYING_META->{data}{volume} || 1;
350     my $base = $MUSIC_PLAYING_META->{data}{jingle} ? 1 : $CFG->{bgm_volume};
351 root 1.18 DC::MixMusic::volume $base * $volume * 128;
352 root 1.1 }
353    
354     sub audio_music_start {
355     my $meta = $MUSIC_PLAYING_META;
356    
357 root 1.18 DC::DB::get res_data => $meta->{name}, sub {
358 root 1.1 return unless $SDL_MIXER;
359    
360     # music might have changed...
361     $meta eq $MUSIC_PLAYING_META
362     or return &audio_music_start ();
363    
364     audio_music_update_volume;
365    
366     $MUSIC_PLAYING_DATA = \$_[0];
367    
368     my $rwops = $meta->{path}
369 root 1.18 ? new_from_file DC::RW $meta->{path}
370     : new DC::RW $$MUSIC_PLAYING_DATA;
371 root 1.1
372 root 1.18 $MUSIC_PLAYER = new DC::MixMusic $rwops
373     or Carp::confess "music face $meta->{face} unloadable: " . DC::Mix_GetError;
374 root 1.1
375     my $NOW = time;
376    
377     if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
378     my $pos = $MUSIC_PLAYING_META->{stop_pos};
379     $MUSIC_PLAYER->fade_in_pos (0, 700, $pos);
380     $MUSIC_START = time - $pos;
381     } else {
382     $MUSIC_PLAYER->play (0);
383     $MUSIC_START = time;
384     }
385    
386     delete $meta->{stop_time};
387     delete $meta->{stop_pos};
388     }
389     }
390    
391     sub audio_music_push {
392     return unless $SDL_MIXER;
393    
394     my $fade_out;
395    
396     if (@MUSIC_JINGLE) {
397 root 1.9 $fade_out = 333;
398 root 1.1 @MUSIC_HAVE = $MUSIC_JINGLE[0];
399 root 1.9
400 root 1.1 } else {
401     return unless $CFG->{bgm_enable};
402    
403 root 1.9 $fade_out = 700;
404    
405     @MUSIC_HAVE =
406 root 1.1 grep $_ && $_->{data},
407     map $CONN->{face}[$_],
408     @$MUSIC_WANT;
409    
410     # randomize music a bit so that the order is not always the same
411 root 1.9 $_->{stop_time} ||= rand for @MUSIC_HAVE;
412 root 1.1
413     # default MUSIC_HAVE == MUSIC_DEFAULT
414 root 1.18 @MUSIC_HAVE = { path => DC::find_rcfile "music/$MUSIC_DEFAULT" }
415 root 1.9 unless @MUSIC_HAVE;
416 root 1.1 }
417    
418     # if the currently playing song is acceptable, let it continue
419     return if grep $MUSIC_PLAYING_META == $_, @MUSIC_HAVE;
420    
421     my $NOW = time;
422    
423     if ($MUSIC_PLAYING_META) {
424     $MUSIC_PLAYING_META->{stop_time} = $NOW;
425     $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
426 root 1.18 DC::MixMusic::fade_out $fade_out;
427 root 1.1 } else {
428     # sort by stop time, oldest first
429     @MUSIC_HAVE = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_HAVE;
430    
431     # if the most recently-played piece played very recently,
432     # resume it, else choose the oldest piece for rotation.
433     audio_music_set_meta
434     $MUSIC_HAVE[-1]{stop_pos} && $MUSIC_HAVE[-1]{stop_time} > $NOW - $MUSIC_RESUME
435     ? $MUSIC_HAVE[-1]
436     : $MUSIC_HAVE[0];
437    
438     audio_music_start;
439     }
440     }
441    
442     sub audio_music_set_ambient {
443     my ($songs) = @_;
444    
445     $MUSIC_WANT = $songs;
446     audio_music_push;
447     }
448    
449     sub audio_music_finished {
450     if ($MUSIC_PLAYING_META) {
451     $MUSIC_PLAYING_META->{stop_time} = time;
452     }
453    
454     # we compress multiple jingles of the same type
455     shift @MUSIC_JINGLE
456     while @MUSIC_JINGLE && $MUSIC_PLAYING_META == $MUSIC_JINGLE[0];
457    
458     $MUSIC_PLAYING_WIDGET->clear;
459    
460     undef $MUSIC_PLAYER;
461     undef $MUSIC_PLAYING_META;
462     undef $MUSIC_PLAYING_DATA;
463    
464     audio_music_push;
465     }
466    
467     sub audio_init {
468     if ($CFG->{audio_enable}) {
469     $ENV{MIX_EFFECTSMAXSPEED} = 1;
470 root 1.18 $SDL_MIXER = !DC::Mix_OpenAudio
471 root 1.1 $CFG->{audio_hw_frequency},
472 root 1.18 DC::MIX_DEFAULT_FORMAT,
473 root 1.1 $CFG->{audio_hw_channels},
474     $CFG->{audio_hw_chunksize};
475    
476     if ($SDL_MIXER) {
477 root 1.18 DC::Mix_AllocateChannels $CFG->{audio_mix_channels};
478 root 1.1
479     audio_music_finished;
480     } else {
481     status "Unable to open sound device: there will be no sound";
482     }
483     } else {
484     undef $SDL_MIXER;
485     }
486    
487     sub audio_tab_update;
488     audio_tab_update;
489     }
490    
491     sub audio_shutdown {
492     undef $MUSIC_PLAYER;
493     undef $MUSIC_PLAYING_META;
494     undef $MUSIC_PLAYING_DATA;
495    
496     $MUSIC_WANT = [];
497     @MUSIC_JINGLE = ();
498     %AUDIO_PLAY = ();
499     %AUDIO_CHUNK = ();
500    
501 root 1.18 DC::Mix_CloseAudio if $SDL_MIXER;
502 root 1.1 undef $SDL_MIXER;
503     }
504    
505     #############################################################################
506    
507     sub destroy_query_dialog {
508     (delete $_[0]{query_dialog})->destroy
509     if $_[0]{query_dialog};
510     }
511    
512     # FIXME: a very ugly hack to wait for stat update look below! #d#
513     our $QUERY_TIMER; #d#
514    
515     # server query dialog
516     sub server_query {
517     my ($conn, $flags, $prompt) = @_;
518    
519     # FIXME: a very ugly hack to wait for stat update #d#
520     if ($prompt =~ /roll new stats/ and not $conn->{stat_change_with}) {
521     unless ($QUERY_TIMER) {
522 root 1.10 $QUERY_TIMER = EV::timer 1, 0, sub {
523     server_query ($conn, $flags, $prompt, 1);
524     $QUERY_TIMER = undef
525     };
526    
527 root 1.1 return;
528     }
529     }
530    
531 root 1.18 $conn->{query_dialog} = my $dialog = new DC::UI::Toplevel
532 root 1.1 x => "center",
533     y => "center",
534     title => "Server Query",
535 root 1.18 child => my $vbox = new DC::UI::VBox,
536 root 1.1 ;
537    
538 root 1.18 my @dialog = my $label = new DC::UI::Label
539 root 1.1 max_w => $::WIDTH * 0.8,
540     ellipsise => 0,
541     text => $prompt;
542    
543     if ($flags & CS_QUERY_YESNO) {
544 root 1.18 push @dialog, my $hbox = new DC::UI::HBox;
545 root 1.1
546 root 1.18 $hbox->add (new DC::UI::Button
547 root 1.1 text => "No",
548     on_activate => sub {
549     $conn->send ("reply n");
550     $dialog->destroy;
551     0
552     }
553     );
554 root 1.18 $hbox->add (new DC::UI::Button
555 root 1.1 text => "Yes",
556     on_activate => sub {
557     $conn->send ("reply y");
558     destroy_query_dialog $conn;
559     0
560     },
561     );
562    
563     $dialog->grab_focus;
564    
565     } elsif ($flags & CS_QUERY_SINGLECHAR) {
566     if ($prompt =~ /Now choose a character|Press any key for the next race/i) {
567     $dialog->{tooltip} = "#charcreation_focus";
568    
569 root 1.18 unshift @dialog, new DC::UI::Label
570 root 1.1 max_w => $::WIDTH * 0.8,
571     ellipsise => 0,
572     markup => "\nOr use your keyboard and the text entry below:\n";
573    
574 root 1.18 unshift @dialog, my $table = new DC::UI::Table;
575 root 1.1
576 root 1.18 $table->add_at (0, 0, new DC::UI::Button
577 root 1.1 text => "Next Race",
578     on_activate => sub {
579     $conn->send ("reply n");
580     destroy_query_dialog $conn;
581     0
582     },
583     );
584 root 1.18 $table->add_at (2, 0, new DC::UI::Button
585 root 1.1 text => "Accept",
586     on_activate => sub {
587     $conn->send ("reply d");
588     destroy_query_dialog $conn;
589     0
590     },
591     );
592    
593     if ($conn->{chargen_race_description}) {
594 root 1.18 unshift @dialog, new DC::UI::Label
595 root 1.1 max_w => $::WIDTH * 0.8,
596     ellipsise => 0,
597     markup => "<span foreground='#ccccff'>$conn->{chargen_race_description}</span>",
598     ;
599     }
600    
601 root 1.18 unshift @dialog, new DC::UI::Face
602 root 1.1 face => $conn->{player}{face},
603     bg => [.2, .2, .2, 1],
604     min_w => 64,
605     min_h => 64,
606     ;
607    
608     if ($conn->{chargen_race_title}) {
609 root 1.18 unshift @dialog, new DC::UI::Label
610 root 1.1 allign => 1,
611     ellipsise => 0,
612     markup => "<span foreground='#ccccff' size='large'>Race: $conn->{chargen_race_title}</span>",
613     ;
614     }
615    
616 root 1.18 unshift @dialog, new DC::UI::Label
617 root 1.1 max_w => $::WIDTH * 0.4,
618     ellipsise => 0,
619 root 1.18 markup => (DC::Pod::section_label ui => "chargen_race"),
620 root 1.1 ;
621    
622     } elsif ($prompt =~ /roll new stats/) {
623     if (my $stat = delete $conn->{stat_change_with}) {
624     $conn->send ("reply $stat");
625     destroy_query_dialog $conn;
626     return;
627     }
628    
629 root 1.18 unshift @dialog, new DC::UI::Label
630 root 1.1 max_w => $::WIDTH * 0.4,
631     ellipsise => 0,
632     markup => "\nOr use your keyboard and the text entry below:\n";
633    
634 root 1.18 unshift @dialog, my $table = new DC::UI::Table;
635 root 1.1
636     # left: re-roll
637 root 1.18 $table->add_at (0, 0, new DC::UI::Button
638 root 1.1 text => "Roll Again",
639     on_activate => sub {
640     $conn->send ("reply y");
641     destroy_query_dialog $conn;
642     0
643     },
644     );
645    
646     # center: swap stats
647 root 1.18 my ($sw1, $sw2) = map +(new DC::UI::Selector
648 root 1.1 expand => 1,
649     value => $_,
650     options => [
651     [1 => "Str", "Strength ($conn->{stat}{+CS_STAT_STR})"],
652     [2 => "Dex", "Dexterity ($conn->{stat}{+CS_STAT_DEX})"],
653     [3 => "Con", "Constitution ($conn->{stat}{+CS_STAT_CON})"],
654     [4 => "Int", "Intelligence ($conn->{stat}{+CS_STAT_INT})"],
655     [5 => "Wis", "Wisdom ($conn->{stat}{+CS_STAT_WIS})"],
656     [6 => "Pow", "Power ($conn->{stat}{+CS_STAT_POW})"],
657     [7 => "Cha", "Charisma ($conn->{stat}{+CS_STAT_CHA})"],
658     ],
659     ), 1 .. 2;
660    
661 root 1.18 $table->add_at (2, 0, new DC::UI::Button
662 root 1.1 text => "Swap Stats",
663     on_activate => sub {
664     $conn->{stat_change_with} = $sw2->{value};
665     $conn->send ("reply $sw1->{value}");
666     destroy_query_dialog $conn;
667     0
668     },
669     );
670 root 1.18 $table->add_at (2, 1, new DC::UI::HBox children => [$sw1, $sw2]);
671 root 1.1
672     # right: accept
673 root 1.18 $table->add_at (4, 0, new DC::UI::Button
674 root 1.1 text => "Accept",
675     on_activate => sub {
676     $conn->send ("reply n");
677     destroy_query_dialog $conn;
678     0
679     },
680     );
681    
682 root 1.18 unshift @dialog, my $hbox = new DC::UI::HBox;
683 root 1.1 for (
684     [Str => CS_STAT_STR],
685     [Dex => CS_STAT_DEX],
686     [Con => CS_STAT_CON],
687     [Int => CS_STAT_INT],
688     [Wis => CS_STAT_WIS],
689     [Pow => CS_STAT_POW],
690     [Cha => CS_STAT_CHA],
691     ) {
692     my ($name, $id) = @$_;
693 root 1.18 $hbox->add (new DC::UI::Label
694 root 1.1 markup => "$conn->{stat}{$id} <span foreground='yellow'>$name</span>",
695     expand => 1,
696     can_events => 1,
697     can_hover => 1,
698     tooltip => "#stat_$name",
699     );
700     }
701    
702 root 1.18 unshift @dialog, new DC::UI::Label
703 root 1.1 max_w => $::WIDTH * 0.4,
704     ellipsise => 0,
705 root 1.18 markup => (DC::Pod::section_label ui => "chargen_stats"),
706 root 1.1 ;
707     }
708    
709 root 1.18 push @dialog, my $entry = new DC::UI::Entry
710 root 1.1 on_changed => sub {
711     $conn->send ("reply $_[1]");
712     destroy_query_dialog $conn;
713     0
714     },
715     ;
716    
717     $entry->grab_focus;
718    
719     } else {
720     $dialog->{tooltip} = "Enter the reply and press return (click on the entry to make sure it has keyboard focus)";
721    
722 root 1.18 push @dialog, my $entry = new DC::UI::Entry
723 root 1.1 $flags & CS_QUERY_HIDEINPUT ? (hidden => "*") : (),
724     on_activate => sub {
725     $conn->send ("reply $_[1]");
726     destroy_query_dialog $conn;
727     0
728     },
729     ;
730    
731     $entry->grab_focus;
732     }
733    
734     $vbox->add (@dialog);
735     $dialog->show;
736     }
737    
738 root 1.46 sub dc_connect {
739     my ($host, $port) = @_;
740 root 1.1
741     my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
742    
743 root 1.44 $CONN =
744 root 1.18 new DC::Protocol
745 root 1.1 host => $host,
746 root 1.51 port => $port,
747 root 1.1 user => $PROFILE->{user},
748     pass => $PROFILE->{password},
749     mapw => $mapsize,
750     maph => $mapsize,
751    
752 root 1.26 client => "$DC::VERSION $] $^O",
753 root 1.1
754     map_widget => $MAPWIDGET,
755     statusbox => $STATUSBOX,
756     map => $MAP,
757     mapmap => $MAPMAP,
758     query => \&server_query,
759    
760     setup_req => {
761     smoothing => $CFG->{map_smoothing}*1,
762     },
763    
764 root 1.44 on_connect => sub {
765     if ($_[0]) {
766     DC::lowdelay fileno $CONN->{fh};
767 root 1.1
768 root 1.44 status "login successful";
769     } else {
770     undef $CONN;
771     status "unable to connect: $!";
772     stop_game();
773     }
774     },
775     ;
776 root 1.1 }
777    
778 root 1.46 sub start_game {
779     status "logging in...";
780    
781     $LOGIN_BUTTON->set_text ("Logout");
782     $SETUP_DIALOG->hide;
783    
784 root 1.51 my ($host, $port) = AnyEvent::Socket::parse_hostport $PROFILE->{host}, "deliantra=13327";
785 root 1.46
786     $MAP = new DC::Map;
787    
788     # hack to make SURE we find the IP address all right
789     # can be removed once AnyEvent::DNS is proven stable.
790     if ($host eq "gameserver.deliantra.net") {
791     AnyEvent::DNS::a "dnstest.deliantra.net", sub {
792     if ($_[0] ne "80.101.114.108") { # Perl
793     status "dns failure, using hardcoded address";
794     $host = "129.13.162.95";
795     }
796    
797     dc_connect $host, $port;
798     };
799     } else {
800     dc_connect $host, $port;
801     }
802     }
803    
804 root 1.1 sub stop_game {
805     $LOGIN_BUTTON->set_text ("Login / Register");
806     $SETUP_NOTEBOOK->set_current_page ($SETUP_LOGIN);
807     $SETUP_DIALOG->show;
808     $PL_WINDOW->hide;
809     $SPELL_LIST->clear_spells;
810 root 1.18 $DC::UI::ROOT->emit (stop_game => ! ! $CONN);
811 root 1.1
812     &audio_music_set_ambient ([]);
813    
814     return unless $CONN;
815    
816     status "connection closed";
817    
818     destroy_query_dialog $CONN;
819     $CONN->destroy;
820     $CONN = 0; # false, does not autovivify
821    
822     undef $MAP;
823     }
824    
825     sub graphics_setup {
826 root 1.18 my $vbox = new DC::UI::VBox;
827 root 1.1
828 root 1.18 $vbox->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]);
829 root 1.1
830     my $row = 0;
831    
832 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "OpenGL Info");
833     $table->add_at (1, $row++, new DC::UI::Label fontsize => 0.8, text => DC::OpenGL::gl_vendor . ", " . DC::OpenGL::gl_version,
834 root 1.1 can_events => 1,
835 root 1.18 tooltip => "<tt><span size='8192'>" . (DC::OpenGL::gl_extensions) . "</span></tt>");
836 root 1.1
837 root 1.53 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Caveats");
838     $table->add_at (1, $row++, $CAVEAT_LABEL = new DC::UI::Label fontsize => 0.8,
839     can_events => 1,
840     tooltip => "This field shows any known issues with your config or driver, such as "
841     . "a non-accelerated display format. You can try to work around these issues "
842     . "by selecting a different video mode, changing the settings below or "
843     . "by installing the right driver for your graphics card.");
844    
845 root 1.1 my $vidmode_tooltip =
846     "<b>Video Mode.</b> The video mode to use for fullscreen (and the window size for windowed operation). "
847     . "The format is <i>width</i> x <i>height</i> \@ <i>depth-per-channel</i> + <i>alpha-channel</i>.";
848    
849 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Video Mode");
850 root 1.18 $table->add_at (1, $row++, my $hbox = new DC::UI::HBox);
851 root 1.1
852 root 1.53 $hbox->add ($MODE_SLIDER = new DC::UI::Slider
853     force_w => $WIDTH * 0.1, expand => 1,
854 root 1.56 range => [ ($CFG->{sdl_mode}) x 3 ],
855 root 1.1 tooltip => $vidmode_tooltip);
856 root 1.18 $hbox->add (my $mode_label = new DC::UI::Label
857 root 1.22 height => 0.8, template => "9999x9999@9+9",
858 root 1.1 can_events => 1, tooltip => $vidmode_tooltip);
859    
860 root 1.53 $MODE_SLIDER->connect (changed => sub {
861 root 1.1 my ($self, $value) = @_;
862    
863     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
864     $mode_label->set_text (sprintf '%dx%d@%d+%d', @{$SDL_MODES[$value]});
865     });
866 root 1.53 $MODE_SLIDER->emit (changed => $MODE_SLIDER->{range}[0]);
867 root 1.1
868 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fullscreen");
869 root 1.18 $table->add_at (1, $row++, $FULLSCREEN_ENABLE = new DC::UI::CheckBox
870 root 1.1 state => $CFG->{fullscreen},
871     tooltip => "Bring the client into fullscreen mode.",
872     on_changed => sub { my ($self, $value) = @_; $CFG->{fullscreen} = $value; 0 }
873     );
874    
875 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Force OpenGL 1.1");
876 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
877 root 1.1 state => $CFG->{force_opengl11},
878 elmex 1.23 tooltip => "Limit Deliantra to use OpenGL 1.1 features only. This will normally result in "
879 root 1.1 . "higher memory usage and slower performance. It will, however, help tremendously on "
880     . "cards that claim to support a feature but fall back to software rendering. "
881     . "Nvidia Geforce FX cards are known to claim features the hardware doesn't support, "
882 root 1.52 . "but cards and drivers from other vendors (ATI) are often just as bad. "
883     . "<b>If you experience extremely low framerates and your card should do better, try this option.</b>",
884 root 1.1 on_changed => sub { my ($self, $value) = @_; $CFG->{force_opengl11} = $value; 0 }
885     );
886    
887 root 1.52 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Forbid Alpha");
888     $table->add_at (1, $row++, new DC::UI::CheckBox
889     state => $CFG->{disable_alpha},
890     tooltip => "Forbid off the use of the alpha channel. This makes Deliantra look a lot worse "
891     . "by disabling a number of textures and transparency effects. Normally, these "
892     . "effects do not cost a lot of resources, but some graphics cards might fall "
893     . "back to etxremely slow rendering if this is enabled. If disabling this option "
894     . "noticably improves the framerate of the client please report this! "
895     . "<b>If you experience extremely low framerates and your card should do better, try this option.</b>",
896     on_changed => sub {
897     my ($self, $value) = @_;
898     $CFG->{disable_alpha} = $value;
899     $SDL_REINIT = 1; # SDL_SetVideoMode ignores GL attr changes
900     0
901     }
902     );
903    
904 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Compress Textures");
905 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
906 root 1.1 state => $CFG->{texture_compression},
907     tooltip => "Use texture compression. Normally this will not reduce visual quality noticable but "
908     . "will save a lot of memory and increase performance. The compression algorithm "
909     . "can differ form card to card, so your mileage may vary. This setting is ignored in "
910     . "forced OpenGL 1.1 mode.",
911     on_changed => sub { my ($self, $value) = @_; $CFG->{texture_compression} = $value; 0 }
912     );
913    
914 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fast & Ugly");
915 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
916 root 1.1 state => $CFG->{fast},
917     tooltip => "Lower the visual quality considerably to speed up rendering.",
918     on_changed => sub { my ($self, $value) = @_; $CFG->{fast} = $value; 0 }
919     );
920    
921 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "GUI Fontsize");
922 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
923 root 1.1 range => [$CFG->{gui_fontsize}, 0.5, 2, 0, 0.1],
924     tooltip => "The base font size used by most GUI elements that do not have their own setting.",
925     on_changed => sub { $CFG->{gui_fontsize} = $_[1]; 0 },
926     );
927    
928 root 1.18 $table->add_at (1, $row++, new DC::UI::Button
929 root 1.22 expand => 1, text => "Apply",
930 root 1.1 tooltip => "Apply the video settings above.",
931     on_activate => sub {
932     video_shutdown ();
933     video_init ();
934     0
935     }
936     );
937    
938 root 1.56 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Smooth Movement");
939     $table->add_at (1, $row++, new DC::UI::CheckBox
940     state => $CFG->{smooth_movement},
941     tooltip => "<b>Smooth Movement</b> tries to make movement, well, smoother, but also increases the framerate. "
942     . "If you have a very slow system, non-accelerated drivers or plain dislike smooth scrolling, "
943     . "then disable this option. Changes take effect immdiately.",
944     on_changed => sub { my ($self, $value) = @_; $CFG->{smooth_movement} = $value; 0 }
945     );
946    
947 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Map Scale");
948 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
949 root 1.1 range => [(log $CFG->{map_scale}) / (log 2), -3, 1, 0, 1],
950     tooltip => "Enlarge or shrink the displayed map. Changes are instant.",
951     on_changed => sub { my ($self, $value) = @_; $CFG->{map_scale} = 2 ** $value; 0 }
952     );
953    
954 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Map Smoothing");
955 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
956 root 1.1 state => $CFG->{map_smoothing},
957     tooltip => "<b>Map Smoothing</b> tries to make tile borders less square. "
958     . "This increases load on the graphics subsystem and works only with TRT servers. "
959     . "Changes take effect at next login only.",
960     on_changed => sub { my ($self, $value) = @_; $CFG->{map_smoothing} = $value; 0 }
961     );
962    
963 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Fog of War");
964 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
965 root 1.1 state => $CFG->{fow_enable},
966     tooltip => "<b>Fog-of-War</b> marks areas that cannot be seen by the player. Changes are instant.",
967     on_changed => sub { my ($self, $value) = @_; $CFG->{fow_enable} = $value; 0 }
968     );
969    
970 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "FoW Intensity");
971 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
972 root 1.1 range => [$CFG->{fow_intensity}, 0, 1, 0, 1 / 256],
973     tooltip => "<b>Fog of War Lightness.</b> The higher the intensity, the lighter the Fog-of-War color. Changes are instant.",
974     on_changed => sub { my ($self, $value) = @_; $CFG->{fow_intensity} = $value; 0 }
975     );
976    
977 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Message Fontsize");
978 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
979 root 1.1 range => [$CFG->{log_fontsize}, 0.5, 2, 0, 0.1],
980     tooltip => "The font size used by the <b>message/server log</b> window only. Changes are instant, "
981     . "but you still need to press apply to correctly re-layout the widget.",
982 elmex 1.23 on_changed => sub { $MESSAGE_DIST->set_fontsize ($CFG->{log_fontsize} = $_[1]); 0 },
983 root 1.1 );
984    
985 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Gauge fontsize");
986 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
987 root 1.1 range => [$CFG->{gauge_fontsize}, 0.5, 2, 0, 0.1],
988     tooltip => "Adjusts the fontsize of the gauges at the bottom right. Changes are instant.",
989     on_changed => sub {
990     $CFG->{gauge_fontsize} = $_[1];
991     &set_gauge_window_fontsize;
992     0
993     }
994     );
995    
996 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Gauge size");
997 root 1.18 $table->add_at (1, $row++, new DC::UI::Slider
998 root 1.1 range => [$CFG->{gauge_size}, 0.2, 0.8],
999     tooltip => "Adjust the size of the stats gauges at the bottom right. Changes are instant.",
1000     on_changed => sub {
1001     $CFG->{gauge_size} = $_[1];
1002     $GAUGES->{win}->set_size ($WIDTH, int $HEIGHT * $CFG->{gauge_size});
1003     0
1004     }
1005     );
1006    
1007     $vbox
1008     }
1009    
1010     our $AUDIO_HW_CHUNKSIZE;
1011     our $AUDIO_INFO;
1012    
1013     sub audio_tab_update {
1014 root 1.18 my ($freq, $format, $chans) = DC::Mix_QuerySpec;
1015 root 1.1
1016     $AUDIO_HW_CHUNKSIZE->set_options ([
1017     [0, "default", "Use System Default"],
1018     map {
1019     my $ms = sprintf "%dms", 1000 * $_ / ($CFG->{audio_hw_frequency} || 22050);
1020     [$_, $ms, "$ms ($_ samples)"],
1021     } 256, 512, 1024, 2048, 4096, 8192, 16384, 32768
1022     ]);
1023    
1024     my $text = !$freq
1025     ? "audio is off"
1026     : "audio is enabled\n"
1027     . "frequency (Hz): $freq\n"
1028     . "channels: $chans";
1029    
1030     $AUDIO_INFO->set_text ($text);
1031     }
1032    
1033     sub audio_setup {
1034 root 1.18 my $vbox = new DC::UI::VBox;
1035 root 1.1
1036 root 1.18 $vbox->add (my $table = new DC::UI::Table expand => 1, col_expand => [0, 0, 1]);
1037 root 1.1
1038     my $row = 0;
1039    
1040 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Audio Enable");
1041 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
1042 root 1.1 state => $CFG->{audio_enable},
1043     tooltip => "<b>Master Audio Enable.</b> If enabled, sound effects and music will be played. If disabled, no audio will be used and the soundcard will not be opened.",
1044     on_changed => sub { $CFG->{audio_enable} = $_[1]; 1 }
1045     );
1046    
1047 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Sound Effects");
1048 root 1.18 $table->add_at (1, $row, new DC::UI::CheckBox
1049 root 1.1 expand => 1, state => $CFG->{effects_enable},
1050     tooltip => "If enabled, sound effects are enabled. If disabled, no sound effects will be played.",
1051     on_changed => sub {
1052     $CFG->{effects_enable} = $_[1];
1053     $CONN->update_fx_want if $CONN;
1054     1
1055     }
1056     );
1057 root 1.18 $table->add_at (2, $row++, new DC::UI::Slider
1058 root 1.1 expand => 1, range => [$CFG->{effects_volume}, 0, 1, 0, 1/128],
1059     tooltip => "The relative volume of sound effects. Best audio quality is achieved if this "
1060     . "is set highest (rightmost) and you use your operating system volume setting. Changes are instant.",
1061     on_changed => sub { $CFG->{effects_volume} = $_[1]; 1 }
1062     );
1063    
1064 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Background Music");
1065 root 1.18 $table->add_at (1, $row, new DC::UI::CheckBox
1066 root 1.1 expand => 1, state => $CFG->{bgm_enable},
1067     tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played.",
1068     on_changed => sub {
1069     $CFG->{bgm_enable} = $_[1];
1070     $CONN->update_fx_want if $CONN;
1071     audio_music_push;
1072     1
1073     }
1074     );
1075 root 1.18 $table->add_at (2, $row++, new DC::UI::Slider
1076 root 1.1 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128],
1077     tooltip => "The volume of the background music. Changes are instant.",
1078     on_changed => sub { $CFG->{bgm_volume} = $_[1]; audio_music_update_volume; 0 }
1079     );
1080    
1081 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Frequency");
1082 root 1.18 $table->add_at (1, $row++, new DC::UI::Selector
1083 root 1.1 c_colspan => 2, expand => 1,
1084     value => $CFG->{audio_hw_frequency},
1085     options => [
1086     [ 0, "default" , "Use System Default"],
1087     [11025, "11 kHz" , "11kHz (low quality)"],
1088     [22050, "22 kHz" , "22kHz (reduced quality)"],
1089     [44100, "44.1 kHz", "44.1kHz (cd quality)"],
1090     [48000, "48 kHz" , "48kHz (studio quality)"],
1091     ],
1092     tooltip => "The sampling frequency to use. Higher sounds better, but also more cpu-intensive and might cause stuttering.",
1093     on_changed => sub {
1094     $CFG->{audio_hw_frequency} = $_[1];
1095     audio_tab_update;
1096     1
1097     }
1098     );
1099    
1100 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Channels");
1101 root 1.18 $table->add_at (1, $row++, new DC::UI::Selector
1102 root 1.1 c_colspan => 2, expand => 1,
1103     value => $CFG->{audio_hw_channels},
1104     options => [
1105     [0, "default" , "Use System Default"],
1106     [1, "Mono" , "Mono (single channel, low quality)"],
1107 root 1.6 [2, "Stereo" , "Stereo (dual channel, standard quality)"],
1108 root 1.1 [4, "4 Ch Surround", "4 Channel Surround Sound (3d sound, high quality)"],
1109     [6, "6 Ch Surround", "6 Channel Surround Sound (3d sound + center + lfe)"],
1110     ],
1111     tooltip => "The number of independent sound channels to use. Higher sounds better, but also more cpu-intensive and might cause stuttering.",
1112     on_changed => sub {
1113     $CFG->{audio_hw_channels} = $_[1];
1114     audio_tab_update;
1115     1
1116     }
1117     );
1118    
1119 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Latency");
1120 root 1.18 $table->add_at (1, $row++, $AUDIO_HW_CHUNKSIZE = new DC::UI::Selector
1121 root 1.1 c_colspan => 2, expand => 1,
1122     value => $CFG->{audio_hw_chunksize},
1123     tooltip => "The guarenteed latency. Lower is better, but also more cpu-intensive and might cause stuttering. If music playback "
1124     . "is stuttering, increase this value. Values of 50-100ms are optimal.",
1125     on_changed => sub {
1126     $CFG->{audio_hw_chunksize} = $_[1];
1127     audio_tab_update;
1128     1
1129     }
1130     );
1131    
1132     # should really be a slider
1133 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Mixer Voices");
1134 root 1.18 $table->add_at (1, $row++, new DC::UI::ValSlider
1135 root 1.1 c_colspan => 2, expand => 1,
1136     tooltip => "The number of simultaneous sound effects possible. Higher is better, but also more cpu-intensive and might cause stuttering.",
1137     range => [$::CFG->{audio_mix_channels}, 4, 32, 0, 1],
1138     template => ">= 99",
1139     on_changed => sub {
1140     my ($slider, $value) = @_;
1141    
1142     $CFG->{audio_mix_channels} = $value
1143     if $value;
1144     1;
1145     }
1146     );
1147    
1148 root 1.18 $table->add_at (1, $row++, new DC::UI::Button
1149 root 1.22 c_colspan => 2, expand => 1, text => "Apply",
1150 root 1.1 tooltip => "Apply the audio settings",
1151     on_activate => sub {
1152     audio_shutdown ();
1153     audio_init ();
1154     0
1155     }
1156     );
1157    
1158 root 1.18 $vbox->add (new DC::UI::FancyFrame
1159 root 1.1 expand => 1,
1160     label => "Audio Info",
1161 root 1.18 child => ($AUDIO_INFO = new DC::UI::Label ellipsise => 0),
1162 root 1.1 );
1163    
1164     audio_tab_update;
1165    
1166     $vbox
1167     }
1168    
1169     sub set_gauge_window_fontsize {
1170     for (map { $GAUGES->{$_} } grep { $_ ne 'win' } keys %{$GAUGES}) {
1171     $_->set_fontsize ($::CFG->{gauge_fontsize});
1172     }
1173     }
1174    
1175     sub make_gauge_window {
1176     my $gh = int $HEIGHT * $CFG->{gauge_size};
1177    
1178 root 1.18 my $win = new DC::UI::Frame (
1179 root 1.1 force_x => 0,
1180     force_y => "max",
1181     force_w => $WIDTH,
1182     force_h => $gh,
1183     );
1184    
1185 root 1.18 $win->add (my $hbox = new DC::UI::HBox
1186 root 1.1 children => [
1187 root 1.18 (new DC::UI::HBox expand => 1),
1188     (new DC::UI::VBox children => [
1189     (new DC::UI::Empty expand => 1),
1190     (new DC::UI::Frame bg => [0, 0, 0, 0.4], child => ($FLOORBOX = new DC::UI::Table)),
1191 root 1.1 ]),
1192 root 1.18 (my $vbox = new DC::UI::VBox),
1193 root 1.1 ],
1194     );
1195    
1196 root 1.18 $vbox->add (new DC::UI::HBox
1197 root 1.1 expand => 1,
1198     children => [
1199 root 1.18 (new DC::UI::Empty expand => 1),
1200     (my $hb = new DC::UI::HBox),
1201 root 1.1 ],
1202     );
1203    
1204 root 1.18 $hb->add (my $hg = new DC::UI::Gauge type => 'hp', tooltip => "#stat_health");
1205     $hb->add (my $mg = new DC::UI::Gauge type => 'mana', tooltip => "#stat_mana");
1206     $hb->add (my $gg = new DC::UI::Gauge type => 'grace', tooltip => "#stat_grace");
1207     $hb->add (my $fg = new DC::UI::Gauge type => 'food', tooltip => "#stat_food");
1208    
1209 root 1.22 $vbox->add (my $exp = new DC::UI::Label align => 1, can_hover => 1, can_events => 1, tooltip => "#stat_exp");
1210 root 1.18 $vbox->add (my $prg = new DC::UI::ExperienceProgress);
1211     $vbox->add (my $sklprg = new DC::UI::ExperienceProgress);
1212 root 1.22 $vbox->add (my $rng = new DC::UI::Label align => 1, can_hover => 1, can_events => 1, tooltip => "#stat_ranged");
1213 root 1.1
1214     $GAUGES = {
1215     exp => $exp, prg => $prg, sklprg => $sklprg,
1216     win => $win, range => $rng,
1217     hp => $hg, mana => $mg, grace => $gg, food => $fg,
1218     };
1219    
1220     &set_gauge_window_fontsize;
1221    
1222     $win
1223     }
1224    
1225     sub debug_setup {
1226 root 1.18 my $table = new DC::UI::Table;
1227 root 1.1
1228 root 1.18 $table->add_at (0, 0, new DC::UI::Label text => "Widget Borders");
1229     $table->add_at (1, 0, new DC::UI::CheckBox on_changed => sub { $ENV{CFPLUS_DEBUG} ^= 1; 0 });
1230     $table->add_at (0, 1, new DC::UI::Label text => "Tooltip Widget Info");
1231     $table->add_at (1, 1, new DC::UI::CheckBox on_changed => sub { $ENV{CFPLUS_DEBUG} ^= 2; 0 });
1232     $table->add_at (0, 2, new DC::UI::Label text => "Show FPS");
1233     $table->add_at (1, 2, new DC::UI::CheckBox on_changed => sub { $ENV{CFPLUS_DEBUG} ^= 4; 0 });
1234     $table->add_at (0, 3, new DC::UI::Label text => "Suppress Tooltips");
1235     $table->add_at (1, 3, new DC::UI::CheckBox on_changed => sub { $ENV{CFPLUS_DEBUG} ^= 8; 0 });
1236     $table->add_at (0, 4, new DC::UI::Button text => "die on click(tm)", on_activate => sub { &DC::debug() } );
1237    
1238 root 1.21 $table->add_at (0, 5, new DC::UI::TextEdit text => "line1\0152\0153\nµikachu\nづx゙つ゛");#d#
1239 root 1.18
1240     $table->add_at (7,7, my $t = new DC::UI::Table expand => 0);
1241 root 1.20 $t->add_at (0,0, new DC::UI::Label text => "a a", c_rowspan => 1, c_colspan => 2);
1242     $t->add_at (2,0, new DC::UI::Label text => "b\nb", c_rowspan => 2, c_colspan => 1, ellipsise => 0 );
1243     $t->add_at (1,2, new DC::UI::Label text => "c c", c_rowspan => 1, c_colspan => 2);
1244     $t->add_at (0,1, new DC::UI::Label text => "d\nd", c_rowspan => 2, c_colspan => 1, ellipsise => 0 );
1245 root 1.18 $t->add_at (1,1, new DC::UI::Label text => "e");
1246 root 1.1
1247 root 1.18 $table->add_at (7, 6, my $c = new DC::UI::Canvas);
1248 root 1.1
1249     $c->add_items ({
1250     type => "line_loop",
1251     color => [0, 1, 0],
1252     width => 9,
1253     coord_mode => "abs",
1254     coord => [[10, 5], [5, 50], [20, 5], [5, 60]],
1255     });
1256    
1257     $c->add_items ({
1258     type => "lines",
1259     color => [1, 1, 0],
1260     width => 2,
1261     coord_mode => "rel",
1262     coord => [[0,0], [1,1], [1,0], [0,1]],
1263     });
1264    
1265     $c->add_items ({
1266     type => "polygon",
1267     color => [0, 0.43, 0],
1268     width => 2,
1269     coord_mode => "rel",
1270     coord => [[0,0.2], [1,.4], [1,.6], [0,.8]],
1271     });
1272    
1273     $table
1274     }
1275    
1276     sub stats_window {
1277 root 1.18 my $r = new DC::UI::ScrolledWindow (
1278 root 1.1 expand => 1,
1279     scroll_y => 1
1280     );
1281 root 1.18 $r->add (my $vb = new DC::UI::VBox);
1282 root 1.1
1283 root 1.18 $vb->add (new DC::UI::FancyFrame
1284 root 1.1 label => "Player",
1285 root 1.18 child => (my $pi = new DC::UI::VBox),
1286 root 1.1 );
1287    
1288 root 1.22 $pi->add ($STATWIDS->{title} = new DC::UI::Label text => "Title:", expand => 1, align => 0,
1289 root 1.1 can_hover => 1, can_events => 1,
1290     tooltip => "Your name and title. You can change your title by using the <b>title</b> command, if supported by the server.");
1291 root 1.22 $pi->add ($STATWIDS->{map} = new DC::UI::Label align => 0, text => "Map:", expand => 1,
1292 root 1.1 can_hover => 1, can_events => 1,
1293     tooltip => "The map you are currently on (if supported by the server).");
1294    
1295 root 1.18 $pi->add (my $hb0 = new DC::UI::HBox);
1296 root 1.22 $hb0->add ($STATWIDS->{weight} = new DC::UI::Label text => "Weight:", expand => 1, align => 0,
1297 root 1.1 can_hover => 1, can_events => 1,
1298     tooltip => "The weight of the player including all inventory items.");
1299 root 1.22 $hb0->add ($STATWIDS->{m_weight} = new DC::UI::Label align => 0, text => "Max weight:", expand => 1,
1300 root 1.1 can_hover => 1, can_events => 1,
1301     tooltip => "The weight limit: you cannot carry more than this.");
1302    
1303 root 1.18 $vb->add (new DC::UI::FancyFrame
1304 root 1.1 label => "Primary/Secondary Statistics",
1305 root 1.18 child => (my $hb = new DC::UI::HBox expand => 1),
1306 root 1.1 );
1307 root 1.18 $hb->add (my $tbl = new DC::UI::Table expand => 1);
1308 root 1.1
1309     my $color2 = [1, 1, 0];
1310    
1311     for (
1312     [0, 0, st_str => "Str", 30],
1313     [0, 1, st_dex => "Dex", 30],
1314     [0, 2, st_con => "Con", 30],
1315     [0, 3, st_int => "Int", 30],
1316     [0, 4, st_wis => "Wis", 30],
1317     [0, 5, st_pow => "Pow", 30],
1318     [0, 6, st_cha => "Cha", 30],
1319    
1320     [2, 0, st_wc => "Wc", -120],
1321     [2, 1, st_ac => "Ac", -120],
1322     [2, 2, st_dam => "Dam", 120],
1323     [2, 3, st_arm => "Arm", 120],
1324     [2, 4, st_spd => "Spd", 10.54],
1325     [2, 5, st_wspd => "WSp", 10.54],
1326     ) {
1327     my ($col, $row, $id, $label, $template) = @$_;
1328    
1329 root 1.18 $tbl->add_at ($col , $row, $STATWIDS->{$id} = new DC::UI::Label
1330 root 1.22 font => $FONT_FIXED, can_hover => 1, can_events => 1,
1331     align => 1, template => $template, tooltip => "#stat_$label");
1332 root 1.18 $tbl->add_at ($col + 1, $row, $STATWIDS->{"$id\_lbl"} = new DC::UI::Label
1333 root 1.22 font => $FONT_FIXED, can_hover => 1, can_events => 1, fg => $color2,
1334     align => 0, text => $label, tooltip => "#stat_$label");
1335 root 1.1 }
1336    
1337 root 1.18 $vb->add (new DC::UI::FancyFrame
1338 root 1.1 label => "Resistancies",
1339 root 1.22 child => (my $tbl2 = new DC::UI::Table expand => 1, col_expand => [1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0]),
1340 root 1.1 );
1341    
1342     my $row = 0;
1343     my $col = 0;
1344    
1345     my %resist_names = (
1346     slow => ["Slow",
1347     "<b>Slow</b> (slows you down when you are hit by the spell. Monsters will have an opportunity to come near you faster and hit you more often.)"],
1348     holyw => ["Holy Word",
1349     "<b>Holy Word</b> (resistance you against getting the fear when someone whose god doesn't like you spells the holy word on you.)"],
1350     conf => ["Confusion",
1351     "<b>Confusion</b> (If you are hit by confusion you will move into random directions, and likely into monsters.)"],
1352     fire => ["Fire",
1353     "<b>Fire</b> (just your resistance to fire spells like burning hands, dragonbreath, meteor swarm fire, ...)"],
1354     depl => ["Depletion",
1355     "<b>Depletion</b> (some monsters and other effects can cause stats depletion)"],
1356     magic => ["Magic",
1357     "<b>Magic</b> (resistance to magic spells like magic missile or similar)"],
1358     drain => ["Draining",
1359     "<b>Draining</b> (some monsters (e.g. vampires) and other effects can steal experience)"],
1360     acid => ["Acid",
1361     "<b>Acid</b> (resistance to acid, acid hurts pretty much and also corrodes your weapons)"],
1362     pois => ["Poison",
1363     "<b>Poison</b> (resistance to getting poisoned)"],
1364     para => ["Paralysation",
1365     "<b>Paralysation</b> (this resistance affects the chance you get paralysed)"],
1366     deat => ["Death",
1367     "<b>Death</b> (resistance against death spells)"],
1368     phys => ["Physical",
1369     "<b>Physical</b> (this is the resistance against physical attacks, like when a monster hit you in melee combat. The value displayed here is also displayed in the 'Arm' field on the left.)"],
1370     blind => ["Blind",
1371     "<b>Blind</b> (blind resistance affects the chance of a successful blinding attack)"],
1372     fear => ["Fear",
1373     "<b>Fear</b> (this attack will drive you away from monsters who cast this and hit you successfully, being resistant to this helps a lot when fighting those monsters)"],
1374     tund => ["Turn undead",
1375     "<b>Turn undead</b> (affects your resistancy to various forms of 'turn undead' spells. Only relevant when you are, in fact, undead..."],
1376     elec => ["Electricity",
1377     "<b>Electricity</b> (resistance against electricity, spells like large lightning, small lightning, ...)"],
1378     cold => ["Cold",
1379     "<b>Cold</b> (this is your resistance against cold spells like icestorm, snowstorm, ...)"],
1380     ghit => ["Ghost hit",
1381     "<b>Ghost hit</b> (special attack used by ghosts and ghost-like beings)"],
1382     );
1383    
1384     for (qw/slow holyw conf fire depl magic
1385     drain acid pois para deat phys
1386     blind fear tund elec cold ghit/)
1387     {
1388 root 1.22 $tbl2->add_at ($col + 2, $row,
1389 root 1.1 $STATWIDS->{"res_$_"} =
1390 root 1.18 new DC::UI::Label
1391 root 1.1 font => $FONT_FIXED,
1392     template => "-100%",
1393 root 1.22 align => 1,
1394 root 1.1 can_events => 1,
1395     can_hover => 1,
1396     tooltip => $resist_names{$_}->[1],
1397     );
1398 root 1.18 $tbl2->add_at ($col + 1, $row, new DC::UI::Image
1399 root 1.1 font => $FONT_FIXED,
1400     can_hover => 1,
1401     can_events => 1,
1402     path => "ui/resist/resist_$_.png",
1403     tooltip => $resist_names{$_}->[1],
1404     );
1405 root 1.22 $tbl2->add_at ($col + 0, $row, new DC::UI::Label
1406 root 1.1 text => $resist_names{$_}->[0],
1407     font => $FONT_FIXED,
1408 root 1.22 align => 1,
1409 root 1.1 can_hover => 1,
1410     can_events => 1,
1411     tooltip => $resist_names{$_}->[1],
1412     );
1413    
1414     $row++;
1415     if ($row % 6 == 0) {
1416 root 1.22 $col += 4;
1417 root 1.1 $row = 0;
1418     }
1419     }
1420    
1421     #update_stats_window ({});
1422    
1423     $r
1424     }
1425    
1426     sub skill_window {
1427 root 1.18 my $sw = new DC::UI::ScrolledWindow (expand => 1);
1428 root 1.1
1429 root 1.18 $sw->add ($STATWIDS->{skill_tbl} = new DC::UI::Table expand => 1, col_expand => [0, 0, 1, .1, 0, 0, 1, .1]);
1430 root 1.1
1431     $sw
1432     }
1433    
1434     sub formsep($) {
1435     scalar reverse join ",", unpack "(A3)*", reverse $_[0] * 1
1436     }
1437    
1438     my $METASERVER_ATIME;
1439    
1440     sub update_metaserver {
1441     my ($metaserver_dialog) = @_;
1442    
1443     $METASERVER = $metaserver_dialog
1444     if defined $metaserver_dialog;
1445    
1446     return if $METASERVER_ATIME > time;
1447     $METASERVER_ATIME = time + 60;
1448    
1449     my $table = $METASERVER->{table};
1450     $table->clear;
1451 root 1.18 $table->add_at (0, 0, my $label = new DC::UI::Label max_w => $WIDTH * 0.8, text => "fetching server list...");
1452 root 1.1
1453     my $ok = 0;
1454    
1455 root 1.18 DC::background {
1456     my $ua = DC::lwp_useragent;
1457 root 1.1
1458 root 1.18 DC::background_msg DC::decode_json +(DC::lwp_check $ua->get ($META_SERVER))->decoded_content;
1459 root 1.1 } sub {
1460     my ($msg) = @_;
1461     if ($msg) {
1462     $table->clear;
1463    
1464     my @tip = (
1465     "The current number of users logged in on the server.",
1466     "The hostname of the server.",
1467     "The time this server has been running without being restarted.",
1468     "Short information about this server provided by its admins.",
1469     );
1470     my @col = qw(#Users Host Uptime Version Description);
1471 root 1.18 $table->add_at ($_, 0, new DC::UI::Label
1472 root 1.22 can_hover => 1, can_events => 1, fg => [1, 1, 0],
1473 root 1.1 text => $col[$_], tooltip => $tip[$_])
1474     for 0 .. $#col;
1475    
1476 root 1.22 my @align = qw(1 0.5 1 1 0);
1477 root 1.1
1478     my $y = 0;
1479     for my $m (@{ $msg->{servers} }) {
1480     my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime, $highlight) =
1481     @$m{qw(ip age hostname users version description ibytes obytes uptime highlight)};
1482    
1483     for ($desc) {
1484     s/<br>/\n/gi;
1485     s/<li>/\n· /gi;
1486     s/<.*?>//sgi;
1487     s/&amp;/&/g;
1488     s/&lt;/</g;
1489     s/&gt;/>/g;
1490     }
1491    
1492     $uptime = sprintf "%dd %02d:%02d:%02d",
1493     (int $uptime / 86400),
1494     (int $uptime / 3600) % 24,
1495     (int $uptime / 60) % 60,
1496     $uptime % 60;
1497    
1498     $m = [$users, $host, $uptime, $version, $desc];
1499    
1500     $y++;
1501    
1502 root 1.18 $table->add_at (scalar @$m, $y, new DC::UI::VBox children => [
1503     (new DC::UI::Button
1504 root 1.1 text => "Use",
1505     tooltip => "Put this server into the <b>Host:Port</b> field",
1506     on_activate => sub {
1507     $HOST_ENTRY->set_text ($CFG->{profile}{default}{host} = $host);
1508     $METASERVER->hide;
1509     0
1510     },
1511     ),
1512 root 1.18 (new DC::UI::Empty expand => 1),
1513 root 1.1 ]);
1514    
1515 root 1.18 $table->add_at ($_, $y, new DC::UI::Label
1516 root 1.1 max_w => $::WIDTH * 0.4,
1517     ellipsise => 0,
1518     align => $align[$_],
1519     text => $m->[$_],
1520     tooltip => $tip[$_],
1521     fg => ($highlight ? [1, 1, 1] : [.7, .7, .7]),
1522     can_hover => 1,
1523     can_events => 1,
1524     fontsize => 0.8)
1525     for 0 .. $#$m;
1526     }
1527     } else {
1528     $ok or $label->set_text ("error while contacting metaserver");
1529     }
1530     };
1531    
1532     }
1533    
1534     sub metaserver_dialog {
1535 root 1.18 my $vbox = new DC::UI::VBox;
1536     my $table = new DC::UI::Table;
1537     $vbox->add (new DC::UI::ScrolledWindow expand => 1, child => $table);
1538 root 1.1
1539 root 1.18 my $dialog = new DC::UI::Toplevel
1540 root 1.1 title => "Server List",
1541     name => 'metaserver_dialog',
1542     x => 'center',
1543     y => 'center',
1544     z => 3,
1545     force_w => $::WIDTH * 0.9,
1546     force_h => $::HEIGHT * 0.7,
1547     child => $vbox,
1548     has_close_button => 1,
1549     table => $table,
1550     on_visibility_change => sub {
1551     update_metaserver ($_[0]) if $_[1];
1552     0
1553     },
1554     ;
1555    
1556     $dialog
1557     }
1558    
1559     sub login_setup {
1560 root 1.18 my $vbox = new DC::UI::VBox;
1561 root 1.1
1562 root 1.18 $vbox->add (new DC::UI::FancyFrame
1563 root 1.1 label => "Login Settings",
1564 root 1.18 child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]),
1565 root 1.1 );
1566    
1567 root 1.22 $table->add_at (0, 4, new DC::UI::Label align => 1, text => "Username");
1568 root 1.18 $table->add_at (1, 4, new DC::UI::Entry
1569 root 1.1 text => $CFG->{profile}{default}{user},
1570     tooltip => "The name of your character on the server.",
1571     on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value; 1 }
1572     );
1573    
1574 root 1.22 $table->add_at (0, 5, new DC::UI::Label align => 1, text => "Password");
1575 root 1.18 $table->add_at (1, 5, new DC::UI::Entry
1576 root 1.1 text => $CFG->{profile}{default}{password},
1577     hidden => 1,
1578     tooltip => "The password for your character.",
1579     on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value; 1 }
1580     );
1581    
1582 root 1.18 $table->add_at (1, 11, $LOGIN_BUTTON = new DC::UI::Button
1583 root 1.1 expand => 1,
1584     text => "Login / Register",
1585     tooltip => "This button will either login to the account configured above or register a new account.",
1586     on_activate => sub {
1587     $CONN ? stop_game
1588     : start_game;
1589     1
1590     },
1591     );
1592    
1593 root 1.18 $vbox->add (new DC::UI::FancyFrame
1594 root 1.1 label => "Registering",
1595     min_h => 200,
1596 root 1.22 child => (new DC::UI::Label valign => 0, ellipsise => 0,
1597 root 1.1 markup =>
1598     "To register a new account, choose a username that hasn't been taken yet and "
1599     . "try to log-in. Follow the instructions in the Log tab in the message window.",
1600     ),
1601     );
1602    
1603     $vbox
1604     }
1605    
1606     sub server_setup {
1607 root 1.18 my $vbox = new DC::UI::VBox;
1608 root 1.1
1609 root 1.18 $vbox->add (new DC::UI::FancyFrame
1610 root 1.1 label => "Connection Settings",
1611 root 1.18 child => (my $table = new DC::UI::Table expand => 1, col_expand => [0, 1]),
1612 root 1.1 );
1613    
1614     my $row = 0;
1615    
1616 root 1.22 $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Host:Port");
1617 root 1.1 {
1618 root 1.18 $table->add_at (1, $row, my $vbox = new DC::UI::VBox);
1619 root 1.1
1620     $vbox->add (
1621 root 1.18 $HOST_ENTRY = new DC::UI::Entry
1622 root 1.1 expand => 1,
1623     text => $CFG->{profile}{default}{host},
1624 root 1.28 tooltip => "The hostname or ip address of the Deliantra server to connect to (e.g. <b>gameserver.deliantra.net</b>)",
1625 root 1.1 on_changed => sub {
1626     my ($self, $value) = @_;
1627     $CFG->{profile}{default}{host} = $value;
1628     1
1629     }
1630     );
1631    
1632 root 1.5 if (0) { #d# disabled
1633 root 1.18 $vbox->add (new DC::UI::Button
1634 root 1.1 expand => 1,
1635     text => "Server List",
1636     other => $METASERVER,
1637 root 1.28 tooltip => "Show a list of available Deliantra servers",
1638 root 1.1 on_activate => sub { $METASERVER->toggle_visibility; 0 },
1639     on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 },
1640     );
1641 root 1.5 }#d#
1642 root 1.1 }
1643    
1644 root 1.22 $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Map Size");
1645 root 1.18 $table->add_at (1, $row, new DC::UI::Slider
1646 root 1.1 force_w => 100,
1647     range => [$CFG->{mapsize}, 10, 100, 0, 1],
1648     tooltip => "This is the size of the portion of the map update the server sends you. "
1649     . "If you set this to a high value you will be able to see further, "
1650     . "but you also increase bandwidth requirements and latency. "
1651     . "This option is only used once at log-in.",
1652     on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 },
1653     );
1654    
1655 root 1.22 $table->add_at (0, ++$row, new DC::UI::Label align => 1, text => "Output-Rate");
1656 root 1.18 $table->add_at (1, $row, new DC::UI::Entry
1657 root 1.1 text => $CFG->{output_rate},
1658     tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1659     . "when sending data. When 0 or unset, the server "
1660     . "default will be used, which is usually around 100kb/s. Most servers will "
1661     . "dynamically find an optimal rate, so adjust this only when necessary.",
1662     on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1663     );
1664    
1665 root 1.18 $vbox->add (new DC::UI::FancyFrame
1666 root 1.1 label => "Server Info",
1667 root 1.18 child => ($SERVER_INFO = new DC::UI::Label ellipsise => 0),
1668 root 1.1 );
1669    
1670     $vbox
1671     }
1672    
1673     sub client_setup {
1674 root 1.18 my $table = new DC::UI::Table expand => 1, col_expand => [0, 1];
1675 root 1.1
1676     my $row = 0;
1677    
1678 root 1.22 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Tip of the day");
1679 root 1.18 $table->add_at (1, $row++, new DC::UI::CheckBox
1680 root 1.1 state => $CFG->{show_tips},
1681     tooltip => "Show the <b>Tip of the day</b> window at startup?",
1682     on_changed => sub {
1683     my ($self, $value) = @_;
1684     $CFG->{show_tips} = $value;
1685     0
1686     }
1687     );
1688    
1689 root 1.37 $table->add_at (0, $row, new DC::UI::Label align => 1, text => "Message Window Size");
1690 root 1.18 $table->add_at (1, $row++, my $saycmd = new DC::UI::Entry
1691 root 1.1 text => $CFG->{logview_max_par},
1692 root 1.37 tooltip => "This is maximum number of messages remembered in the <b>Message</b> window. If the server "
1693 root 1.1 . "sends more messages than this number, older messages get removed to save memory and "
1694     . "computing time. A value of <b>0</b> disables this feature, but that is not recommended.",
1695     on_changed => sub {
1696     my ($self, $value) = @_;
1697 root 1.28 $MESSAGE_DIST->set_max_par ($CFG->{logview_max_par} = $value*1);
1698 root 1.1 0
1699     },
1700     );
1701    
1702     $table
1703     }
1704    
1705     sub autopickup_setup {
1706 root 1.18 my $r = new DC::UI::ScrolledWindow (
1707 root 1.1 expand => 1,
1708     scroll_y => 1
1709     );
1710 root 1.18 $r->add (my $table = new DC::UI::Table
1711 root 1.1 row_expand => [0],
1712     col_expand => [0, 1, 0, 1],
1713     );
1714    
1715     for (
1716     ["General", 0, 0,
1717     ["Enable autopickup" => PICKUP_NEWMODE, \$PICKUP_ENABLE],
1718     ["Inhibit autopickup" => PICKUP_INHIBIT],
1719     ["Stop before pickup" => PICKUP_STOP],
1720     ["Debug autopickup" => PICKUP_DEBUG],
1721     ],
1722     ["Weapons", 0, 6,
1723     ["All weapons" => PICKUP_ALLWEAPON],
1724     ["Missile weapons" => PICKUP_MISSILEWEAPON],
1725     ["Bows" => PICKUP_BOW],
1726     ["Arrows" => PICKUP_ARROW],
1727     ],
1728     ["Armour", 0, 12,
1729     ["Helmets" => PICKUP_HELMET],
1730     ["Shields" => PICKUP_SHIELD],
1731     ["Body Armour" => PICKUP_ARMOUR],
1732     ["Boots" => PICKUP_BOOTS],
1733     ["Gloves" => PICKUP_GLOVES],
1734     ["Cloaks" => PICKUP_CLOAK],
1735     ],
1736    
1737     ["Readables", 2, 0,
1738     ["Spellbooks" => PICKUP_SPELLBOOK],
1739     ["Skillscrolls" => PICKUP_SKILLSCROLL],
1740     ["Normal Books/Scrolls" => PICKUP_READABLES],
1741     ],
1742     ["Misc", 2, 5,
1743     ["Food" => PICKUP_FOOD],
1744     ["Drinks" => PICKUP_DRINK],
1745     ["Valuables (Money, Gems)" => PICKUP_VALUABLES],
1746     ["Keys" => PICKUP_KEY],
1747     ["Magical Items" => PICKUP_MAGICAL],
1748     ["Potions" => PICKUP_POTION],
1749     ["Magic Devices" => PICKUP_MAGIC_DEVICE],
1750     ["Ignore cursed" => PICKUP_NOT_CURSED],
1751     ["Jewelery" => PICKUP_JEWELS],
1752     ["Flesh" => PICKUP_FLESH],
1753     ],
1754 root 1.58 ["Value/Weight ratio", 2, 17]
1755 root 1.1 )
1756     {
1757     my ($title, $x, $y, @bits) = @$_;
1758 root 1.18 $table->add_at ($x, $y, new DC::UI::Label text => $title, align => 1, fg => [1, 1, 0]);
1759 root 1.1
1760     for (@bits) {
1761     ++$y;
1762    
1763     my $mask = $_->[1];
1764 root 1.18 $table->add_at ($x , $y, new DC::UI::Label text => $_->[0], align => 1, expand => 1);
1765     $table->add_at ($x+1, $y, my $checkbox = new DC::UI::CheckBox
1766 root 1.1 state => $::CFG->{pickup} & $mask,
1767     on_changed => sub {
1768     my ($box, $value) = @_;
1769    
1770     if ($value) {
1771     $::CFG->{pickup} |= $mask;
1772     } else {
1773     $::CFG->{pickup} &= ~$mask;
1774     }
1775    
1776     $::CONN->send_command ("pickup $::CFG->{pickup}")
1777     if defined $::CONN;
1778    
1779     0
1780     });
1781    
1782     ${$_->[2]} = $checkbox if $_->[2];
1783     }
1784     }
1785    
1786 root 1.18 $table->add_at (2, 18, new DC::UI::ValSlider
1787 root 1.1 range => [$::CFG->{pickup} & 0xF, 0, 16, 1, 1],
1788     template => ">= 99",
1789 root 1.58 tooltip => "Pick up items whose value/weight (silver/kg) ratio is equal or higher than this setting (which is specified in gold coins).",
1790 root 1.1 to_value => sub { ">= " . 5 * $_[0] },
1791     on_changed => sub {
1792     my ($slider, $value) = @_;
1793    
1794     $::CFG->{pickup} &= ~0xF;
1795     $::CFG->{pickup} |= int $value
1796     if $value;
1797     1;
1798     });
1799    
1800 root 1.18 $table->add_at (3, 18, new DC::UI::Button
1801 root 1.1 text => "set",
1802     on_activate => sub {
1803     $::CONN->send_command ("pickup $::CFG->{pickup}")
1804     if defined $::CONN;
1805     0
1806     });
1807    
1808     $r
1809     }
1810    
1811     my %SORT_ORDER = (
1812 elmex 1.41 type => sub {
1813     sort { $a->{type} <=> $b->{type} or $a->{name} cmp $b->{name} } @_
1814     },
1815 root 1.1 mtime => sub {
1816     my $NOW = time;
1817     sort {
1818     my $atime = $a->{mtime} - $NOW; $atime = $atime < 5 * 60 ? int $atime / 60 : 6;
1819     my $btime = $b->{mtime} - $NOW; $btime = $btime < 5 * 60 ? int $btime / 60 : 6;
1820    
1821     ($a->{flags} & F_LOCKED) <=> ($b->{flags} & F_LOCKED)
1822     or $btime <=> $atime
1823     or $a->{type} <=> $b->{type}
1824     } @_
1825     },
1826     weight => sub { sort {
1827     $a->{weight} * ($a->{nrof} || 1) <=> $b->{weight} * ($b->{nrof} || 1)
1828     or $a->{type} <=> $b->{type}
1829     } @_ },
1830     );
1831    
1832     sub inventory_widget {
1833 root 1.18 my $hb = new DC::UI::HBox homogeneous => 1;
1834 root 1.1
1835 root 1.18 $hb->add (my $vb1 = new DC::UI::VBox);
1836 root 1.22 $vb1->add (new DC::UI::Label text => "Player");
1837 root 1.1
1838 root 1.18 $vb1->add (my $hb1 = new DC::UI::HBox);
1839 root 1.1
1840     use sort 'stable';
1841    
1842 root 1.18 $hb1->add (new DC::UI::Selector
1843 root 1.1 value => $::CFG->{inv_sort},
1844     options => [
1845     [type => "Type/Name"],
1846     [mtime => "Recent/Normal/Locked"],
1847     [weight => "Weight/Type"],
1848     ],
1849     on_changed => sub {
1850     $::CFG->{inv_sort} = $_[1];
1851     $INV->set_sort_order ($SORT_ORDER{$_[1]});
1852     },
1853     );
1854 root 1.18 $hb1->add (new DC::UI::Label text => "Weight: ", align => 1, expand => 1);
1855 root 1.58 #TODO# update to weight/maxweight
1856 root 1.22 $hb1->add ($STATWIDS->{i_weight} = new DC::UI::Label align => 0);
1857 root 1.1
1858 root 1.18 $vb1->add (my $sw1 = new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
1859     $sw1->add ($INV = new DC::UI::Inventory);
1860 root 1.1 $INV->set_sort_order ($SORT_ORDER{$::CFG->{inv_sort}});
1861    
1862 root 1.18 $hb->add (my $vb2 = new DC::UI::VBox);
1863 root 1.1
1864 root 1.18 $vb2->add ($INVR_HB = new DC::UI::HBox);
1865 root 1.1
1866 root 1.18 $vb2->add (my $sw2 = new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
1867     $sw2->add ($INVR = new DC::UI::Inventory);
1868 root 1.1
1869     # XXX: Call after $INVR = ... because set_opencont sets the items
1870 root 1.18 DC::Protocol::set_opencont ($::CONN, 0, "Floor");
1871 root 1.1
1872     $hb
1873     }
1874    
1875     sub media_window {
1876 root 1.18 my $vb = new DC::UI::VBox;
1877 root 1.1
1878 root 1.18 $vb->add (new DC::UI::FancyFrame
1879 root 1.1 label => "Currently playing music",
1880 root 1.18 child => new DC::UI::ScrolledWindow scroll_x => 1, scroll_y => 0,
1881     child => ($MUSIC_PLAYING_WIDGET = new DC::UI::Label ellipsise => 0, fontsize => 0.8),
1882 root 1.1 );
1883    
1884 root 1.18 $vb->add (new DC::UI::FancyFrame
1885 root 1.1 label => "Other media used in this session",
1886     expand => 1,
1887 root 1.18 child => ($LICENSE_WIDGET = new DC::UI::TextScroller
1888 root 1.1 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4),
1889     );
1890    
1891     $vb
1892     }
1893    
1894     sub add_license {
1895     my ($meta) = @_;
1896    
1897     $meta = $meta->{data}
1898     or return;
1899    
1900     $meta->{license} || $meta->{author} || $meta->{source}
1901     or return;
1902    
1903     $LICENSE_WIDGET->add_paragraph ({
1904     fg => [1, 1, 1, 1],
1905     markup => "<small>"
1906 root 1.18 . "<b>Name:</b> " . (DC::asxml $meta->{name}) . "\n"
1907     . "<b>Author:</b> " . (DC::asxml $meta->{author}) . "\n"
1908     . "<b>Source:</b> " . (DC::asxml $meta->{source}) . "\n"
1909     . "<b>License:</b> " . (DC::asxml $meta->{license}) . "\n"
1910 root 1.1 . "</small>",
1911     });
1912     $LICENSE_WIDGET->scroll_to_bottom;
1913     }
1914    
1915     sub toggle_player_page {
1916     my ($widget) = @_;
1917    
1918     if ($PL_WINDOW->{visible} && $PL_NOTEBOOK->get_current_page == $widget) {
1919     $PL_WINDOW->hide;
1920     } else {
1921     $PL_NOTEBOOK->set_current_page ($widget);
1922     $PL_WINDOW->show;
1923     }
1924     }
1925    
1926     sub player_window {
1927 root 1.18 my $plwin = $PL_WINDOW = new DC::UI::Toplevel
1928 root 1.1 x => "center",
1929     y => "center",
1930     force_w => $WIDTH * 9/10,
1931     force_h => $HEIGHT * 9/10,
1932     title => "Player",
1933     name => "playerbook",
1934     has_close_button => 1
1935     ;
1936    
1937     my $ntb =
1938     $PL_NOTEBOOK =
1939 root 1.18 new DC::UI::Notebook expand => 1;
1940 root 1.1
1941     $ntb->add_tab (
1942     "Statistics (F2)" => $STATS_PAGE = stats_window,
1943     "Shows statistics, where all your Stats and Resistances are shown."
1944     );
1945     $ntb->add_tab (
1946     "Skills (F3)" => $SKILL_PAGE = skill_window,
1947     "Shows all your Skills."
1948     );
1949    
1950 root 1.18 my $spellsw = $SPELL_PAGE = new DC::UI::ScrolledWindow (expand => 1, scroll_y => 1);
1951     $spellsw->add ($SPELL_LIST = new DC::UI::SpellList);
1952 root 1.1 $ntb->add_tab (
1953     "Spellbook (F4)" => $spellsw,
1954     "Displays all spells you have and lets you edit keyboard shortcuts for them."
1955     );
1956     $ntb->add_tab (
1957     "Inventory (F5)" => $INVENTORY_PAGE = inventory_widget,
1958     "Toggles the inventory window, where you can manage your loot (or treasures :). "
1959     . "You can also hit the <b>Tab</b>-key to show/hide the Inventory."
1960     );
1961     $ntb->add_tab (Pickup => $PICKUP_PAGE = autopickup_setup,
1962     "Configure autopickup settings, i.e. which items you will pick up automatically when walking (or running) over them.");
1963    
1964     $ntb->add_tab (Media => media_window,
1965     "License, Author and Source info for media sent by the server.");
1966    
1967     $ntb->set_current_page ($INVENTORY_PAGE);
1968    
1969     $plwin->add ($ntb);
1970     $plwin
1971     }
1972    
1973     sub keyboard_setup {
1974 root 1.18 DC::Macro::keyboard_setup
1975 root 1.1 }
1976    
1977     sub help_window {
1978 root 1.18 my $win = new DC::UI::Toplevel
1979 root 1.1 x => 'center',
1980     y => 'center',
1981     z => 4,
1982     name => 'doc_browser',
1983     force_w => int $WIDTH * 7/8,
1984     force_h => int $HEIGHT * 7/8,
1985     title => "Help Browser",
1986     has_close_button => 1;
1987    
1988 root 1.18 $win->add (my $vbox = new DC::UI::VBox);
1989 root 1.1
1990 root 1.18 $vbox->add (new DC::UI::FancyFrame
1991 root 1.1 label => "Navigation",
1992 root 1.18 child => (my $buttons = new DC::UI::HBox),
1993 root 1.1 );
1994 root 1.18 $vbox->add (my $viewer = new DC::UI::TextScroller
1995 root 1.1 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4);
1996    
1997     my @history;
1998     my @future;
1999     my $curnode;
2000    
2001     my $load_node; $load_node = sub {
2002     my ($node, $para) = @_;
2003    
2004     $buttons->clear;
2005    
2006 root 1.18 $buttons->add (new DC::UI::Button
2007 root 1.1 text => "⇤",
2008     tooltip => "back to the starting page",
2009     on_activate => sub {
2010     unshift @future, [$curnode, $viewer->current_paragraph] if $curnode;
2011     unshift @future, @history;
2012     @history = ();
2013     $load_node->(@{shift @future});
2014     },
2015     );
2016    
2017     if (@history) {
2018 root 1.18 $buttons->add (new DC::UI::Button
2019 root 1.1 text => "⋘",
2020 root 1.18 tooltip => "back to <i>" . (DC::asxml DC::Pod::full_path $history[-1][0]) . "</i>",
2021 root 1.1 on_activate => sub {
2022     unshift @future, [$curnode, $viewer->current_paragraph] if $curnode;
2023     $load_node->(@{pop @history});
2024     },
2025     );
2026     }
2027    
2028     if (@future) {
2029 root 1.18 $buttons->add (new DC::UI::Button
2030 root 1.1 text => "⋙",
2031 root 1.18 tooltip => "forward to <i>" . (DC::asxml DC::Pod::full_path $future[0][0]) . "</i>",
2032 root 1.1 on_activate => sub {
2033     push @history, [$curnode, $viewer->current_paragraph];
2034     $load_node->(@{shift @future});
2035     },
2036     );
2037     }
2038    
2039 root 1.18 $buttons->add (new DC::UI::Label text => " ");
2040 root 1.1
2041 root 1.18 my @path = DC::Pod::full_path_of $node;
2042 root 1.1 pop @path; # drop current node
2043    
2044     for my $node (@path) {
2045 root 1.18 $buttons->add (new DC::UI::Button
2046 root 1.31 text => $node->[DC::Pod::N_KW][0],
2047 root 1.18 tooltip => "go to <i>" . (DC::asxml DC::Pod::full_path $node) . "</i>",
2048 root 1.1 on_activate => sub {
2049     push @history, [$curnode, $viewer->current_paragraph] if $curnode; @future = ();
2050     $load_node->($node);
2051     },
2052     );
2053 root 1.18 $buttons->add (new DC::UI::Label text => "/");
2054 root 1.1 }
2055    
2056 root 1.31 $buttons->add (new DC::UI::Label text => $node->[DC::Pod::N_KW][0], padding_x => 4, padding_y => 4);
2057 root 1.1
2058     $curnode = $node;
2059    
2060     $viewer->clear;
2061 root 1.18 $viewer->add_paragraph (DC::Pod::as_paragraphs DC::Pod::section_of $curnode);
2062 root 1.1 $viewer->scroll_to ($para);
2063     };
2064    
2065 root 1.18 $load_node->(DC::Pod::find pod => "mainpage");
2066 root 1.1
2067 root 1.18 $DC::Pod::goto_document = sub {
2068 root 1.1 my (@path) = @_;
2069    
2070     push @history, [$curnode, $viewer->current_paragraph] if $curnode; @future = ();
2071    
2072 root 1.18 $load_node->((DC::Pod::find @path)[0]);
2073 root 1.1 $win->show;
2074     };
2075    
2076     $win
2077     }
2078    
2079     sub open_string_query {
2080     my ($title, $cb, $txt, $tooltip) = @_;
2081 root 1.18 my $dialog = new DC::UI::Toplevel
2082 root 1.1 x => "center",
2083     y => "center",
2084     z => 50,
2085     force_w => $WIDTH * 4/5,
2086     title => $title;
2087    
2088     $dialog->add (
2089 root 1.18 my $e = new DC::UI::Entry
2090 root 1.1 on_activate => sub { $cb->(@_); $dialog->hide; 0 },
2091     on_key_down => sub { $_[1]->{sym} == 27 and $dialog->hide; 0 },
2092     tooltip => $tooltip
2093     );
2094    
2095     $e->grab_focus;
2096     $e->set_text ($txt) if $txt;
2097     $dialog->show;
2098     }
2099    
2100     sub open_quit_dialog {
2101     unless ($QUIT_DIALOG) {
2102 root 1.18 $QUIT_DIALOG = new DC::UI::Toplevel
2103 root 1.1 x => "center",
2104     y => "center",
2105     z => 50,
2106     title => "Really Quit?",
2107     on_key_down => sub {
2108     my ($dialog, $ev) = @_;
2109     $ev->{sym} == 27 and $dialog->hide;
2110     }
2111     ;
2112    
2113 root 1.18 $QUIT_DIALOG->add (my $vb = new DC::UI::VBox expand => 1);
2114 root 1.1
2115 root 1.18 $vb->add (new DC::UI::Label
2116 root 1.1 text => "You should find a savebed and apply it first!",
2117     max_w => $WIDTH * 0.25,
2118     ellipsize => 0,
2119     );
2120 root 1.18 $vb->add (my $hb = new DC::UI::HBox expand => 1);
2121     $hb->add (new DC::UI::Button
2122 root 1.1 text => "Ok",
2123     expand => 1,
2124     on_activate => sub { $QUIT_DIALOG->hide; 0 },
2125     );
2126 root 1.18 $hb->add (new DC::UI::Button
2127 root 1.1 text => "Quit anyway",
2128     expand => 1,
2129 root 1.4 on_activate => sub { EV::unloop EV::UNLOOP_ALL },
2130 root 1.1 );
2131     }
2132    
2133     $QUIT_DIALOG->show;
2134     $QUIT_DIALOG->grab_focus;
2135     }
2136    
2137     sub show_tip_of_the_day {
2138     # find all tips
2139 root 1.18 my @tod = DC::Pod::find tip_of_the_day => "*";
2140 root 1.1
2141 root 1.18 DC::DB::get state => "tip_of_the_day", sub {
2142 root 1.1 my ($todindex) = @_;
2143     $todindex = 0 if $todindex >= @tod;
2144 root 1.18 DC::DB::put state => tip_of_the_day => $todindex + 1, sub { };
2145 root 1.1
2146     # create dialog
2147     my $dialog;
2148    
2149     my $close = sub {
2150     $dialog->destroy;
2151     };
2152    
2153 root 1.18 $dialog = new DC::UI::Toplevel
2154 root 1.1 x => "center",
2155     y => "center",
2156     z => 3,
2157     name => 'tip_of_the_day',
2158     force_w => int $WIDTH * 4/9,
2159     force_h => int $WIDTH * 2/9,
2160     title => "Tip of the day #" . (1 + $todindex),
2161 root 1.18 child => my $vbox = new DC::UI::VBox,
2162 root 1.1 has_close_button => 1,
2163     on_delete => $close,
2164     ;
2165    
2166 root 1.18 $vbox->add (my $viewer = new DC::UI::TextScroller
2167 root 1.1 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4);
2168 root 1.18 $viewer->add_paragraph (DC::Pod::as_paragraphs DC::Pod::section_of $tod[$todindex]);
2169 root 1.1
2170 root 1.18 $vbox->add (my $table = new DC::UI::Table col_expand => [0, 1]);
2171 root 1.1
2172 root 1.18 $table->add_at (0, 0, new DC::UI::Button
2173 root 1.1 text => "Close",
2174     tooltip => "Close the tip of the day window. To never see it again, disable the tip of the day in the <b>Server Setup</b>.",
2175     on_activate => $close,
2176     );
2177    
2178 root 1.18 $table->add_at (2, 0, new DC::UI::Button
2179 root 1.1 text => "Next",
2180     tooltip => "Show the next <b>Tip of the day</b>.",
2181     on_activate => sub {
2182     $close->();
2183     &show_tip_of_the_day;
2184     },
2185     );
2186    
2187     $dialog->show;
2188     };
2189     }
2190    
2191     sub sdl_init {
2192 root 1.52 DC::SDL_Init DC::SDL_INIT_AUDIO #| DC::SDL_NOPARACHUTE
2193 root 1.1 and die "SDL::Init failed!\n";
2194     }
2195    
2196     sub video_init {
2197 root 1.52 DC::SDL_InitSubSystem DC::SDL_INIT_VIDEO if $SDL_REINIT;
2198     $SDL_REINIT = 0;
2199    
2200     @SDL_MODES = DC::SDL_ListModes 8, $CFG->{disable_alpha} ? 0 : 8;
2201     @SDL_MODES = DC::SDL_ListModes 8, 8 unless @SDL_MODES;
2202     @SDL_MODES = DC::SDL_ListModes 5, 0 unless @SDL_MODES;
2203     @SDL_MODES or DC::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
2204    
2205     @SDL_MODES = sort { $a->[0] * $a->[1] <=> $b->[0] * $b->[1] } @SDL_MODES;
2206    
2207 root 1.56 if (!exists $CFG->{sdl_mode} or $CFG->{sdl_mode} > $#SDL_MODES) {
2208 root 1.52 $CFG->{sdl_mode} = 0;
2209     for (reverse 0 .. $#SDL_MODES) {
2210     $CFG->{sdl_mode} = $_ if $SDL_MODES[$_][0] * $SDL_MODES[$_][1] >= 800 * 600;
2211     }
2212     }
2213 root 1.1
2214     my ($old_w, $old_h) = ($WIDTH, $HEIGHT);
2215    
2216     ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
2217     $FULLSCREEN = $CFG->{fullscreen};
2218     $FAST = $CFG->{fast};
2219    
2220 root 1.59 # due to mac os x braindamage, we simply retry with !fullscreen in case of an error
2221 root 1.18 DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN
2222 root 1.59 or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN
2223 root 1.18 or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n";
2224 root 1.1
2225     $SDL_ACTIVE = 1;
2226     $LAST_REFRESH = time - 0.01;
2227    
2228 root 1.18 DC::OpenGL::init;
2229     DC::Macro::init;
2230 root 1.1
2231     $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
2232    
2233 root 1.18 $DC::UI::ROOT->configure (0, 0, $WIDTH, $HEIGHT);#d#
2234 root 1.1
2235     #############################################################################
2236    
2237     if ($DEBUG_STATUS) {
2238 root 1.18 DC::UI::rescale_widgets $WIDTH / $old_w, $HEIGHT / $old_h;
2239 root 1.1 } else {
2240     # create/configure the widgets
2241    
2242 root 1.18 $DC::UI::ROOT->connect (key_down => sub {
2243 root 1.1 my (undef, $ev) = @_;
2244    
2245 root 1.18 if (my @macros = DC::Macro::find $ev) {
2246     DC::Macro::execute $_ for @macros;
2247 root 1.1
2248     return 1;
2249     }
2250    
2251     0
2252     });
2253    
2254 root 1.18 $DEBUG_STATUS = new DC::UI::Label
2255 root 1.1 padding => 0,
2256     z => 100,
2257     force_x => "max",
2258     force_y => 0;
2259     $DEBUG_STATUS->show;
2260    
2261 root 1.18 $STATUSBOX = new DC::UI::Statusbox;
2262 root 1.29
2263     $MODBOX = new DC::UI::Label
2264     can_events => 1,
2265     can_hover => 1,
2266     markup => "",
2267     align => 0,
2268     font => $FONT_FIXED,
2269 root 1.30 tooltip => "#modifier_box",
2270     tooltip_width => 0.67,
2271     ;
2272 root 1.29
2273     update_modbox;
2274 root 1.1
2275 root 1.18 (new DC::UI::Frame
2276 root 1.1 bg => [0, 0, 0, 0.4],
2277     force_x => 0,
2278     force_y => "max",
2279 root 1.29 child => (my $LR = new DC::UI::VBox),
2280 root 1.1 )->show;
2281    
2282 root 1.29 $LR->add ($STATUSBOX);
2283     $LR->add ($MODBOX);
2284     $LR->add (new DC::UI::Label
2285     align => 0,
2286     markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode",
2287     fontsize => 0.5,
2288     fg => [1, 1, 0, 0.7],
2289     );
2290    
2291 root 1.18 DC::UI::Toplevel->new (
2292 root 1.2 title => "Minimap",
2293 root 1.1 name => "mapmap",
2294     x => 0,
2295     y => $FONTSIZE + 8,
2296     border_bg => [1, 1, 1, 192/255],
2297     bg => [1, 1, 1, 0],
2298 root 1.18 child => ($MAPMAP = new DC::MapWidget::MapMap
2299 root 1.1 tooltip => "<b>Map</b>. On servers that support this feature, this will display an overview of the surrounding areas.",
2300     ),
2301     )->show;
2302    
2303 root 1.18 $MAPWIDGET = new DC::MapWidget;
2304 root 1.1 $MAPWIDGET->connect (activate_console => sub {
2305     my ($mapwidget, $preset) = @_;
2306    
2307 elmex 1.23 $MESSAGE_DIST->activate_console ($preset)
2308     if $MESSAGE_DIST;
2309 root 1.1 });
2310     $MAPWIDGET->show;
2311     $MAPWIDGET->grab_focus;
2312    
2313 root 1.18 $COMPLETER = new DC::MapWidget::Command::
2314 root 1.1 command => { },
2315     tooltip => "#completer_help",
2316     ;
2317    
2318 root 1.18 $SETUP_DIALOG = new DC::UI::Toplevel
2319 root 1.1 title => "Setup",
2320     name => "setup_dialog",
2321     x => 'center',
2322     y => 'center',
2323     z => 2,
2324     force_w => $::WIDTH * 0.6,
2325     force_h => $::HEIGHT * 0.6,
2326     has_close_button => 1,
2327     ;
2328    
2329     $METASERVER = metaserver_dialog;
2330 root 1.39 # the name is changed to not conflict with the older name as users could have hidden it
2331 root 1.40 $MESSAGE_WINDOW = new DC::UI::Dockbar
2332     name => "message_window2",
2333     title => 'Messages',
2334     force_w => $::WIDTH * 0.6,
2335     force_h => $::HEIGHT * 0.25,
2336     ;
2337    
2338 elmex 1.23 $MESSAGE_DIST = new DC::MessageDistributor dockbar => $MESSAGE_WINDOW;
2339 root 1.1
2340 root 1.38 $SETUP_DIALOG->add ($SETUP_NOTEBOOK = new DC::UI::Notebook expand => 1,
2341 root 1.18 filter => new DC::UI::ScrolledWindow expand => 1, scroll_y => 1);
2342 root 1.1
2343     $SETUP_NOTEBOOK->add_tab (Login => $SETUP_LOGIN = login_setup,
2344     "Configure the server to play on, your username and password.");
2345     $SETUP_NOTEBOOK->add_tab (Server => $SETUP_SERVER = server_setup,
2346     "Configure other server related options.");
2347     $SETUP_NOTEBOOK->add_tab (Client => client_setup,
2348     "Configure various client-specific settings.");
2349     $SETUP_NOTEBOOK->add_tab (Graphics => graphics_setup,
2350     "Configure the video mode, performance, fonts and other graphical aspects of the game.");
2351     $SETUP_NOTEBOOK->add_tab (Audio => audio_setup,
2352     "Configure the use of audio, sound effects and background music.");
2353     $SETUP_NOTEBOOK->add_tab (Keyboard => $SETUP_KEYBOARD = keyboard_setup,
2354     "Lets you define, edit and delete key bindings."
2355     . "There is a shortcut for making bindings: <b>Control-Insert</b> opens the binding editor "
2356     . "with nothing set and the recording started. After doing the actions you "
2357     . "want to record press <b>Insert</b> and you will be asked to press a key-combo. "
2358     . "After pressing the combo the binding will be saved automatically and the "
2359     . "binding editor closes");
2360     $SETUP_NOTEBOOK->add_tab (Debug => debug_setup,
2361     "Some debuggin' options. Do not ask.");
2362    
2363 root 1.18 $BUTTONBAR = new DC::UI::Buttonbar x => 0, y => 0, z => 200; # put on top
2364 root 1.1
2365 root 1.18 $BUTTONBAR->add (new DC::UI::Flopper text => "Setup", other => $SETUP_DIALOG,
2366 root 1.1 tooltip => "Toggles a dialog where you can configure all aspects of this client.");
2367    
2368 root 1.37 # $BUTTONBAR->add (new DC::UI::Flopper text => "Message Window", other => $MESSAGE_WINDOW,
2369     # tooltip => "Toggles the server message log, where the client collects <i>all</i> messages from the server.");
2370 root 1.1
2371     make_gauge_window->show; # XXX: this has to be set before make_stats_window as make_stats_window calls update_stats_window which updated the gauges also X-D
2372    
2373 root 1.18 $BUTTONBAR->add (new DC::UI::Flopper text => "Playerbook", other => player_window,
2374 root 1.1 tooltip => "Toggles the player view, where you can manage Inventory, Spells, Skills and see your Stats.");
2375    
2376 root 1.18 $BUTTONBAR->add (new DC::UI::Button
2377 root 1.1 text => "Save Config",
2378     tooltip => "Saves the options chosen in the client setting, server settings and the window layout to be restored on later runs.",
2379     on_activate => sub {
2380 root 1.18 $::CFG->{layout} = DC::UI::get_layout;
2381 root 1.35 DC::write_cfg;
2382 root 1.1 status "Configuration Saved";
2383     0
2384     },
2385     );
2386    
2387 root 1.18 $BUTTONBAR->add (new DC::UI::Flopper text => "Help!", other => $HELP_WINDOW = help_window,
2388 root 1.1 tooltip => "View Documentation");
2389    
2390 root 1.18 $BUTTONBAR->add (new DC::UI::Button
2391 root 1.1 text => "Quit",
2392     tooltip => "Terminates the program",
2393     on_activate => sub {
2394     if ($CONN) {
2395     open_quit_dialog;
2396     } else {
2397 root 1.4 EV::unloop EV::UNLOOP_ALL;
2398 root 1.1 }
2399     0
2400     },
2401     );
2402    
2403     $BUTTONBAR->show;
2404     $SETUP_DIALOG->show;
2405     $MESSAGE_WINDOW->show;
2406     }
2407    
2408 root 1.53 $MODE_SLIDER->set_range ([$CFG->{sdl_mode}, 0, $#SDL_MODES, 1, 1]);
2409     $MODE_SLIDER->emit (changed => $CFG->{sdl_mode});
2410    
2411     $CAVEAT_LABEL->set_text ("None :)");
2412 root 1.55 $CAVEAT_LABEL->set_text ("Software Rendering (very slow)")
2413 root 1.53 unless DC::SDL_GL_GetAttribute DC::SDL_GL_ACCELERATED_VISUAL;
2414    
2415 root 1.1 $STATUSBOX->add ("Set video mode $WIDTH×$HEIGHT", timeout => 10, fg => [1, 1, 1, 0.5]);
2416     }
2417    
2418     sub video_shutdown {
2419 root 1.18 DC::OpenGL::shutdown;
2420 root 1.52 DC::SDL_QuitSubSystem DC::SDL_INIT_VIDEO if $SDL_REINIT;
2421 root 1.1
2422     undef $SDL_ACTIVE;
2423     }
2424    
2425     my %animate_object;
2426     my $animate_timer;
2427    
2428     my $fps = 9;
2429    
2430     sub force_refresh {
2431     if ($ENV{CFPLUS_DEBUG} & 4) {
2432     $fps = $fps * 0.98 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.02;
2433     debug sprintf "%3.2f", $fps;
2434     }
2435    
2436 root 1.16 undef $WANT_REFRESH;
2437     $_[0]->stop;
2438 root 1.12
2439 root 1.18 $DC::UI::ROOT->draw;
2440     DC::SDL_GL_SwapBuffers;
2441 root 1.1 $LAST_REFRESH = $NOW;
2442     }
2443    
2444 root 1.19 my $want_refresh = EV::prepare_ns \&force_refresh;
2445 root 1.1
2446 root 1.19 my $input = EV::periodic 0, 1 / $MAX_FPS, undef, sub {
2447     $NOW = EV::now;
2448 root 1.1
2449     ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
2450 root 1.18 for DC::poll_events;
2451 root 1.1
2452     if (%animate_object) {
2453     $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
2454 root 1.16 $WANT_REFRESH = 1;
2455 root 1.1 }
2456 root 1.16
2457     $want_refresh->start
2458     if $WANT_REFRESH;
2459 root 1.4 };
2460 root 1.1
2461     sub animation_start {
2462     my ($widget) = @_;
2463     $animate_object{$widget} = $widget;
2464     }
2465    
2466     sub animation_stop {
2467     my ($widget) = @_;
2468     delete $animate_object{$widget};
2469     }
2470    
2471     %SDL_CB = (
2472 root 1.18 DC::SDL_QUIT => sub {
2473 root 1.4 EV::unloop EV::UNLOOP_ALL;
2474 root 1.1 },
2475 root 1.18 DC::SDL_VIDEORESIZE => sub {
2476 root 1.1 },
2477 root 1.18 DC::SDL_VIDEOEXPOSE => sub {
2478     DC::UI::full_refresh;
2479 root 1.1 },
2480 root 1.18 DC::SDL_ACTIVEEVENT => sub {
2481 root 1.12 # not useful, as APPACTIVE includes only iconified state, not unmapped
2482 root 1.18 # printf "active %x %x %x\n", $_[0]{gain}, $_[0]{state}, DC::SDL_GetAppState;#d#
2483     # printf "a %x\n", DC::SDL_GetAppState & DC::SDL_APPACTIVE;#d#
2484     # printf "A\n" if $_[0]{state} & DC::SDL_APPACTIVE;
2485     # printf "K\n" if $_[0]{state} & DC::SDL_APPINPUTFOCUS;
2486     # printf "M\n" if $_[0]{state} & DC::SDL_APPMOUSEFOCUS;
2487 root 1.1 },
2488 root 1.18 DC::SDL_KEYDOWN => sub {
2489     if ($_[0]{mod} & DC::KMOD_ALT && $_[0]{sym} == 13) {
2490 root 1.1 # alt-enter
2491 root 1.52 video_shutdown;
2492 root 1.1 $FULLSCREEN_ENABLE->toggle;
2493     video_init;
2494     } else {
2495 root 1.29 &DC::UI::feed_sdl_key_down_event;
2496 root 1.1 }
2497 root 1.29 update_modbox;
2498     },
2499     DC::SDL_KEYUP => sub {
2500     &DC::UI::feed_sdl_key_up_event;
2501     update_modbox;
2502 root 1.1 },
2503 root 1.18 DC::SDL_MOUSEMOTION => \&DC::UI::feed_sdl_motion_event,
2504     DC::SDL_MOUSEBUTTONDOWN => \&DC::UI::feed_sdl_button_down_event,
2505     DC::SDL_MOUSEBUTTONUP => \&DC::UI::feed_sdl_button_up_event,
2506     DC::SDL_USEREVENT => sub {
2507 root 1.1 if ($_[0]{code} == 1) {
2508     audio_channel_finished $_[0]{data1};
2509     } elsif ($_[0]{code} == 0) {
2510     audio_music_finished;
2511     }
2512     },
2513     );
2514    
2515     #############################################################################
2516    
2517 root 1.11 $SIG{INT} = $SIG{TERM} = sub {
2518     EV::unloop;
2519     #d# TODO calling exit here hangs the process in some futex
2520     };
2521 root 1.1
2522 root 1.59 # due to mac os x + sdl combined briandamage, we need this contortion
2523     sub main {
2524     {
2525     DC::Pod::load_docwiki DC::find_rcfile "docwiki.pst";
2526 root 1.34
2527 root 1.59 if (-e "$Deliantra::VARDIR/client.cf") {
2528     DC::read_cfg "$Deliantra::VARDIR/client.cf";
2529     } else {
2530     #TODO: compatibility cruft
2531     DC::read_cfg "$Deliantra::OLDDIR/cfplusrc";
2532     print STDERR "INFO: used old configuration file\n";
2533     }
2534 root 1.15
2535 root 1.59 DC::DB::Server::run;
2536 root 1.35
2537 root 1.59 if ($CFG->{db_schema} < 1) {
2538     warn "INFO: upgrading database schema from 0 to 1, mapcache and tilecache will be lost\n";
2539     DC::DB::nuke_db;
2540     $CFG->{db_schema} = 1;
2541     DC::write_cfg;
2542     }
2543 root 1.35
2544 root 1.59 DC::DB::open_db;
2545 root 1.1
2546 root 1.59 DC::UI::set_layout ($::CFG->{layout});
2547 root 1.1
2548 root 1.59 my %DEF_CFG = (
2549     sdl_mode => 0,
2550     fullscreen => 1,
2551     fast => 0,
2552     force_opengl11 => undef,
2553     disable_alpha => 0,
2554     smooth_movement => 1,
2555     texture_compression => 1,
2556     map_scale => 1,
2557     fow_enable => 1,
2558     fow_intensity => 0,
2559     map_smoothing => 1,
2560     gui_fontsize => 1,
2561     log_fontsize => 0.7,
2562     gauge_fontsize => 1,
2563     gauge_size => 0.35,
2564     stat_fontsize => 0.7,
2565     mapsize => 100,
2566     audio_enable => 1,
2567     audio_hw_channels => 0,
2568     audio_hw_frequency => 0,
2569     audio_hw_chunksize => 0,
2570     audio_mix_channels => 8,
2571     effects_enable => 1,
2572     effects_volume => 1,
2573     bgm_enable => 1,
2574     bgm_volume => 0.5,
2575     output_rate => "",
2576     pickup => 0,
2577     inv_sort => "mtime",
2578     default => "profile", # default profile
2579     show_tips => 1,
2580     logview_max_par => 1000,
2581     shift_fire_stop => 0,
2582     );
2583    
2584     while (my ($k, $v) = each %DEF_CFG) {
2585     $CFG->{$k} = $v unless exists $CFG->{$k};
2586     }
2587 root 1.1
2588 root 1.59 $CFG->{profile}{default}{host} ||= "gameserver.deliantra.net";
2589     $PROFILE = $CFG->{profile}{default};
2590 root 1.1
2591 root 1.59 # convert old bindings (only default profile matters)
2592     if (my $bindings = delete $PROFILE->{bindings}) {
2593     while (my ($mod, $syms) = each %$bindings) {
2594     while (my ($sym, $cmds) = each %$syms) {
2595     push @{ $PROFILE->{macro} }, {
2596     accelkey => [$mod*1, $sym*1],
2597     action => $cmds,
2598     };
2599     }
2600 root 1.1 }
2601     }
2602    
2603 root 1.59 sdl_init;
2604 root 1.1
2605 root 1.59 {
2606     my @fonts = map DC::find_rcfile "fonts/$_", qw(
2607     DejaVuSans.ttf
2608     DejaVuSansMono.ttf
2609     DejaVuSans-Bold.ttf
2610     DejaVuSansMono-Bold.ttf
2611     DejaVuSans-Oblique.ttf
2612     DejaVuSansMono-Oblique.ttf
2613     DejaVuSans-BoldOblique.ttf
2614     DejaVuSansMono-BoldOblique.ttf
2615     );
2616    
2617     DC::add_font $_ for @fonts;
2618    
2619     $FONT_PROP = new_from_file DC::Font $fonts[0];
2620     $FONT_FIXED = new_from_file DC::Font $fonts[1];
2621 root 1.1
2622 root 1.59 $FONT_PROP->make_default;
2623 root 1.30
2624 root 1.59 DC::pango_init;
2625     }
2626 root 1.1
2627     # compare mono (ft) vs. rgba (cairo)
2628     # ft - 1.8s, cairo 3s, even in alpha-only mode
2629     # for my $rgba (0..1) {
2630     # my $t1 = Time::HiRes::time;
2631     # for (1..1000) {
2632 root 1.18 # my $layout = DC::Layout->new ($rgba);
2633 root 1.1 # $layout->set_text ("hallo" x 100);
2634     # $layout->render;
2635     # }
2636     # my $t2 = Time::HiRes::time;
2637     # warn $t2-$t1;
2638     # }
2639    
2640 root 1.59 video_init;
2641     audio_init;
2642     }
2643 root 1.1
2644 root 1.59 show_tip_of_the_day if $CFG->{show_tips};
2645 root 1.1
2646 root 1.59 our $STARTUP_CANCEL = EV::idle sub {
2647     undef $::STARTUP_CANCEL;
2648     $startup_done->();
2649     };
2650 root 1.1
2651 root 1.59 delete $SIG{__DIE__};
2652     EV::loop;
2653 root 1.1
2654     #video_shutdown;
2655     #audio_shutdown;
2656 root 1.59 DC::OpenGL::quit;
2657     DC::SDL_Quit;
2658     DC::DB::Server::stop;
2659     }
2660    
2661     DC::SDL_braino; # see sub above
2662 root 1.1
2663     =head1 NAME
2664    
2665     deliantra - A Deliantra MORPG game client
2666    
2667     =head1 SYNOPSIS
2668    
2669     Just run it - no commandline arguments are supported.
2670    
2671     =head1 USAGE
2672    
2673     deliantra utilises OpenGL for all UI elements and the game. It is supposed to
2674     be used in fullscreen mode and interactively.
2675    
2676     =head1 DEBUGGING
2677    
2678    
2679     CFPLUS_DEBUG - environment variable
2680    
2681     1 draw borders around widgets
2682     2 add low-level widget info to tooltips
2683     4 show fps
2684     8 suppress tooltips
2685    
2686     =head1 AUTHOR
2687    
2688 root 1.57 Marc Lehmann <deliantra@schmorp.de>, Robin Redeker <elmex@ta-sa.org>
2689 root 1.1
2690    
2691