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.188 by root, Mon May 8 21:16:09 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
527 530
528 $self 531 $self
529} 532}
530 533
531sub add { 534sub add {
532 my ($self, $child) = @_; 535 my ($self, @widgets) = @_;
533 536
534 $child->set_parent ($self); 537 $_->set_parent ($self)
538 for @widgets;
535 539
536 use sort 'stable'; 540 use sort 'stable';
537 541
538 $self->{children} = [ 542 $self->{children} = [
539 sort { $a->{z} <=> $b->{z} } 543 sort { $a->{z} <=> $b->{z} }
540 @{$self->{children}}, $child 544 @{$self->{children}}, @widgets
541 ]; 545 ];
542 546
543 $child->check_size; 547 $_->check_size
548 for @widgets;
549
550 $self->update;
544} 551}
545 552
546sub children { 553sub children {
547 @{ $_[0]{children} } 554 @{ $_[0]{children} }
548} 555}
567 574
568 for (@$children) { 575 for (@$children) {
569 delete $_->{parent}; 576 delete $_->{parent};
570 $_->hide; 577 $_->hide;
571 } 578 }
579
580 $self->check_size;
581 $self->update;
572} 582}
573 583
574sub find_widget { 584sub find_widget {
575 my ($self, $x, $y) = @_; 585 my ($self, $x, $y) = @_;
576 586
662 672
663 $self->SUPER::size_allocate ($w, $h); 673 $self->SUPER::size_allocate ($w, $h);
664 $self->update; 674 $self->update;
665} 675}
666 676
677sub _render {
678 $_[0]{children}[0]->draw;
679}
680
667sub render_child { 681sub render_child {
668 my ($self) = @_; 682 my ($self) = @_;
669 683
670 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub { 684 $self->{texture} = new_from_opengl CFClient::Texture $self->{w}, $self->{h}, sub {
671 glClearColor 0, 0, 0, 0; 685 glClearColor 0, 0, 0, 0;
672 glClear GL_COLOR_BUFFER_BIT; 686 glClear GL_COLOR_BUFFER_BIT;
673 $self->child->draw; 687
688 $self->_render;
674# glColorMask 1, 1, 1, 0; 689# glColorMask 1, 1, 1, 0;
675# glEnable GL_BLEND; 690# glEnable GL_BLEND;
676# glBlendFunc GL_SRC_ALPHA, GL_ZERO; 691# glBlendFunc GL_SRC_ALPHA, GL_ZERO;
677# glRasterPos 0, 0; 692# glRasterPos 0, 0;
678# glCopyPixels 0, 0, $self->{w}, $self->{h}; 693# glCopyPixels 0, 0, $self->{w}, $self->{h};
705 720
706package CFClient::UI::ViewPort; 721package CFClient::UI::ViewPort;
707 722
708our @ISA = CFClient::UI::Window::; 723our @ISA = CFClient::UI::Window::;
709 724
710sub new { die }
711
712sub size_request { 725sub size_request {
713 my ($self) = @_; 726 my ($self) = @_;
714 727
715 @$self{qw(child_w child_h)} = @{$self->child}{qw(req_w req_h)}; 728 @$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)}); 729 $self->child->configure (0, 0, @$self{qw(child_w child_h)});
717 730
718 @$self{qw(child_w child_h)} 731 @$self{qw(child_w child_h)}
719} 732}
720 733
721sub _draw { 734sub size_allocate {
722 my ($self) = @_; 735 my ($self, $w, $h) = @_;
723 736
724 $self->{children}[1]->draw; 737 $self->update;
725} 738}
726 739
740sub set_offset {
741 my ($self, $x, $y) = @_;
742
743 $self->{view_x} = int $x;
744 $self->{view_y} = int $y;
745
746 $self->update;
747}
748
749# hmm, this does not work for topleft of $self... but we should not aks for that
750sub _topleft {
751 my ($self, $x, $y) = @_;
752
753 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y})
754}
755
756sub find_widget {
757 my ($self, $x, $y) = @_;
758
759 if ( $x >= $self->{x} && $x < $self->{x} + $self->{w}
760 && $y >= $self->{y} && $y < $self->{y} + $self->{h}
761 ) {
762 $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y})
763 } else {
764 $self->CFClient::UI::Base::find_widget ($x, $y)
765 }
766}
767
768sub _render {
769 my ($self) = @_;
770
771 CFClient::OpenGL::glTranslate -$self->{view_x}, -$self->{view_y};
772
773 $self->SUPER::_render;
774}
775
776#############################################################################
777
778package CFClient::UI::ScrolledWindow;
779
780our @ISA = CFClient::UI::HBox::;
781
782sub new {
783 my $class = shift;
784
785 my $self;
786
787 my $slider = new CFClient::UI::Slider
788 vertical => 1,
789 range => [0, 0, 1, 0.01], # HACK fix
790 connect_changed => sub {
791 $self->{vp}->set_offset (0, $_[1] * ($self->{vp}{child_h} - $self->{vp}{h}));
792 },
793 ;
794
795 $self = $class->SUPER::new (
796 vp => (new CFClient::UI::ViewPort),
797 slider => $slider,
798 @_,
799 );
800
801 $self->{vp}->add ($self->{scrolled});
802 $self->add ($self->{vp});
803 $self->add ($self->{slider});
804
805 $self
806}
807
808#TODO# update range on size_allocate depeneing on child
809# update viewport offset on scroll
727 810
728############################################################################# 811#############################################################################
729 812
730package CFClient::UI::Frame; 813package CFClient::UI::Frame;
731 814
1244 1327
1245 delete $self->{texture}; 1328 delete $self->{texture};
1246 $self->SUPER::update; 1329 $self->SUPER::update;
1247} 1330}
1248 1331
1249sub reconfigure {
1250 my ($self) = @_;
1251
1252 delete $self->{texture};
1253}
1254
1255sub set_text { 1332sub set_text {
1256 my ($self, $text) = @_; 1333 my ($self, $text) = @_;
1257 1334
1258 return if $self->{text} eq "T$text"; 1335 return if $self->{text} eq "T$text";
1259 $self->{text} = "T$text"; 1336 $self->{text} = "T$text";
1312sub set_fontsize { 1389sub set_fontsize {
1313 my ($self, $fontsize) = @_; 1390 my ($self, $fontsize) = @_;
1314 1391
1315 $self->{fontsize} = $fontsize; 1392 $self->{fontsize} = $fontsize;
1316 delete $self->{texture}; 1393 delete $self->{texture};
1394
1395 $self->update;
1317 $self->check_size; 1396 $self->check_size;
1318 $self->update;
1319} 1397}
1320 1398
1321sub _draw { 1399sub _draw {
1322 my ($self) = @_; 1400 my ($self) = @_;
1323 1401
1957 # TODO: calculations are off 2035 # TODO: calculations are off
1958 my $self = $class->SUPER::new ( 2036 my $self = $class->SUPER::new (
1959 fg => [1, 1, 1], 2037 fg => [1, 1, 1],
1960 active_fg => [0, 0, 0], 2038 active_fg => [0, 0, 0],
1961 range => [0, 0, 100, 10], 2039 range => [0, 0, 100, 10],
1962 req_w => 20, 2040 req_w => $::WIDTH / 80,
1963 req_h => 20, 2041 req_h => $::WIDTH / 80,
1964 vertical => 0, 2042 vertical => 0,
1965 can_hover => 1, 2043 can_hover => 1,
1966 inner_pad => 5, 2044 inner_pad => 5,
1967 @_ 2045 @_
1968 ); 2046 );
2405} 2483}
2406 2484
2407sub _draw { 2485sub _draw {
2408 my ($self) = @_; 2486 my ($self) = @_;
2409 2487
2488 return unless $::CONN;#d# manage and cache textures differently
2410 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2489 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2411 2490
2412 # TODO animation 2491 # TODO animation
2413 if ($tex) { 2492 if ($tex) {
2414 glEnable GL_BLEND; 2493 glEnable GL_BLEND;
2422 } 2501 }
2423} 2502}
2424 2503
2425############################################################################# 2504#############################################################################
2426 2505
2506package CFClient::UI::InventoryItem;
2507
2508our @ISA = CFClient::UI::HBox::;
2509
2510sub new {
2511 my $class = shift;
2512
2513 my %args = @_;
2514
2515 my $item = $args{item};
2516
2517 my $desc = $item->{nrof} < 2
2518 ? $item->{name}
2519 : "$item->{nrof} $item->{name_pl}";
2520
2521
2522 my $self = $class->SUPER::new (
2523 can_hover => 1,
2524 can_events => 1,
2525 tooltip => (CFClient::UI::Label->escape ($desc)
2526 . "\n<small>leftclick - pick up\nmiddle click - apply\nrightclick - menu</small>"),
2527 connect_button_down => sub {
2528 my ($self, $ev, $x, $y) = @_;
2529
2530 # todo: maybe put examine on 1? but should just be a tooltip :(
2531 if ($ev->{button} == 1) {
2532 $::CONN->send ("move $::CONN->{player}{tag} $item->{tag} 0");
2533 } elsif ($ev->{button} == 2) {
2534 $::CONN->send ("apply $item->{tag}");
2535 } elsif ($ev->{button} == 3) {
2536 CFClient::UI::Menu->new (
2537 items => [
2538 ["examine", sub { $::CONN->send ("examine $item->{tag}") }],
2539 [
2540 $item->{flags} & Crossfire::Protocol::F_LOCKED ? "lock" : "unlock",
2541 sub { $::CONN->send ("lock $item->{tag}") },
2542 ],
2543 ["mark", sub { $::CONN->send ("mark $item->{tag}") }],
2544 ["apply", sub { $::CONN->send ("apply $item->{tag}") }],
2545 ["drop", sub { $::CONN->send ("move 0 $item->{tag} 0") }],
2546 ],
2547 )->popup ($ev);
2548 }
2549
2550 1
2551 },
2552 %args
2553 );
2554 $self->add(new CFClient::UI::Face
2555 can_events => 0,
2556 face => $item->{face},
2557 anim => $item->{anim},
2558 animspeed => $item->{animspeed});
2559 $self->add(new CFClient::UI::Label
2560 can_events => 0,
2561 text => $desc);
2562
2563 $self
2564}
2565
2566#############################################################################
2567
2568package CFClient::UI::Inventory;
2569
2570our @ISA = CFClient::UI::ScrolledWindow::;
2571
2572sub new {
2573 my $class = shift;
2574
2575 my $self = $class->SUPER::new (
2576 scrolled => (new CFClient::UI::VBox),
2577 @_,
2578 );
2579
2580 $self
2581}
2582
2583sub set_items {
2584 my ($self, $items) = @_;
2585
2586 $self->{scrolled}->clear;
2587 return unless $items;
2588
2589 my @items = sort { $a->{type} <=> $b->{type} } @$items;
2590
2591 $self->{real_items} = \@items;
2592
2593 for my $item (@items) {
2594 my $desc = $item->{nrof} < 2
2595 ? $item->{name}
2596 : "$item->{nrof} $item->{name_pl}";
2597
2598 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item);
2599 }
2600
2601# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2602}
2603
2604sub size_request {
2605 my ($self) = @_;
2606 ($self->{req_w}, $self->{req_h});
2607}
2608
2609#############################################################################
2610
2427package CFClient::UI::Menu; 2611package CFClient::UI::Menu;
2428 2612
2429our @ISA = CFClient::UI::FancyFrame::; 2613our @ISA = CFClient::UI::FancyFrame::;
2430 2614
2431use CFClient::OpenGL; 2615use CFClient::OpenGL;
2475} 2659}
2476 2660
2477sub mouse_motion { 2661sub mouse_motion {
2478 my ($self, $ev, $x, $y) = @_; 2662 my ($self, $ev, $x, $y) = @_;
2479 2663
2480 # TODO: should use vbox->fdind_widget or so 2664 # TODO: should use vbox->find_widget or so
2481 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); 2665 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2482 $self->{hover} = $self->{item}{$HOVER}; 2666 $self->{hover} = $self->{item}{$HOVER};
2483} 2667}
2484 2668
2485sub button_up { 2669sub button_up {
2503use CFClient::OpenGL; 2687use CFClient::OpenGL;
2504 2688
2505sub check_size { 2689sub check_size {
2506 my ($self) = @_; 2690 my ($self) = @_;
2507 2691
2508 $self->configure (0, 0, $::WIDTH, $::HEIGHT); 2692 $self->configure (0, 0, $self->{w}, $self->{h});
2509} 2693}
2510 2694
2511sub size_request { 2695sub size_request {
2512 ($::WIDTH, $::HEIGHT) 2696 my ($self) = @_;
2697
2698 ($self->{w}, $self->{h})
2699}
2700
2701sub size_allocate {
2702 my ($self, $w, $h) = @_;
2703
2704 my $old_w = $self->{old_w};
2705 my $old_h = $self->{old_h};
2706
2707 if ($old_w && $old_h) {
2708 for my $child ($self->children) {
2709 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2710 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2711 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2712 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2713 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2714 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2715 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2716 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2717 }
2718 }
2719
2720 $self->{old_w} = $w;
2721 $self->{old_h} = $h;
2513} 2722}
2514 2723
2515sub configure { 2724sub configure {
2516 my ($self, $x, $y, $w, $h) = @_; 2725 my ($self, $x, $y, $w, $h) = @_;
2517 2726
2518 $self->SUPER::configure ($x, $y, $w, $h); 2727 $self->SUPER::configure ($x, $y, $w, $h);
2519 2728
2520 for my $child (@{$self->{children}}) { 2729 for my $child ($self->children) {
2521 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2730 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2522 2731
2523 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2732 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2524 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2733 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2525 $child->configure ($X, $Y, $W,$H); 2734 $child->configure ($X, $Y, $W, $H);
2526 } 2735 }
2527} 2736}
2528 2737
2529sub _topleft { 2738sub _topleft {
2530 my ($self, $x, $y) = @_; 2739 my ($self, $x, $y) = @_;
2575 $self->_draw; 2784 $self->_draw;
2576} 2785}
2577 2786
2578############################################################################# 2787#############################################################################
2579 2788
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; 2789package CFClient::UI;
2688 2790
2689$ROOT = new CFClient::UI::Root; 2791$ROOT = new CFClient::UI::Root;
2690$TOOLTIP = new CFClient::UI::Tooltip; 2792$TOOLTIP = new CFClient::UI::Tooltip;
2691 2793

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines