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.190 by root, Mon May 8 22:17:38 2006 UTC vs.
Revision 1.192 by root, Tue May 9 22:49:32 2006 UTC

232 $self->{y} = $y; 232 $self->{y} = $y;
233 $self->update; 233 $self->update;
234 } 234 }
235 235
236 if ($self->{w} != $w || $self->{h} != $h) { 236 if ($self->{w} != $w || $self->{h} != $h) {
237 $self->{w} = $w; 237 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $w, $h];
238 $self->{h} = $h;
239
240 $self->size_allocate ($w, $h);
241 $self->update;
242 $self->emit (size_allocate => $w, $h);
243 } 238 }
244} 239}
245 240
246sub size_allocate { 241sub size_allocate {
247 # nothing to be done 242 # nothing to be done
256 251
257 $_->reconfigure 252 $_->reconfigure
258 for $self->children; 253 for $self->children;
259 254
260 $self->check_size; 255 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h}); 256 $CFClient::UI::ROOT->{size_alloc}{$self} = [$self, $self->{w}, $self->{h}];
262 $self->update; 257 $self->update;
263} 258}
264 259
265sub set_max_size { 260sub set_max_size {
266 my ($self, $w, $h) = @_; 261 my ($self, $w, $h) = @_;
267 262
268 delete $self->{max_w}; $self->{max_w} = $w if $w; 263 delete $self->{max_w}; $self->{max_w} = $w if $w;
269 delete $self->{max_h}; $self->{max_h} = $h if $h; 264 delete $self->{max_h}; $self->{max_h} = $h if $h;
270}
271
272# return top left coordinates
273sub _topleft {
274 my ($self, $x, $y) = @_;
275
276 $self->{parent}
277 or Carp::confess "no parent widget in _topleft\n";#d#
278
279 $self->{parent}->_topleft ($x + $self->{x}, $y + $self->{y});
280} 265}
281 266
282# translate global coordinates to local coordinate system 267# translate global coordinates to local coordinate system
283sub coord2local { 268sub coord2local {
284 my ($self, $x, $y) = @_; 269 my ($self, $x, $y) = @_;
285 270
286 my ($X, $Y) = $self->_topleft; 271 $self->{parent}->coord2local ($x - $self->{x}, $y - $self->{y})
287 ($x - $X, $y - $Y)
288} 272}
289 273
290# translate local coordinates to global coordinate system 274# translate local coordinates to global coordinate system
291sub coord2global { 275sub coord2global {
292 my ($self, $x, $y) = @_; 276 my ($self, $x, $y) = @_;
293 277
294 my ($X, $Y) = $self->_topleft; 278 $self->{parent}->coord2global ($x + $self->{x}, $y + $self->{y})
295 ($x + $X, $y + $Y)
296} 279}
297 280
298sub focus_in { 281sub focus_in {
299 my ($self) = @_; 282 my ($self) = @_;
300 283
397} 380}
398 381
399sub set_parent { 382sub set_parent {
400 my ($self, $parent) = @_; 383 my ($self, $parent) = @_;
401 384
385 $self->{_tree_depth} = $parent->{_tree_depth} + 1;
402 Scalar::Util::weaken ($self->{parent} = $parent); 386 Scalar::Util::weaken ($self->{parent} = $parent);
403} 387}
404 388
405sub check_size { 389sub check_size {
406 my ($self) = @_; 390 my ($self) = @_;
407 391
408 $self->_check_size;
409 return;
410 #TODO: but needs a vastly more difficult algorithm
411 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
412
413 $check_size->{$self} = $self;
414
415 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
416 while (%$check_size) {
417 my @widgets = values %$check_size;
418 $_->_check_size
419 for @widgets;
420 }
421 });
422}
423
424sub _check_size {
425 my ($self) = @_;
426
427 delete $CFClient::UI::ROOT->{_check_size}{$self}; 392 $CFClient::UI::ROOT->{check_size}{$self} = $self;
428
429 $self->{parent}
430 or return 1;
431
432 my ($w, $h) = $self->{user_w} && $self->{user_h}
433 ? @$self{qw(user_w user_h)}
434 : $self->size_request;
435
436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
437 $self->{req_w} = $w;
438 $self->{req_h} = $h;
439
440 $self->{parent}->_check_size
441 or $self->size_allocate (
442 (List::Util::max $self->{w}, $w),
443 (List::Util::max $self->{h}, $h),
444 );
445
446 1
447 } else {
448 0
449 }
450} 393}
451 394
452sub update { 395sub update {
453 my ($self) = @_; 396 my ($self) = @_;
454 397
455 my $update = $CFClient::UI::ROOT->{_update} ||= {}; 398 $self->{parent}->update
456 399 if $self->{parent};
457 $update->{$self} = $self;
458
459 $CFClient::UI::ROOT->on_refresh (_update => sub {
460 while (%$update) {
461 my @widgets = values %$update;
462 %$update = ();
463
464 $_->{parent} && $_->{parent}->update
465 for @widgets;
466 }
467 });
468} 400}
469 401
470sub connect { 402sub connect {
471 my ($self, $signal, $cb) = @_; 403 my ($self, $signal, $cb) = @_;
472 404
776 $self->{view_y} = int $y; 708 $self->{view_y} = int $y;
777 709
778 $self->update; 710 $self->update;
779} 711}
780 712
781# hmm, this does not work for topleft of $self... but we should not aks for that 713# hmm, this does not work for topleft of $self... but we should not ask for that
782sub _topleft { 714sub coord2local {
783 my ($self, $x, $y) = @_; 715 my ($self, $x, $y) = @_;
784 716
785 $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) 717 $self->SUPER::coord2local ($x + $self->{view_x}, $y + $self->{view_y})
718}
719
720sub coord2global {
721 my ($self, $x, $y) = @_;
722
723 $x = List::Util::min $self->{w}, $x - $self->{view_x};
724 $y = List::Util::min $self->{h}, $y - $self->{view_y};
725
726 $self->SUPER::coord2global ($x, $y)
786} 727}
787 728
788sub find_widget { 729sub find_widget {
789 my ($self, $x, $y) = @_; 730 my ($self, $x, $y) = @_;
790 731
1367 return if $self->{text} eq "T$text"; 1308 return if $self->{text} eq "T$text";
1368 $self->{text} = "T$text"; 1309 $self->{text} = "T$text";
1369 1310
1370 $self->{layout}->set_text ($text); 1311 $self->{layout}->set_text ($text);
1371 1312
1372 delete $self->{texture};
1373 $self->update; 1313 $self->update;
1374 $self->check_size; 1314 $self->check_size;
1375} 1315}
1376 1316
1377sub set_markup { 1317sub set_markup {
1380 return if $self->{text} eq "M$markup"; 1320 return if $self->{text} eq "M$markup";
1381 $self->{text} = "M$markup"; 1321 $self->{text} = "M$markup";
1382 1322
1383 $self->{layout}->set_markup ($markup); 1323 $self->{layout}->set_markup ($markup);
1384 1324
1385 delete $self->{texture};
1386 $self->update; 1325 $self->update;
1387 $self->check_size; 1326 $self->check_size;
1388} 1327}
1389 1328
1390sub size_request { 1329sub size_request {
2542sub new { 2481sub new {
2543 my $class = shift; 2482 my $class = shift;
2544 2483
2545 my %args = @_; 2484 my %args = @_;
2546 2485
2547 my $item = $args{item}; 2486 my $item = delete $args{item};
2548 2487
2549 my $desc = $item->{nrof} < 2 2488 my $desc = $item->{nrof} < 2
2550 ? $item->{name} 2489 ? $item->{name}
2551 : "$item->{nrof} $item->{name_pl}"; 2490 : "$item->{nrof} $item->{name_pl}";
2552 2491
2581 2520
2582 1 2521 1
2583 }, 2522 },
2584 %args 2523 %args
2585 ); 2524 );
2525
2586 $self->add(new CFClient::UI::Face 2526 $self->add (new CFClient::UI::Face
2587 can_events => 0, 2527 can_events => 0,
2588 face => $item->{face}, 2528 face => $item->{face},
2589 anim => $item->{anim}, 2529 anim => $item->{anim},
2590 animspeed => $item->{animspeed}); 2530 animspeed => $item->{animspeed},
2531 );
2532
2591 $self->add(new CFClient::UI::Label 2533 $self->add (new CFClient::UI::Label
2592 can_events => 0, 2534 can_events => 0,
2593 text => $desc); 2535 text => $desc,
2536 );
2594 2537
2595 $self 2538 $self
2596} 2539}
2597 2540
2598############################################################################# 2541#############################################################################
2625 for my $item (@items) { 2568 for my $item (@items) {
2626 my $desc = $item->{nrof} < 2 2569 my $desc = $item->{nrof} < 2
2627 ? $item->{name} 2570 ? $item->{name}
2628 : "$item->{nrof} $item->{name_pl}"; 2571 : "$item->{nrof} $item->{name_pl}";
2629 2572
2630 $self->{scrolled}->add (new CFClient::UI::InventoryItem item => $item); 2573 $self->{scrolled}->add ($item->{widget} ||= new CFClient::UI::InventoryItem item => $item);
2631 } 2574 }
2632 2575
2633# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page]; 2576# $range->{range} = [$self->{pos}, 0, $self->{max_pos}, $page];
2634} 2577}
2635 2578
2716 2659
2717our @ISA = CFClient::UI::Container::; 2660our @ISA = CFClient::UI::Container::;
2718 2661
2719use CFClient::OpenGL; 2662use CFClient::OpenGL;
2720 2663
2664sub new {
2665 my $class = shift;
2666
2667 $class->SUPER::new (
2668 @_,
2669 )
2670}
2671
2672sub configure {
2673 my ($self, $x, $y, $w, $h) = @_;
2674
2675 $self->{w} = $w;
2676 $self->{h} = $h;
2677}
2678
2721sub check_size { 2679sub check_size {
2722 my ($self) = @_; 2680 my ($self) = @_;
2723 2681
2724 $self->configure (0, 0, $self->{w}, $self->{h}); 2682 $self->size_allocate ($self->{w}, $self->{h})
2683 if $self->{w};
2725} 2684}
2726 2685
2727sub size_request { 2686sub size_request {
2728 my ($self) = @_; 2687 my ($self) = @_;
2729 2688
2747 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; 2706 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2748 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; 2707 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2749 } 2708 }
2750 } 2709 }
2751 2710
2752 $self->{old_w} = $w;
2753 $self->{old_h} = $h;
2754}
2755
2756sub configure {
2757 my ($self, $x, $y, $w, $h) = @_;
2758
2759 $self->SUPER::configure ($x, $y, $w, $h);
2760
2761 for my $child ($self->children) { 2711 for my $child ($self->children) {
2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; 2712 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2763 2713
2764 $X = List::Util::max 0, List::Util::min $w - $W, $X; 2714 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y; 2715 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2766 $child->configure ($X, $Y, $W, $H); 2716 $child->configure ($X, $Y, $W, $H);
2767 } 2717 }
2768}
2769 2718
2770sub _topleft { 2719 $self->{old_w} = $w;
2720 $self->{old_h} = $h;
2721}
2722
2723sub coord2local {
2771 my ($self, $x, $y) = @_; 2724 my ($self, $x, $y) = @_;
2772 2725
2773 ($x, $y) 2726 ($x, $y)
2774} 2727}
2775 2728
2729sub coord2global {
2730 my ($self, $x, $y) = @_;
2731
2732 ($x, $y)
2733}
2734
2776sub update { 2735sub update {
2777 my ($self) = @_; 2736 my ($self) = @_;
2778 2737
2779 $self->check_size; 2738 $self->check_size;
2780 ::refresh (); 2739 $::WANT_REFRESH++;
2781} 2740}
2782 2741
2783sub add { 2742sub add {
2784 my ($self, $child) = @_; 2743 my ($self, $child) = @_;
2785 2744
2786 # integerize window positions 2745 # integerise window positions
2787 $child->{x} = int $child->{x}; 2746 $child->{x} = int $child->{x};
2788 $child->{y} = int $child->{y}; 2747 $child->{y} = int $child->{y};
2789 2748
2790 $self->SUPER::add ($child); 2749 $self->SUPER::add ($child);
2791} 2750}
2794 my ($self, $id, $cb) = @_; 2753 my ($self, $id, $cb) = @_;
2795 2754
2796 $self->{refresh_hook}{$id} = $cb; 2755 $self->{refresh_hook}{$id} = $cb;
2797} 2756}
2798 2757
2799sub draw { 2758sub draw {
2800 my ($self) = @_; 2759 my ($self) = @_;
2801 2760
2761 if ($self->{check_size}) {
2762 my @queue = ([], []);
2763
2764 for (;;) {
2765 if ($self->{check_size}) {
2766 # heuristic: check containers last
2767 push @{ $queue[ ! ! $_->isa ("CFClient::UI::Container") ] }, $_
2768 for values %{delete $self->{check_size}}
2769 }
2770
2771 my $widget = (pop @{ $queue[0] }) || (pop @{ $queue[1] }) || last;
2772
2773 my ($w, $h) = $widget->{user_w} && $widget->{user_h}
2774 ? @$widget{qw(user_w user_h)}
2775 : $widget->size_request;
2776
2777 if ($w != $widget->{req_w} || $h != $widget->{req_h}) {
2778 $widget->{req_w} = $w;
2779 $widget->{req_h} = $h;
2780
2781 $self->{size_alloc}{$widget} = [$widget, $widget->{w}, $widget->{h}];
2782
2783 $widget->{parent}->check_size
2784 if $widget->{parent};
2785 }
2786 }
2787 }
2788
2789 while ($self->{size_alloc}) {
2790 for (values %{delete $self->{size_alloc}}) {
2791 my ($widget, $w, $h) = @$_;
2792
2793 $widget->{w} = $w;
2794 $widget->{h} = $h;
2795 $widget->size_allocate ($w, $h);
2796 $widget->emit (size_allocate => $w, $h);
2797 }
2798 }
2799
2802 while (my $rcb = delete $self->{refresh_hook}) { 2800 while ($self->{refresh_hook}) {
2803 $_->() for values %$rcb; 2801 $_->()
2802 for values %{delete $self->{refresh_hook}};
2804 } 2803 }
2805 2804
2806 glViewport 0, 0, $::WIDTH, $::HEIGHT; 2805 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2807 glClearColor +($::CFG->{fow_intensity}) x 3, 1; 2806 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2808 glClear GL_COLOR_BUFFER_BIT; 2807 glClear GL_COLOR_BUFFER_BIT;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines