ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.72
Committed: Tue Apr 11 18:06:53 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.71: +39 -29 lines
Log Message:
CFClient::Widget => CFClient::UI

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2 root 1.25
3 root 1.2 use strict;
4 root 1.25 use utf8;
5 root 1.2
6 root 1.13 use Glib;
7     use Gtk2 -init;
8    
9     use SDL;
10     use SDL::App;
11     use SDL::Event;
12     use SDL::Surface;
13     use SDL::OpenGL;
14     use SDL::OpenGL::Constants;
15    
16 elmex 1.11 use Crossfire;
17 root 1.2 use Crossfire::Protocol;
18    
19 root 1.67 use CFClient;
20 root 1.72 use CFClient::UI;
21 elmex 1.10
22 root 1.63 our $VERSION = '0.1';
23    
24     my $MAX_FPS = 30;
25     my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26    
27 root 1.19 our $FACECACHE;
28    
29 elmex 1.10 our $CFG;
30 root 1.13 our $CONN;
31 root 1.2
32 root 1.30 our $WIDTH;
33     our $HEIGHT;
34     our $FULLSCREEN;
35    
36 root 1.71 our $NOW;
37    
38 root 1.69 our $MAPWIDGET;
39 root 1.39 our $FONTSIZE;
40 root 1.57
41 root 1.13 our $SDL_TIMER;
42     our $SDL_APP;
43 root 1.58 our $SDL_EV;
44 root 1.13 our %SDL_CB;
45 root 1.18
46 root 1.30 our $ALT_ENTER_MESSAGE;
47 root 1.51 our $STATUS_LINE;
48 root 1.64 our $DEBUG_STATUS;
49 root 1.30
50 root 1.45 my $last_refresh;
51 root 1.50 my %ANIMATE;
52     my $refresh_handler;
53 root 1.45
54 root 1.58 our ($tw, $te); # Test widget #d#
55    
56 root 1.13 sub init_screen {
57     $SDL_APP = new SDL::App
58 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
59     -title => "Crossfire+ Client",
60 root 1.30 -width => $WIDTH,
61     -height => $HEIGHT,
62 root 1.5 -opengl => 1,
63 root 1.51 -red_size => 5,
64     -green_size => 5,
65     -blue_size => 5,
66     -alpha_size => 0,
67 root 1.2 -double_buffer => 1,
68 root 1.30 -fullscreen => $FULLSCREEN,
69 root 1.5 -resizeable => 0;
70 root 1.2
71 root 1.58 $SDL_EV = new SDL::Event;
72     $SDL_EV->set_unicode (1);
73    
74 root 1.62 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
75     ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
76     while $SDL_EV->poll;
77    
78     1
79     };
80    
81 root 1.45 $last_refresh = SDL::GetTicks;
82    
83 root 1.67 CFClient::gl_init;
84 root 1.30
85 root 1.40 $FONTSIZE = int $HEIGHT / 50;
86 root 1.39
87 root 1.52 #############################################################################
88    
89 root 1.62 glClearColor 0.45, 0.45, 0.45, 1;
90 root 1.52
91     glEnable GL_TEXTURE_2D;
92     glEnable GL_COLOR_MATERIAL;
93     glShadeModel GL_FLAT;
94     glDisable GL_DEPTH_TEST;
95     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
96    
97     #############################################################################
98 root 1.64
99 root 1.72 $DEBUG_STATUS = new CFClient::UI::Label;
100     $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
101 root 1.52
102 root 1.72 $STATUS_LINE = new CFClient::UI::Label
103 root 1.68 y => $HEIGHT * 59 / 60 - $FONTSIZE;
104 root 1.72 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
105 root 1.51
106 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
107 root 1.68 y => $HEIGHT * 59 / 60,
108     height => $HEIGHT / 60,
109     text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
110 root 1.72 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
111 root 1.30
112 root 1.72 $MAPWIDGET = new CFClient::UI::MapWidget;
113     $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
114 root 1.69 $MAPWIDGET->focus_in;
115    
116 elmex 1.34 # Test code #d#
117 elmex 1.43 unless ($tw) { # haha...
118 root 1.72 my $w = new CFClient::UI::Window
119 root 1.70 w => 300, h => 300,
120 root 1.72 child => (my $frame = new CFClient::UI::FancyFrame);
121     $CFClient::UI::TOPLEVEL->add ($w);
122 root 1.71
123 root 1.72 $frame->add (my $vbox = new CFClient::UI::VBox);
124 root 1.71
125 root 1.72 $vbox->add (new CFClient::UI::Entry text => "hallo");
126     $vbox->add (new CFClient::UI::Slider);
127 root 1.58
128 root 1.72 $tw = new CFClient::UI::Animator x => $WIDTH - 200, w => 600, h => 300;
129     my $lbl1 = new CFClient::UI::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
130     my $lbl2 = new CFClient::UI::Label text => "LBL2";
131     my $vb = new CFClient::UI::VBox;
132     my $f = new CFClient::UI::FancyFrame;
133     my $f2 = new CFClient::UI::FancyFrame;
134 elmex 1.46 $f->add ($lbl1);
135     $f2->add ($lbl2);
136     $vb->add ($f);
137     $vb->add ($f2, 1);
138    
139     $tw->add ($vb);
140 root 1.45 $tw->moveto (0, 0);
141 root 1.72 $CFClient::UI::TOPLEVEL->add ($tw);
142 elmex 1.46
143     # $f->move ($WIDTH - 200, 0);
144 root 1.72 # $CFClient::UI::TOPLEVEL->add ($f);
145 root 1.39 }
146 root 1.2 }
147    
148 root 1.62 sub destroy_screen {
149     remove Glib::Source $SDL_TIMER;
150     undef $SDL_APP;
151     undef $SDL_EV;
152     SDL::Quit;
153     }
154    
155 root 1.72 sub config_dialog {
156     my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
157     child => (my $vbox = new CFClient::UI::VBox);
158     $vbox->add (new CFClient::UI::Label align => 0, text => "Setup");
159    
160     $CFClient::UI::TOPLEVEL->add ($dialog);
161     }
162    
163 root 1.13 sub start_game {
164 root 1.30 $WIDTH = $CFG->{width};
165     $HEIGHT = $CFG->{height};
166     $FULLSCREEN = 0;
167    
168 root 1.13 init_screen;
169    
170 root 1.72 config_dialog;
171    
172 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
173 root 1.50
174 root 1.13 $CONN = new conn
175     host => $CFG->{host},
176 root 1.28 port => $CFG->{port},
177     user => $CFG->{user},
178 root 1.37 pass => $CFG->{password},
179 root 1.50 mapw => $mapsize,
180     maph => $mapsize,
181 root 1.37 ;
182 root 1.45
183 root 1.67 CFClient::lowdelay fileno $CONN->{fh};
184 root 1.13 }
185    
186     sub stop_game {
187 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
188 root 1.50 undef $refresh_handler;
189 root 1.13
190 root 1.50 undef $CONN;
191 root 1.62 destroy_screen;
192 root 1.13 }
193    
194 root 1.30 sub force_refresh {
195     glViewport 0, 0, $WIDTH, $HEIGHT;
196 root 1.35
197     glMatrixMode GL_PROJECTION;
198     glLoadIdentity;
199 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
200 root 1.35 glMatrixMode GL_MODELVIEW;
201    
202 root 1.5 glClear GL_COLOR_BUFFER_BIT;
203 root 1.2
204 root 1.72 $CFClient::UI::TOPLEVEL->draw;
205 root 1.1
206 root 1.2 SDL::GLSwapBuffers;
207 root 1.1 }
208    
209 root 1.64 sub debug {
210     $DEBUG_STATUS->set_text ($_[0]);
211     $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
212     $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
213     }
214    
215     my $FPS;
216    
217 root 1.30 sub refresh {
218     $refresh_handler ||= add Glib::Idle sub {
219 root 1.58 if ($SDL_APP) {
220 root 1.71 $NOW = SDL::GetTicks;
221 root 1.63
222 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
223     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
224     $NOW = SDL::GetTicks;
225 root 1.63 }
226    
227 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
228     $last_refresh = $NOW;
229 root 1.58
230 root 1.64 if ($interval) {
231     $FPS ||= 1 / $interval;
232     $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
233 root 1.66 debug sprintf "%5.02f", $FPS;
234 root 1.64 }
235    
236 root 1.58 force_refresh;
237     $_->animate ($interval) for grep $_, values %ANIMATE;
238    
239     if (%ANIMATE) {
240     1
241     } else {
242     undef $refresh_handler;
243     0
244     }
245 root 1.45 } else {
246     undef $refresh_handler;
247     0
248     }
249 root 1.30 };
250     }
251    
252 root 1.45 sub animation_start {
253     my ($widget) = @_;
254     $ANIMATE{$widget} = $widget;
255     Scalar::Util::weaken $ANIMATE{$widget};
256    
257     refresh;
258     }
259    
260     sub animation_stop {
261     my ($widget) = @_;
262     delete $ANIMATE{$widget};
263     }
264    
265 root 1.13 %SDL_CB = (
266     SDL_QUIT() => sub {
267 root 1.19 main_quit Gtk2;
268 root 1.13 },
269     SDL_VIDEORESIZE() => sub {
270     },
271     SDL_VIDEOEXPOSE() => sub {
272     refresh;
273     },
274     SDL_KEYDOWN() => sub {
275 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
276     # alt-enter
277 root 1.30 $FULLSCREEN = !$FULLSCREEN;
278 root 1.18 init_screen;
279     } else {
280 root 1.72 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
281 root 1.18 }
282 root 1.13 },
283     SDL_KEYUP() => sub {
284 root 1.72 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
285 root 1.13 },
286     SDL_MOUSEMOTION() => sub {
287 root 1.72 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
288 root 1.13 },
289     SDL_MOUSEBUTTONDOWN() => sub {
290 root 1.72 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
291 root 1.13 },
292     SDL_MOUSEBUTTONUP() => sub {
293 root 1.72 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
294 root 1.13 },
295     SDL_ACTIVEEVENT() => sub {
296 root 1.70 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
297 root 1.13 },
298     );
299 root 1.1
300 root 1.2 @conn::ISA = Crossfire::Protocol::;
301 root 1.1
302 root 1.2 sub conn::map_update {
303 root 1.1 my ($self, $dirty) = @_;
304    
305 root 1.69 $MAPWIDGET->update;
306 root 1.1 }
307    
308 root 1.2 sub conn::map_scroll {
309 root 1.1 my ($self, $dx, $dy) = @_;
310    
311 root 1.45 # refresh;
312 root 1.1 }
313    
314 root 1.2 sub conn::map_clear {
315 root 1.1 my ($self) = @_;
316    
317 root 1.45 # refresh;
318 root 1.1 }
319    
320 root 1.19 sub conn::face_find {
321     my ($self, $face) = @_;
322    
323     $FACECACHE->{"$face->{chksum},$face->{name}"}
324     }
325    
326 root 1.2 sub conn::face_update {
327 root 1.19 my ($self, $face) = @_;
328    
329     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
330 root 1.1
331 root 1.67 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
332 root 1.1 }
333    
334 root 1.33 sub conn::query {
335     my ($self, $flags, $prompt) = @_;
336    
337     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
338     }
339    
340 elmex 1.23 sub gtk_add_cfg_field {
341     my ($tbl, $cfg, $klbl, $key, $value) = @_;
342     my $i = $cfg->{_i}++;
343     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
344     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
345     if ($key eq 'password') {
346     $ent->set_invisible_char ("*");
347     $ent->set (visibility => 0)
348     }
349     $ent->set_text ($value);
350     $ent->signal_connect (changed => sub {
351     my ($ent) = @_;
352     $cfg->{$key} = $ent->get_text;
353 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
354 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
355     $cfg->{$key} = 100;
356 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
357     $cfg->{$key} = 50;
358 elmex 1.47 }
359 elmex 1.23 });
360     }
361    
362     sub run_config_dialog {
363     my (%events) = @_;
364    
365     my $w = Gtk2::Window->new;
366    
367     my @cfg = (
368     [qw/Host host/],
369     [qw/Port port/],
370 elmex 1.47 [qw/Mapsize% mapsize/],
371 elmex 1.23 [qw/Username user/],
372     [qw/Password password/],
373     );
374    
375     my $cfg = {};
376    
377     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
378     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
379    
380     $w->add (my $vb = Gtk2::VBox->new);
381     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
382     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
383     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
384     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
385     my $i = 0;
386     my $act = 0;
387     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
388     if ($_ eq $selmode) { $act = $i }
389     $cb->append_text ($_);
390     $i++;
391     }
392     $cb->set_active ($act);
393     $cb->signal_connect (changed => sub {
394     my ($cb) = @_;
395     my $txt = $cb->get_active_text;
396     if ($txt =~ m/(\d+)x(\d+)/) {
397     $::CFG->{width} = $1;
398     $::CFG->{height} = $2;
399     }
400     });
401    
402     $cfg->{_i} = 1;
403     for (@cfg) {
404     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
405     }
406    
407     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
408     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
409     $cb->signal_connect (clicked => sub {
410     for (keys %$cfg) {
411     $::CFG->{$_} = $cfg->{$_}
412     if $_ ne '_i';
413     }
414 root 1.67 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
415 elmex 1.23 });
416     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
417     $cb->signal_connect (clicked => sub {
418     for (keys %$cfg) {
419     $::CFG->{$_} = $cfg->{$_}
420     if $_ ne '_i';
421     }
422     my $cb = $events{login} || sub {};
423     $cb->($::CFG->{user}, $::CFG->{password});
424     });
425     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
426     $cb->signal_connect (clicked => sub {
427 root 1.50 my $cb = $events{logout} || sub {};
428 elmex 1.23 $cb->();
429     });
430     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
431     $cb->signal_connect (clicked => sub { $w->destroy });
432    
433     $w->show_all;
434    
435     $w->signal_connect (destroy => sub { Gtk2->main_quit });
436     }
437    
438    
439 root 1.1 #############################################################################
440    
441 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
442 elmex 1.20
443 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
444 elmex 1.10
445 root 1.13 $CFG ||= {
446     width => 640,
447     height => 480,
448 elmex 1.47 mapsize => 100,
449 root 1.13 fullscreen => 0,
450     host => "crossfire.schmorp.de",
451     port => 13327,
452     };
453 elmex 1.12
454 root 1.65 {
455 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
456 root 1.65
457 root 1.67 CFClient::add_font $_ for @fonts;
458     CFClient::set_font $fonts[0];
459 root 1.65 }
460 root 1.40
461 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
462    
463 elmex 1.23 run_config_dialog
464 elmex 1.16 login => sub { start_game },
465     logout => sub { stop_game };
466 root 1.1
467 root 1.13 main Gtk2;
468 root 1.19
469 root 1.68 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";