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.441 by root, Mon Oct 8 16:10:14 2007 UTC vs.
Revision 1.452 by root, Wed Dec 26 20:09:38 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}
337sub hide { 338sub hide {
338 my ($self) = @_; 339 my ($self) = @_;
339 340
340 $self->set_invisible; 341 $self->set_invisible;
341 342
343 # extra $parent copy for 5.8.8+ bug workaround
344 # (otherwise $_[0] in remove gets freed
345 if (my $parent = $self->{parent}) {
342 $self->{parent}->remove ($self) 346 $parent->remove ($self);
343 if $self->{parent}; 347 }
344} 348}
345 349
346sub move_abs { 350sub move_abs {
347 my ($self, $x, $y, $z) = @_; 351 my ($self, $x, $y, $z) = @_;
348 352
439 443
440 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
441 445
442 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
443 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $CFPlus::UI::TOOLTIP->{owner};
444 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
445 } 449 }
446} 450}
447 451
448# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
449sub coord2local { 453sub coord2local {
450 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
451 455
452 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
453 457
454 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
455} 459}
456 460
457# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
458sub coord2global { 462sub coord2global {
459 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
460 464
461 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
462 466
463 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
464} 468}
465 469
466sub invoke_focus_in { 470sub invoke_focus_in {
3672 ); 3676 );
3673 3677
3674 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3675 CFPlus::weaken (my $widget = $self); 3679 CFPlus::weaken (my $widget = $self);
3676 3680
3677 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3678 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3679 $self->{timer} = Event->timer (
3680 parked => 1,
3681 cb => sub {
3682 return unless $::CONN; 3683 return unless $::CONN;
3683 3684
3684 my $w = $widget 3685 my $w = $widget
3685 or return; 3686 or return;
3686 3687
3687 ++$w->{frame}; 3688 ++$w->{frame};
3688 $w->update_face; 3689 $w->update_face;
3689 3690
3690 # somehow, $widget can go away 3691 # somehow, $widget can go away
3691 $w->update; 3692 $w->update;
3692 $w->update_timer; 3693 $w->update_timer;
3693 },
3694 ); 3694 };
3695 3695
3696 $self->update_face; 3696 $self->update_face;
3697 $self->update_timer; 3697 $self->update_timer;
3698 } 3698 }
3699 3699
3704 my ($self) = @_; 3704 my ($self) = @_;
3705 3705
3706 return unless $self->{timer}; 3706 return unless $self->{timer};
3707 3707
3708 if ($self->{visible}) { 3708 if ($self->{visible}) {
3709 $self->{timer}->at (
3710 $self->{anim_start}
3711 + $self->{animspeed}
3712 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3713 );
3714 $self->{timer}->start; 3709 $self->{timer}->start;
3715 } else { 3710 } else {
3716 $self->{timer}->stop; 3711 $self->{timer}->stop;
3717 } 3712 }
3718} 3713}
4131 $item ||= $self->{options}[0] 4126 $item ||= $self->{options}[0]
4132 or return; 4127 or return;
4133 4128
4134 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
4135 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
4136 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
4137} 4132}
4138 4133
4139sub set_value { 4134sub set_value {
4140 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
4141 4136
4166 @_, 4161 @_,
4167 ); 4162 );
4168 4163
4169 CFPlus::weaken (my $this = $self); 4164 CFPlus::weaken (my $this = $self);
4170 4165
4171 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4172 4167
4173 $self 4168 $self
4174} 4169}
4175 4170
4176sub reorder { 4171sub reorder {
4184 4179
4185 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
4186 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4187 } 4182 }
4188 4183
4184 $self->{timer}->set (1, 1);
4185
4189 my @widgets; 4186 my @widgets;
4190 4187
4191 my @items = sort { 4188 my @items = sort {
4192 $a->{pri} <=> $b->{pri} 4189 $a->{pri} <=> $b->{pri}
4193 or $b->{id} <=> $a->{id} 4190 or $b->{id} <=> $a->{id}
4194 } values %{ $self->{item} }; 4191 } values %{ $self->{item} };
4195
4196 $self->{timer}->interval (1);
4197 4192
4198 my $count = 10 + 1; 4193 my $count = 10 + 1;
4199 for my $item (@items) { 4194 for my $item (@items) {
4200 last unless --$count; 4195 last unless --$count;
4201 4196
4225 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4220 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4226 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4221 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4227 $label->update; 4222 $label->update;
4228 $label->set_max_size (undef, $label->{req_h} * $diff) 4223 $label->set_max_size (undef, $label->{req_h} * $diff)
4229 if $diff < 1; 4224 if $diff < 1;
4230 $self->{timer}->interval (1/30); 4225 $self->{timer}->set (1/30, 1/30);
4231 } else { 4226 } else {
4232 $label->{fg}[3] = $item->{fg}[3] || 1; 4227 $label->{fg}[3] = $item->{fg}[3] || 1;
4233 } 4228 }
4234 4229
4235 push @widgets, $label; 4230 push @widgets, $label;
4371} 4366}
4372 4367
4373sub update { 4368sub update {
4374 my ($self) = @_; 4369 my ($self) = @_;
4375 4370
4376 $::WANT_REFRESH->start; 4371 $::WANT_REFRESH = 1;
4377} 4372}
4378 4373
4379sub add { 4374sub add {
4380 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4381 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines