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.436 by root, Wed Aug 22 21:40:57 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) = @_;
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}
358 359
359 $self->{force_w} = $w; 360 $self->{force_w} = $w;
360 $self->{force_h} = $h; 361 $self->{force_h} = $h;
361 362
362 $self->realloc; 363 $self->realloc;
364}
365
366# traverse the widget chain up to find the maximum "physical" size constraints
367sub get_max_wh {
368 my ($self) = @_;
369
370 return $self->{parent}->get_max_wh
371 if $self->{parent};
372
373 ($::WIDTH, $::HEIGHT)
363} 374}
364 375
365sub size_request { 376sub size_request {
366 require Carp; 377 require Carp;
367 Carp::confess "size_request is abstract"; 378 Carp::confess "size_request is abstract";
429 440
430 $self->{tooltip} = $tooltip; 441 $self->{tooltip} = $tooltip;
431 442
432 if ($CFPlus::UI::TOOLTIP->{owner} == $self) { 443 if ($CFPlus::UI::TOOLTIP->{owner} == $self) {
433 delete $CFPlus::UI::TOOLTIP->{owner}; 444 delete $CFPlus::UI::TOOLTIP->{owner};
434 $CFPlus::UI::TOOLTIP_WATCHER->cb->(); 445 $CFPlus::UI::TOOLTIP_WATCHER->invoke;
435 } 446 }
436} 447}
437 448
438# translate global coordinates to local coordinate system 449# translate global coordinates to local coordinate system
439sub coord2local { 450sub coord2local {
440 my ($self, $x, $y) = @_; 451 my ($self, $x, $y) = @_;
441 452
442 Carp::confess unless $self->{parent};#d# 453 return (undef, undef) unless $self->{parent};
443 454
444 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y}) 455 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
445} 456}
446 457
447# translate local coordinates to global coordinate system 458# translate local coordinates to global coordinate system
448sub coord2global { 459sub coord2global {
449 my ($self, $x, $y) = @_; 460 my ($self, $x, $y) = @_;
450 461
451 Carp::confess unless $self->{parent};#d# 462 return (undef, undef) unless $self->{parent};
452 463
453 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y}) 464 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
454} 465}
455 466
456sub invoke_focus_in { 467sub invoke_focus_in {
817 828
818 1 829 1
819} 830}
820 831
821############################################################################# 832#############################################################################
822
823# back-buffered drawing area 833# back-buffered drawing area
824 834
825package CFPlus::UI::Window; 835package CFPlus::UI::Window;
826 836
827our @ISA = CFPlus::UI::Bin::; 837our @ISA = CFPlus::UI::Bin::;
1376 $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};
1377} 1387}
1378 1388
1379sub border { 1389sub border {
1380 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
1381} 1400}
1382 1401
1383sub size_request { 1402sub size_request {
1384 my ($self) = @_; 1403 my ($self) = @_;
1385 1404
1942 1961
1943sub size_request { 1962sub size_request {
1944 my ($self) = @_; 1963 my ($self) = @_;
1945 1964
1946 $self->{size_req} ||= do { 1965 $self->{size_req} ||= do {
1966 my ($max_w, $max_h) = $self->get_max_wh;
1967
1947 $self->{layout}->set_font ($self->{font}) if $self->{font}; 1968 $self->{layout}->set_font ($self->{font}) if $self->{font};
1948 $self->{layout}->set_width ($self->{max_w} || -1); 1969 $self->{layout}->set_width ($self->{max_w} || $max_w || -1);
1949 $self->{layout}->set_ellipsise ($self->{ellipsise}); 1970 $self->{layout}->set_ellipsise ($self->{ellipsise});
1950 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); 1971 $self->{layout}->set_single_paragraph_mode ($self->{ellipsise});
1951 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 1972 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
1952 1973
1953 my ($w, $h) = $self->{layout}->size; 1974 my ($w, $h) = $self->{layout}->size;
2233 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text); 2254 @$self{qw(cur_x cur_y cur_h)} = $self->{layout}->cursor_pos (length $text);
2234 } 2255 }
2235 2256
2236 glColor_premultiply @{$self->{active_fg}}; 2257 glColor_premultiply @{$self->{active_fg}};
2237 glBegin GL_LINES; 2258 glBegin GL_LINES;
2238 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy}; 2259 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy};
2239 glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; 2260 glVertex $self->{cur_x} + $self->{ox} + .5, $self->{cur_y} + $self->{oy} + $self->{cur_h};
2240 glEnd; 2261 glEnd;
2241 2262
2242 glLineWidth 3; 2263 glLineWidth 3;
2243 glColor @{$self->{active_outline}}; 2264 glColor @{$self->{active_outline}};
2244 glRect_lineloop 0, 0, $self->{w} - 1, $self->{h} - 1; 2265 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2245 glLineWidth 1; 2266 glLineWidth 1;
2246 2267
2247 } else { 2268 } else {
2248 glColor @{$self->{outline}}; 2269 glColor @{$self->{outline}};
2249 glTranslate .375, .375;
2250 glBegin GL_LINE_STRIP; 2270 glBegin GL_LINE_STRIP;
2251 glVertex 0, $self->{h} * .5; 2271 glVertex .5, $self->{h} * .5;
2252 glVertex 0, $self->{h} - 3; 2272 glVertex .5, $self->{h} - 2.5;
2253 glVertex $self->{w} - 1, $self->{h} - 3; 2273 glVertex $self->{w} - .5, $self->{h} - 2.5;
2254 glVertex $self->{w} - 1, $self->{h} * .5; 2274 glVertex $self->{w} - .5, $self->{h} * .5;
2255 glEnd; 2275 glEnd;
2256 } 2276 }
2257} 2277}
2258 2278
2259############################################################################# 2279#############################################################################
2508sub _draw { 2528sub _draw {
2509 my ($self) = @_; 2529 my ($self) = @_;
2510 2530
2511 $self->SUPER::_draw; 2531 $self->SUPER::_draw;
2512 2532
2513 glTranslate $self->{padding_x} + 0.375, $self->{padding_y} + 0.375, 0; 2533 glTranslate $self->{padding_x}, $self->{padding_y}, 0;
2514 2534
2515 my ($w, $h) = @$self{qw(w h)}; 2535 my ($w, $h) = @$self{qw(w h)};
2516 2536
2517 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2; 2537 my $s = List::Util::min $w - $self->{padding_x} * 2, $h - $self->{padding_y} * 2;
2518 2538
2837 2857
2838 glEnable GL_BLEND; 2858 glEnable GL_BLEND;
2839 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA; 2859 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2840 2860
2841 if ($self->{value} >= 0) { 2861 if ($self->{value} >= 0) {
2842 my $s = 3 + ($self->{w} - 6) * $self->{value}; 2862 my $s = int 2 + ($self->{w} - 4) * $self->{value};
2843 2863
2844 glColor_premultiply @{$self->{bar}}; 2864 glColor_premultiply @{$self->{bar}};
2845 glRect 2, 2, $s, $self->{h} - 2; 2865 glRect 2, 2, $s, $self->{h} - 2;
2846 glColor_premultiply @{$self->{bg}}; 2866 glColor_premultiply @{$self->{bg}};
2847 glRect $s + 1, 0, $self->{w} - 2, $self->{h} - 2; 2867 glRect $s, 2, $self->{w} - 2, $self->{h} - 2;
2848 } 2868 }
2849 2869
2850 glColor_premultiply @{$self->{outline}}; 2870 glColor_premultiply @{$self->{outline}};
2851 glRect_lineloop 1, 1, $self->{w} - 2, $self->{h} - 2; 2871 glRect_lineloop 1.5, 1.5, $self->{w} - 1.5, $self->{h} - 1.5;
2852 2872
2853 glDisable GL_BLEND; 2873 glDisable GL_BLEND;
2854 2874
2855 { 2875 {
2856 local $self->{bg}; # do not draw background 2876 local $self->{bg}; # do not draw background
3618} 3638}
3619 3639
3620sub _draw { 3640sub _draw {
3621 my ($self) = @_; 3641 my ($self) = @_;
3622 3642
3623 glTranslate 0.375, 0.375;
3624
3625 my ($w, $h) = @$self{qw(w h)}; 3643 my ($w, $h) = @$self{qw(w h)};
3626 3644
3627 glColor 1, 0.8, 0.4; 3645 glColor 1, 0.8, 0.4;
3628 glRect 0, 0, $w, $h; 3646 glRect 0, 0, $w, $h;
3629 3647
3630 glColor 0, 0, 0; 3648 glColor 0, 0, 0;
3631 glRect_lineloop 0, 0, $w, $h; 3649 glRect_lineloop .5, .5, $w + .5, $h + .5;
3632 3650
3633 glTranslate 2 - 0.375, 2 - 0.375; 3651 glTranslate 2, 2;
3634 3652
3635 $self->SUPER::_draw; 3653 $self->SUPER::_draw;
3636} 3654}
3637 3655
3638############################################################################# 3656#############################################################################
3655 ); 3673 );
3656 3674
3657 if ($self->{anim} && $self->{animspeed}) { 3675 if ($self->{anim} && $self->{animspeed}) {
3658 CFPlus::weaken (my $widget = $self); 3676 CFPlus::weaken (my $widget = $self);
3659 3677
3660 $widget->{animspeed} = List::Util::max 0.05, $widget->{animspeed}; 3678 $self->{animspeed} = List::Util::max 0.05, $self->{animspeed};
3661 $widget->{anim_start} = $self->{animspeed} * int Event::time / $self->{animspeed}; 3679 $self->{timer} = EV::periodic_ns 0, $self->{animspeed}, undef, sub {
3662 $self->{timer} = Event->timer (
3663 parked => 1,
3664 cb => sub {
3665 return unless $::CONN; 3680 return unless $::CONN;
3666 3681
3667 my $w = $widget 3682 my $w = $widget
3668 or return; 3683 or return;
3669 3684
3670 ++$w->{frame}; 3685 ++$w->{frame};
3671 $w->update_face; 3686 $w->update_face;
3672 3687
3673 # somehow, $widget can go away 3688 # somehow, $widget can go away
3674 $w->update; 3689 $w->update;
3675 $w->update_timer; 3690 $w->update_timer;
3676 },
3677 ); 3691 };
3678 3692
3679 $self->update_face; 3693 $self->update_face;
3680 $self->update_timer; 3694 $self->update_timer;
3681 } 3695 }
3682 3696
3687 my ($self) = @_; 3701 my ($self) = @_;
3688 3702
3689 return unless $self->{timer}; 3703 return unless $self->{timer};
3690 3704
3691 if ($self->{visible}) { 3705 if ($self->{visible}) {
3692 $self->{timer}->at (
3693 $self->{anim_start}
3694 + $self->{animspeed}
3695 * int 1.5 + (Event::time - $self->{anim_start}) / $self->{animspeed}
3696 );
3697 $self->{timer}->start; 3706 $self->{timer}->start;
3698 } else { 3707 } else {
3699 $self->{timer}->stop; 3708 $self->{timer}->stop;
3700 } 3709 }
3701} 3710}
3965 my $class = shift; 3974 my $class = shift;
3966 3975
3967 my $self = $class->SUPER::new ( 3976 my $self = $class->SUPER::new (
3968 buttonbar => (new CFPlus::UI::Buttonbar), 3977 buttonbar => (new CFPlus::UI::Buttonbar),
3969 multiplexer => (new CFPlus::UI::Multiplexer expand => 1), 3978 multiplexer => (new CFPlus::UI::Multiplexer expand => 1),
3970 active_outline => [1, 1, 0], 3979 active_outline => [.7, .7, 0.2],
3971 # filter => # will be put between multiplexer and $self 3980 # filter => # will be put between multiplexer and $self
3972 @_, 3981 @_,
3973 ); 3982 );
3974 3983
3975 $self->{filter}->add ($self->{multiplexer}) if $self->{filter}; 3984 $self->{filter}->add ($self->{multiplexer}) if $self->{filter};
4056 4065
4057 $self->SUPER::_draw (); 4066 $self->SUPER::_draw ();
4058 4067
4059 if (my $cur = $self->{multiplexer}{current}) { 4068 if (my $cur = $self->{multiplexer}{current}) {
4060 if ($cur = $cur->{c_tab_}) { 4069 if ($cur = $cur->{c_tab_}) {
4061 glTranslate $cur->{x}, $cur->{y}; 4070 glTranslate $self->{buttonbar}{x} + $cur->{x},
4071 $self->{buttonbar}{y} + $cur->{y};
4062 glLineWidth 3; 4072 glLineWidth 3;
4073 #glEnable GL_BLEND;
4074 #glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
4063 glColor @{$self->{active_outline}}; 4075 glColor @{$self->{active_outline}};
4064 glRect_lineloop 1, 1, $cur->{w} - 2, $cur->{h} - 2; 4076 glRect_lineloop 1.5, 1.5, $cur->{w} - 1.5, $cur->{h} - 1.5;
4065 glLineWidth 1; 4077 glLineWidth 1;
4078 #glDisable GL_BLEND;
4066 } 4079 }
4067 } 4080 }
4068} 4081}
4069 4082
4070############################################################################# 4083#############################################################################
4104} 4117}
4105 4118
4106sub _set_value { 4119sub _set_value {
4107 my ($self, $value) = @_; 4120 my ($self, $value) = @_;
4108 4121
4109 my ($item) = grep $_->[0] eq $value, @{ $self->{options} } 4122 my ($item) = grep $_->[0] eq $value, @{ $self->{options} };
4123 $item ||= $self->{options}[0]
4110 or return; 4124 or return;
4111 4125
4112 $self->{value} = $item->[0]; 4126 $self->{value} = $item->[0];
4113 $self->set_markup ("$item->[1] ⇓"); 4127 $self->set_markup ("$item->[1] ⇓");
4114 $self->set_tooltip ($item->[2]); 4128# $self->set_tooltip ($item->[2]);
4115} 4129}
4116 4130
4117sub set_value { 4131sub set_value {
4118 my ($self, $value) = @_; 4132 my ($self, $value) = @_;
4119 4133
4120 return unless $self->{value} ne $value; 4134 return unless $self->{value} ne $value;
4121 4135
4122 $self->_set_value ($value); 4136 $self->_set_value ($value);
4123 $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});
4124} 4145}
4125 4146
4126############################################################################# 4147#############################################################################
4127 4148
4128package CFPlus::UI::Statusbox; 4149package CFPlus::UI::Statusbox;
4137 @_, 4158 @_,
4138 ); 4159 );
4139 4160
4140 CFPlus::weaken (my $this = $self); 4161 CFPlus::weaken (my $this = $self);
4141 4162
4142 $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); 4163 $self->{timer} = EV::timer 1, 1, sub { $this->reorder };
4143 4164
4144 $self 4165 $self
4145} 4166}
4146 4167
4147sub reorder { 4168sub reorder {
4155 4176
4156 while (my ($k, $v) = each %{ $self->{item} }) { 4177 while (my ($k, $v) = each %{ $self->{item} }) {
4157 delete $self->{item}{$k} if $v->{timeout} < $NOW; 4178 delete $self->{item}{$k} if $v->{timeout} < $NOW;
4158 } 4179 }
4159 4180
4181 $self->{timer}->set (1, 1);
4182
4160 my @widgets; 4183 my @widgets;
4161 4184
4162 my @items = sort { 4185 my @items = sort {
4163 $a->{pri} <=> $b->{pri} 4186 $a->{pri} <=> $b->{pri}
4164 or $b->{id} <=> $a->{id} 4187 or $b->{id} <=> $a->{id}
4165 } values %{ $self->{item} }; 4188 } values %{ $self->{item} };
4166
4167 $self->{timer}->interval (1);
4168 4189
4169 my $count = 10 + 1; 4190 my $count = 10 + 1;
4170 for my $item (@items) { 4191 for my $item (@items) {
4171 last unless --$count; 4192 last unless --$count;
4172 4193
4196 if ((my $diff = $item->{timeout} - $NOW) < 2) { 4217 if ((my $diff = $item->{timeout} - $NOW) < 2) {
4197 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2; 4218 $label->{fg}[3] = ($item->{fg}[3] || 1) * $diff / 2;
4198 $label->update; 4219 $label->update;
4199 $label->set_max_size (undef, $label->{req_h} * $diff) 4220 $label->set_max_size (undef, $label->{req_h} * $diff)
4200 if $diff < 1; 4221 if $diff < 1;
4201 $self->{timer}->interval (1/30); 4222 $self->{timer}->set (1/30, 1/30);
4202 } else { 4223 } else {
4203 $label->{fg}[3] = $item->{fg}[3] || 1; 4224 $label->{fg}[3] = $item->{fg}[3] || 1;
4204 } 4225 }
4205 4226
4206 push @widgets, $label; 4227 push @widgets, $label;
4342} 4363}
4343 4364
4344sub update { 4365sub update {
4345 my ($self) = @_; 4366 my ($self) = @_;
4346 4367
4347 $::WANT_REFRESH->start; 4368 $::WANT_REFRESH = 1;
4348} 4369}
4349 4370
4350sub add { 4371sub add {
4351 my ($self, @children) = @_; 4372 my ($self, @children) = @_;
4352 4373

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines