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.361 by root, Sat Dec 9 02:21:25 2006 UTC vs.
Revision 1.367 by root, Fri Apr 27 00:59:14 2007 UTC

1package CFPlus::UI; 1package CFPlus::UI;
2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use Scalar::Util ();
7use List::Util (); 6use List::Util ();
8use Event; 7use Event;
9 8
10use CFPlus; 9use CFPlus;
11use CFPlus::Pod; 10use CFPlus::Pod;
23our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub {
24 if (!$GRAB) { 23 if (!$GRAB) {
25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
26 if (length $widget->{tooltip}) { 25 if (length $widget->{tooltip}) {
27 if ($TOOLTIP->{owner} != $widget) { 26 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
28 $TOOLTIP->hide; 28 $TOOLTIP->hide;
29 29
30 $TOOLTIP->{owner} = $widget; 30 $TOOLTIP->{owner} = $widget;
31 $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner};
31 32
32 return if $ENV{CFPLUS_DEBUG} & 8; 33 return if $ENV{CFPLUS_DEBUG} & 8;
33 34
34 my $tip = $widget->{tooltip}; 35 my $tip = $widget->{tooltip};
35 36
43 } 44 }
44 } 45 }
45 } 46 }
46 47
47 $TOOLTIP->hide; 48 $TOOLTIP->hide;
49 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
48 delete $TOOLTIP->{owner}; 50 delete $TOOLTIP->{owner};
49}); 51});
50 52
51sub get_layout { 53sub get_layout {
52 my $layout; 54 my $layout;
235 h => undef, 237 h => undef,
236 can_events => 1, 238 can_events => 1,
237 @_ 239 @_
238 }, $class; 240 }, $class;
239 241
240 Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); 242 CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self);
241 243
242 for (keys %$self) { 244 for (keys %$self) {
243 if (/^on_(.*)$/) { 245 if (/^on_(.*)$/) {
244 $self->connect ($1 => delete $self->{$_}); 246 $self->connect ($1 => delete $self->{$_});
245 } 247 }
530 ? $args[0]->xy ($self) 532 ? $args[0]->xy ($self)
531 : (); 533 : ();
532 534
533 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# 535 #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d#
534 536
535 #d##TODO# stop propagating at first true, do not use sum 537 for my $cb (
536 (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before 538 @{$self->{signal_cb}{$signal} || []}, # before
537 || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure 539 ($self->can ("invoke_$signal") || sub { 1 }), # closure
540 ) {
541 return $cb->($self, @args, @append) || next;
542 }
543
544 # parent
538 || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent 545 $self->{parent} && $self->{parent}->emit ($signal, @args)
539} 546}
540 547
541sub find_widget { 548sub find_widget {
542 my ($self, $x, $y) = @_; 549 my ($self, $x, $y) = @_;
543 550
551} 558}
552 559
553sub set_parent { 560sub set_parent {
554 my ($self, $parent) = @_; 561 my ($self, $parent) = @_;
555 562
556 Scalar::Util::weaken ($self->{parent} = $parent); 563 CFPlus::weaken ($self->{parent} = $parent);
557 $self->set_visible if $parent->{visible}; 564 $self->set_visible if $parent->{visible};
558} 565}
559 566
560sub realloc { 567sub realloc {
561 my ($self) = @_; 568 my ($self) = @_;
640 my ($self) = @_; 647 my ($self) = @_;
641 648
642 warn "no draw defined for $self\n"; 649 warn "no draw defined for $self\n";
643} 650}
644 651
652my $cntx;#d#
645sub DESTROY { 653sub DESTROY {
646 my ($self) = @_; 654 my ($self) = @_;
647 655
648 return if CFPlus::in_destruct; 656 return if CFPlus::in_destruct;
649 657
1962 my $sym = $ev->{sym}; 1970 my $sym = $ev->{sym};
1963 my $uni = $ev->{unicode}; 1971 my $uni = $ev->{unicode};
1964 1972
1965 my $text = $self->get_text; 1973 my $text = $self->get_text;
1966 1974
1975 $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text;
1976
1967 if ($uni == 8) { 1977 if ($uni == 8) {
1968 substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; 1978 substr $text, --$self->{cursor}, 1, "" if $self->{cursor};
1969 } elsif ($uni == 127) { 1979 } elsif ($uni == 127) {
1970 substr $text, $self->{cursor}, 1, ""; 1980 substr $text, $self->{cursor}, 1, "";
1971 } elsif ($sym == CFPlus::SDLK_LEFT) { 1981 } elsif ($sym == CFPlus::SDLK_LEFT) {
1972 --$self->{cursor} if $self->{cursor}; 1982 --$self->{cursor} if $self->{cursor};
1973 } elsif ($sym == CFPlus::SDLK_RIGHT) { 1983 } elsif ($sym == CFPlus::SDLK_RIGHT) {
1974 ++$self->{cursor} if $self->{cursor} < length $self->{text}; 1984 ++$self->{cursor} if $self->{cursor} < length $self->{text};
1975 } elsif ($sym == CFPlus::SDLK_HOME) { 1985 } elsif ($sym == CFPlus::SDLK_HOME) {
1986 # what a hack
1976 $self->{cursor} = 0; 1987 $self->{cursor} =
1988 (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/
1989 ? length $1
1990 : 0;
1977 } elsif ($sym == CFPlus::SDLK_END) { 1991 } elsif ($sym == CFPlus::SDLK_END) {
1992 # uh, again
1993 $self->{cursor} =
1994 (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/
1978 $self->{cursor} = length $text; 1995 ? $self->{cursor} + length $1
1996 : length $self->{text};
1979 } elsif ($uni == 21) { # ctrl-u 1997 } elsif ($uni == 21) { # ctrl-u
1980 $text = ""; 1998 $text = "";
1981 $self->{cursor} = 0; 1999 $self->{cursor} = 0;
1982 } elsif ($uni == 27) { 2000 } elsif ($uni == 27) {
1983 $self->emit ('escape'); 2001 $self->emit ('escape');
1984 } elsif ($uni >= 0x20 || $uni == 0x0d) { 2002 } elsif ($uni == 0x0d) {
2003 substr $text, $self->{cursor}++, 0, "\012";
2004 } elsif ($uni >= 0x20) {
1985 substr $text, $self->{cursor}++, 0, chr $uni; 2005 substr $text, $self->{cursor}++, 0, chr $uni;
1986 } else { 2006 } else {
1987 return 0; 2007 return 0;
1988 } 2008 }
1989 2009
2310 or Carp::croak "'path' or 'tex' attributes required"; 2330 or Carp::croak "'path' or 'tex' attributes required";
2311 2331
2312 $self->{tex} ||= $texture_cache{$self->{path}} ||= 2332 $self->{tex} ||= $texture_cache{$self->{path}} ||=
2313 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; 2333 new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1;
2314 2334
2315 Scalar::Util::weaken $texture_cache{$self->{path}}; 2335 CFPlus::weaken $texture_cache{$self->{path}};
2316 2336
2317 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; 2337 $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h};
2318 2338
2319 $self 2339 $self
2320} 2340}
2693sub invoke_mouse_wheel { 2713sub invoke_mouse_wheel {
2694 my ($self, $ev) = @_; 2714 my ($self, $ev) = @_;
2695 2715
2696 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; 2716 my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx};
2697 2717
2718 my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2;
2719
2698 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); 2720 $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart);
2699 2721
2700 ! ! $delta 2722 ! ! $delta
2701} 2723}
2702 2724
2703sub update { 2725sub update {
2813 #font => default_font 2835 #font => default_font
2814 @_, 2836 @_,
2815 2837
2816 layout => (new CFPlus::Layout), 2838 layout => (new CFPlus::Layout),
2817 par => [], 2839 par => [],
2840 max_par => 0,
2818 height => 0, 2841 height => 0,
2819 children => [ 2842 children => [
2820 (new CFPlus::UI::Empty expand => 1), 2843 (new CFPlus::UI::Empty expand => 1),
2821 (new CFPlus::UI::Slider vertical => 1), 2844 (new CFPlus::UI::Slider vertical => 1),
2822 ], 2845 ],
2945 wrapped => 1, 2968 wrapped => 1,
2946 }; 2969 };
2947 2970
2948 $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; 2971 $self->add (@{ $para->{widget} }) if @{ $para->{widget} };
2949 push @{$self->{par}}, $para; 2972 push @{$self->{par}}, $para;
2973 }
2974
2975 if (my $max = $self->{max_par}) {
2976 shift @{$self->{par}} while @{$self->{par}} > $max;
2950 } 2977 }
2951 2978
2952 $self->{need_reflow}++; 2979 $self->{need_reflow}++;
2953 $self->update; 2980 $self->update;
2954} 2981}
3268 can_events => 0, 3295 can_events => 0,
3269 @_, 3296 @_,
3270 ); 3297 );
3271 3298
3272 if ($self->{anim} && $self->{animspeed}) { 3299 if ($self->{anim} && $self->{animspeed}) {
3273 Scalar::Util::weaken (my $widget = $self); 3300 CFPlus::weaken (my $widget = $self);
3274 3301
3275 $self->{timer} = Event->timer ( 3302 $self->{timer} = Event->timer (
3276 at => $self->{animspeed} * int $::NOW / $self->{animspeed}, 3303 at => $self->{animspeed} * int $::NOW / $self->{animspeed},
3277 hard => 1, 3304 hard => 1,
3278 interval => $self->{animspeed}, 3305 interval => $self->{animspeed},
3532} 3559}
3533 3560
3534sub add { 3561sub add {
3535 my ($self, $title, $widget, $tooltip) = @_; 3562 my ($self, $title, $widget, $tooltip) = @_;
3536 3563
3537 Scalar::Util::weaken $self; 3564 CFPlus::weaken $self;
3538 3565
3539 $self->{buttonbar}->add (new CFPlus::UI::Button 3566 $self->{buttonbar}->add (new CFPlus::UI::Button
3540 markup => $title, 3567 markup => $title,
3541 tooltip => $tooltip, 3568 tooltip => $tooltip,
3542 on_activate => sub { $self->set_current_page ($widget) }, 3569 on_activate => sub { $self->set_current_page ($widget) },
3626 my $self = $class->SUPER::new ( 3653 my $self = $class->SUPER::new (
3627 fontsize => 0.8, 3654 fontsize => 0.8,
3628 @_, 3655 @_,
3629 ); 3656 );
3630 3657
3631 Scalar::Util::weaken (my $this = $self); 3658 CFPlus::weaken (my $this = $self);
3632 3659
3633 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 3660 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder });
3634 3661
3635 $self 3662 $self
3636} 3663}
3775 my $self = $class->SUPER::new ( 3802 my $self = $class->SUPER::new (
3776 visible => 1, 3803 visible => 1,
3777 @_, 3804 @_,
3778 ); 3805 );
3779 3806
3780 Scalar::Util::weaken ($self->{root} = $self); 3807 CFPlus::weaken ($self->{root} = $self);
3781 3808
3782 $self 3809 $self
3783} 3810}
3784 3811
3785sub size_request { 3812sub size_request {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines