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.439 by root, Sun Sep 16 13:04:36 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}
360 $self->{force_h} = $h; 361 $self->{force_h} = $h;
361 362
362 $self->realloc; 363 $self->realloc;
363} 364}
364 365
365# travsle the widget chain up to find the maximum "physical" size constraints 366# traverse the widget chain up to find the maximum "physical" size constraints
366sub get_max_wh { 367sub get_max_wh {
367 my ($self) = @_; 368 my ($self) = @_;
368
369 return ($self->{alloc_w}, $self->{alloc_h})
370 if $self->{alloc_w} && !$self->{root}{realloc}{$self+0};
371 369
372 return $self->{parent}->get_max_wh 370 return $self->{parent}->get_max_wh
373 if $self->{parent}; 371 if $self->{parent};
374 372
375 ($::WIDTH, $::HEIGHT) 373 ($::WIDTH, $::HEIGHT)
442 440
443 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
444 442
445 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
446 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
447 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
448 } 446 }
449} 447}
450 448
451# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
452sub coord2local { 450sub coord2local {
453 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
454 452
455 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
456 454
457 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
458} 456}
459 457
460# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
461sub coord2global { 459sub coord2global {
462 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
463 461
464 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
465 463
466 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
467} 465}
468 466
469sub invoke_focus_in { 467sub invoke_focus_in {
1388 $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};
1389} 1387}
1390 1388
1391sub border { 1389sub border {
1392 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
1393} 1400}
1394 1401
1395sub size_request { 1402sub size_request {
1396 my ($self) = @_; 1403 my ($self) = @_;
1397 1404
3667 3674
3668 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3669 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3670 3677
3671 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $widget->{anim_start} = $self->{animspeed} * int EV::now / $self->{animspeed};
3673 $self->{timer} = Event->timer ( 3680 $self->{timer} = EV::timer_ns 0, 0, sub {
3674 parked => 1,
3675 cb => sub {
3676 return unless $::CONN; 3681 return unless $::CONN;
3677 3682
3678 my $w = $widget 3683 my $w = $widget
3679 or return; 3684 or return;
3680 3685
3681 ++$w->{frame}; 3686 ++$w->{frame};
3682 $w->update_face; 3687 $w->update_face;
3683 3688
3684 # somehow, $widget can go away 3689 # somehow, $widget can go away
3685 $w->update; 3690 $w->update;
3686 $w->update_timer; 3691 $w->update_timer;
3687 },
3688 ); 3692 };
3689 3693
3690 $self->update_face; 3694 $self->update_face;
3691 $self->update_timer; 3695 $self->update_timer;
3692 } 3696 }
3693 3697
3697sub update_timer { 3701sub update_timer {
3698 my ($self) = @_; 3702 my ($self) = @_;
3699 3703
3700 return unless $self->{timer}; 3704 return unless $self->{timer};
3701 3705
3706 $self->{timer}->stop;
3702 if ($self->{visible}) { 3707 if ($self->{visible}) {
3703 $self->{timer}->at ( 3708 $self->{timer}->set (
3704 $self->{anim_start} 3709 $self->{anim_start}
3705 + $self->{animspeed} 3710 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed} 3711 * int 1.5 + (EV::now - $self->{anim_start}) / $self->{animspeed}
3707 ); 3712 );
3708 $self->{timer}->start; 3713 $self->{timer}->start;
3709 } else {
3710 $self->{timer}->stop;
3711 } 3714 }
3712} 3715}
3713 3716
3714sub update_face { 3717sub update_face {
3715 my ($self) = @_; 3718 my ($self) = @_;
4119} 4122}
4120 4123
4121sub _set_value { 4124sub _set_value {
4122 my ($self, $value) = @_; 4125 my ($self, $value) = @_;
4123 4126
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4127 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4128 $item ||= $self->{options}[0]
4125 or return; 4129 or return;
4126 4130
4127 $self->{value} = $item->[0]; 4131 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4132 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4133# $self->set_tooltip ($item->[2]);
4130} 4134}
4131 4135
4132sub set_value { 4136sub set_value {
4133 my ($self, $value) = @_; 4137 my ($self, $value) = @_;
4134 4138
4135 return unless $self->{value} ne $value; 4139 return unless $self->{value} ne $value;
4136 4140
4137 $self->_set_value ($value); 4141 $self->_set_value ($value);
4138 $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});
4139} 4150}
4140 4151
4141############################################################################# 4152#############################################################################
4142 4153
4143package CFPlus::UI::Statusbox; 4154package CFPlus::UI::Statusbox;
4152 @_, 4163 @_,
4153 ); 4164 );
4154 4165
4155 CFPlus::weaken (my $this = $self); 4166 CFPlus::weaken (my $this = $self);
4156 4167
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4168 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4169
4159 $self 4170 $self
4160} 4171}
4161 4172
4162sub reorder { 4173sub reorder {
4176 4187
4177 my @items = sort { 4188 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4192
4184 my $count = 10 + 1; 4193 my $count = 10 + 1;
4185 for my $item (@items) { 4194 for my $item (@items) {
4186 last unless --$count; 4195 last unless --$count;
4187 4196
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4222 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4224 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4217 } else { 4226 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4228 }
4220 4229
4221 push @widgets, $label; 4230 push @widgets, $label;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines