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.179 by elmex, Fri Apr 28 05:17:21 2006 UTC vs.
Revision 1.190 by root, Mon May 8 22:17:38 2006 UTC

237 $self->{w} = $w; 237 $self->{w} = $w;
238 $self->{h} = $h; 238 $self->{h} = $h;
239 239
240 $self->size_allocate ($w, $h); 240 $self->size_allocate ($w, $h);
241 $self->update; 241 $self->update;
242 $self->emit (size_allocate => $w, $h);
242 } 243 }
243} 244}
244 245
245sub size_allocate { 246sub size_allocate {
246 # nothing to be done 247 # nothing to be done
247} 248}
248 249
249sub children { 250sub children {
250} 251}
251 252
252# call when resoltuion changes etc. 253# call when resolution changes etc.
253sub reconfigure { 254sub reconfigure {
254 my ($self) = @_; 255 my ($self) = @_;
255 256
256 $_->reconfigure 257 $_->reconfigure
257 for $self->children; 258 for $self->children;
258 259
259 $_->check_size; 260 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
260} 263}
261 264
262sub set_max_size { 265sub set_max_size {
263 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
264 267
400} 403}
401 404
402sub check_size { 405sub check_size {
403 my ($self) = @_; 406 my ($self) = @_;
404 407
408 $self->_check_size;
409 return;
410 #TODO: but needs a vastly more difficult algorithm
411 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
412
413 $check_size->{$self} = $self;
414
415 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
416 while (%$check_size) {
417 my @widgets = values %$check_size;
418 $_->_check_size
419 for @widgets;
420 }
421 });
422}
423
424sub _check_size {
425 my ($self) = @_;
426
427 delete $CFClient::UI::ROOT->{_check_size}{$self};
428
405 $self->{parent} 429 $self->{parent}
406 or return 1; 430 or return 1;
407 431
408 my ($w, $h) = $self->{user_w} && $self->{user_h} 432 my ($w, $h) = $self->{user_w} && $self->{user_h}
409 ? @$self{qw(user_w user_h)} 433 ? @$self{qw(user_w user_h)}
411 435
412 if ($w != $self->{req_w} || $h != $self->{req_h}) { 436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
413 $self->{req_w} = $w; 437 $self->{req_w} = $w;
414 $self->{req_h} = $h; 438 $self->{req_h} = $h;
415 439
416 $self->{parent}->check_size 440 $self->{parent}->_check_size
417 or $self->size_allocate ( 441 or $self->size_allocate (
418 (List::Util::max $self->{w}, $w), 442 (List::Util::max $self->{w}, $w),
419 (List::Util::max $self->{h}, $h), 443 (List::Util::max $self->{h}, $h),
420 ); 444 );
421 445
426} 450}
427 451
428sub update { 452sub update {
429 my ($self) = @_; 453 my ($self) = @_;
430 454
431 $self->{parent}->update 455 my $update = $CFClient::UI::ROOT->{_update} ||= {};
432 if $self->{parent}; 456
457 $update->{$self} = $self;
458
459 $CFClient::UI::ROOT->on_refresh (_update => sub {
460 while (%$update) {
461 my @widgets = values %$update;
462 %$update = ();
463
464 $_->{parent} && $_->{parent}->update
465 for @widgets;
466 }
467 });
433} 468}
434 469
435sub connect { 470sub connect {
436 my ($self, $signal, $cb) = @_; 471 my ($self, $signal, $cb) = @_;
437 472
527 562
528 $self 563 $self
529} 564}
530 565
531sub add { 566sub add {
532 my ($self, $child) = @_; 567 my ($self, @widgets) = @_;
533 568
534 $child->set_parent ($self); 569 $_->set_parent ($self)
570 for @widgets;
535 571
536 use sort 'stable'; 572 use sort 'stable';
537 573
538 $self->{children} = [ 574 $self->{children} = [
539 sort { $a->{z} <=> $b->{z} } 575 sort { $a->{z} <=> $b->{z} }
540 @{$self->{children}}, $child 576 @{$self->{children}}, @widgets
541 ]; 577 ];
542 578
543 $child->check_size; 579 $_->check_size
580 for @widgets;
581
582 $self->update;
544} 583}
545 584
546sub children { 585sub children {
547 @{ $_[0]{children} } 586 @{ $_[0]{children} }
548} 587}
567 606
568 for (@$children) { 607 for (@$children) {
569 delete $_->{parent}; 608 delete $_->{parent};
570 $_->hide; 609 $_->hide;
571 } 610 }
611
612 $self->check_size;
613 $self->update;
572} 614}
573 615
574sub find_widget { 616sub find_widget {
575 my ($self, $x, $y) = @_; 617 my ($self, $x, $y) = @_;
576 618
662 704
663 $self->SUPER::size_allocate ($w, $h); 705 $self->SUPER::size_allocate ($w, $h);
664 $self->update; 706 $self->update;
665} 707}
666 708
709sub _render {
710 $_[0]{children}[0]->draw;
711}
712
667sub render_child { 713sub render_child {
668 my ($self) = @_; 714 my ($self) = @_;
669 715
670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 716 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
671 glClearColor 0, 0, 0, 0; 717 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 718 glClear GL_COLOR_BUFFER_BIT;
673 $self->child->draw; 719
720 $self->_render;
674# glColorMask 1, 1, 1, 0; 721# glColorMask 1, 1, 1, 0;
675# glEnable GL_BLEND; 722# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO; 723# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0; 724# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h}; 725# glCopyPixels 0, 0, $self->{w}, $self->{h};
705 752
706package CFClient::UI::ViewPort; 753package CFClient::UI::ViewPort;
707 754
708our @ISA = CFClient::UI::Window::; 755our @ISA = CFClient::UI::Window::;
709 756
710sub new { die }
711
712sub size_request { 757sub size_request {
713 my ($self) = @_; 758 my ($self) = @_;
714 759
715 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 760 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)};
716 $self->child->size_allocate (0, 0, @$self{qw(child_w child_h)}); 761 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
717 762
718 @$self{qw(child_w child_h)} 763 @$self{qw(child_w child_h)}
719} 764}
720 765
721sub _draw { 766sub size_allocate {
722 my ($self) = @_; 767 my ($self, $w, $h) = @_;
723 768
724 $self->{children}[1]->draw; 769 $self->update;
725} 770}
726 771
772sub set_offset {
773 my ($self, $x, $y) = @_;
774
775 $self->{view_x} = int $x;
776 $self->{view_y} = int $y;
777
778 $self->update;
779}
780
781# hmm, this does not work for topleft of $self... but we should not aks for that
782sub _topleft {
783 my ($self, $x, $y) = @_;
784
785 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
786}
787
788sub find_widget {
789 my ($self, $x, $y) = @_;
790
791 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
792 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
793 ) {
794 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
795 } else {
796 $self->CFClient::UI::Base::find_widget ($x, $y)
797 }
798}
799
800sub _render {
801 my ($self) = @_;
802
803 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
804
805 $self->SUPER::_render;
806}
807
808#############################################################################
809
810package CFClient::UI::ScrolledWindow;
811
812our @ISA = CFClient::UI::HBox::;
813
814sub new {
815 my $class = shift;
816
817 my $self;
818
819 my $slider = new CFClient::UI::Slider
820 vertical => 1,
821 range => [0, 0, 1, 0.01], # HACK fix
822 connect_changed => sub {
823 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
824 },
825 ;
826
827 $self = $class->SUPER::new (
828 vp => (new CFClient::UI::ViewPort),
829 slider => $slider,
830 @_,
831 );
832
833 $self->{vp}->add ($self->{scrolled});
834 $self->add ($self->{vp});
835 $self->add ($self->{slider});
836
837 $self
838}
839
840#TODO# update range on size_allocate depeneing on child
841# update viewport offset on scroll
727 842
728############################################################################# 843#############################################################################
729 844
730package CFClient::UI::Frame; 845package CFClient::UI::Frame;
731 846
1244 1359
1245 delete $self->{texture}; 1360 delete $self->{texture};
1246 $self->SUPER::update; 1361 $self->SUPER::update;
1247} 1362}
1248 1363
1249sub reconfigure {
1250 my ($self) = @_;
1251
1252 delete $self->{texture};
1253}
1254
1255sub set_text { 1364sub set_text {
1256 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1257 1366
1258 return if $self->{text} eq "T$text"; 1367 return if $self->{text} eq "T$text";
1259 $self->{text} = "T$text"; 1368 $self->{text} = "T$text";
1312sub set_fontsize { 1421sub set_fontsize {
1313 my ($self, $fontsize) = @_; 1422 my ($self, $fontsize) = @_;
1314 1423
1315 $self->{fontsize} = $fontsize; 1424 $self->{fontsize} = $fontsize;
1316 delete $self->{texture}; 1425 delete $self->{texture};
1426
1427 $self->update;
1317 $self->check_size; 1428 $self->check_size;
1318 $self->update;
1319} 1429}
1320 1430
1321sub _draw { 1431sub _draw {
1322 my ($self) = @_; 1432 my ($self) = @_;
1323 1433
1957 # TODO: calculations are off 2067 # TODO: calculations are off
1958 my $self = $class->SUPER::new ( 2068 my $self = $class->SUPER::new (
1959 fg => [1, 1, 1], 2069 fg => [1, 1, 1],
1960 active_fg => [0, 0, 0], 2070 active_fg => [0, 0, 0],
1961 range => [0, 0, 100, 10], 2071 range => [0, 0, 100, 10],
1962 req_w => 20, 2072 req_w => $::WIDTH / 80,
1963 req_h => 20, 2073 req_h => $::WIDTH / 80,
1964 vertical => 0, 2074 vertical => 0,
1965 can_hover => 1, 2075 can_hover => 1,
1966 inner_pad => 5, 2076 inner_pad => 5,
1967 @_ 2077 @_
1968 ); 2078 );
2405} 2515}
2406 2516
2407sub _draw { 2517sub _draw {
2408 my ($self) = @_; 2518 my ($self) = @_;
2409 2519
2520 return unless $::CONN;#d# manage and cache textures differently
2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2411 2522
2412 # TODO animation 2523 # TODO animation
2413 if ($tex) { 2524 if ($tex) {
2414 glEnable GL_BLEND; 2525 glEnable GL_BLEND;
2422 } 2533 }
2423} 2534}
2424 2535
2425############################################################################# 2536#############################################################################
2426 2537
2538package CFClient::UI::InventoryItem;
2539
2540our @ISA = CFClient::UI::HBox::;
2541
2542sub new {
2543 my $class = shift;
2544
2545 my %args = @_;
2546
2547 my $item = $args{item};
2548
2549 my $desc = $item->{nrof} < 2
2550 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}";
2552
2553
2554 my $self = $class->SUPER::new (
2555 can_hover => 1,
2556 can_events => 1,
2557 tooltip => (CFClient::UI::Label->escape ($desc)
2558 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2559 connect_button_down => sub {
2560 my ($self, $ev, $x, $y) = @_;
2561
2562 # todo: maybe put examine on 1? but should just be a tooltip :(
2563 if ($ev->{button} == 1) {
2564 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2565 } elsif ($ev->{button} == 2) {
2566 $::CONN->send ("apply $item->{tag}");
2567 } elsif ($ev->{button} == 3) {
2568 CFClient::UI::Menu->new (
2569 items => [
2570 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2571 [
2572 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2573 sub { $::CONN->send ("lock $item->{tag}") },
2574 ],
2575 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2576 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2577 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2578 ],
2579 )->popup ($ev);
2580 }
2581
2582 1
2583 },
2584 %args
2585 );
2586 $self->add(new CFClient::UI::Face
2587 can_events => 0,
2588 face => $item->{face},
2589 anim => $item->{anim},
2590 animspeed => $item->{animspeed});
2591 $self->add(new CFClient::UI::Label
2592 can_events => 0,
2593 text => $desc);
2594
2595 $self
2596}
2597
2598#############################################################################
2599
2600package CFClient::UI::Inventory;
2601
2602our @ISA = CFClient::UI::ScrolledWindow::;
2603
2604sub new {
2605 my $class = shift;
2606
2607 my $self = $class->SUPER::new (
2608 scrolled => (new CFClient::UI::VBox),
2609 @_,
2610 );
2611
2612 $self
2613}
2614
2615sub set_items {
2616 my ($self, $items) = @_;
2617
2618 $self->{scrolled}->clear;
2619 return unless $items;
2620
2621 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2622
2623 $self->{real_items} = \@items;
2624
2625 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2
2627 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}";
2629
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2631 }
2632
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634}
2635
2636sub size_request {
2637 my ($self) = @_;
2638 ($self->{req_w}, $self->{req_h});
2639}
2640
2641#############################################################################
2642
2427package CFClient::UI::Menu; 2643package CFClient::UI::Menu;
2428 2644
2429our @ISA = CFClient::UI::FancyFrame::; 2645our @ISA = CFClient::UI::FancyFrame::;
2430 2646
2431use CFClient::OpenGL; 2647use CFClient::OpenGL;
2475} 2691}
2476 2692
2477sub mouse_motion { 2693sub mouse_motion {
2478 my ($self, $ev, $x, $y) = @_; 2694 my ($self, $ev, $x, $y) = @_;
2479 2695
2480 # TODO: should use vbox->fdind_widget or so 2696 # TODO: should use vbox->find_widget or so
2481 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2697 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2482 $self->{hover} = $self->{item}{$HOVER}; 2698 $self->{hover} = $self->{item}{$HOVER};
2483} 2699}
2484 2700
2485sub button_up { 2701sub button_up {
2503use CFClient::OpenGL; 2719use CFClient::OpenGL;
2504 2720
2505sub check_size { 2721sub check_size {
2506 my ($self) = @_; 2722 my ($self) = @_;
2507 2723
2508 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2724 $self->configure (0, 0, $self->{w}, $self->{h});
2509} 2725}
2510 2726
2511sub size_request { 2727sub size_request {
2512 ($::WIDTH, $::HEIGHT) 2728 my ($self) = @_;
2729
2730 ($self->{w}, $self->{h})
2731}
2732
2733sub size_allocate {
2734 my ($self, $w, $h) = @_;
2735
2736 my $old_w = $self->{old_w};
2737 my $old_h = $self->{old_h};
2738
2739 if ($old_w && $old_h) {
2740 for my $child ($self->children) {
2741 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2742 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2743 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2744 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2745 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2746 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2748 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 }
2750 }
2751
2752 $self->{old_w} = $w;
2753 $self->{old_h} = $h;
2513} 2754}
2514 2755
2515sub configure { 2756sub configure {
2516 my ($self, $x, $y, $w, $h) = @_; 2757 my ($self, $x, $y, $w, $h) = @_;
2517 2758
2518 $self->SUPER::configure ($x, $y, $w, $h); 2759 $self->SUPER::configure ($x, $y, $w, $h);
2519 2760
2520 for my $child (@{$self->{children}}) { 2761 for my $child ($self->children) {
2521 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2522 2763
2523 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2764 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2524 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2525 $child->configure ($X, $Y, $W,$H); 2766 $child->configure ($X, $Y, $W, $H);
2526 } 2767 }
2527} 2768}
2528 2769
2529sub _topleft { 2770sub _topleft {
2530 my ($self, $x, $y) = @_; 2771 my ($self, $x, $y) = @_;
2575 $self->_draw; 2816 $self->_draw;
2576} 2817}
2577 2818
2578############################################################################# 2819#############################################################################
2579 2820
2580package CFClient::UI::Inventory;
2581
2582our @ISA = CFClient::UI::Container::;
2583
2584use CFClient::OpenGL;
2585
2586sub new {
2587 my $class = shift;
2588
2589 my $self = $class->SUPER::new (@_);
2590
2591 $self
2592}
2593
2594sub size_allocate {
2595 my ($self, $w, $h) = @_;
2596
2597 $self->{w} = $w;
2598 $self->{h} = $h;
2599
2600 $self->check_size;
2601}
2602
2603sub set_items {
2604 my ($self, $items) = @_;
2605 my @items = values %{$items};
2606 @items = sort { $a->{type} <=> $b->{type} } @items;
2607
2608 $self->{real_items} = \@items;
2609
2610 for my $item (@items) {
2611 my $desc = $item->{nrof} < 2
2612 ? $item->{name}
2613 : "$item->{nrof} $item->{name_pl}";
2614
2615 $self->add (my $hb = new CFClient::UI::HBox);
2616
2617 $hb->add (my $f = new CFClient::UI::Face
2618 can_events => 0,
2619 face => $item->{face},
2620 anim => $item->{anim},
2621 animspeed => $item->{animspeed},
2622 expand => 1
2623 );
2624 $hb->add (new CFClient::UI::Label text => $desc, expand => 1);
2625 }
2626
2627 $self->{max_pos} = (scalar @items) - 1;
2628
2629 my $range = $self->{range};
2630 my $page = $self->{h} / 32; # waht information souce to use for face size?
2631 # it should be configurable i guess...
2632 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2633 $range->update;
2634}
2635
2636sub size_request {
2637 my ($self) = @_;
2638 (100, 200)
2639}
2640
2641sub set_range {
2642 my ($self, $range) = @_;
2643
2644 $self->{range} = $range;
2645 $range->connect (changed => sub { $self->update });
2646
2647 my $page = $self->{h} / 32; # waht information souce to use for face size?
2648 # it should be configurable i guess...
2649
2650 $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2651 $range->update;
2652
2653 $self->update;
2654}
2655
2656sub _draw {
2657 my ($self) = @_;
2658
2659 my ($w, $h) = ($self->{w}, $self->{h});
2660
2661 $self->{pos} = int $self->{range}{range}[0];
2662
2663 my $y = 0;
2664 my $cnt = 0;
2665 my $hrem = $self->{h}; # horiz. remaining space
2666
2667 for (my $i = $self->{pos}; $i < @{$self->{children} || []}; $i++) {
2668 my $chld = $self->{children}->[$i];
2669
2670 if ($hrem >= $chld->{h}) {
2671 $chld->configure (0, $y, $chld->{w}, $chld->{h});
2672
2673 $chld->show;
2674 $chld->draw;
2675
2676 $hrem -= $chld->{h};
2677 $y += $chld->{h};
2678 } else {
2679 last
2680 }
2681 }
2682}
2683
2684
2685#############################################################################
2686
2687package CFClient::UI; 2821package CFClient::UI;
2688 2822
2689$ROOT = new CFClient::UI::Root; 2823$ROOT = new CFClient::UI::Root;
2690$TOOLTIP = new CFClient::UI::Tooltip; 2824$TOOLTIP = new CFClient::UI::Tooltip;
2691 2825

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines