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.331 by root, Sun Jul 23 04:46:02 2006 UTC vs.
Revision 1.336 by root, Sun Jul 23 16:46:06 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);
437} 437}
438 438
439# translate local coordinates to global coordinate system 439# translate local coordinates to global coordinate system
440sub coord2global { 440sub coord2global {
441 my ($self, $x, $y) = @_; 441 my ($self, $x, $y) = @_;
442
443 Carp::confess unless $self->{parent};#d#
442 444
443 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 445 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
444} 446}
445 447
446sub invoke_focus_in { 448sub invoke_focus_in {
494 496
495sub connect { 497sub connect {
496 my ($self, $signal, $cb) = @_; 498 my ($self, $signal, $cb) = @_;
497 499
498 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 }
499} 506}
500 507
501my %has_coords = ( 508my %has_coords = (
502 button_down => 1, 509 button_down => 1,
503 button_up => 1, 510 button_up => 1,
506); 513);
507 514
508sub emit { 515sub emit {
509 my ($self, $signal, @args) = @_; 516 my ($self, $signal, @args) = @_;
510 517
518 # I do not really like this solution, but I dislike duplication
519 # and needlessly verbose code, too.
511 my @append 520 my @append
512 = $has_coords{$signal} 521 = $has_coords{$signal}
513 ? $args[0]->xy ($self) 522 ? $args[0]->xy ($self)
514 : (); 523 : ();
515 524
722 if $children; 731 if $children;
723 732
724 $self 733 $self
725} 734}
726 735
736sub realloc {
737 my ($self) = @_;
738
739 $self->{force_realloc} = 1;
740 $self->{force_size_alloc} = 1;
741 $self->SUPER::realloc;
742}
743
727sub add { 744sub add {
728 my ($self, @widgets) = @_; 745 my ($self, @widgets) = @_;
729 746
730 $_->set_parent ($self) 747 $_->set_parent ($self)
731 for @widgets; 748 for @widgets;
1347sub children { 1364sub children {
1348 grep $_, map @$_, grep $_, @{ $_[0]{children} } 1365 grep $_, map @$_, grep $_, @{ $_[0]{children} }
1349} 1366}
1350 1367
1351sub add { 1368sub add {
1352 my ($self, $x, $y, $child) = @_; 1369 my ($self) = shift;
1353 1370
1371 while (@_) {
1372 my ($x, $y, $child) = splice @_, 0, 3, ();
1354 $child->set_parent ($self); 1373 $child->set_parent ($self);
1355 $self->{children}[$y][$x] = $child; 1374 $self->{children}[$y][$x] = $child;
1375 }
1356 1376
1377 $self->{force_realloc} = 1;
1378 $self->{force_size_alloc} = 1;
1357 $self->realloc; 1379 $self->realloc;
1358} 1380}
1359 1381
1360sub remove { 1382sub remove {
1361 my ($self, $child) = @_; 1383 my ($self, $child) = @_;
1613 } 1635 }
1614 1636
1615 $self 1637 $self
1616} 1638}
1617 1639
1618sub escape($) {
1619 local $_ = $_[0];
1620
1621 s/&/&/g;
1622 s/>/>/g;
1623 s/</&lt;/g;
1624
1625 $_
1626}
1627
1628sub update { 1640sub update {
1629 my ($self) = @_; 1641 my ($self) = @_;
1630 1642
1631 delete $self->{texture}; 1643 delete $self->{texture};
1632 $self->SUPER::update; 1644 $self->SUPER::update;
2041 $self->SUPER::_draw; 2053 $self->SUPER::_draw;
2042} 2054}
2043 2055
2044############################################################################# 2056#############################################################################
2045 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
2046package CFClient::UI::ImageButton; 2207package CFClient::UI::ImageButton;
2047 2208
2048our @ISA = CFClient::UI::Image::; 2209our @ISA = CFClient::UI::Image::;
2049 2210
2050use CFClient::OpenGL; 2211use CFClient::OpenGL;
2073 $self->emit ("activate") 2234 $self->emit ("activate")
2074 if $x >= 0 && $x < $self->{w} 2235 if $x >= 0 && $x < $self->{w}
2075 && $y >= 0 && $y < $self->{h}; 2236 && $y >= 0 && $y < $self->{h};
2076 2237
2077 1 2238 1
2078}
2079
2080#############################################################################
2081
2082package CFClient::UI::CheckBox;
2083
2084our @ISA = CFClient::UI::DrawBG::;
2085
2086my @tex =
2087 map { new_from_file CFClient::Texture CFClient::find_rcfile $_, mipmap => 1 }
2088 qw(c1_checkbox_bg.png c1_checkbox_active.png);
2089
2090use CFClient::OpenGL;
2091
2092sub new {
2093 my $class = shift;
2094
2095 $class->SUPER::new (
2096 padding_x => 2,
2097 padding_y => 2,
2098 fg => [1, 1, 1],
2099 active_fg => [1, 1, 0],
2100 bg => [0, 0, 0, 0.2],
2101 active_bg => [1, 1, 1, 0.5],
2102 state => 0,
2103 can_hover => 1,
2104 @_
2105 )
2106}
2107
2108sub size_request {
2109 my ($self) = @_;
2110
2111 (6) x 2
2112}
2113
2114sub toggle {
2115 my ($self) = @_;
2116
2117 $self->{state} = !$self->{state};
2118 $self->emit (changed => $self->{state});
2119 $self->update;
2120}
2121
2122sub invoke_button_down {
2123 my ($self, $ev, $x, $y) = @_;
2124
2125 if ($x >= $self->{padding_x} && $x < $self->{w} - $self->{padding_x}
2126 && $y >= $self->{padding_y} && $y < $self->{h} - $self->{padding_y}) {
2127 $self->toggle;
2128 } else {
2129 return 0
2130 }
2131
2132 1
2133}
2134
2135sub _draw {
2136 my ($self) = @_;
2137
2138 $self->SUPER::_draw;
2139
2140 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0;
2141
2142 my ($w, $h) = @$self{qw(w h)};
2143
2144 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2145
2146 glColor @{ $FOCUS == $self ? $self->{active_fg} : $self->{fg} };
2147
2148 my $tex = $self->{state} ? $tex[1] : $tex[0];
2149
2150 glEnable GL_TEXTURE_2D;
2151 $tex->draw_quad_alpha (0, 0, $s, $s);
2152 glDisable GL_TEXTURE_2D;
2153}
2154
2155#############################################################################
2156
2157package CFClient::UI::Image;
2158
2159our @ISA = CFClient::UI::Base::;
2160
2161use CFClient::OpenGL;
2162
2163our %texture_cache;
2164
2165sub new {
2166 my $class = shift;
2167
2168 my $self = $class->SUPER::new (
2169 can_events => 0,
2170 @_,
2171 );
2172
2173 $self->{path} || $self->{tex}
2174 or Carp::croak "'path' or 'tex' attributes required";
2175
2176 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2177 new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1;
2178
2179 Scalar::Util::weaken $texture_cache{$self->{path}};
2180
2181 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2182
2183 $self
2184}
2185
2186sub size_request {
2187 my ($self) = @_;
2188
2189 ($self->{tex}{w}, $self->{tex}{h})
2190}
2191
2192sub _draw {
2193 my ($self) = @_;
2194
2195 my $tex = $self->{tex};
2196
2197 my ($w, $h) = ($self->{w}, $self->{h});
2198
2199 if ($self->{rot90}) {
2200 glRotate 90, 0, 0, 1;
2201 glTranslate 0, -$self->{w}, 0;
2202
2203 ($w, $h) = ($h, $w);
2204 }
2205
2206 glEnable GL_TEXTURE_2D;
2207 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE;
2208
2209 $tex->draw_quad_alpha (0, 0, $w, $h);
2210
2211 glDisable GL_TEXTURE_2D;
2212} 2239}
2213 2240
2214############################################################################# 2241#############################################################################
2215 2242
2216package CFClient::UI::VGauge; 2243package CFClient::UI::VGauge;
2506sub invoke_mouse_wheel { 2533sub invoke_mouse_wheel {
2507 my ($self, $ev) = @_; 2534 my ($self, $ev) = @_;
2508 2535
2509 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2536 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2510 2537
2511 $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);
2512 2539
2513 ! ! $delta 2540 ! ! $delta
2514} 2541}
2515 2542
2516sub update { 2543sub update {
2986 3013
2987 $self->{root}->on_post_alloc ("move_$self" => sub { 3014 $self->{root}->on_post_alloc ("move_$self" => sub {
2988 my $widget = $self->{owner} 3015 my $widget = $self->{owner}
2989 or return; 3016 or return;
2990 3017
3018 if ($widget->{visible}) {
2991 my ($x, $y) = $widget->coord2global ($widget->{w}, 0); 3019 my ($x, $y) = $widget->coord2global ($widget->{w}, 0);
2992 3020
2993 ($x, $y) = $widget->coord2global (-$self->{w}, 0) 3021 ($x, $y) = $widget->coord2global (-$self->{w}, 0)
2994 if $x + $self->{w} > $self->{root}{w}; 3022 if $x + $self->{w} > $self->{root}{w};
2995 3023
2996 $self->move_abs ($x, $y); 3024 $self->move_abs ($x, $y);
3025 } else {
3026 $self->hide;
3027 }
2997 }); 3028 });
2998} 3029}
2999 3030
3000sub _draw { 3031sub _draw {
3001 my ($self) = @_; 3032 my ($self) = @_;
3539sub new { 3570sub new {
3540 my $class = shift; 3571 my $class = shift;
3541 3572
3542 my $self = $class->SUPER::new ( 3573 my $self = $class->SUPER::new (
3543 col_expand => [0, 1, 0], 3574 col_expand => [0, 1, 0],
3575 items => [],
3544 @_, 3576 @_,
3545 ); 3577 );
3546 3578
3579 $self->set_sort_order (undef);
3580
3547 $self 3581 $self
3582}
3583
3584sub update_items {
3585 my ($self) = @_;
3586
3587 $self->clear;
3588
3589 my @item = $self->{sort}->(@{ $self->{items} });
3590
3591 my @adds;
3592 my $row = 0;
3593 for my $item ($self->{sort}->(@{ $self->{items} })) {
3594 CFClient::Item::update_widgets $item;
3595
3596 push @adds, 0, $row, $item->{face_widget};
3597 push @adds, 1, $row, $item->{desc_widget};
3598 push @adds, 2, $row, $item->{weight_widget};
3599
3600 $row++;
3601 }
3602
3603 $self->add (@adds);
3604}
3605
3606sub set_sort_order {
3607 my ($self, $order) = @_;
3608
3609 $self->{sort} = $order ||= sub {
3610 sort {
3611 $a->{type} <=> $b->{type}
3612 or $a->{name} cmp $b->{name}
3613 } @_
3614 };
3615
3616 $self->update_items;
3548} 3617}
3549 3618
3550sub set_items { 3619sub set_items {
3551 my ($self, $items) = @_; 3620 my ($self, $items) = @_;
3552 3621
3553 $self->clear; 3622 $self->{items} = [$items ? values %$items : ()];
3554 return unless $items; 3623 $self->update_items;
3555
3556 my @items = sort {
3557 ($a->{type} <=> $b->{type})
3558 or ($a->{name} cmp $b->{name})
3559 } values %$items;
3560
3561 $self->{real_items} = \@items;
3562
3563 my $row = 0;
3564 for my $item (@items) {
3565 CFClient::Item::update_widgets $item;
3566
3567 $self->add (0, $row, $item->{face_widget});
3568 $self->add (1, $row, $item->{desc_widget});
3569 $self->add (2, $row, $item->{weight_widget});
3570
3571 $row++;
3572 }
3573} 3624}
3574 3625
3575############################################################################# 3626#############################################################################
3576 3627
3577package CFClient::UI::SpellList; 3628package CFClient::UI::SpellList;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines