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.437 by root, Wed Aug 22 22:36:26 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) = @_;
269 $self->emit ("destroy"); 270 $self->emit ("destroy");
270 %$self = (); 271 %$self = ();
271} 272}
272 273
273sub TO_JSON { 274sub TO_JSON {
274 { __w_ => $_[0]{s_id} } 275 { "\fw" => $_[0]{s_id} }
275} 276}
276 277
277sub show { 278sub show {
278 my ($self) = @_; 279 my ($self) = @_;
279 280
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
358 362
359 $self->{force_w} = $w; 363 $self->{force_w} = $w;
360 $self->{force_h} = $h; 364 $self->{force_h} = $h;
361 365
362 $self->realloc; 366 $self->realloc;
367}
368
369# traverse the widget chain up to find the maximum "physical" size constraints
370sub get_max_wh {
371 my ($self) = @_;
372
373 return $self->{parent}->get_max_wh
374 if $self->{parent};
375
376 ($::WIDTH, $::HEIGHT)
363} 377}
364 378
365sub size_request { 379sub size_request {
366 require Carp; 380 require Carp;
367 Carp::confess "size_request is abstract"; 381 Carp::confess "size_request is abstract";
429 443
430 $self->{tooltip} = $tooltip; 444 $self->{tooltip} = $tooltip;
431 445
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 446 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 447 delete $CFPlus::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 448 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
435 } 449 }
436} 450}
437 451
438# translate global coordinates to local coordinate system 452# translate global coordinates to local coordinate system
439sub coord2local { 453sub coord2local {
440 my ($self, $x, $y) = @_; 454 my ($self, $x, $y) = @_;
441 455
442 Carp::confess unless $self->{parent};#d# 456 return (undef, undef) unless $self->{parent};
443 457
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 458 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 459}
446 460
447# translate local coordinates to global coordinate system 461# translate local coordinates to global coordinate system
448sub coord2global { 462sub coord2global {
449 my ($self, $x, $y) = @_; 463 my ($self, $x, $y) = @_;
450 464
451 Carp::confess unless $self->{parent};#d# 465 return (undef, undef) unless $self->{parent};
452 466
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 467 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 468}
455 469
456sub invoke_focus_in { 470sub invoke_focus_in {
817 831
818 1 832 1
819} 833}
820 834
821############################################################################# 835#############################################################################
822
823# back-buffered drawing area 836# back-buffered drawing area
824 837
825package CFPlus::UI::Window; 838package CFPlus::UI::Window;
826 839
827our @ISA = CFPlus::UI::Bin::; 840our @ISA = CFPlus::UI::Bin::;
1376 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget}; 1389 $self->CFPlus::UI::Container::add ($self->{title_widget}) if $self->{title_widget};
1377} 1390}
1378 1391
1379sub border { 1392sub border {
1380 int $_[0]{border} * $::FONTSIZE 1393 int $_[0]{border} * $::FONTSIZE
1394}
1395
1396sub get_max_wh {
1397 my ($self) = @_;
1398
1399 return ($self->{w}, $self->{h})
1400 if $self->{visible} && $self->{w};
1401
1402 $self->SUPER::get_max_wh
1381} 1403}
1382 1404
1383sub size_request { 1405sub size_request {
1384 my ($self) = @_; 1406 my ($self) = @_;
1385 1407
1942 1964
1943sub size_request { 1965sub size_request {
1944 my ($self) = @_; 1966 my ($self) = @_;
1945 1967
1946 $self->{size_req} ||= do { 1968 $self->{size_req} ||= do {
1969 my ($max_w, $max_h) = $self->get_max_wh;
1970
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1971 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1972 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1973 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1974 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1975 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1976
1953 my ($w, $h) = $self->{layout}->size; 1977 my ($w, $h) = $self->{layout}->size;
3652 ); 3676 );
3653 3677
3654 if ($self->{anim} && $self->{animspeed}) { 3678 if ($self->{anim} && $self->{animspeed}) {
3655 CFPlus::weaken (my $widget = $self); 3679 CFPlus::weaken (my $widget = $self);
3656 3680
3657 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3681 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3658 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3682 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3659 $self->{timer} = Event->timer (
3660 parked => 1,
3661 cb => sub {
3662 return unless $::CONN; 3683 return unless $::CONN;
3663 3684
3664 my $w = $widget 3685 my $w = $widget
3665 or return; 3686 or return;
3666 3687
3667 ++$w->{frame}; 3688 ++$w->{frame};
3668 $w->update_face; 3689 $w->update_face;
3669 3690
3670 # somehow, $widget can go away 3691 # somehow, $widget can go away
3671 $w->update; 3692 $w->update;
3672 $w->update_timer; 3693 $w->update_timer;
3673 },
3674 ); 3694 };
3675 3695
3676 $self->update_face; 3696 $self->update_face;
3677 $self->update_timer; 3697 $self->update_timer;
3678 } 3698 }
3679 3699
3684 my ($self) = @_; 3704 my ($self) = @_;
3685 3705
3686 return unless $self->{timer}; 3706 return unless $self->{timer};
3687 3707
3688 if ($self->{visible}) { 3708 if ($self->{visible}) {
3689 $self->{timer}->at (
3690 $self->{anim_start}
3691 + $self->{animspeed}
3692 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3693 );
3694 $self->{timer}->start; 3709 $self->{timer}->start;
3695 } else { 3710 } else {
3696 $self->{timer}->stop; 3711 $self->{timer}->stop;
3697 } 3712 }
3698} 3713}
4105} 4120}
4106 4121
4107sub _set_value { 4122sub _set_value {
4108 my ($self, $value) = @_; 4123 my ($self, $value) = @_;
4109 4124
4110 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4125 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4126 $item ||= $self->{options}[0]
4111 or return; 4127 or return;
4112 4128
4113 $self->{value} = $item->[0]; 4129 $self->{value} = $item->[0];
4114 $self->set_markup ("$item->[1] ⇓"); 4130 $self->set_markup ("$item->[1] ⇓");
4115 $self->set_tooltip ($item->[2]); 4131# $self->set_tooltip ($item->[2]);
4116} 4132}
4117 4133
4118sub set_value { 4134sub set_value {
4119 my ($self, $value) = @_; 4135 my ($self, $value) = @_;
4120 4136
4121 return unless $self->{value} ne $value; 4137 return unless $self->{value} ne $value;
4122 4138
4123 $self->_set_value ($value); 4139 $self->_set_value ($value);
4124 $self->emit (changed => $value); 4140 $self->emit (changed => $value);
4141}
4142
4143sub set_options {
4144 my ($self, $options) = @_;
4145
4146 $self->{options} = $options;
4147 $self->_set_value ($self->{value});
4125} 4148}
4126 4149
4127############################################################################# 4150#############################################################################
4128 4151
4129package CFPlus::UI::Statusbox; 4152package CFPlus::UI::Statusbox;
4138 @_, 4161 @_,
4139 ); 4162 );
4140 4163
4141 CFPlus::weaken (my $this = $self); 4164 CFPlus::weaken (my $this = $self);
4142 4165
4143 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4166 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4144 4167
4145 $self 4168 $self
4146} 4169}
4147 4170
4148sub reorder { 4171sub reorder {
4156 4179
4157 while (my ($k, $v) = each %{ $self->{item} }) { 4180 while (my ($k, $v) = each %{ $self->{item} }) {
4158 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4181 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4159 } 4182 }
4160 4183
4184 $self->{timer}->set (1, 1);
4185
4161 my @widgets; 4186 my @widgets;
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;
4343} 4366}
4344 4367
4345sub update { 4368sub update {
4346 my ($self) = @_; 4369 my ($self) = @_;
4347 4370
4348 $::WANT_REFRESH->start; 4371 $::WANT_REFRESH = 1;
4349} 4372}
4350 4373
4351sub add { 4374sub add {
4352 my ($self, @children) = @_; 4375 my ($self, @children) = @_;
4353 4376

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines