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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines