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.317 by root, Sun Jul 2 18:52:05 2006 UTC vs.
Revision 1.329 by root, Sun Jul 23 02:44:06 2006 UTC

452 452
453 $FOCUS->emit ("focus_out") if $FOCUS; 453 $FOCUS->emit ("focus_out") if $FOCUS;
454 $self->emit ("focus_in"); 454 $self->emit ("focus_in");
455} 455}
456 456
457sub invoke_mouse_motion { 1 } 457sub invoke_mouse_motion { 0 }
458sub invoke_button_up { 1 } 458sub invoke_button_up { 0 }
459sub invoke_key_down { 1 } 459sub invoke_key_down { 0 }
460sub invoke_key_up { 1 } 460sub invoke_key_up { 0 }
461 461
462sub invoke_button_down { 462sub invoke_button_down {
463 my ($self, $ev, $x, $y) = @_; 463 my ($self, $ev, $x, $y) = @_;
464 464
465 $self->grab_focus; 465 $self->grab_focus;
466 466
467 1 467 0
468} 468}
469 469
470sub connect { 470sub connect {
471 my ($self, $signal, $cb) = @_; 471 my ($self, $signal, $cb) = @_;
472 472
473 push @{ $self->{signal_cb}{$signal} }, $cb; 473 push @{ $self->{signal_cb}{$signal} }, $cb;
474} 474}
475 475
476sub emit { 476sub emit {
477 my ($self, $signal, @args) = @_; 477 my ($self, $signal, @args) = @_;
478
479 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
478 480
479 #d##TODO# stop propagating at first true, do not use sum 481 #d##TODO# stop propagating at first true, do not use sum
480 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before 482 (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before
481 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure 483 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure
482 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 484 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent
587} 589}
588 590
589sub DESTROY { 591sub DESTROY {
590 my ($self) = @_; 592 my ($self) = @_;
591 593
594 return if CFClient::in_destruct;
595
592 delete $WIDGET{$self+0}; 596 delete $WIDGET{$self+0};
593 597
594 eval { $self->destroy }; 598 eval { $self->destroy };
595 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; 599 warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/;
596} 600}
994 my ($self, $widget) = @_; 998 my ($self, $widget) = @_;
995 999
996 $self->{vp}->add ($self->{child} = $widget); 1000 $self->{vp}->add ($self->{child} = $widget);
997} 1001}
998 1002
1003sub invoke_button_up {
1004 my ($self, $ev) = @_;
1005
1006 warn "button up $ev->{button}\n";#d#
1007
1008 0
1009}
1010
999sub update_slider { 1011sub update_slider {
1000 my ($self) = @_; 1012 my ($self) = @_;
1001 1013
1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); 1014 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]);
1003} 1015}
1080 my $self = $class->SUPER::new ( 1092 my $self = $class->SUPER::new (
1081 bg => [1, 1, 1, 1], 1093 bg => [1, 1, 1, 1],
1082 border_bg => [1, 1, 1, 1], 1094 border_bg => [1, 1, 1, 1],
1083 border => 0.6, 1095 border => 0.6,
1084 can_events => 1, 1096 can_events => 1,
1085 min_w => 16, 1097 min_w => 64,
1086 min_h => 16, 1098 min_h => 32,
1087 %arg, 1099 %arg,
1088 ); 1100 );
1089 1101
1090 $self->{title_widget} = new CFClient::UI::Label 1102 $self->{title_widget} = new CFClient::UI::Label
1091 align => 0, 1103 align => 0,
1591 my ($self, $text) = @_; 1603 my ($self, $text) = @_;
1592 1604
1593 return if $self->{text} eq "T$text"; 1605 return if $self->{text} eq "T$text";
1594 $self->{text} = "T$text"; 1606 $self->{text} = "T$text";
1595 1607
1596 $self->{layout} = new CFClient::Layout if $self->{layout}->is_rgba;
1597 $self->{layout}->set_text ($text); 1608 $self->{layout}->set_text ($text);
1598 1609
1599 delete $self->{size_req}; 1610 delete $self->{size_req};
1600 $self->realloc; 1611 $self->realloc;
1601 $self->update; 1612 $self->update;
1607 return if $self->{text} eq "M$markup"; 1618 return if $self->{text} eq "M$markup";
1608 $self->{text} = "M$markup"; 1619 $self->{text} = "M$markup";
1609 1620
1610 my $rgba = $markup =~ /span.*(?:foreground|background)/; 1621 my $rgba = $markup =~ /span.*(?:foreground|background)/;
1611 1622
1612 $self->{layout} = new CFClient::Layout $rgba if $self->{layout}->is_rgba != $rgba;
1613 $self->{layout}->set_markup ($markup); 1623 $self->{layout}->set_markup ($markup);
1614 1624
1615 delete $self->{size_req}; 1625 delete $self->{size_req};
1616 $self->realloc; 1626 $self->realloc;
1617 $self->update; 1627 $self->update;
1629 1639
1630 my ($w, $h) = $self->{layout}->size; 1640 my ($w, $h) = $self->{layout}->size;
1631 1641
1632 if (exists $self->{template}) { 1642 if (exists $self->{template}) {
1633 $self->{template}->set_font ($self->{font}) if $self->{font}; 1643 $self->{template}->set_font ($self->{font}) if $self->{font};
1644 $self->{template}->set_width ($self->{max_w} || -1);
1634 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); 1645 $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE);
1635 1646
1636 my ($w2, $h2) = $self->{template}->size; 1647 my ($w2, $h2) = $self->{template}->size;
1637 1648
1638 $w = List::Util::max $w, $w2; 1649 $w = List::Util::max $w, $w2;
1672 1683
1673sub reconfigure { 1684sub reconfigure {
1674 my ($self) = @_; 1685 my ($self) = @_;
1675 1686
1676 delete $self->{size_req}; 1687 delete $self->{size_req};
1688 delete $self->{texture};
1677 1689
1678 $self->SUPER::reconfigure; 1690 $self->SUPER::reconfigure;
1679} 1691}
1680 1692
1681sub _draw { 1693sub _draw {
1682 my ($self) = @_; 1694 my ($self) = @_;
1683 1695
1684 $self->SUPER::_draw; # draw background, if applicable 1696 $self->SUPER::_draw; # draw background, if applicable
1685 1697
1686 my $tex = $self->{texture} ||= do { 1698 my $size = $self->{texture} ||= do {
1687 $self->{layout}->set_foreground (@{$self->{fg}}); 1699 $self->{layout}->set_foreground (@{$self->{fg}});
1688 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1700 $self->{layout}->set_font ($self->{font}) if $self->{font};
1689 $self->{layout}->set_width ($self->{w}); 1701 $self->{layout}->set_width ($self->{w});
1690 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1702 $self->{layout}->set_ellipsise ($self->{ellipsise});
1691 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1703 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1692 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1704 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1693 1705
1694 new_from_layout CFClient::Texture $self->{layout} 1706 [$self->{layout}->size]
1695 }; 1707 };
1696 1708
1697 unless (exists $self->{ox}) { 1709 unless (exists $self->{ox}) {
1698 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} 1710 $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x}
1699 : $self->{align} > 0 ? $self->{w} - $tex->{w} - $self->{padding_x} 1711 : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x}
1700 : ($self->{w} - $tex->{w}) * 0.5); 1712 : ($self->{w} - $size->[0]) * 0.5);
1701 1713
1702 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y} 1714 $self->{oy} = int ($self->{valign} < 0 ? $self->{padding_y}
1703 : $self->{valign} > 0 ? $self->{h} - $tex->{h} - $self->{padding_y} 1715 : $self->{valign} > 0 ? $self->{h} - $size->[1] - $self->{padding_y}
1704 : ($self->{h} - $tex->{h}) * 0.5); 1716 : ($self->{h} - $size->[1]) * 0.5);
1705 }; 1717 };
1706 1718
1707 glEnable GL_TEXTURE_2D;
1708
1709 my $w = List::Util::min $self->{w} + 4, $tex->{w}; 1719 my $w = List::Util::min $self->{w} + 4, $size->[0];
1710 my $h = List::Util::min $self->{h} + 2, $tex->{h}; 1720 my $h = List::Util::min $self->{h} + 2, $size->[1];
1711 1721
1712 if ($tex->{format} == GL_ALPHA) { 1722 $self->{layout}->render ($self->{ox}, $self->{oy});
1713 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
1714 glColor @{$self->{fg}};
1715 $tex->draw_quad_alpha ($self->{ox}, $self->{oy}, $w, $h);
1716 } else {
1717 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
1718 $tex->draw_quad_alpha_premultiplied ($self->{ox}, $self->{oy}, $w, $h);
1719 }
1720
1721 glDisable GL_TEXTURE_2D;
1722} 1723}
1723 1724
1724############################################################################# 1725#############################################################################
1725 1726
1726package CFClient::UI::EntryBase; 1727package CFClient::UI::EntryBase;
1837 1838
1838 my $idx = $self->{layout}->xy_to_index ($x, $y); 1839 my $idx = $self->{layout}->xy_to_index ($x, $y);
1839 1840
1840 # byte-index to char-index 1841 # byte-index to char-index
1841 my $text = $self->{text}; 1842 my $text = $self->{text};
1842 utf8::encode $text; 1843 utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text;
1843 $self->{cursor} = length substr $text, 0, $idx; 1844 $self->{cursor} = length $text;
1844 1845
1845 $self->_set_text ($self->{text}); 1846 $self->_set_text ($self->{text});
1846 $self->update; 1847 $self->update;
1847 1848
1848 1 1849 1
2068 my ($self) = @_; 2069 my ($self) = @_;
2069 2070
2070 (6) x 2 2071 (6) x 2
2071} 2072}
2072 2073
2074sub toggle {
2075 my ($self) = @_;
2076
2077 $self->{state} = !$self->{state};
2078 $self->emit (changed => $self->{state});
2079 $self->update;
2080}
2081
2073sub invoke_button_down { 2082sub invoke_button_down {
2074 my ($self, $ev, $x, $y) = @_; 2083 my ($self, $ev, $x, $y) = @_;
2075 2084
2076 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x} 2085 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2077 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) { 2086 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2078 $self->{state} = !$self->{state}; 2087 $self->toggle;
2079 $self->emit (changed => $self->{state});
2080 } else { 2088 } else {
2081 return 0 2089 return 0
2082 } 2090 }
2083 2091
2084 1 2092 1
2120 my $self = $class->SUPER::new ( 2128 my $self = $class->SUPER::new (
2121 can_events => 0, 2129 can_events => 0,
2122 @_, 2130 @_,
2123 ); 2131 );
2124 2132
2125 $self->{path} 2133 $self->{path} || $self->{tex}
2126 or Carp::croak "required attribute 'path' not set"; 2134 or Carp::croak "'path' or 'tex' attributes required";
2127 2135
2128 $self->{tex} = $texture_cache{$self->{path}} ||= 2136 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2129 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; 2137 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2130 2138
2131 Scalar::Util::weaken $texture_cache{$self->{path}}; 2139 Scalar::Util::weaken $texture_cache{$self->{path}};
2132 2140
2133 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2141 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2566 can_events => 0, 2574 can_events => 0,
2567 indent => 0, 2575 indent => 0,
2568 #font => default_font 2576 #font => default_font
2569 @_, 2577 @_,
2570 2578
2571 layout => (new CFClient::Layout 1), 2579 layout => (new CFClient::Layout),
2572 par => [], 2580 par => [],
2573 height => 0, 2581 height => 0,
2574 children => [ 2582 children => [
2575 (new CFClient::UI::Empty expand => 1), 2583 (new CFClient::UI::Empty expand => 1),
2576 (new CFClient::UI::Slider vertical => 1), 2584 (new CFClient::UI::Slider vertical => 1),
2737 my $y0 = $top; 2745 my $y0 = $top;
2738 my $y1 = $top + $H; 2746 my $y1 = $top + $H;
2739 2747
2740 my $y = 0; 2748 my $y = 0;
2741 2749
2742 glEnable GL_BLEND;
2743 #TODO# not correct in windows where rgba is forced off
2744 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2745
2746 for my $para (@{$self->{par}}) { 2750 for my $para (@{$self->{par}}) {
2747 my $h = $para->{h}; 2751 my $h = $para->{h};
2748 2752
2749 if ($y0 < $y + $h && $y < $y1) { 2753 if ($y0 < $y + $h && $y < $y1) {
2750 2754
2751 my $layout = $self->get_layout ($para); 2755 my $layout = $self->get_layout ($para);
2752 2756
2753 my ($w, $h, $data, $format, $internalformat) = $layout->render;
2754
2755 glRasterPos $para->{indent}, $y - $y0; 2757 $layout->render ($para->{indent}, $y - $y0);
2756 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2757 2758
2758 if (my @w = @{ $para->{widget} }) { 2759 if (my @w = @{ $para->{widget} }) {
2759 my @s = $layout->get_shapes; 2760 my @s = $layout->get_shapes;
2760 2761
2761 glDisable GL_BLEND;
2762 for (@w) { 2762 for (@w) {
2763 my ($dx, $dy) = splice @s, 0, 2, (); 2763 my ($dx, $dy) = splice @s, 0, 2, ();
2764 2764
2765 $_->{x} = $dx + $para->{indent}; 2765 $_->{x} = $dx + $para->{indent};
2766 $_->{y} = $dy + $y - $y0; 2766 $_->{y} = $dy + $y - $y0;
2767 2767
2768 $_->draw; 2768 $_->draw;
2769 } 2769 }
2770 glEnable GL_BLEND;
2771 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2772 } 2770 }
2773 } 2771 }
2774 2772
2775 $y += $h; 2773 $y += $h;
2776 } 2774 }
2777
2778 glDisable GL_BLEND;
2779 }; 2775 };
2780 }); 2776 });
2781} 2777}
2782 2778
2783sub reconfigure { 2779sub reconfigure {
3079 for my $item (@{ $self->{items} }) { 3075 for my $item (@{ $self->{items} }) {
3080 my ($widget, $cb, $tooltip) = @$item; 3076 my ($widget, $cb, $tooltip) = @$item;
3081 3077
3082 # handle various types of items, only text for now 3078 # handle various types of items, only text for now
3083 if (!ref $widget) { 3079 if (!ref $widget) {
3080 if ($widget =~ /\t/) {
3081 my ($left, $right) = split /\t/, $widget, 2;
3082
3083 $widget = new CFClient::UI::HBox
3084 can_hover => 1,
3085 can_events => 1,
3086 tooltip => $tooltip,
3087 children => [
3088 (new CFClient::UI::Label markup => $left, expand => 1),
3089 (new CFClient::UI::Label markup => $right, align => +1),
3090 ],
3091 ;
3092
3093 } else {
3084 $widget = new CFClient::UI::Label 3094 $widget = new CFClient::UI::Label
3085 can_hover => 1, 3095 can_hover => 1,
3086 can_events => 1, 3096 can_events => 1,
3087 markup => $widget, 3097 markup => $widget,
3088 tooltip => $tooltip 3098 tooltip => $tooltip;
3099 }
3089 } 3100 }
3090 3101
3091 $self->{item}{$widget} = $item; 3102 $self->{item}{$widget} = $item;
3092 3103
3093 $self->{vbox}->add ($widget); 3104 $self->{vbox}->add ($widget);
3160 3171
3161 $self->SUPER::add (@widgets); 3172 $self->SUPER::add (@widgets);
3162 3173
3163 $self->{current} = $self->{children}[0] 3174 $self->{current} = $self->{children}[0]
3164 if @{ $self->{children} }; 3175 if @{ $self->{children} };
3176}
3177
3178sub get_current_page {
3179 my ($self) = @_;
3180
3181 $self->{current}
3165} 3182}
3166 3183
3167sub set_current_page { 3184sub set_current_page {
3168 my ($self, $page_or_widget) = @_; 3185 my ($self, $page_or_widget) = @_;
3169 3186
3235 tooltip => $tooltip, 3252 tooltip => $tooltip,
3236 on_activate => sub { $self->set_current_page ($widget) }, 3253 on_activate => sub { $self->set_current_page ($widget) },
3237 ); 3254 );
3238 3255
3239 $self->{multiplexer}->add ($widget); 3256 $self->{multiplexer}->add ($widget);
3257}
3258
3259sub get_current_page {
3260 my ($self) = @_;
3261
3262 $self->{multiplexer}->get_current_page
3240} 3263}
3241 3264
3242sub set_current_page { 3265sub set_current_page {
3243 my ($self, $page) = @_; 3266 my ($self, $page) = @_;
3244 3267
3449 3472
3450############################################################################# 3473#############################################################################
3451 3474
3452package CFClient::UI::Inventory; 3475package CFClient::UI::Inventory;
3453 3476
3454our @ISA = CFClient::UI::ScrolledWindow::; 3477our @ISA = CFClient::UI::Table::;
3455 3478
3456sub new { 3479sub new {
3457 my $class = shift; 3480 my $class = shift;
3458 3481
3459 my $self = $class->SUPER::new ( 3482 my $self = $class->SUPER::new (
3460 child => (new CFClient::UI::Table col_expand => [0, 1, 0]), 3483 col_expand => [0, 1, 0],
3461 @_, 3484 @_,
3462 ); 3485 );
3463 3486
3464 $self 3487 $self
3465} 3488}
3466 3489
3467sub set_items { 3490sub set_items {
3468 my ($self, $items) = @_; 3491 my ($self, $items) = @_;
3469 3492
3470 $self->{child}->clear; 3493 $self->clear;
3471 return unless $items; 3494 return unless $items;
3472 3495
3473 my @items = sort { 3496 my @items = sort {
3474 ($a->{type} <=> $b->{type}) 3497 ($a->{type} <=> $b->{type})
3475 or ($a->{name} cmp $b->{name}) 3498 or ($a->{name} cmp $b->{name})
3479 3502
3480 my $row = 0; 3503 my $row = 0;
3481 for my $item (@items) { 3504 for my $item (@items) {
3482 CFClient::Item::update_widgets $item; 3505 CFClient::Item::update_widgets $item;
3483 3506
3484 $self->{child}->add (0, $row, $item->{face_widget}); 3507 $self->add (0, $row, $item->{face_widget});
3485 $self->{child}->add (1, $row, $item->{desc_widget}); 3508 $self->add (1, $row, $item->{desc_widget});
3486 $self->{child}->add (2, $row, $item->{weight_widget}); 3509 $self->add (2, $row, $item->{weight_widget});
3487 3510
3488 $row++; 3511 $row++;
3489 } 3512 }
3490} 3513}
3491 3514
3803 my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; 3826 my ($w, $h) = @$widget{qw(alloc_w alloc_h)};
3804 3827
3805 $w = 0 if $w < 0; 3828 $w = 0 if $w < 0;
3806 $h = 0 if $h < 0; 3829 $h = 0 if $h < 0;
3807 3830
3831 $w = max $widget->{min_w}, $w;
3832 $h = max $widget->{min_h}, $h;
3833
3834 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3835 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3836
3808 $w = int $w + 0.5; 3837 $w = int $w + 0.5;
3809 $h = int $h + 0.5; 3838 $h = int $h + 0.5;
3810 3839
3811 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { 3840 if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) {
3812 $widget->{old_w} = $widget->{w}; 3841 $widget->{old_w} = $widget->{w};

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines