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.438 by root, Sat Sep 1 07:22:21 2007 UTC vs.
Revision 1.447 by root, Sat Dec 22 06:50:45 2007 UTC

2 2
3use utf8; 3use utf8;
4use strict; 4use strict;
5 5
6use List::Util (); 6use List::Util ();
7use Event;
8 7
9use CFPlus; 8use CFPlus;
10use CFPlus::Pod; 9use CFPlus::Pod;
11use CFPlus::Texture; 10use CFPlus::Texture;
12 11
17our $TOOLTIP; 16our $TOOLTIP;
18our $BUTTON_STATE; 17our $BUTTON_STATE;
19 18
20our %WIDGET; # all widgets, weak-referenced 19our %WIDGET; # all widgets, weak-referenced
21 20
22our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { 21our $TOOLTIP_WATCHER = EV::timer_ns 0, 1/60, sub {
22 $_[0]->stop;
23
23 if (!$GRAB) { 24 if (!$GRAB) {
24 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { 25 for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) {
25 if (length $widget->{tooltip}) { 26 if (length $widget->{tooltip}) {
26 if ($TOOLTIP->{owner} != $widget) { 27 if ($TOOLTIP->{owner} != $widget) {
27 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 28 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
42 } 43 }
43 44
44 $TOOLTIP->hide; 45 $TOOLTIP->hide;
45 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; 46 $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner};
46 delete $TOOLTIP->{owner}; 47 delete $TOOLTIP->{owner};
47}); 48};
48 49
49sub get_layout { 50sub get_layout {
50 my $layout; 51 my $layout;
51 52
52 for (grep { $_->{name} } values %WIDGET) { 53 for (grep { $_->{name} } values %WIDGET) {
87 my $hover = $HOVER; $HOVER = $widget; 88 my $hover = $HOVER; $HOVER = $widget;
88 89
89 $hover->update if $hover && $hover->{can_hover}; 90 $hover->update if $hover && $hover->{can_hover};
90 $HOVER->update if $HOVER && $HOVER->{can_hover}; 91 $HOVER->update if $HOVER && $HOVER->{can_hover};
91 92
92 $TOOLTIP_WATCHER->start; 93 $TOOLTIP_WATCHER->again;
93 } 94 }
94} 95}
95 96
96sub feed_sdl_button_down_event { 97sub feed_sdl_button_down_event {
97 my ($ev) = @_; 98 my ($ev) = @_;
103 my $widget = $ROOT->find_widget ($x, $y); 104 my $widget = $ROOT->find_widget ($x, $y);
104 105
105 $GRAB = $widget; 106 $GRAB = $widget;
106 $GRAB->update if $GRAB; 107 $GRAB->update if $GRAB;
107 108
108 $TOOLTIP_WATCHER->cb->(); 109 $TOOLTIP_WATCHER->invoke;
109 } 110 }
110 111
111 if ($GRAB) { 112 if ($GRAB) {
112 if ($ev->{button} == 4 || $ev->{button} == 5) { 113 if ($ev->{button} == 4 || $ev->{button} == 5) {
113 # mousewheel 114 # mousewheel
138 my $grab = $GRAB; undef $GRAB; 139 my $grab = $GRAB; undef $GRAB;
139 $grab->update if $grab; 140 $grab->update if $grab;
140 $GRAB->update if $GRAB; 141 $GRAB->update if $GRAB;
141 142
142 check_hover $widget; 143 check_hover $widget;
143 $TOOLTIP_WATCHER->cb->(); 144 $TOOLTIP_WATCHER->invoke;
144 } 145 }
145} 146}
146 147
147sub feed_sdl_motion_event { 148sub feed_sdl_motion_event {
148 my ($ev) = @_; 149 my ($ev) = @_;
308 delete $self->{root}; 309 delete $self->{root};
309 310
310 undef $GRAB if $GRAB == $self; 311 undef $GRAB if $GRAB == $self;
311 undef $HOVER if $HOVER == $self; 312 undef $HOVER if $HOVER == $self;
312 313
313 $CFPlus::UI::TOOLTIP_WATCHER->cb->() 314 $CFPlus::UI::TOOLTIP_WATCHER->invoke
314 if $TOOLTIP->{owner} == $self; 315 if $TOOLTIP->{owner} == $self;
315 316
316 $self->emit ("focus_out"); 317 $self->emit ("focus_out");
317 $self->emit (visibility_change => 0); 318 $self->emit (visibility_change => 0);
318} 319}
358 359
359 $self->{force_w} = $w; 360 $self->{force_w} = $w;
360 $self->{force_h} = $h; 361 $self->{force_h} = $h;
361 362
362 $self->realloc; 363 $self->realloc;
364}
365
366# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh {
368 my ($self) = @_;
369
370 return $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT)
363} 374}
364 375
365sub size_request { 376sub size_request {
366 require Carp; 377 require Carp;
367 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
429 440
430 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
431 442
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
435 } 446 }
436} 447}
437 448
438# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
439sub coord2local { 450sub coord2local {
440 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
441 452
442 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
443 454
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 456}
446 457
447# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
448sub coord2global { 459sub coord2global {
449 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
450 461
451 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
452 463
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 465}
455 466
456sub invoke_focus_in { 467sub invoke_focus_in {
817 828
818 1 829 1
819} 830}
820 831
821############################################################################# 832#############################################################################
822
823# back-buffered drawing area 833# back-buffered drawing area
824 834
825package CFPlus::UI::Window; 835package CFPlus::UI::Window;
826 836
827our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
1376 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1386 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1377} 1387}
1378 1388
1379sub border { 1389sub border {
1380 int $_[0]{border} * $::FONTSIZE 1390 int $_[0]{border} * $::FONTSIZE
1391}
1392
1393sub get_max_wh {
1394 my ($self) = @_;
1395
1396 return ($self->{w}, $self->{h})
1397 if $self->{visible} && $self->{w};
1398
1399 $self->SUPER::get_max_wh
1381} 1400}
1382 1401
1383sub size_request { 1402sub size_request {
1384 my ($self) = @_; 1403 my ($self) = @_;
1385 1404
1942 1961
1943sub size_request { 1962sub size_request {
1944 my ($self) = @_; 1963 my ($self) = @_;
1945 1964
1946 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1973
1953 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
3653 3674
3654 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3655 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3656 3677
3657 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3658 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3659 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3660 parked => 1,
3661 cb => sub {
3662 return unless $::CONN; 3681 return unless $::CONN;
3663 3682
3664 my $w = $widget 3683 my $w = $widget
3665 or return; 3684 or return;
3666 3685
3667 ++$w->{frame}; 3686 ++$w->{frame};
3668 $w->update_face; 3687 $w->update_face;
3669 3688
3670 # somehow, $widget can go away 3689 # somehow, $widget can go away
3671 $w->update; 3690 $w->update;
3672 $w->update_timer; 3691 $w->update_timer;
3673 },
3674 ); 3692 };
3675 3693
3676 $self->update_face; 3694 $self->update_face;
3677 $self->update_timer; 3695 $self->update_timer;
3678 } 3696 }
3679 3697
3683sub update_timer { 3701sub update_timer {
3684 my ($self) = @_; 3702 my ($self) = @_;
3685 3703
3686 return unless $self->{timer}; 3704 return unless $self->{timer};
3687 3705
3706 $self->{timer}->stop;
3688 if ($self->{visible}) { 3707 if ($self->{visible}) {
3689 $self->{timer}->at ( 3708 $self->{timer}->set (
3690 $self->{anim_start} 3709 $self->{anim_start}
3691 + $self->{animspeed} 3710 + $self->{animspeed}
3692 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3693 ); 3712 );
3694 $self->{timer}->start; 3713 $self->{timer}->start;
3695 } else {
3696 $self->{timer}->stop;
3697 } 3714 }
3698} 3715}
3699 3716
3700sub update_face { 3717sub update_face {
3701 my ($self) = @_; 3718 my ($self) = @_;
4105} 4122}
4106 4123
4107sub _set_value { 4124sub _set_value {
4108 my ($self, $value) = @_; 4125 my ($self, $value) = @_;
4109 4126
4110 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4127 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4128 $item ||= $self->{options}[0]
4111 or return; 4129 or return;
4112 4130
4113 $self->{value} = $item->[0]; 4131 $self->{value} = $item->[0];
4114 $self->set_markup ("$item->[1] ⇓"); 4132 $self->set_markup ("$item->[1] ⇓");
4115 $self->set_tooltip ($item->[2]); 4133# $self->set_tooltip ($item->[2]);
4116} 4134}
4117 4135
4118sub set_value { 4136sub set_value {
4119 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
4120 4138
4121 return unless $self->{value} ne $value; 4139 return unless $self->{value} ne $value;
4122 4140
4123 $self->_set_value ($value); 4141 $self->_set_value ($value);
4124 $self->emit (changed => $value); 4142 $self->emit (changed => $value);
4143}
4144
4145sub set_options {
4146 my ($self, $options) = @_;
4147
4148 $self->{options} = $options;
4149 $self->_set_value ($self->{value});
4125} 4150}
4126 4151
4127############################################################################# 4152#############################################################################
4128 4153
4129package CFPlus::UI::Statusbox; 4154package CFPlus::UI::Statusbox;
4138 @_, 4163 @_,
4139 ); 4164 );
4140 4165
4141 CFPlus::weaken (my $this = $self); 4166 CFPlus::weaken (my $this = $self);
4142 4167
4143 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4144 4169
4145 $self 4170 $self
4146} 4171}
4147 4172
4148sub reorder { 4173sub reorder {
4162 4187
4163 my @items = sort { 4188 my @items = sort {
4164 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4165 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4166 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4167
4168 $self->{timer}->interval (1);
4169 4192
4170 my $count = 10 + 1; 4193 my $count = 10 + 1;
4171 for my $item (@items) { 4194 for my $item (@items) {
4172 last unless --$count; 4195 last unless --$count;
4173 4196
4197 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4198 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4199 $label->update; 4222 $label->update;
4200 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4201 if $diff < 1; 4224 if $diff < 1;
4202 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4203 } else { 4226 } else {
4204 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4205 } 4228 }
4206 4229
4207 push @widgets, $label; 4230 push @widgets, $label;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines