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.309 by elmex, Wed Jun 21 12:59:23 2006 UTC vs.
Revision 1.310 by root, Fri Jun 23 20:28:20 2006 UTC

1091 if exists $self->{title}; 1091 if exists $self->{title};
1092 1092
1093 if ($self->{has_close_button}) { 1093 if ($self->{has_close_button}) {
1094 $self->{close_button} = 1094 $self->{close_button} =
1095 new CFClient::UI::ImageButton 1095 new CFClient::UI::ImageButton
1096 image => 'x1_close.png', 1096 path => 'x1_close.png',
1097 on_activate => sub { $self->hide }; 1097 on_activate => sub { $self->hide };
1098 1098
1099 $self->CFClient::UI::Container::add ($self->{close_button}); 1099 $self->CFClient::UI::Container::add ($self->{close_button});
1100 } 1100 }
1101 1101
1438 1438
1439sub invoke_size_allocate { 1439sub invoke_size_allocate {
1440 my ($self, $w, $h) = @_; 1440 my ($self, $w, $h) = @_;
1441 1441
1442 my $space = $self->{vertical} ? $h : $w; 1442 my $space = $self->{vertical} ? $h : $w;
1443 my $children = $self->{children}; 1443 my @children = $self->visible_children;
1444 1444
1445 my @req; 1445 my @req;
1446 1446
1447 if ($self->{homogeneous}) { 1447 if ($self->{homogeneous}) {
1448 @req = ($space / (@$children || 1)) x @$children; 1448 @req = ($space / (@children || 1)) x @children;
1449 } else { 1449 } else {
1450 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @$children; 1450 @req = map $_->{$self->{vertical} ? "req_h" : "req_w"}, @children;
1451 my $req = List::Util::sum @req; 1451 my $req = List::Util::sum @req;
1452 1452
1453 if ($req > $space) { 1453 if ($req > $space) {
1454 # ah well, not enough space 1454 # ah well, not enough space
1455 $_ *= $space / $req for @req; 1455 $_ *= $space / $req for @req;
1456 } else { 1456 } else {
1457 my $expand = (List::Util::sum map $_->{expand}, @$children) || 1; 1457 my $expand = (List::Util::sum map $_->{expand}, @children) || 1;
1458 1458
1459 $space = ($space - $req) / $expand; # remaining space to give away 1459 $space = ($space - $req) / $expand; # remaining space to give away
1460 1460
1461 $req[$_] += $space * $children->[$_]{expand} 1461 $req[$_] += $space * $children[$_]{expand}
1462 for 0 .. $#$children; 1462 for 0 .. $#children;
1463 } 1463 }
1464 } 1464 }
1465 1465
1466 CFClient::UI::harmonize \@req; 1466 CFClient::UI::harmonize \@req;
1467 1467
1468 my $pos = 0; 1468 my $pos = 0;
1469 for (0 .. $#$children) { 1469 for (0 .. $#children) {
1470 my $alloc = $req[$_]; 1470 my $alloc = $req[$_];
1471 $children->[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h)); 1471 $children[$_]->configure ($self->{vertical} ? (0, $pos, $w, $alloc) : ($pos, 0, $alloc, $h));
1472 1472
1473 $pos += $alloc; 1473 $pos += $alloc;
1474 } 1474 }
1475 1475
1476 1 1476 1
2092package CFClient::UI::Image; 2092package CFClient::UI::Image;
2093 2093
2094our @ISA = CFClient::UI::Base::; 2094our @ISA = CFClient::UI::Base::;
2095 2095
2096use CFClient::OpenGL; 2096use CFClient::OpenGL;
2097use Carp qw/confess/;
2098 2097
2099our %loaded_images; 2098our %texture_cache;
2100 2099
2101sub new { 2100sub new {
2102 my $class = shift; 2101 my $class = shift;
2103 2102
2104 my $self = $class->SUPER::new (can_events => 0, @_); 2103 my $self = $class->SUPER::new (
2104 can_events => 0,
2105 @_,
2106 );
2105 2107
2106 $self->{image} or confess "Image has 'image' not set. This is a fatal error!"; 2108 $self->{path}
2109 or Carp::croak "required attribute 'path' not set";
2107 2110
2108 $loaded_images{$self->{image}} ||= 2111 $self->{tex} = $texture_cache{$self->{path}} ||=
2109 new_from_file CFClient::Texture CFClient::find_rcfile $self->{image}, mipmap => 1; 2112 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2110 2113
2111 my $tex = $self->{tex} = $loaded_images{$self->{image}}; 2114 Scalar::Util::weaken $texture_cache{$self->{path}};
2112 2115
2113 Scalar::Util::weaken $loaded_images{$self->{image}}; 2116 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2114
2115 $self->{aspect} = $tex->{w} / $tex->{h};
2116 2117
2117 $self 2118 $self
2118} 2119}
2119 2120
2120sub size_request { 2121sub size_request {
2121 my ($self) = @_; 2122 my ($self) = @_;
2122 2123
2123 ($self->{tex}->{w}, $self->{tex}->{h}) 2124 ($self->{tex}{w}, $self->{tex}{h})
2124} 2125}
2125 2126
2126sub _draw { 2127sub _draw {
2127 my ($self) = @_; 2128 my ($self) = @_;
2128 2129
2566 2567
2567 $self->{fontsize} = $fontsize; 2568 $self->{fontsize} = $fontsize;
2568 $self->reflow; 2569 $self->reflow;
2569} 2570}
2570 2571
2572sub visible_children {
2573 my ($self) = @_;
2574
2575 @{$self->{children}}[0,1]
2576}
2577
2571sub invoke_size_allocate { 2578sub invoke_size_allocate {
2572 my ($self, $w, $h) = @_; 2579 my ($self, $w, $h) = @_;
2580
2581 my (undef, undef, @other) = @{ $self->{children} };
2582 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2573 2583
2574 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2584 $self->{layout}->set_font ($self->{font}) if $self->{font};
2575 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2585 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2576 $self->{layout}->set_width ($self->{children}[0]{w}); 2586 $self->{layout}->set_width ($self->{children}[0]{w});
2577 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2587 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2579 $self->reflow; 2589 $self->reflow;
2580 2590
2581 $self->SUPER::invoke_size_allocate ($w, $h) 2591 $self->SUPER::invoke_size_allocate ($w, $h)
2582} 2592}
2583 2593
2584sub text_size { 2594sub get_layout {
2585 my ($self, $text, $indent) = @_; 2595 my ($self, $para) = @_;
2586 2596
2587 my $layout = $self->{layout}; 2597 my $layout = $self->{layout};
2588 2598
2599 $layout->set_font ($self->{font}) if $self->{font};
2589 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2600 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2590 $layout->set_width ($self->{children}[0]{w} - $indent); 2601 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2591 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2602 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2592 $layout->set_markup ($text); 2603 $layout->set_markup ($para->{markup});
2593 2604 $layout->set_shapes (map +($_->{req_w}, $_->{req_h}), @{$para->{widget}});
2605
2594 $layout->size 2606 $layout
2595} 2607}
2596 2608
2597sub reflow { 2609sub reflow {
2598 my ($self) = @_; 2610 my ($self) = @_;
2599 2611
2608 $self->{children}[1]->set_value ($offset); 2620 $self->{children}[1]->set_value ($offset);
2609} 2621}
2610 2622
2611sub clear { 2623sub clear {
2612 my ($self) = @_; 2624 my ($self) = @_;
2625
2626 my (undef, undef, @other) = @{ $self->{children} };
2627 $self->remove ($_) for @other;
2613 2628
2614 $self->{par} = []; 2629 $self->{par} = [];
2615 $self->{height} = 0; 2630 $self->{height} = 0;
2616 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2631 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2617} 2632}
2618 2633
2619sub add_paragraph { 2634sub add_paragraph {
2620 my ($self, $color, $text, $indent) = @_; 2635 my ($self, $color, $para, $indent) = @_;
2621 2636
2622 for my $line (split /\n/, $text) { 2637 my ($text, @w) = ref $para ? @$para : $para;
2623 my ($w, $h) = $self->text_size ($line); 2638
2624 $self->{height} += $h; 2639 $para = {
2625 push @{$self->{par}}, [$w + $indent, $h, $color, $indent, $line]; 2640 w => 1e10,
2641 wrapped => 1,
2642 fg => $color,
2643 indent => $indent,
2644 markup => $text,
2645 widget => \@w,
2626 } 2646 };
2627 2647
2628 $self->{children}[1]->set_range ([$self->{height}, 0, $self->{height}, $self->{h}, 1]); 2648 $self->add (@w) if @w;
2649 push @{$self->{par}}, $para;
2650
2651 $self->{need_reflow}++;
2652 $self->update;
2653}
2654
2655sub scroll_to_bottom {
2656 my ($self) = @_;
2657
2658 $self->{scroll_to_bottom} = 1;
2659 $self->update;
2629} 2660}
2630 2661
2631sub update { 2662sub update {
2632 my ($self) = @_; 2663 my ($self) = @_;
2633 2664
2641 my ($W, $H) = @{$self->{children}[0]}{qw(w h)}; 2672 my ($W, $H) = @{$self->{children}[0]}{qw(w h)};
2642 2673
2643 if (delete $self->{need_reflow}) { 2674 if (delete $self->{need_reflow}) {
2644 my $height = 0; 2675 my $height = 0;
2645 2676
2646 my $layout = $self->{layout};
2647
2648 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2649
2650 for (@{$self->{par}}) { 2677 for my $para (@{$self->{par}}) {
2651 if (1 || $_->[0] >= $W) { # TODO: works,but needs reconfigure etc. support 2678 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2652 $layout->set_width ($W - $_->[3]); 2679 my $layout = $self->get_layout ($para);
2653 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2654 $layout->set_markup ($_->[4]);
2655 my ($w, $h) = $layout->size; 2680 my ($w, $h) = $layout->size;
2656 $_->[0] = $w + $_->[3]; 2681
2682 $para->{w} = $w + $para->{indent};
2657 $_->[1] = $h; 2683 $para->{h} = $h;
2684 $para->{wrapped} = $layout->has_wrapped;
2658 } 2685 }
2659 2686
2660 $height += $_->[1]; 2687 $height += $para->{h};
2661 } 2688 }
2662 2689
2663 $self->{height} = $height; 2690 $self->{height} = $height;
2664 2691
2665 $self->{children}[1]->set_range ([$height, 0, $height, $H, 1]); 2692 $self->{children}[1]->set_range ([$self->{children}[1]{range}[0], 0, $height, $H, 1]);
2666 2693
2667 delete $self->{texture}; 2694 delete $self->{texture};
2695 }
2696
2697 if (delete $self->{scroll_to_bottom}) {
2698 $self->{children}[1]->set_value (1e10);
2668 } 2699 }
2669 2700
2670 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub { 2701 $self->{texture} ||= new_from_opengl CFClient::Texture $W, $H, sub {
2671 glClearColor 0, 0, 0, 0; 2702 glClearColor 0, 0, 0, 0;
2672 glClear GL_COLOR_BUFFER_BIT; 2703 glClear GL_COLOR_BUFFER_BIT;
2676 my $y0 = $top; 2707 my $y0 = $top;
2677 my $y1 = $top + $H; 2708 my $y1 = $top + $H;
2678 2709
2679 my $y = 0; 2710 my $y = 0;
2680 2711
2681 my $layout = $self->{layout};
2682
2683 $layout->set_font ($self->{font}) if $self->{font};
2684
2685 glEnable GL_BLEND; 2712 glEnable GL_BLEND;
2686 #TODO# not correct in windows where rgba is forced off 2713 #TODO# not correct in windows where rgba is forced off
2687 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2714 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2688 2715
2689 for my $par (@{$self->{par}}) { 2716 for my $para (@{$self->{par}}) {
2690 my $h = $par->[1]; 2717 my $h = $para->{h};
2691 2718
2692 if ($y0 < $y + $h && $y < $y1) { 2719 if ($y0 < $y + $h && $y < $y1) {
2693 $layout->set_foreground (@{ $par->[2] }); 2720
2694 $layout->set_width ($W - $par->[3]); 2721 my $layout = $self->get_layout ($para);
2695 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2696 $layout->set_markup ($par->[4]);
2697 2722
2698 my ($w, $h, $data, $format, $internalformat) = $layout->render; 2723 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2699 2724
2700 glRasterPos $par->[3], $y - $y0; 2725 glRasterPos $para->{indent}, $y - $y0;
2701 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2726 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2727
2728 if (my @w = @{ $para->{widget} }) {
2729 my @s = $layout->get_shapes;
2730
2731 for (@w) {
2732 my ($dx, $dy) = splice @s, 0, 2, ();
2733
2734 $_->{x} = $dx + $para->{indent};
2735 $_->{y} = $dy + $y - $y0;
2736
2737 $_->draw;
2738 }
2739 }
2702 } 2740 }
2703 2741
2704 $y += $h; 2742 $y += $h;
2705 } 2743 }
2706 2744
2707 glDisable GL_BLEND; 2745 glDisable GL_BLEND;
2708 }; 2746 };
2709 }); 2747 });
2748}
2749
2750sub reconfigure {
2751 my ($self) = @_;
2752
2753 $self->SUPER::reconfigure;
2754
2755 $_->{w} = 1e10 for @{ $self->{par} };
2756 $self->reflow;
2710} 2757}
2711 2758
2712sub _draw { 2759sub _draw {
2713 my ($self) = @_; 2760 my ($self) = @_;
2714 2761
2717 glColor 0, 0, 0, 1; 2764 glColor 0, 0, 0, 1;
2718 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h}); 2765 $self->{texture}->draw_quad_alpha_premultiplied (0, 0, $self->{children}[0]{w}, $self->{children}[0]{h});
2719 glDisable GL_TEXTURE_2D; 2766 glDisable GL_TEXTURE_2D;
2720 2767
2721 $self->{children}[1]->draw; 2768 $self->{children}[1]->draw;
2722
2723} 2769}
2724 2770
2725############################################################################# 2771#############################################################################
2726 2772
2727package CFClient::UI::Animator; 2773package CFClient::UI::Animator;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines