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.72 by root, Tue Apr 11 18:00:45 2006 UTC vs.
Revision 1.75 by root, Tue Apr 11 20:44:49 2006 UTC

1package CFClient::Widget; 1package CFClient::UI;
2 2
3use strict; 3use strict;
4 4
5use Scalar::Util; 5use Scalar::Util ();
6 6use List::Util ();
7use SDL::OpenGL;
8use SDL::OpenGL::Constants;
9 7
10use CFClient; 8use CFClient;
11 9
12our ($FOCUS, $HOVER, $GRAB); # various widgets 10our ($FOCUS, $HOVER, $GRAB); # various widgets
13 11
70 } 68 }
71 69
72 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER; 70 $HOVER->mouse_motion ($ev, $HOVER->translate ($x, $y)) if $HOVER;
73} 71}
74 72
73#############################################################################
74
75package CFClient::UI::Base;
76
77use strict;
78
79use SDL::OpenGL;
80
75sub new { 81sub new {
76 my $class = shift; 82 my $class = shift;
77 83
78 bless { 84 bless {
79 x => 0, 85 x => 0,
80 y => 0, 86 y => 0,
81 z => 0, 87 z => 0,
88 w => -1,
89 h => -1,
82 @_ 90 @_
83 }, $class 91 }, $class
84} 92}
85 93
86sub move { 94sub move {
97sub size_request { 105sub size_request {
98 require Carp; 106 require Carp;
99 Carp::confess "size_request is abtract"; 107 Carp::confess "size_request is abtract";
100} 108}
101 109
102sub size_allocate { 110sub _size_allocate {
103 my ($self, $w, $h) = @_; 111 my ($self, $x, $y, $w, $h) = @_;
112
113 $self->{x} = $x;
114 $self->{y} = $y;
115
116 return unless $self->{w} != $w || $self->{h} != $h;
104 117
105 $self->{w} = $w; 118 $self->{w} = $w;
106 $self->{h} = $h; 119 $self->{h} = $h;
120
121 1
122}
123
124sub size_allocate {
125 my ($self, $x, $y, $w, $h) = @_;
126
127 $self->_size_allocate ($x, $y, $w, $h);
107} 128}
108 129
109# translate global koordinates to local coordinate system 130# translate global koordinates to local coordinate system
110sub translate { 131sub translate {
111 my ($self, $x, $y) = @_; 132 my ($self, $x, $y) = @_;
158 glTranslate $self->{x}, $self->{y}, 0; 179 glTranslate $self->{x}, $self->{y}, 0;
159 $self->_draw; 180 $self->_draw;
160 glPopMatrix; 181 glPopMatrix;
161 182
162 if ($self == $HOVER) { 183 if ($self == $HOVER) {
163 my ($x, $y) = @$self->{qw(x y)}; 184 my ($x, $y) = @$self{qw(x y)};
164 185
165 glColor 1, 1, 1, 0.1; 186 glColor 1, 1, 1, 0.1;
166 glEnable GL_BLEND; 187 glEnable GL_BLEND;
188 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
167 glBegin GL_QUADS; 189 glBegin GL_QUADS;
168 glVertex $x , $y; 190 glVertex $x , $y;
169 glVertex $x + $self->{w}, $y; 191 glVertex $x + $self->{w}, $y;
170 glVertex $x + $self->{w}, $y + $self->{h}; 192 glVertex $x + $self->{w}, $y + $self->{h};
171 glVertex $x , $y + $self->{h}; 193 glVertex $x , $y + $self->{h};
240 #$self->deactivate; 262 #$self->deactivate;
241} 263}
242 264
243############################################################################# 265#############################################################################
244 266
245package CFClient::Widget::DrawBG; 267package CFClient::UI::DrawBG;
246 268
247our @ISA = CFClient::Widget::; 269our @ISA = CFClient::UI::Base::;
248 270
249use strict; 271use strict;
250use SDL::OpenGL; 272use SDL::OpenGL;
251 273
252sub new { 274sub new {
275 glEnd; 297 glEnd;
276} 298}
277 299
278############################################################################# 300#############################################################################
279 301
280package CFClient::Widget::Empty; 302package CFClient::UI::Empty;
281 303
282our @ISA = CFClient::Widget::; 304our @ISA = CFClient::UI::Base::;
283 305
284sub size_request { 306sub size_request {
285 (0, 0) 307 (0, 0)
286} 308}
287 309
288sub draw { } 310sub draw { }
289 311
290############################################################################# 312#############################################################################
291 313
292package CFClient::Widget::Container; 314package CFClient::UI::Container;
293 315
294our @ISA = CFClient::Widget::; 316our @ISA = CFClient::UI::Base::;
295 317
296sub new { 318sub new {
297 my ($class, %arg) = @_; 319 my ($class, %arg) = @_;
298 320
299 my $children = delete $arg{children} || []; 321 my $children = delete $arg{children} || [];
313 $self->{children} = [ 335 $self->{children} = [
314 sort { $a->{z} <=> $b->{z} } 336 sort { $a->{z} <=> $b->{z} }
315 @{$self->{children}}, $chld 337 @{$self->{children}}, $chld
316 ]; 338 ];
317 339
318 $self->size_allocate ($self->{w}, $self->{h}) 340 $self->{w} = $self->{h} = -1;
319 if $self->{w}; #TODO: check for "realised state" 341 $self->update;
320} 342}
321 343
322sub remove { 344sub remove {
323 my ($self, $widget) = @_; 345 my ($self, $widget) = @_;
324 346
325 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ]; 347 $self->{children} = [ grep $_ != $widget, @{ $self->{children} } ];
326 348
327 $self->size_allocate ($self->{w}, $self->{h}); 349 $self->size_allocate (0, 0, $self->{w}, $self->{h});
328} 350}
329 351
330sub find_widget { 352sub find_widget {
331 my ($self, $x, $y) = @_; 353 my ($self, $x, $y) = @_;
332 354
349 $_->draw for @{$self->{children}}; 371 $_->draw for @{$self->{children}};
350} 372}
351 373
352############################################################################# 374#############################################################################
353 375
354package CFClient::Widget::Bin; 376package CFClient::UI::Bin;
355 377
356our @ISA = CFClient::Widget::Container::; 378our @ISA = CFClient::UI::Container::;
357 379
358sub new { 380sub new {
359 my ($class, %arg) = @_; 381 my ($class, %arg) = @_;
360 382
361 my $child = (delete $arg{child}) || new CFClient::Widget::Empty::; 383 my $child = (delete $arg{child}) || new CFClient::UI::Empty::;
362 384
363 $class->SUPER::new (children => [$child], %arg) 385 $class->SUPER::new (children => [$child], %arg)
364} 386}
365 387
366sub add { 388sub add {
374sub remove { 396sub remove {
375 my ($self, $widget) = @_; 397 my ($self, $widget) = @_;
376 398
377 $self->SUPER::remove ($widget); 399 $self->SUPER::remove ($widget);
378 400
379 $self->{children} = [new CFClient::Widget::Empty] 401 $self->{children} = [new CFClient::UI::Empty]
380 unless @{$self->{children}}; 402 unless @{$self->{children}};
381} 403}
382 404
383sub child { $_[0]->{children}[0] } 405sub child { $_[0]->{children}[0] }
384 406
385sub size_request { 407sub size_request {
386 $_[0]{children}[0]->size_request 408 $_[0]{children}[0]->size_request
387} 409}
388 410
389sub size_allocate { 411sub size_allocate {
390 my ($self, $w, $h) = @_; 412 my ($self, $x, $y, $w, $h) = @_;
391 413
392 return unless $self->{w} != $w || $self->{h} != $h; 414 $self->_size_allocate ($x, $y, $w, $h) or return;
393 415
394 $self->SUPER::size_allocate ($w, $h);
395 $self->{children}[0]->size_allocate ($w, $h); 416 $self->{children}[0]->size_allocate (0, 0, $w, $h);
396} 417}
397 418
398############################################################################# 419#############################################################################
399 420
400package CFClient::Widget::Window; 421package CFClient::UI::Window;
401 422
402our @ISA = CFClient::Widget::Bin::; 423our @ISA = CFClient::UI::Bin::;
403 424
404use SDL::OpenGL; 425use SDL::OpenGL;
405 426
406sub new { 427sub new {
407 my ($class, %arg) = @_; 428 my ($class, %arg) = @_;
425 $self->{w}, $self->{h}, sub { $self->child->draw } 446 $self->{w}, $self->{h}, sub { $self->child->draw }
426 ); 447 );
427} 448}
428 449
429sub size_allocate { 450sub size_allocate {
430 my ($self, $w, $h) = @_; 451 my ($self, $x, $y, $w, $h) = @_;
431 452
432 return unless $self->{w} != $w || $self->{h} != $h; 453 $self->_size_allocate ($x, $y, $w, $h) or return;
433 454
434 $self->{w} = $w;
435 $self->{h} = $h;
436
437 $self->child->size_allocate ($w, $h); 455 $self->child->size_allocate (0, 0, $w, $h);
438 456
439 $self->render_chld; 457 $self->render_chld;
440} 458}
441 459
442sub _draw { 460sub _draw {
446 464
447 my $tex = $self->{texture} 465 my $tex = $self->{texture}
448 or return; 466 or return;
449 467
450 glEnable GL_BLEND; 468 glEnable GL_BLEND;
469 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
451 glEnable GL_TEXTURE_2D; 470 glEnable GL_TEXTURE_2D;
452 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 471 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
453 472
454 $tex->draw_quad (0, 0, $w, $h); 473 $tex->draw_quad (0, 0, $w, $h);
455 474
457 glDisable GL_TEXTURE_2D; 476 glDisable GL_TEXTURE_2D;
458} 477}
459 478
460############################################################################# 479#############################################################################
461 480
462package CFClient::Widget::Frame; 481package CFClient::UI::Frame;
463 482
464our @ISA = CFClient::Widget::Bin::; 483our @ISA = CFClient::UI::Bin::;
465 484
466use SDL::OpenGL; 485use SDL::OpenGL;
467 486
468sub size_request { 487sub size_request {
469 my ($self) = @_; 488 my ($self) = @_;
474 493
475 map { $_ + 4 } $chld->size_request; 494 map { $_ + 4 } $chld->size_request;
476} 495}
477 496
478sub size_allocate { 497sub size_allocate {
479 my ($self, $w, $h) = @_; 498 my ($self, $x, $y, $w, $h) = @_;
480 499
481 return unless $self->{w} != $w || $self->{h} != $h; 500 $self->_size_allocate ($x, $y, $w, $h) or return;
482 501
483 $self->{w} = $w;
484 $self->{h} = $h;
485
486 $self->child->size_allocate ($w - 4, $h - 4); 502 $self->child->size_allocate (2, 2, $w - 4, $h - 4);
487 $self->child->move (2, 2);
488} 503}
489 504
490sub _draw { 505sub _draw {
491 my ($self) = @_; 506 my ($self) = @_;
492 507
505 $chld->draw; 520 $chld->draw;
506} 521}
507 522
508############################################################################# 523#############################################################################
509 524
510package CFClient::Widget::FancyFrame; 525package CFClient::UI::FancyFrame;
511 526
512our @ISA = CFClient::Widget::Bin::; 527our @ISA = CFClient::UI::Bin::;
513 528
514use SDL::OpenGL; 529use SDL::OpenGL;
515 530
516my @tex = 531my @tex =
517 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ } 532 map { new_from_file CFClient::Texture CFClient::find_rcfile $_ }
529 544
530 ($w, $h) 545 ($w, $h)
531} 546}
532 547
533sub size_allocate { 548sub size_allocate {
534 my ($self, $w, $h) = @_; 549 my ($self, $x, $y, $w, $h) = @_;
535 550
536 return unless $self->{w} != $w || $self->{h} != $h; 551 $self->_size_allocate ($x, $y, $w, $h) or return;
537
538 $self->SUPER::size_allocate ($w, $h);
539 552
540 $h -= $tex[1]->{h}; 553 $h -= $tex[1]->{h};
541 $h -= $tex[4]->{h}; 554 $h -= $tex[4]->{h};
542 $w -= $tex[2]->{w}; 555 $w -= $tex[2]->{w};
543 $w -= $tex[3]->{w}; 556 $w -= $tex[3]->{w};
545 $h = $h < 0 ? 0 : $h; 558 $h = $h < 0 ? 0 : $h;
546 $w = $w < 0 ? 0 : $w; 559 $w = $w < 0 ? 0 : $w;
547 560
548 my $child = $self->child; 561 my $child = $self->child;
549 562
550 $child->size_allocate ($w, $h);
551 $child->move ($tex[3]->{w}, $tex[1]->{h}); 563 $child->size_allocate ($tex[3]->{w}, $tex[1]->{h}, $w, $h);
552} 564}
553 565
554sub _draw { 566sub _draw {
555 my ($self) = @_; 567 my ($self) = @_;
556 568
557 my ($w, $h) = ($self->{w}, $self->{h}); 569 my ($w, $h) = ($self->{w}, $self->{h});
558 my ($cw, $ch) = ($self->child->{w}, $self->child->{h}); 570 my ($cw, $ch) = ($self->child->{w}, $self->child->{h});
559 571
560 glEnable GL_BLEND; 572 glEnable GL_BLEND;
573 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
561 glEnable GL_TEXTURE_2D; 574 glEnable GL_TEXTURE_2D;
562 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
563 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 575 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
564 576
565 my $top = $tex[1]; 577 my $top = $tex[1];
566 $top->draw_quad (0, 0, $w, $top->{h}); 578 $top->draw_quad (0, 0, $w, $top->{h});
567 579
592 604
593} 605}
594 606
595############################################################################# 607#############################################################################
596 608
597package CFClient::Widget::Table; 609package CFClient::UI::Table;
598 610
599our @ISA = CFClient::Widget::; 611our @ISA = CFClient::UI::Base::;
612
613use List::Util qw(max sum);
600 614
601use SDL::OpenGL; 615use SDL::OpenGL;
602 616
603sub add { 617sub add {
604 my ($self, $x, $y, $chld) = @_; 618 my ($self, $x, $y, $chld) = @_;
605 my $old_chld = $self->{children}[$y][$x];
606 619
607 $self->{children}[$y][$x] = $chld; 620 $self->{children}[$y][$x] = $chld;
608 $chld->set_parent ($self); 621 $chld->set_parent ($self);
622
623 $self->{w} = $self->{h} = -1;
609 $self->update; 624 $self->update;
610} 625}
611 626
612sub max_row_height { 627sub get_wh {
613 my ($self, $row) = @_; 628 my ($self) = @_;
614 629
615 my $hs = 0; 630 my (@w, @h);
616 for (my $xi = 0; $xi <= $#{$self->{children}->[$row] || []}; $xi++) { 631
632 for my $y (0 .. $#{$self->{children}}) {
617 my $c = $self->{children}->[$row]->[$xi]; 633 my $row = $self->{children}[$y]
618 if ($c) { 634 or next;
635
636 for my $x (0 .. $#$row) {
637 my $widget = $row->[$x]
638 or next;
619 my ($w, $h) = $c->size_request; 639 my ($w, $h) = $widget->size_request;
620 if ($hs < $h) { $hs = $h } 640
641 $w[$x] = max $w[$x], $w;
642 $h[$y] = max $h[$y], $h;
621 } 643 }
622 } 644 }
623 return $hs;
624}
625 645
626sub max_col_width { 646 (\@w, \@h)
647}
648
649sub size_request {
627 my ($self, $col) = @_; 650 my ($self) = @_;
628 651
652 my ($ws, $hs) = $self->get_wh;
653
654 (
655 (List::Util::sum @$ws),
656 (List::Util::sum @$hs),
657 )
658}
659
660sub size_allocate {
661 my ($self, $x, $y, $w, $h) = @_;
662
663 $self->_size_allocate ($x, $y, $w, $h) or return;
664
665 my ($ws, $hs) = $self->get_wh;
666
667 my $req_w = List::Util::sum @$ws;
668 my $req_h = List::Util::sum @$hs;
669
670 # linearly scale sizes
671 $_ *= $req_w / $w for @$ws;
672 $_ *= $req_h / $h for @$hs;
673
674 my $y;
675
676 for my $r (0 .. $#{$self->{children}}) {
677 my $row = $self->{children}[$r]
678 or next;
679
629 my $ws = 0; 680 my $x = 0;
630 for (my $yi = 0; $yi <= $#{$self->{children} || []}; $yi++) { 681 my $row_h = $hs->[$r];
631 my $c = ($self->{children}->[$yi] || [])->[$col]; 682
632 if ($c) { 683 for my $c (0 .. $#$row) {
633 my ($w, $h) = $c->size_request; 684 my $widget = $row->[$c]
634 if ($ws < $w) { $ws = $w } 685 or next;
686
687 my $col_w = $ws->[$c];
688
689 $widget->size_allocate ($x, $y, $col_w, $row_h);
690
691 $x += $col_w;
635 } 692 }
636 }
637 return $ws;
638}
639 693
640sub size_request { 694 $y += $row_h;
641 my ($self) = @_;
642
643 my ($hs, $ws) = (0, 0);
644
645 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
646 $hs += $self->max_row_height ($yi);
647 } 695 }
648 696
649 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) {
650 my $wm = 0;
651 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
652 $wm += $self->max_col_width ($xi)
653 }
654 if ($ws < $wm) { $ws = $wm }
655 }
656
657 return ($ws, $hs);
658} 697}
659 698
660sub _draw { 699sub _draw {
661 my ($self) = @_; 700 my ($self) = @_;
662 701
663 my $y = 0; 702 for (grep $_, @{$self->{children}}) {
664 for (my $yi = 0; $yi <= $#{$self->{children}}; $yi++) { 703 $_->draw for grep $_, @$_;
665 my $x = 0;
666
667 for (my $xi = 0; $xi <= $#{$self->{children}->[$yi]}; $xi++) {
668
669 my $c = $self->{children}->[$yi]->[$xi];
670 if ($c) {
671 $c->move ($x, $y, 0); #TODO: Move to size_request
672 $c->draw if $c;
673 }
674
675 $x += $self->max_col_width ($xi);
676 }
677
678 $y += $self->max_row_height ($yi);
679 } 704 }
680} 705}
681 706
682############################################################################# 707#############################################################################
683 708
684package CFClient::Widget::VBox; 709package CFClient::UI::VBox;
685 710
686our @ISA = CFClient::Widget::Container::; 711our @ISA = CFClient::UI::Container::;
687 712
688use SDL::OpenGL; 713use SDL::OpenGL;
689 714
690sub size_request { 715sub size_request {
691 my ($self) = @_; 716 my ($self) = @_;
697 (List::Util::sum map $_->[1], @alloc), 722 (List::Util::sum map $_->[1], @alloc),
698 ) 723 )
699} 724}
700 725
701sub size_allocate { 726sub size_allocate {
702 my ($self, $w, $h) = @_; 727 my ($self, $x, $y, $w, $h) = @_;
703 728
704 return unless $self->{w} != $w || $self->{h} != $h; 729 $self->_size_allocate ($x, $y, $w, $h) or return;
705
706 $self->{w} = $w;
707 $self->{h} = $h;
708 730
709 return unless $self->{h}; 731 return unless $self->{h};
710 732
711 my $children = $self->{children}; 733 my $children = $self->{children};
712 734
734 756
735 my $y = 0; 757 my $y = 0;
736 for (0 .. $#$children) { 758 for (0 .. $#$children) {
737 my $child = $children->[$_]; 759 my $child = $children->[$_];
738 my $h = $h[$_]; 760 my $h = $h[$_];
739 $child->move (0, $y);
740 $child->size_allocate ($w, $h); 761 $child->size_allocate (0, $y, $w, $h);
741 762
742 $y += $h; 763 $y += $h;
743 } 764 }
744} 765}
745 766
746############################################################################# 767#############################################################################
747 768
748package CFClient::Widget::Label; 769package CFClient::UI::Label;
749 770
750our @ISA = CFClient::Widget::; 771our @ISA = CFClient::UI::Base::;
751 772
752use SDL::OpenGL; 773use SDL::OpenGL;
753 774
754sub new { 775sub new {
755 my ($class, %arg) = @_; 776 my ($class, %arg) = @_;
811# ($w, $h) 832# ($w, $h)
812# } 833# }
813} 834}
814 835
815sub size_allocate { 836sub size_allocate {
816 my ($self, $w, $h) = @_; 837 my ($self, $x, $y, $w, $h) = @_;
817 838
818 return unless $self->{w} != $w || $self->{h} != $h; 839 $self->_size_allocate ($x, $y, $w, $h) or return;
819 840
820 $self->SUPER::size_allocate ($w, $h);
821 delete $self->{texture}; 841 delete $self->{texture};
822} 842}
823 843
824sub update { 844sub update {
825 my ($self) = @_; 845 my ($self) = @_;
831sub _draw { 851sub _draw {
832 my ($self) = @_; 852 my ($self) = @_;
833 853
834 my $tex = $self->{texture} ||= do { 854 my $tex = $self->{texture} ||= do {
835 $self->{layout}->set_width ($self->{w}); 855 $self->{layout}->set_width ($self->{w});
836 new_from_layout CFClient::Texture $self->{layout}; 856 new_from_layout CFClient::Texture $self->{layout}
837 }; 857 };
838 858
839 glEnable GL_BLEND; 859 glEnable GL_BLEND;
860 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
840 glEnable GL_TEXTURE_2D; 861 glEnable GL_TEXTURE_2D;
841 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
842 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 862 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
843 863
844 glColor @{$self->{fg}}; 864 glColor @{$self->{fg}};
845 865
846 my $x = 866 my $x =
847 $self->{align} < 0 ? 0 867 $self->{align} < 0 ? 0
848 : $self->{align} > 0 ? $self->{w} - $self->{texture}{w} 868 : $self->{align} > 0 ? $self->{w} - $tex->{w}
849 : ($self->{w} + $self->{texture}{w}) * 0.5; 869 : ($self->{w} - $tex->{w}) * 0.5;
850 870
851 $tex->draw_quad ($x, 0); 871 $tex->draw_quad ($x, 0);
852 872
873 glDisable GL_TEXTURE_2D;
853 glDisable GL_BLEND; 874 glDisable GL_BLEND;
854 glDisable GL_TEXTURE_2D;
855} 875}
856 876
857############################################################################# 877#############################################################################
858 878
859package CFClient::Widget::Entry; 879package CFClient::UI::Entry;
860 880
861our @ISA = CFClient::Widget::Label::; 881our @ISA = CFClient::UI::Label::;
862 882
863use SDL; 883use SDL;
864use SDL::OpenGL; 884use SDL::OpenGL;
865 885
866sub new { 886sub new {
901 921
902 ($w + 1, $h) # add 1 for cursor 922 ($w + 1, $h) # add 1 for cursor
903} 923}
904 924
905sub size_allocate { 925sub size_allocate {
906 my ($self, $w, $h) = @_; 926 my ($self, $x, $y, $w, $h) = @_;
907 927
908 return unless $self->{w} != $w || $self->{h} != $h;
909
910 $self->SUPER::size_allocate ($w, $h); 928 $self->SUPER::size_allocate ($x, $y, $w, $h);
911 929
912 $self->_set_text ($self->{text}); 930 $self->_set_text ($self->{text});
913} 931}
914 932
915sub set_text { 933sub set_text {
1006 } 1024 }
1007} 1025}
1008 1026
1009############################################################################# 1027#############################################################################
1010 1028
1011package CFClient::Widget::Slider; 1029package CFClient::UI::Slider;
1012 1030
1013use strict; 1031use strict;
1014 1032
1015use SDL::OpenGL; 1033use SDL::OpenGL;
1016use SDL::OpenGL::Constants;
1017 1034
1018our @ISA = CFClient::Widget::DrawBG::; 1035our @ISA = CFClient::UI::DrawBG::;
1019 1036
1020sub size_request { 1037sub size_request {
1021 my ($self) = @_; 1038 my ($self) = @_;
1022 1039
1023 my $w = 50; 1040 my $w = 50;
1120 glEnd; 1137 glEnd;
1121} 1138}
1122 1139
1123############################################################################# 1140#############################################################################
1124 1141
1125package CFClient::Widget::MapWidget; 1142package CFClient::UI::MapWidget;
1126 1143
1127use strict; 1144use strict;
1128 1145
1129use List::Util qw(min max); 1146use List::Util qw(min max);
1130 1147
1131use SDL; 1148use SDL;
1132use SDL::OpenGL; 1149use SDL::OpenGL;
1133use SDL::OpenGL::Constants;
1134 1150
1135our @ISA = CFClient::Widget::; 1151our @ISA = CFClient::UI::Base::;
1136 1152
1137sub new { 1153sub new {
1138 my $class = shift; 1154 my $class = shift;
1139 1155
1140 $class->SUPER::new ( 1156 $class->SUPER::new (
1160 1176
1161sub update { 1177sub update {
1162 my ($self) = @_; 1178 my ($self) = @_;
1163 1179
1164 $self->{need_update} = 1; 1180 $self->{need_update} = 1;
1181 $self->SUPER::update;
1165} 1182}
1166 1183
1167sub _draw { 1184sub _draw {
1168 my ($self) = @_; 1185 my ($self) = @_;
1169 1186
1192 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs}; 1209 $yofs = $self->{yofs} = min $my, max $my + $::CONN->{maph} - $sh + 1, $self->{yofs};
1193 } 1210 }
1194 1211
1195 glEnable GL_TEXTURE_2D; 1212 glEnable GL_TEXTURE_2D;
1196 glEnable GL_BLEND; 1213 glEnable GL_BLEND;
1214 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1197 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; 1215 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1198 1216
1199 my $sw4 = ($sw + 3) & ~3; 1217 my $sw4 = ($sw + 3) & ~3;
1200 my $darkness = "\x00" x ($sw4 * $sh); 1218 my $darkness = "\x00" x ($sw4 * $sh);
1201 1219
1231# 1249#
1232# $lighting = $pb->get_pixels; 1250# $lighting = $pb->get_pixels;
1233# $lighting =~ s/(.)../$1/gs; 1251# $lighting =~ s/(.)../$1/gs;
1234# } 1252# }
1235 1253
1236 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
1237 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 1254 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1238 1255
1239 $darkness = new CFClient::Texture 1256 $darkness = new CFClient::Texture
1240 width => $sw4, 1257 w => $sw4,
1241 height => $sh, 1258 h => $sh,
1242 data => $darkness, 1259 data => $darkness,
1243 internalformat => GL_ALPHA, 1260 internalformat => GL_ALPHA,
1244 format => GL_ALPHA; 1261 format => GL_ALPHA;
1245 1262
1246 glColor 0.45, 0.45, 0.45, 1; 1263 glColor 0.45, 0.45, 0.45, 1;
1306 } 1323 }
1307} 1324}
1308 1325
1309############################################################################# 1326#############################################################################
1310 1327
1311package CFClient::Widget::Animator; 1328package CFClient::UI::Animator;
1312 1329
1313use SDL::OpenGL; 1330use SDL::OpenGL;
1314 1331
1315our @ISA = CFClient::Widget::Bin::; 1332our @ISA = CFClient::UI::Bin::;
1316 1333
1317sub moveto { 1334sub moveto {
1318 my ($self, $x, $y) = @_; 1335 my ($self, $x, $y) = @_;
1319 1336
1320 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y]; 1337 $self->{moveto} = [$self->{x}, $self->{y}, $x, $y];
1348 glPopMatrix; 1365 glPopMatrix;
1349} 1366}
1350 1367
1351############################################################################# 1368#############################################################################
1352 1369
1353package CFClient::Widget::Toplevel; 1370package CFClient::UI::Toplevel;
1354 1371
1355our @ISA = CFClient::Widget::Container::; 1372our @ISA = CFClient::UI::Container::;
1356 1373
1357sub size_request { 1374sub size_request {
1358 ($::WIDTH, $::HEIGHT) 1375 ($::WIDTH, $::HEIGHT)
1359} 1376}
1360 1377
1361sub size_allocate { 1378sub size_allocate {
1362 my ($self, $w, $h) = @_; 1379 my ($self, $x, $y, $w, $h) = @_;
1363 1380
1364 $self->SUPER::size_allocate ($w, $h); 1381 $self->_size_allocate ($x, $y, $w, $h);
1365 1382
1366 $_->size_allocate ($_->size_request) 1383 $_->size_allocate ($_->{x}, $_->{y}, $_->size_request)
1367 for @{$self->{children}}; 1384 for @{$self->{children}};
1368} 1385}
1369 1386
1370sub translate { 1387sub translate {
1371 my ($self, $x, $y) = @_; 1388 my ($self, $x, $y) = @_;
1374} 1391}
1375 1392
1376sub update { 1393sub update {
1377 my ($self) = @_; 1394 my ($self) = @_;
1378 1395
1379 $self->size_allocate ($self->size_request); 1396 $self->size_allocate (0, 0, $::WIDTH, $::HEIGHT);
1380 ::refresh (); 1397 ::refresh ();
1381} 1398}
1382 1399
1383sub add { 1400sub add {
1384 my ($self, $widget) = @_; 1401 my ($self, $widget) = @_;
1385 1402
1386 $self->SUPER::add ($widget); 1403 $self->SUPER::add ($widget);
1387 1404
1388 $widget->size_allocate ($widget->size_request); 1405 $widget->size_allocate ($widget->{x}, $widget->{y}, $widget->size_request);
1389} 1406}
1390 1407
1391sub draw { 1408sub draw {
1392 my ($self) = @_; 1409 my ($self) = @_;
1393 1410
1394 $self->_draw; 1411 $self->_draw;
1395} 1412}
1396 1413
1397############################################################################# 1414#############################################################################
1398 1415
1399package CFClient::Widget; 1416package CFClient::UI;
1400 1417
1401$TOPLEVEL = new CFClient::Widget::Toplevel; 1418$TOPLEVEL = new CFClient::UI::Toplevel;
1402 1419
14031 14201
1404 1421

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines