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.330 by root, Sun Jul 23 04:37:51 2006 UTC vs.
Revision 1.338 by root, Mon Jul 24 08:23:28 2006 UTC

367 my ($self, $x, $y, $w, $h) = @_; 367 my ($self, $x, $y, $w, $h) = @_;
368 368
369 if ($self->{aspect}) { 369 if ($self->{aspect}) {
370 my ($ow, $oh) = ($w, $h); 370 my ($ow, $oh) = ($w, $h);
371 371
372 $w = List::Util::min $w, int $h * $self->{aspect}; 372 $w = List::Util::min $w, CFClient::ceil $h * $self->{aspect};
373 $h = List::Util::min $h, int $w / $self->{aspect}; 373 $h = List::Util::min $h, CFClient::ceil $w / $self->{aspect};
374 374
375 # use alignment to adjust x, y 375 # use alignment to adjust x, y
376 376
377 $x += int 0.5 * ($ow - $w); 377 $x += int 0.5 * ($ow - $w);
378 $y += int 0.5 * ($oh - $h); 378 $y += int 0.5 * ($oh - $h);
429 429
430# translate global coordinates to local coordinate system 430# translate global coordinates to local coordinate system
431sub coord2local { 431sub coord2local {
432 my ($self, $x, $y) = @_; 432 my ($self, $x, $y) = @_;
433 433
434 Carp::confess unless $self->{parent};#d#
435
434 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 436 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
435} 437}
436 438
437# translate local coordinates to global coordinate system 439# translate local coordinates to global coordinate system
438sub coord2global { 440sub coord2global {
439 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
442
443 Carp::confess unless $self->{parent};#d#
440 444
441 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 445 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
442} 446}
443 447
444sub invoke_focus_in { 448sub invoke_focus_in {
492 496
493sub connect { 497sub connect {
494 my ($self, $signal, $cb) = @_; 498 my ($self, $signal, $cb) = @_;
495 499
496 push @{ $self->{signal_cb}{$signal} }, $cb; 500 push @{ $self->{signal_cb}{$signal} }, $cb;
501
502 defined wantarray and CFClient::guard {
503 @{ $self->{signal_cb}{$signal} } = grep $_ != $cb,
504 @{ $self->{signal_cb}{$signal} };
505 }
497} 506}
507
508my %has_coords = (
509 button_down => 1,
510 button_up => 1,
511 mouse_motion => 1,
512 mouse_wheel => 1,
513);
498 514
499sub emit { 515sub emit {
500 my ($self, $signal, @args) = @_; 516 my ($self, $signal, @args) = @_;
501 517
518 # I do not really like this solution, but I dislike duplication
519 # and needlessly verbose code, too.
502 my @append 520 my @append
503 = ref $args[0] && $args[0]->isa ("CFClient::UI::Event") 521 = $has_coords{$signal}
504 ? $args[0]->xy ($self) 522 ? $args[0]->xy ($self)
505 : (); 523 : ();
506 524
507 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 525 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
508 526
713 if $children; 731 if $children;
714 732
715 $self 733 $self
716} 734}
717 735
736sub realloc {
737 my ($self) = @_;
738
739 $self->{force_realloc} = 1;
740 $self->{force_size_alloc} = 1;
741 $self->SUPER::realloc;
742}
743
718sub add { 744sub add {
719 my ($self, @widgets) = @_; 745 my ($self, @widgets) = @_;
720 746
721 $_->set_parent ($self) 747 $_->set_parent ($self)
722 for @widgets; 748 for @widgets;
1338sub children { 1364sub children {
1339 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1365 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1340} 1366}
1341 1367
1342sub add { 1368sub add {
1343 my ($self, $x, $y, $child) = @_; 1369 my ($self) = shift;
1344 1370
1371 while (@_) {
1372 my ($x, $y, $child) = splice @_, 0, 3, ();
1345 $child->set_parent ($self); 1373 $child->set_parent ($self);
1346 $self->{children}[$y][$x] = $child; 1374 $self->{children}[$y][$x] = $child;
1375 }
1347 1376
1377 $self->{force_realloc} = 1;
1378 $self->{force_size_alloc} = 1;
1348 $self->realloc; 1379 $self->realloc;
1349} 1380}
1350 1381
1351sub remove { 1382sub remove {
1352 my ($self, $child) = @_; 1383 my ($self, $child) = @_;
1604 } 1635 }
1605 1636
1606 $self 1637 $self
1607} 1638}
1608 1639
1609sub escape($) {
1610 local $_ = $_[0];
1611
1612 s/&/&/g;
1613 s/>/>/g;
1614 s/</&lt;/g;
1615
1616 $_
1617}
1618
1619sub update { 1640sub update {
1620 my ($self) = @_; 1641 my ($self) = @_;
1621 1642
1622 delete $self->{texture}; 1643 delete $self->{texture};
1623 $self->SUPER::update; 1644 $self->SUPER::update;
2032 $self->SUPER::_draw; 2053 $self->SUPER::_draw;
2033} 2054}
2034 2055
2035############################################################################# 2056#############################################################################
2036 2057
2058package CFClient::UI::CheckBox;
2059
2060our @ISA = CFClient::UI::DrawBG::;
2061
2062my @tex =
2063 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2064 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2065
2066use CFClient::OpenGL;
2067
2068sub new {
2069 my $class = shift;
2070
2071 $class->SUPER::new (
2072 padding_x => 2,
2073 padding_y => 2,
2074 fg => [1, 1, 1],
2075 active_fg => [1, 1, 0],
2076 bg => [0, 0, 0, 0.2],
2077 active_bg => [1, 1, 1, 0.5],
2078 state => 0,
2079 can_hover => 1,
2080 @_
2081 )
2082}
2083
2084sub size_request {
2085 my ($self) = @_;
2086
2087 (6) x 2
2088}
2089
2090sub toggle {
2091 my ($self) = @_;
2092
2093 $self->{state} = !$self->{state};
2094 $self->emit (changed => $self->{state});
2095 $self->update;
2096}
2097
2098sub invoke_button_down {
2099 my ($self, $ev, $x, $y) = @_;
2100
2101 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2102 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2103 $self->toggle;
2104 } else {
2105 return 0
2106 }
2107
2108 1
2109}
2110
2111sub _draw {
2112 my ($self) = @_;
2113
2114 $self->SUPER::_draw;
2115
2116 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2117
2118 my ($w, $h) = @$self{qw(w h)};
2119
2120 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2121
2122 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2123
2124 my $tex = $self->{state} ? $tex[1] : $tex[0];
2125
2126 glEnable GL_TEXTURE_2D;
2127 $tex->draw_quad_alpha (0, 0, $s, $s);
2128 glDisable GL_TEXTURE_2D;
2129}
2130
2131#############################################################################
2132
2133package CFClient::UI::Image;
2134
2135our @ISA = CFClient::UI::Base::;
2136
2137use CFClient::OpenGL;
2138
2139our %texture_cache;
2140
2141sub new {
2142 my $class = shift;
2143
2144 my $self = $class->SUPER::new (
2145 can_events => 0,
2146 @_,
2147 );
2148
2149 $self->{path} || $self->{tex}
2150 or Carp::croak "'path' or 'tex' attributes required";
2151
2152 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2153 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2154
2155 Scalar::Util::weaken $texture_cache{$self->{path}};
2156
2157 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2158
2159 $self
2160}
2161
2162sub STORABLE_freeze {
2163 my ($self, $cloning) = @_;
2164
2165 $self->{path}
2166 or die "cannot serialise CFClient::UI::Image on non-loadable images\n";
2167
2168 $self->{path}
2169}
2170
2171sub STORABLE_attach {
2172 my ($self, $cloning, $path) = @_;
2173
2174 $self->new (path => $path)
2175}
2176
2177sub size_request {
2178 my ($self) = @_;
2179
2180 ($self->{tex}{w}, $self->{tex}{h})
2181}
2182
2183sub _draw {
2184 my ($self) = @_;
2185
2186 my $tex = $self->{tex};
2187
2188 my ($w, $h) = ($self->{w}, $self->{h});
2189
2190 if ($self->{rot90}) {
2191 glRotate 90, 0, 0, 1;
2192 glTranslate 0, -$self->{w}, 0;
2193
2194 ($w, $h) = ($h, $w);
2195 }
2196
2197 glEnable GL_TEXTURE_2D;
2198 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2199
2200 $tex->draw_quad (0, 0, $w, $h);
2201
2202 glDisable GL_TEXTURE_2D;
2203}
2204
2205#############################################################################
2206
2037package CFClient::UI::ImageButton; 2207package CFClient::UI::ImageButton;
2038 2208
2039our @ISA = CFClient::UI::Image::; 2209our @ISA = CFClient::UI::Image::;
2040 2210
2041use CFClient::OpenGL; 2211use CFClient::OpenGL;
2064 $self->emit ("activate") 2234 $self->emit ("activate")
2065 if $x >= 0 && $x < $self->{w} 2235 if $x >= 0 && $x < $self->{w}
2066 && $y >= 0 && $y < $self->{h}; 2236 && $y >= 0 && $y < $self->{h};
2067 2237
2068 1 2238 1
2069}
2070
2071#############################################################################
2072
2073package CFClient::UI::CheckBox;
2074
2075our @ISA = CFClient::UI::DrawBG::;
2076
2077my @tex =
2078 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2079 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2080
2081use CFClient::OpenGL;
2082
2083sub new {
2084 my $class = shift;
2085
2086 $class->SUPER::new (
2087 padding_x => 2,
2088 padding_y => 2,
2089 fg => [1, 1, 1],
2090 active_fg => [1, 1, 0],
2091 bg => [0, 0, 0, 0.2],
2092 active_bg => [1, 1, 1, 0.5],
2093 state => 0,
2094 can_hover => 1,
2095 @_
2096 )
2097}
2098
2099sub size_request {
2100 my ($self) = @_;
2101
2102 (6) x 2
2103}
2104
2105sub toggle {
2106 my ($self) = @_;
2107
2108 $self->{state} = !$self->{state};
2109 $self->emit (changed => $self->{state});
2110 $self->update;
2111}
2112
2113sub invoke_button_down {
2114 my ($self, $ev, $x, $y) = @_;
2115
2116 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2117 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2118 $self->toggle;
2119 } else {
2120 return 0
2121 }
2122
2123 1
2124}
2125
2126sub _draw {
2127 my ($self) = @_;
2128
2129 $self->SUPER::_draw;
2130
2131 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2132
2133 my ($w, $h) = @$self{qw(w h)};
2134
2135 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2136
2137 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2138
2139 my $tex = $self->{state} ? $tex[1] : $tex[0];
2140
2141 glEnable GL_TEXTURE_2D;
2142 $tex->draw_quad_alpha (0, 0, $s, $s);
2143 glDisable GL_TEXTURE_2D;
2144}
2145
2146#############################################################################
2147
2148package CFClient::UI::Image;
2149
2150our @ISA = CFClient::UI::Base::;
2151
2152use CFClient::OpenGL;
2153
2154our %texture_cache;
2155
2156sub new {
2157 my $class = shift;
2158
2159 my $self = $class->SUPER::new (
2160 can_events => 0,
2161 @_,
2162 );
2163
2164 $self->{path} || $self->{tex}
2165 or Carp::croak "'path' or 'tex' attributes required";
2166
2167 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2168 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2169
2170 Scalar::Util::weaken $texture_cache{$self->{path}};
2171
2172 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2173
2174 $self
2175}
2176
2177sub size_request {
2178 my ($self) = @_;
2179
2180 ($self->{tex}{w}, $self->{tex}{h})
2181}
2182
2183sub _draw {
2184 my ($self) = @_;
2185
2186 my $tex = $self->{tex};
2187
2188 my ($w, $h) = ($self->{w}, $self->{h});
2189
2190 if ($self->{rot90}) {
2191 glRotate 90, 0, 0, 1;
2192 glTranslate 0, -$self->{w}, 0;
2193
2194 ($w, $h) = ($h, $w);
2195 }
2196
2197 glEnable GL_TEXTURE_2D;
2198 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2199
2200 $tex->draw_quad_alpha (0, 0, $w, $h);
2201
2202 glDisable GL_TEXTURE_2D;
2203} 2239}
2204 2240
2205############################################################################# 2241#############################################################################
2206 2242
2207package CFClient::UI::VGauge; 2243package CFClient::UI::VGauge;
2497sub invoke_mouse_wheel { 2533sub invoke_mouse_wheel {
2498 my ($self, $ev) = @_; 2534 my ($self, $ev) = @_;
2499 2535
2500 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2536 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2501 2537
2502 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.1); 2538 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2);
2503 2539
2504 ! ! $delta 2540 ! ! $delta
2505} 2541}
2506 2542
2507sub update { 2543sub update {
2719 $self->{height} = 0; 2755 $self->{height} = 0;
2720 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]); 2756 $self->{children}[1]->set_range ([0, 0, 0, 1, 1]);
2721} 2757}
2722 2758
2723sub add_paragraph { 2759sub add_paragraph {
2724 my ($self, $color, $para, $indent) = @_; 2760 my $self = shift;
2725 2761
2726 my ($text, @w) = ref $para ? @$para : $para; 2762 for my $para (@_) {
2727
2728 $para = { 2763 $para = {
2764 fg => [1, 1, 1, 1],
2765 indent => 0,
2766 markup => "",
2767 widget => [],
2768 ref $para ? %$para : (markup => $para),
2729 w => 1e10, 2769 w => 1e10,
2730 wrapped => 1, 2770 wrapped => 1,
2731 fg => $color,
2732 indent => $indent,
2733 markup => $text,
2734 widget => \@w,
2735 }; 2771 };
2736 2772
2737 $self->add (@w) if @w; 2773 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2738 push @{$self->{par}}, $para; 2774 push @{$self->{par}}, $para;
2775 }
2739 2776
2740 $self->{need_reflow}++; 2777 $self->{need_reflow}++;
2741 $self->update; 2778 $self->update;
2742} 2779}
2743 2780
2977 3014
2978 $self->{root}->on_post_alloc ("move_$self" => sub { 3015 $self->{root}->on_post_alloc ("move_$self" => sub {
2979 my $widget = $self->{owner} 3016 my $widget = $self->{owner}
2980 or return; 3017 or return;
2981 3018
3019 if ($widget->{visible}) {
2982 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3020 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2983 3021
2984 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3022 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2985 if $x + $self->{w} > $self->{root}{w}; 3023 if $x + $self->{w} > $self->{root}{w};
2986 3024
2987 $self->move_abs ($x, $y); 3025 $self->move_abs ($x, $y);
3026 } else {
3027 $self->hide;
3028 }
2988 }); 3029 });
2989} 3030}
2990 3031
2991sub _draw { 3032sub _draw {
2992 my ($self) = @_; 3033 my ($self) = @_;
3018 3059
3019############################################################################# 3060#############################################################################
3020 3061
3021package CFClient::UI::Face; 3062package CFClient::UI::Face;
3022 3063
3023our @ISA = CFClient::UI::Base::; 3064our @ISA = CFClient::UI::DrawBG::;
3024 3065
3025use CFClient::OpenGL; 3066use CFClient::OpenGL;
3026 3067
3027sub new { 3068sub new {
3028 my $class = shift; 3069 my $class = shift;
3064 3105
3065sub _draw { 3106sub _draw {
3066 my ($self) = @_; 3107 my ($self) = @_;
3067 3108
3068 return unless $::CONN; 3109 return unless $::CONN;
3110
3111 $self->SUPER::_draw;
3069 3112
3070 my $face; 3113 my $face;
3071 3114
3072 if ($self->{frame}) { 3115 if ($self->{frame}) {
3073 my $anim = $::CONN->{anim}[$self->{anim}]; 3116 my $anim = $::CONN->{anim}[$self->{anim}];
3320 $self->emit (page_changed => $self->{multiplexer}{current}); 3363 $self->emit (page_changed => $self->{multiplexer}{current});
3321} 3364}
3322 3365
3323############################################################################# 3366#############################################################################
3324 3367
3325package CFClient::UI::Combobox; 3368package CFClient::UI::Selector;
3326 3369
3327use utf8; 3370use utf8;
3328 3371
3329our @ISA = CFClient::UI::Button::; 3372our @ISA = CFClient::UI::Button::;
3330 3373
3530sub new { 3573sub new {
3531 my $class = shift; 3574 my $class = shift;
3532 3575
3533 my $self = $class->SUPER::new ( 3576 my $self = $class->SUPER::new (
3534 col_expand => [0, 1, 0], 3577 col_expand => [0, 1, 0],
3578 items => [],
3535 @_, 3579 @_,
3536 ); 3580 );
3537 3581
3582 $self->set_sort_order (undef);
3583
3538 $self 3584 $self
3585}
3586
3587sub update_items {
3588 my ($self) = @_;
3589
3590 $self->clear;
3591
3592 my @item = $self->{sort}->(@{ $self->{items} });
3593
3594 my @adds;
3595 my $row = 0;
3596 for my $item ($self->{sort}->(@{ $self->{items} })) {
3597 CFClient::Item::update_widgets $item;
3598
3599 push @adds, 0, $row, $item->{face_widget};
3600 push @adds, 1, $row, $item->{desc_widget};
3601 push @adds, 2, $row, $item->{weight_widget};
3602
3603 $row++;
3604 }
3605
3606 $self->add (@adds);
3607}
3608
3609sub set_sort_order {
3610 my ($self, $order) = @_;
3611
3612 $self->{sort} = $order ||= sub {
3613 sort {
3614 $a->{type} <=> $b->{type}
3615 or $a->{name} cmp $b->{name}
3616 } @_
3617 };
3618
3619 $self->update_items;
3539} 3620}
3540 3621
3541sub set_items { 3622sub set_items {
3542 my ($self, $items) = @_; 3623 my ($self, $items) = @_;
3543 3624
3544 $self->clear; 3625 $self->{items} = [$items ? values %$items : ()];
3545 return unless $items; 3626 $self->update_items;
3546
3547 my @items = sort {
3548 ($a->{type} <=> $b->{type})
3549 or ($a->{name} cmp $b->{name})
3550 } values %$items;
3551
3552 $self->{real_items} = \@items;
3553
3554 my $row = 0;
3555 for my $item (@items) {
3556 CFClient::Item::update_widgets $item;
3557
3558 $self->add (0, $row, $item->{face_widget});
3559 $self->add (1, $row, $item->{desc_widget});
3560 $self->add (2, $row, $item->{weight_widget});
3561
3562 $row++;
3563 }
3564} 3627}
3565 3628
3566############################################################################# 3629#############################################################################
3567 3630
3568package CFClient::UI::SpellList; 3631package CFClient::UI::SpellList;
3880 $h = 0 if $h < 0; 3943 $h = 0 if $h < 0;
3881 3944
3882 $w = max $widget->{min_w}, $w; 3945 $w = max $widget->{min_w}, $w;
3883 $h = max $widget->{min_h}, $h; 3946 $h = max $widget->{min_h}, $h;
3884 3947
3948# $w = min $self->{w} - $widget->{x}, $w if $self->{w};
3949# $h = min $self->{h} - $widget->{y}, $h if $self->{h};
3950
3885 $w = min $widget->{max_w}, $w if exists $widget->{max_w}; 3951 $w = min $widget->{max_w}, $w if exists $widget->{max_w};
3886 $h = min $widget->{max_h}, $h if exists $widget->{max_h}; 3952 $h = min $widget->{max_h}, $h if exists $widget->{max_h};
3887 3953
3888 $w = int $w + 0.5; 3954 $w = int $w + 0.5;
3889 $h = int $h + 0.5; 3955 $h = int $h + 0.5;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines