ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/UI.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/UI.pm (file contents):
Revision 1.38 by root, Sun Apr 9 21:34:49 2006 UTC vs.
Revision 1.43 by root, Sun Apr 9 22:12:11 2006 UTC

4 4
5use Scalar::Util; 5use Scalar::Util;
6 6
7use SDL::OpenGL; 7use SDL::OpenGL;
8use SDL::OpenGL::Constants; 8use SDL::OpenGL::Constants;
9
10use Crossfire::Client;
9 11
10our $FOCUS; # the widget with current focus 12our $FOCUS; # the widget with current focus
11 13
12# class methods for events 14# class methods for events
13sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS } 15sub feed_sdl_key_down_event { $FOCUS->key_down ($_[0]) if $FOCUS }
37 Carp::confess "size_request is abtract"; 39 Carp::confess "size_request is abtract";
38} 40}
39 41
40sub size_allocate { 42sub size_allocate {
41 my ($self, $w, $h) = @_; 43 my ($self, $w, $h) = @_;
44
42 $self->w ($w); 45 $self->{w} = $w;
43 $self->h ($h); 46 $self->{h} = $h;
44} 47}
45 48
46sub focus_in { 49sub focus_in {
47 my ($widget) = @_; 50 my ($widget) = @_;
48 $FOCUS = $widget; 51 $FOCUS = $widget;
134 my ($self) = @_; 137 my ($self) = @_;
135 138
136 #$self->deactivate; 139 #$self->deactivate;
137} 140}
138 141
142#############################################################################
143
139package Crossfire::Client::Widget::Container; 144package Crossfire::Client::Widget::Container;
140 145
141our @ISA = Crossfire::Client::Widget::; 146our @ISA = Crossfire::Client::Widget::;
142 147
143sub new { 148sub new {
157 162
158 @{$self->{children}} = 163 @{$self->{children}} =
159 sort { $a->{z} <=> $b->{z} } 164 sort { $a->{z} <=> $b->{z} }
160 @{$self->{children}}, $chld; 165 @{$self->{children}}, $chld;
161 166
162 $self->size_allocate ($self->{w}, $self->{h}); 167 $self->size_allocate ($self->{w}, $self->{h})
168 if $self->{w}; #TODO: check for "realised state"
163} 169}
164 170
165sub remove { 171sub remove {
166 my ($self, $widget) = @_; 172 my ($self, $widget) = @_;
167 173
181 } 187 }
182 188
183 () 189 ()
184} 190}
185 191
186sub size_request {
187 my ($self) = @_;
188
189 my ($hs, $ws) = (0, 0);
190 for (@{$self->{children} || []}) {
191 my ($w, $h) = $_->size_request;
192 $hs += $h;
193 if ($ws < $w) { $ws = $w }
194 }
195
196 return ($ws, $hs);
197}
198
199sub _draw { 192sub _draw {
200 my ($self) = @_; 193 my ($self) = @_;
201 194
202 $_->draw for @{$self->{children}}; 195 $_->draw for @{$self->{children}};
203} 196}
204 197
198#############################################################################
199
205package Crossfire::Client::Widget::Bin; 200package Crossfire::Client::Widget::Bin;
206 201
207our @ISA = Crossfire::Client::Widget::Container::; 202our @ISA = Crossfire::Client::Widget::Container::;
208 203
209sub get { $_[0]->{children}[0] } 204sub child { $_[0]->{children}[0] }
210 205
211sub size_request { 206sub size_request {
212 $_[0]{children}[0]->size_request if $_[0]{children}[0]; 207 $_[0]{children}[0]->size_request if $_[0]{children}[0];
213} 208}
214 209
215sub size_allocate { 210sub size_allocate {
216 my ($self, $w, $h) = @_; 211 my ($self, $w, $h) = @_;
212
217 $self->SUPER::size_allocate ($w, $h); 213 $self->SUPER::size_allocate ($w, $h);
218 $self->{children}[0]->size_allocate ($w, $h) 214 $self->{children}[0]->size_allocate ($w, $h)
219 if $self->{children}[0] 215 if $self->{children}[0]
220} 216}
221 217
218#############################################################################
219
222package Crossfire::Client::Widget::Toplevel; 220package Crossfire::Client::Widget::Toplevel;
223 221
224our @ISA = Crossfire::Client::Widget::Container::; 222our @ISA = Crossfire::Client::Widget::Container::;
225 223
226sub update { 224sub update {
227 my ($self) = @_; 225 my ($self) = @_;
228 226
229 ::refresh (); 227 ::refresh ();
230} 228}
231 229
230sub add {
231 my ($self, $widget) = @_;
232
233 $self->SUPER::add ($widget);
234
235 $widget->size_allocate ($widget->size_request);
236}
237
238#############################################################################
239
232package Crossfire::Client::Widget::Window; 240package Crossfire::Client::Widget::Window;
233 241
234our @ISA = Crossfire::Client::Widget::Bin::; 242our @ISA = Crossfire::Client::Widget::Bin::;
235 243
236use SDL::OpenGL; 244use SDL::OpenGL;
237 245
238sub add { 246sub new {
239 my ($self, $chld) = @_; 247 my ($class, $x, $y, $z, $w, $h) = @_;
240 warn "ADD $chld\n";
241 $self->SUPER::add ($chld);
242 $chld->set_parent ($self);
243}
244 248
245sub remove { 249 my $self = $class->SUPER::new;
246 my ($self) = @_; 250
247 # TODO FIXME: removing a child from a window will crash, see render_chld 251 @$self{qw(x y z w h)} = ($x, $y, $z, $w, $h);
248 # $self->update;
249} 252}
250 253
251sub update { 254sub update {
252 my ($self) = @_; 255 my ($self) = @_;
256
253 $self->render_chld; 257 $self->render_chld;
258 $self->SUPER::update;
254} 259}
255 260
256sub render_chld { 261sub render_chld {
257 my ($self) = @_; 262 my ($self) = @_;
258 my $chld = $self->get;
259 my ($w, $h) = $self->size_request;
260 263
261 require Carp;
262 Carp::cluck "RENDERCHI $w $h";
263 warn "RENDERCHI $w $h\n";
264 $self->{texture} = 264 $self->{texture} =
265 Crossfire::Client::Texture->new_from_opengl ( 265 Crossfire::Client::Texture->new_from_opengl (
266 $w, $h, sub { $chld->draw } 266 $self->{w}, $self->{h}, sub { $self->child->draw }
267 ); 267 );
268 $self->{texture}->upload;
269}
270
271sub size_request {
272 my ($self) = @_;
273 ($self->w, $self->h)
274} 268}
275 269
276sub size_allocate { 270sub size_allocate {
277 my ($self, $w, $h) = @_; 271 my ($self, $w, $h) = @_;
278 272
279 $self->w ($w); 273 $self->{w} = $w;
280 $self->h ($h); 274 $self->{h} = $h;
275
281 $self->get->size_allocate ($w, $h); 276 $self->child->size_allocate ($w, $h);
282 277
283 $self->update; #TODO: Move this to the size_request event propably? 278 $self->render_chld;
284} 279}
285 280
286sub _draw { 281sub _draw {
287 my ($self) = @_; 282 my ($self) = @_;
288 283
305 300
306 glDisable GL_BLEND; 301 glDisable GL_BLEND;
307 glDisable GL_TEXTURE_2D; 302 glDisable GL_TEXTURE_2D;
308} 303}
309 304
305#############################################################################
306
310package Crossfire::Client::Widget::Frame; 307package Crossfire::Client::Widget::Frame;
311 308
312our @ISA = Crossfire::Client::Widget::Bin::; 309our @ISA = Crossfire::Client::Widget::Bin::;
313 310
314use SDL::OpenGL; 311use SDL::OpenGL;
315 312
316sub size_request { 313sub size_request {
317 my ($self) = @_; 314 my ($self) = @_;
318 my $chld = $self->get 315 my $chld = $self->child
319 or return (0, 0); 316 or return (0, 0);
320 317
321 $chld->move (2, 2); 318 $chld->move (2, 2);
322 319
323 map { $_ + 4 } $chld->size_request; 320 map { $_ + 4 } $chld->size_request;
324} 321}
325 322
326sub size_allocate { 323sub size_allocate {
327 my ($self, $w, $h) = @_; 324 my ($self, $w, $h) = @_;
328 325
329 $self->w ($w); 326 $self->{w} = $w;
330 $self->h ($h); 327 $self->{h} = $h;
331 328
332 $self->get->size_allocate ($w - 4, $h - 4); 329 $self->child->size_allocate ($w - 4, $h - 4);
333 $self->get->move (2, 2); 330 $self->child->move (2, 2);
334} 331}
335 332
336sub _draw { 333sub _draw {
337 my ($self) = @_; 334 my ($self) = @_;
338 335
339 my $chld = $self->get; 336 my $chld = $self->child;
340 337
341 my ($w, $h) = $chld->size_request; 338 my ($w, $h) = $chld->size_request;
342 339
343 glBegin GL_QUADS; 340 glBegin GL_QUADS;
344 glColor 0, 0, 0; 341 glColor 0, 0, 0;
349 glEnd; 346 glEnd;
350 347
351 $chld->draw; 348 $chld->draw;
352} 349}
353 350
351#############################################################################
352
354package Crossfire::Client::Widget::FancyFrame; 353package Crossfire::Client::Widget::FancyFrame;
355 354
356our @ISA = Crossfire::Client::Widget::Frame::; 355our @ISA = Crossfire::Client::Widget::Bin::;
357 356
358use SDL::OpenGL; 357use SDL::OpenGL;
359 358
360sub new { 359my @tex =
361 my ($self, $theme) = @_;
362 $self = $self->SUPER::new;
363
364 $self->{txts} = [
365 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ } 360 map { new_from_file Crossfire::Client::Texture Crossfire::Client::find_rcfile $_ }
366 qw/d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png/ 361 qw(d1_bg.png d1_border_top.png d1_border_right.png d1_border_left.png d1_border_bottom.png);
367 ];
368 $self
369}
370 362
371sub size_request { 363sub size_request {
372 my ($self) = @_; 364 my ($self) = @_;
365
373 my ($w, $h) = $self->get->size_request; 366 my ($w, $h) = $self->SUPER::size_request;
374 367
375 $h += $self->{txts}->[1]->{height}; 368 $h += $tex[1]->{height};
376 $h += $self->{txts}->[4]->{height}; 369 $h += $tex[4]->{height};
377 $w += $self->{txts}->[2]->{width}; 370 $w += $tex[2]->{width};
378 $w += $self->{txts}->[3]->{width}; 371 $w += $tex[3]->{width};
379 372
380 ($w, $h) 373 ($w, $h)
381} 374}
382 375
383sub size_allocate { 376sub size_allocate {
384 my ($self, $w, $h) = @_; 377 my ($self, $w, $h) = @_;
385 378
386 $self->w ($w); 379 $self->SUPER::size_allocate ($w, $h);
387 $self->h ($h); 380
388 $h -= $self->{txts}->[1]->{height}; 381 $h -= $tex[1]->{height};
389 $h -= $self->{txts}->[4]->{height}; 382 $h -= $tex[4]->{height};
390 $w -= $self->{txts}->[2]->{width}; 383 $w -= $tex[2]->{width};
391 $w -= $self->{txts}->[3]->{width}; 384 $w -= $tex[3]->{width};
392 385
393 $h = $h < 0 ? 0 : $h; 386 $h = $h < 0 ? 0 : $h;
394 $w = $w < 0 ? 0 : $w; 387 $w = $w < 0 ? 0 : $w;
395 warn "CHILD:$w $h\n"; 388
396 $self->get->size_allocate ($w, $h); 389 $self->child->size_allocate ($w, $h);
397 $self->get->move ($self->{txts}->[3]->{width}, $self->{txts}->[1]->{height}); 390 $self->child->move ($tex[3]->{width}, $tex[1]->{height});
398} 391}
399 392
400sub _draw { 393sub _draw {
401 my ($self) = @_; 394 my ($self) = @_;
402 395
403 my ($w, $h) = ($self->w, $self->h); 396 my ($w, $h) = ($self->{w}, $self->{h});
404 my ($cw, $ch) = ($self->get->w, $self->get->h); 397 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
405 398
406 glEnable GL_BLEND; 399 glEnable GL_BLEND;
407 glEnable GL_TEXTURE_2D; 400 glEnable GL_TEXTURE_2D;
408 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 401 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
409 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 402 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
410 403
411 my $top = $self->{txts}->[1]; 404 my $top = $tex[1];
412 glBindTexture GL_TEXTURE_2D, $top->{name}; 405 glBindTexture GL_TEXTURE_2D, $top->{name};
413 406
414 glBegin GL_QUADS; 407 glBegin GL_QUADS;
415 glTexCoord 0, 0; glVertex 0 , 0; 408 glTexCoord 0, 0; glVertex 0 , 0;
416 glTexCoord 0, 1; glVertex 0 , $top->{height}; 409 glTexCoord 0, 1; glVertex 0 , $top->{height};
417 glTexCoord 1, 1; glVertex $w , $top->{height}; 410 glTexCoord 1, 1; glVertex $w , $top->{height};
418 glTexCoord 1, 0; glVertex $w , 0; 411 glTexCoord 1, 0; glVertex $w , 0;
419 glEnd; 412 glEnd;
420 413
421 my $left = $self->{txts}->[3]; 414 my $left = $tex[3];
422 glBindTexture GL_TEXTURE_2D, $left->{name}; 415 glBindTexture GL_TEXTURE_2D, $left->{name};
423 416
424 glBegin GL_QUADS; 417 glBegin GL_QUADS;
425 glTexCoord 0, 0; glVertex 0 , $top->{height}; 418 glTexCoord 0, 0; glVertex 0 , $top->{height};
426 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch; 419 glTexCoord 0, 1; glVertex 0 , $top->{height} + $ch;
427 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch; 420 glTexCoord 1, 1; glVertex $left->{width}, $top->{height} + $ch;
428 glTexCoord 1, 0; glVertex $left->{width}, $top->{height}; 421 glTexCoord 1, 0; glVertex $left->{width}, $top->{height};
429 glEnd; 422 glEnd;
430 423
431 my $right = $self->{txts}->[2]; 424 my $right = $tex[2];
432 glBindTexture GL_TEXTURE_2D, $right->{name}; 425 glBindTexture GL_TEXTURE_2D, $right->{name};
433 426
434 glBegin GL_QUADS; 427 glBegin GL_QUADS;
435 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height}; 428 glTexCoord 0, 0; glVertex $w - $right->{width}, $top->{height};
436 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch; 429 glTexCoord 0, 1; glVertex $w - $right->{width}, $top->{height} + $ch;
437 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch; 430 glTexCoord 1, 1; glVertex $w , $top->{height} + $ch;
438 glTexCoord 1, 0; glVertex $w , $top->{height}; 431 glTexCoord 1, 0; glVertex $w , $top->{height};
439 glEnd; 432 glEnd;
440 433
441 my $bottom = $self->{txts}->[4]; 434 my $bottom = $tex[4];
442 glBindTexture GL_TEXTURE_2D, $bottom->{name}; 435 glBindTexture GL_TEXTURE_2D, $bottom->{name};
443 436
444 glBegin GL_QUADS; 437 glBegin GL_QUADS;
445 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height}; 438 glTexCoord 0, 0; glVertex 0 , $h - $bottom->{height};
446 glTexCoord 0, 1; glVertex 0 , $h; 439 glTexCoord 0, 1; glVertex 0 , $h;
447 glTexCoord 1, 1; glVertex $w , $h; 440 glTexCoord 1, 1; glVertex $w , $h;
448 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height}; 441 glTexCoord 1, 0; glVertex $w , $h - $bottom->{height};
449 glEnd; 442 glEnd;
450 443
451 my $bg = $self->{txts}->[0]; 444 my $bg = $tex[0];
452 glBindTexture GL_TEXTURE_2D, $bg->{name}; 445 glBindTexture GL_TEXTURE_2D, $bg->{name};
453 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 446 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
454 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT; 447 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT;
455 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT; 448 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT;
456 449
465 glEnd; 458 glEnd;
466 459
467 glDisable GL_BLEND; 460 glDisable GL_BLEND;
468 glDisable GL_TEXTURE_2D; 461 glDisable GL_TEXTURE_2D;
469 462
470 $self->get->draw; 463 $self->child->draw;
471 464
472} 465}
466
467#############################################################################
473 468
474package Crossfire::Client::Widget::Table; 469package Crossfire::Client::Widget::Table;
475 470
476our @ISA = Crossfire::Client::Widget::Bin::; 471our @ISA = Crossfire::Client::Widget::Bin::;
477 472
554 549
555 $y += $self->max_row_height ($yi); 550 $y += $self->max_row_height ($yi);
556 } 551 }
557} 552}
558 553
554#############################################################################
555
559package Crossfire::Client::Widget::VBox; 556package Crossfire::Client::Widget::VBox;
560 557
561our @ISA = Crossfire::Client::Widget::Container::; 558our @ISA = Crossfire::Client::Widget::Container::;
562 559
563use SDL::OpenGL; 560use SDL::OpenGL;
561
562sub size_request {
563 my ($self) = @_;
564
565 my @alloc = map [$_->size_request], @{$self->{children}};
566
567 (
568 (List::Util::max map $_->[0], @alloc),
569 (List::Util::sum map $_->[1], @alloc),
570 )
571}
564 572
565sub size_allocate { 573sub size_allocate {
566 my ($self, $w, $h) = @_; 574 my ($self, $w, $h) = @_;
567 575
568 $self->w ($w); 576 $self->w ($w);
601 $y += $h; 609 $y += $h;
602 } 610 }
603 } 611 }
604} 612}
605 613
606sub _draw { 614#############################################################################
607 my ($self) = @_;
608
609 my ($x, $y);
610 for (@{$self->{children} || []}) {
611 $_->draw;
612 $y += $_->h;
613 }
614}
615 615
616package Crossfire::Client::Widget::Label; 616package Crossfire::Client::Widget::Label;
617 617
618our @ISA = Crossfire::Client::Widget::; 618our @ISA = Crossfire::Client::Widget::;
619 619
676 676
677 glDisable GL_BLEND; 677 glDisable GL_BLEND;
678 glDisable GL_TEXTURE_2D; 678 glDisable GL_TEXTURE_2D;
679} 679}
680 680
681#############################################################################
682
681package Crossfire::Client::Widget::TextEntry; 683package Crossfire::Client::Widget::TextEntry;
682 684
683our @ISA = Crossfire::Client::Widget::Label::; 685our @ISA = Crossfire::Client::Widget::Label::;
684 686
685use SDL; 687use SDL;
702 } elsif ($uni) { 704 } elsif ($uni) {
703 $text .= chr $uni; 705 $text .= chr $uni;
704 } 706 }
705 $self->set_text ($text); 707 $self->set_text ($text);
706} 708}
709
710#############################################################################
707 711
708package Crossfire::Client::Widget::MapWidget; 712package Crossfire::Client::Widget::MapWidget;
709 713
710use strict; 714use strict;
711 715
878 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) { 882 if (!($mod & KMOD_CTRL ) && delete $self->{ctrl}) {
879 $::CONN->send ("command run_stop"); 883 $::CONN->send ("command run_stop");
880 } 884 }
881} 885}
882 886
887#############################################################################
888
883package Crossfire::Client::Widget::Animator; 889package Crossfire::Client::Widget::Animator;
884 890
885use SDL::OpenGL; 891use SDL::OpenGL;
886 892
887our @ISA = Crossfire::Client::Widget::Bin::; 893our @ISA = Crossfire::Client::Widget::Bin::;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines