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.185 by root, Mon May 8 17:23:08 2006 UTC vs.
Revision 1.189 by root, Mon May 8 22:17:24 2006 UTC

255 my ($self) = @_; 255 my ($self) = @_;
256 256
257 $_->reconfigure 257 $_->reconfigure
258 for $self->children; 258 for $self->children;
259 259
260 $_->check_size; 260 $self->check_size;
261 $self->size_allocate ($self->{w}, $self->{h});
262 $self->update;
261} 263}
262 264
263sub set_max_size { 265sub set_max_size {
264 my ($self, $w, $h) = @_; 266 my ($self, $w, $h) = @_;
265 267
401} 403}
402 404
403sub check_size { 405sub check_size {
404 my ($self) = @_; 406 my ($self) = @_;
405 407
408 $self->_check_size;
409 return;
410 my $check_size = $CFClient::UI::ROOT->{_check_size} ||= {};
411
412 $check_size->{$self} = $self;
413
414 $CFClient::UI::ROOT->on_refresh (_check_size => sub {
415 while (%$check_size) {
416 my @widgets = values %$check_size;
417 $_->_check_size
418 for @widgets;
419 }
420 });
421}
422
423sub _check_size {
424 my ($self) = @_;
425
426 delete $CFClient::UI::ROOT->{_check_size}{$self};
427
406 $self->{parent} 428 $self->{parent}
407 or return 1; 429 or return 1;
408 430
409 my ($w, $h) = $self->{user_w} && $self->{user_h} 431 my ($w, $h) = $self->{user_w} && $self->{user_h}
410 ? @$self{qw(user_w user_h)} 432 ? @$self{qw(user_w user_h)}
412 434
413 if ($w != $self->{req_w} || $h != $self->{req_h}) { 435 if ($w != $self->{req_w} || $h != $self->{req_h}) {
414 $self->{req_w} = $w; 436 $self->{req_w} = $w;
415 $self->{req_h} = $h; 437 $self->{req_h} = $h;
416 438
417 $self->{parent}->check_size 439 $self->{parent}->_check_size
418 or $self->size_allocate ( 440 or $self->size_allocate (
419 (List::Util::max $self->{w}, $w), 441 (List::Util::max $self->{w}, $w),
420 (List::Util::max $self->{h}, $h), 442 (List::Util::max $self->{h}, $h),
421 ); 443 );
422 444
427} 449}
428 450
429sub update { 451sub update {
430 my ($self) = @_; 452 my ($self) = @_;
431 453
432 $self->{parent}->update 454 my $update = $CFClient::UI::ROOT->{_update} ||= {};
433 if $self->{parent}; 455
456 $update->{$self} = $self;
457
458 $CFClient::UI::ROOT->on_refresh (_update => sub {
459 while (%$update) {
460 my @widgets = values %$update;
461 %$update = ();
462
463 $_->{parent} && $_->{parent}->update
464 for @widgets;
465 }
466 });
434} 467}
435 468
436sub connect { 469sub connect {
437 my ($self, $signal, $cb) = @_; 470 my ($self, $signal, $cb) = @_;
438 471
528 561
529 $self 562 $self
530} 563}
531 564
532sub add { 565sub add {
533 my ($self, $child) = @_; 566 my ($self, @widgets) = @_;
534 567
535 $child->set_parent ($self); 568 $_->set_parent ($self)
569 for @widgets;
536 570
537 use sort 'stable'; 571 use sort 'stable';
538 572
539 $self->{children} = [ 573 $self->{children} = [
540 sort { $a->{z} <=> $b->{z} } 574 sort { $a->{z} <=> $b->{z} }
541 @{$self->{children}}, $child 575 @{$self->{children}}, @widgets
542 ]; 576 ];
543 577
544 $child->check_size; 578 $_->check_size
579 for @widgets;
580
545 $self->update; 581 $self->update;
546} 582}
547 583
548sub children { 584sub children {
549 @{ $_[0]{children} } 585 @{ $_[0]{children} }
1322 1358
1323 delete $self->{texture}; 1359 delete $self->{texture};
1324 $self->SUPER::update; 1360 $self->SUPER::update;
1325} 1361}
1326 1362
1327sub reconfigure {
1328 my ($self) = @_;
1329
1330 delete $self->{texture};
1331}
1332
1333sub set_text { 1363sub set_text {
1334 my ($self, $text) = @_; 1364 my ($self, $text) = @_;
1335 1365
1336 return if $self->{text} eq "T$text"; 1366 return if $self->{text} eq "T$text";
1337 $self->{text} = "T$text"; 1367 $self->{text} = "T$text";
1390sub set_fontsize { 1420sub set_fontsize {
1391 my ($self, $fontsize) = @_; 1421 my ($self, $fontsize) = @_;
1392 1422
1393 $self->{fontsize} = $fontsize; 1423 $self->{fontsize} = $fontsize;
1394 delete $self->{texture}; 1424 delete $self->{texture};
1425
1426 $self->update;
1395 $self->check_size; 1427 $self->check_size;
1396 $self->update;
1397} 1428}
1398 1429
1399sub _draw { 1430sub _draw {
1400 my ($self) = @_; 1431 my ($self) = @_;
1401 1432
2497 glColor 1, 1, 1, 1; 2528 glColor 1, 1, 1, 1;
2498 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2529 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2499 glDisable GL_TEXTURE_2D; 2530 glDisable GL_TEXTURE_2D;
2500 glDisable GL_BLEND; 2531 glDisable GL_BLEND;
2501 } 2532 }
2502}
2503
2504#############################################################################
2505
2506package CFClient::UI::Menu;
2507
2508our @ISA = CFClient::UI::FancyFrame::;
2509
2510use CFClient::OpenGL;
2511
2512sub new {
2513 my $class = shift;
2514
2515 my $self = $class->SUPER::new (
2516 items => [],
2517 z => 100,
2518 @_,
2519 );
2520
2521 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2522
2523 for my $item (@{ $self->{items} }) {
2524 my ($widget, $cb) = @$item;
2525
2526 # handle various types of items, only text for now
2527 if (!ref $widget) {
2528 $widget = new CFClient::UI::Label
2529 can_hover => 1,
2530 can_events => 1,
2531 text => $widget;
2532 }
2533
2534 $self->{item}{$widget} = $item;
2535
2536 $self->{vbox}->add ($widget);
2537 }
2538
2539 $self
2540}
2541
2542# popup given the event (must be a mouse button down event currently)
2543sub popup {
2544 my ($self, $ev) = @_;
2545
2546 $self->emit ("popdown");
2547
2548 # maybe save $GRAB? must be careful about events...
2549 $GRAB = $self;
2550 $self->{button} = $ev->{button};
2551
2552 $self->show;
2553 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2554}
2555
2556sub mouse_motion {
2557 my ($self, $ev, $x, $y) = @_;
2558
2559 # TODO: should use vbox->find_widget or so
2560 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2561 $self->{hover} = $self->{item}{$HOVER};
2562}
2563
2564sub button_up {
2565 my ($self, $ev, $x, $y) = @_;
2566
2567 if ($ev->{button} == $self->{button}) {
2568 undef $GRAB;
2569 $self->hide;
2570
2571 $self->emit ("popdown");
2572 $self->{hover}[1]->() if $self->{hover};
2573 }
2574}
2575
2576#############################################################################
2577
2578package CFClient::UI::Root;
2579
2580our @ISA = CFClient::UI::Container::;
2581
2582use CFClient::OpenGL;
2583
2584sub check_size {
2585 my ($self) = @_;
2586
2587 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2588}
2589
2590sub size_request {
2591 ($::WIDTH, $::HEIGHT)
2592}
2593
2594sub configure {
2595 my ($self, $x, $y, $w, $h) = @_;
2596
2597 $self->SUPER::configure ($x, $y, $w, $h);
2598
2599 for my $child (@{$self->{children}}) {
2600 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2601
2602 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2603 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2604 $child->configure ($X, $Y, $W,$H);
2605 }
2606}
2607
2608sub _topleft {
2609 my ($self, $x, $y) = @_;
2610
2611 ($x, $y)
2612}
2613
2614sub update {
2615 my ($self) = @_;
2616
2617 $self->check_size;
2618 ::refresh ();
2619}
2620
2621sub add {
2622 my ($self, $child) = @_;
2623
2624 # integerize window positions
2625 $child->{x} = int $child->{x};
2626 $child->{y} = int $child->{y};
2627
2628 $self->SUPER::add ($child);
2629}
2630
2631sub on_refresh {
2632 my ($self, $id, $cb) = @_;
2633
2634 $self->{refresh_hook}{$id} = $cb;
2635}
2636
2637sub draw {
2638 my ($self) = @_;
2639
2640 while (my $rcb = delete $self->{refresh_hook}) {
2641 $_->() for values %$rcb;
2642 }
2643
2644 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2645 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2646 glClear GL_COLOR_BUFFER_BIT;
2647
2648 glMatrixMode GL_PROJECTION;
2649 glLoadIdentity;
2650 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2651 glMatrixMode GL_MODELVIEW;
2652 glLoadIdentity;
2653
2654 $self->_draw;
2655} 2533}
2656 2534
2657############################################################################# 2535#############################################################################
2658 2536
2659package CFClient::UI::InventoryItem; 2537package CFClient::UI::InventoryItem;
2759 ($self->{req_w}, $self->{req_h}); 2637 ($self->{req_w}, $self->{req_h});
2760} 2638}
2761 2639
2762############################################################################# 2640#############################################################################
2763 2641
2642package CFClient::UI::Menu;
2643
2644our @ISA = CFClient::UI::FancyFrame::;
2645
2646use CFClient::OpenGL;
2647
2648sub new {
2649 my $class = shift;
2650
2651 my $self = $class->SUPER::new (
2652 items => [],
2653 z => 100,
2654 @_,
2655 );
2656
2657 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2658
2659 for my $item (@{ $self->{items} }) {
2660 my ($widget, $cb) = @$item;
2661
2662 # handle various types of items, only text for now
2663 if (!ref $widget) {
2664 $widget = new CFClient::UI::Label
2665 can_hover => 1,
2666 can_events => 1,
2667 text => $widget;
2668 }
2669
2670 $self->{item}{$widget} = $item;
2671
2672 $self->{vbox}->add ($widget);
2673 }
2674
2675 $self
2676}
2677
2678# popup given the event (must be a mouse button down event currently)
2679sub popup {
2680 my ($self, $ev) = @_;
2681
2682 $self->emit ("popdown");
2683
2684 # maybe save $GRAB? must be careful about events...
2685 $GRAB = $self;
2686 $self->{button} = $ev->{button};
2687
2688 $self->show;
2689 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2690}
2691
2692sub mouse_motion {
2693 my ($self, $ev, $x, $y) = @_;
2694
2695 # TODO: should use vbox->find_widget or so
2696 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2697 $self->{hover} = $self->{item}{$HOVER};
2698}
2699
2700sub button_up {
2701 my ($self, $ev, $x, $y) = @_;
2702
2703 if ($ev->{button} == $self->{button}) {
2704 undef $GRAB;
2705 $self->hide;
2706
2707 $self->emit ("popdown");
2708 $self->{hover}[1]->() if $self->{hover};
2709 }
2710}
2711
2712#############################################################################
2713
2714package CFClient::UI::Root;
2715
2716our @ISA = CFClient::UI::Container::;
2717
2718use CFClient::OpenGL;
2719
2720sub check_size {
2721 my ($self) = @_;
2722
2723 $self->configure (0, 0, $self->{w}, $self->{h});
2724}
2725
2726sub size_request {
2727 my ($self) = @_;
2728
2729 ($self->{w}, $self->{h})
2730}
2731
2732sub size_allocate {
2733 my ($self, $w, $h) = @_;
2734
2735 my $old_w = $self->{old_w};
2736 my $old_h = $self->{old_h};
2737
2738 if ($old_w && $old_h) {
2739 for my $child ($self->children) {
2740 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2741 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2742 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2743 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2744 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2745 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2746 $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h};
2747 $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h};
2748 }
2749 }
2750
2751 $self->{old_w} = $w;
2752 $self->{old_h} = $h;
2753}
2754
2755sub configure {
2756 my ($self, $x, $y, $w, $h) = @_;
2757
2758 $self->SUPER::configure ($x, $y, $w, $h);
2759
2760 for my $child ($self->children) {
2761 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2762
2763 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2764 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2765 $child->configure ($X, $Y, $W, $H);
2766 }
2767}
2768
2769sub _topleft {
2770 my ($self, $x, $y) = @_;
2771
2772 ($x, $y)
2773}
2774
2775sub update {
2776 my ($self) = @_;
2777
2778 $self->check_size;
2779 ::refresh ();
2780}
2781
2782sub add {
2783 my ($self, $child) = @_;
2784
2785 # integerize window positions
2786 $child->{x} = int $child->{x};
2787 $child->{y} = int $child->{y};
2788
2789 $self->SUPER::add ($child);
2790}
2791
2792sub on_refresh {
2793 my ($self, $id, $cb) = @_;
2794
2795 $self->{refresh_hook}{$id} = $cb;
2796}
2797
2798sub draw {
2799 my ($self) = @_;
2800
2801 while (my $rcb = delete $self->{refresh_hook}) {
2802 $_->() for values %$rcb;
2803 }
2804
2805 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2806 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2807 glClear GL_COLOR_BUFFER_BIT;
2808
2809 glMatrixMode GL_PROJECTION;
2810 glLoadIdentity;
2811 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2812 glMatrixMode GL_MODELVIEW;
2813 glLoadIdentity;
2814
2815 $self->_draw;
2816}
2817
2818#############################################################################
2819
2764package CFClient::UI; 2820package CFClient::UI;
2765 2821
2766$ROOT = new CFClient::UI::Root; 2822$ROOT = new CFClient::UI::Root;
2767$TOOLTIP = new CFClient::UI::Tooltip; 2823$TOOLTIP = new CFClient::UI::Tooltip;
2768 2824

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines