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.450 by root, Wed Dec 26 15:29:43 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, 0.03, 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
3666 ); 3673 );
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 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3672 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3673 $self->{timer} = Event->timer (
3674 parked => 1,
3675 cb => sub {
3676 return unless $::CONN; 3680 return unless $::CONN;
3677 3681
3678 my $w = $widget 3682 my $w = $widget
3679 or return; 3683 or return;
3680 3684
3681 ++$w->{frame}; 3685 ++$w->{frame};
3682 $w->update_face; 3686 $w->update_face;
3683 3687
3684 # somehow, $widget can go away 3688 # somehow, $widget can go away
3685 $w->update; 3689 $w->update;
3686 $w->update_timer; 3690 $w->update_timer;
3687 },
3688 ); 3691 };
3689 3692
3690 $self->update_face; 3693 $self->update_face;
3691 $self->update_timer; 3694 $self->update_timer;
3692 } 3695 }
3693 3696
3698 my ($self) = @_; 3701 my ($self) = @_;
3699 3702
3700 return unless $self->{timer}; 3703 return unless $self->{timer};
3701 3704
3702 if ($self->{visible}) { 3705 if ($self->{visible}) {
3703 $self->{timer}->at (
3704 $self->{anim_start}
3705 + $self->{animspeed}
3706 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3707 );
3708 $self->{timer}->start; 3706 $self->{timer}->start;
3709 } else { 3707 } else {
3710 $self->{timer}->stop; 3708 $self->{timer}->stop;
3711 } 3709 }
3712} 3710}
4119} 4117}
4120 4118
4121sub _set_value { 4119sub _set_value {
4122 my ($self, $value) = @_; 4120 my ($self, $value) = @_;
4123 4121
4124 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4122 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4123 $item ||= $self->{options}[0]
4125 or return; 4124 or return;
4126 4125
4127 $self->{value} = $item->[0]; 4126 $self->{value} = $item->[0];
4128 $self->set_markup ("$item->[1] ⇓"); 4127 $self->set_markup ("$item->[1] ⇓");
4129 $self->set_tooltip ($item->[2]); 4128# $self->set_tooltip ($item->[2]);
4130} 4129}
4131 4130
4132sub set_value { 4131sub set_value {
4133 my ($self, $value) = @_; 4132 my ($self, $value) = @_;
4134 4133
4135 return unless $self->{value} ne $value; 4134 return unless $self->{value} ne $value;
4136 4135
4137 $self->_set_value ($value); 4136 $self->_set_value ($value);
4138 $self->emit (changed => $value); 4137 $self->emit (changed => $value);
4138}
4139
4140sub set_options {
4141 my ($self, $options) = @_;
4142
4143 $self->{options} = $options;
4144 $self->_set_value ($self->{value});
4139} 4145}
4140 4146
4141############################################################################# 4147#############################################################################
4142 4148
4143package CFPlus::UI::Statusbox; 4149package CFPlus::UI::Statusbox;
4152 @_, 4158 @_,
4153 ); 4159 );
4154 4160
4155 CFPlus::weaken (my $this = $self); 4161 CFPlus::weaken (my $this = $self);
4156 4162
4157 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4163 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4158 4164
4159 $self 4165 $self
4160} 4166}
4161 4167
4162sub reorder { 4168sub reorder {
4170 4176
4171 while (my ($k, $v) = each %{ $self->{item} }) { 4177 while (my ($k, $v) = each %{ $self->{item} }) {
4172 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4178 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4173 } 4179 }
4174 4180
4181 $self->{timer}->set (1, 1);
4182
4175 my @widgets; 4183 my @widgets;
4176 4184
4177 my @items = sort { 4185 my @items = sort {
4178 $a->{pri} <=> $b->{pri} 4186 $a->{pri} <=> $b->{pri}
4179 or $b->{id} <=> $a->{id} 4187 or $b->{id} <=> $a->{id}
4180 } values %{ $self->{item} }; 4188 } values %{ $self->{item} };
4181
4182 $self->{timer}->interval (1);
4183 4189
4184 my $count = 10 + 1; 4190 my $count = 10 + 1;
4185 for my $item (@items) { 4191 for my $item (@items) {
4186 last unless --$count; 4192 last unless --$count;
4187 4193
4211 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4217 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4212 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4213 $label->update; 4219 $label->update;
4214 $label->set_max_size (undef, $label->{req_h} * $diff) 4220 $label->set_max_size (undef, $label->{req_h} * $diff)
4215 if $diff < 1; 4221 if $diff < 1;
4216 $self->{timer}->interval (1/30); 4222 $self->{timer}->set (1/30, 1/30);
4217 } else { 4223 } else {
4218 $label->{fg}[3] = $item->{fg}[3] || 1; 4224 $label->{fg}[3] = $item->{fg}[3] || 1;
4219 } 4225 }
4220 4226
4221 push @widgets, $label; 4227 push @widgets, $label;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines