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.440 by root, Sun Sep 16 18:27:48 2007 UTC vs.
Revision 1.451 by root, Wed Dec 26 19:11:13 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}
439 440
440 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
441 442
442 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
443 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
444 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
445 } 446 }
446} 447}
447 448
448# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
449sub coord2local { 450sub coord2local {
450 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
451 452
452 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
453 454
454 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
455} 456}
456 457
457# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
458sub coord2global { 459sub coord2global {
459 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
460 461
461 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
462 463
463 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
464} 465}
465 466
466sub invoke_focus_in { 467sub invoke_focus_in {
3672 ); 3673 );
3673 3674
3674 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3675 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3676 3677
3677 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3678 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $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; 3680 return unless $::CONN;
3683 3681
3684 my $w = $widget 3682 my $w = $widget
3685 or return; 3683 or return;
3686 3684
3687 ++$w->{frame}; 3685 ++$w->{frame};
3688 $w->update_face; 3686 $w->update_face;
3689 3687
3690 # somehow, $widget can go away 3688 # somehow, $widget can go away
3691 $w->update; 3689 $w->update;
3692 $w->update_timer; 3690 $w->update_timer;
3693 },
3694 ); 3691 };
3695 3692
3696 $self->update_face; 3693 $self->update_face;
3697 $self->update_timer; 3694 $self->update_timer;
3698 } 3695 }
3699 3696
3704 my ($self) = @_; 3701 my ($self) = @_;
3705 3702
3706 return unless $self->{timer}; 3703 return unless $self->{timer};
3707 3704
3708 if ($self->{visible}) { 3705 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; 3706 $self->{timer}->start;
3715 } else { 3707 } else {
3716 $self->{timer}->stop; 3708 $self->{timer}->stop;
3717 } 3709 }
3718} 3710}
4125} 4117}
4126 4118
4127sub _set_value { 4119sub _set_value {
4128 my ($self, $value) = @_; 4120 my ($self, $value) = @_;
4129 4121
4130 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4122 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4123 $item ||= $self->{options}[0]
4131 or return; 4124 or return;
4132 4125
4133 $self->{value} = $item->[0]; 4126 $self->{value} = $item->[0];
4134 $self->set_markup ("$item->[1] ⇓"); 4127 $self->set_markup ("$item->[1] ⇓");
4135 $self->set_tooltip ($item->[2]); 4128# $self->set_tooltip ($item->[2]);
4136} 4129}
4137 4130
4138sub set_value { 4131sub set_value {
4139 my ($self, $value) = @_; 4132 my ($self, $value) = @_;
4140 4133
4141 return unless $self->{value} ne $value; 4134 return unless $self->{value} ne $value;
4142 4135
4143 $self->_set_value ($value); 4136 $self->_set_value ($value);
4144 $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});
4145} 4145}
4146 4146
4147############################################################################# 4147#############################################################################
4148 4148
4149package CFPlus::UI::Statusbox; 4149package CFPlus::UI::Statusbox;
4158 @_, 4158 @_,
4159 ); 4159 );
4160 4160
4161 CFPlus::weaken (my $this = $self); 4161 CFPlus::weaken (my $this = $self);
4162 4162
4163 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4163 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4164 4164
4165 $self 4165 $self
4166} 4166}
4167 4167
4168sub reorder { 4168sub reorder {
4176 4176
4177 while (my ($k, $v) = each %{ $self->{item} }) { 4177 while (my ($k, $v) = each %{ $self->{item} }) {
4178 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4178 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4179 } 4179 }
4180 4180
4181 $self->{timer}->set (1, 1);
4182
4181 my @widgets; 4183 my @widgets;
4182 4184
4183 my @items = sort { 4185 my @items = sort {
4184 $a->{pri} <=> $b->{pri} 4186 $a->{pri} <=> $b->{pri}
4185 or $b->{id} <=> $a->{id} 4187 or $b->{id} <=> $a->{id}
4186 } values %{ $self->{item} }; 4188 } values %{ $self->{item} };
4187
4188 $self->{timer}->interval (1);
4189 4189
4190 my $count = 10 + 1; 4190 my $count = 10 + 1;
4191 for my $item (@items) { 4191 for my $item (@items) {
4192 last unless --$count; 4192 last unless --$count;
4193 4193
4217 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4217 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4219 $label->update; 4219 $label->update;
4220 $label->set_max_size (undef, $label->{req_h} * $diff) 4220 $label->set_max_size (undef, $label->{req_h} * $diff)
4221 if $diff < 1; 4221 if $diff < 1;
4222 $self->{timer}->interval (1/30); 4222 $self->{timer}->set (1/30, 1/30);
4223 } else { 4223 } else {
4224 $label->{fg}[3] = $item->{fg}[3] || 1; 4224 $label->{fg}[3] = $item->{fg}[3] || 1;
4225 } 4225 }
4226 4226
4227 push @widgets, $label; 4227 push @widgets, $label;
4363} 4363}
4364 4364
4365sub update { 4365sub update {
4366 my ($self) = @_; 4366 my ($self) = @_;
4367 4367
4368 $::WANT_REFRESH->start; 4368 $::WANT_REFRESH = 1;
4369} 4369}
4370 4370
4371sub add { 4371sub add {
4372 my ($self, @children) = @_; 4372 my ($self, @children) = @_;
4373 4373

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines