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.184 by root, Wed May 3 19:56:05 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
2483} 2514}
2484 2515
2485sub _draw { 2516sub _draw {
2486 my ($self) = @_; 2517 my ($self) = @_;
2487 2518
2519 return unless $::CONN;#d# manage and cache textures differently
2488 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2520 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2489 2521
2490 # TODO animation 2522 # TODO animation
2491 if ($tex) { 2523 if ($tex) {
2492 glEnable GL_BLEND; 2524 glEnable GL_BLEND;
2496 glColor 1, 1, 1, 1; 2528 glColor 1, 1, 1, 1;
2497 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2529 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2498 glDisable GL_TEXTURE_2D; 2530 glDisable GL_TEXTURE_2D;
2499 glDisable GL_BLEND; 2531 glDisable GL_BLEND;
2500 } 2532 }
2501}
2502
2503#############################################################################
2504
2505package CFClient::UI::Menu;
2506
2507our @ISA = CFClient::UI::FancyFrame::;
2508
2509use CFClient::OpenGL;
2510
2511sub new {
2512 my $class = shift;
2513
2514 my $self = $class->SUPER::new (
2515 items => [],
2516 z => 100,
2517 @_,
2518 );
2519
2520 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2521
2522 for my $item (@{ $self->{items} }) {
2523 my ($widget, $cb) = @$item;
2524
2525 # handle various types of items, only text for now
2526 if (!ref $widget) {
2527 $widget = new CFClient::UI::Label
2528 can_hover => 1,
2529 can_events => 1,
2530 text => $widget;
2531 }
2532
2533 $self->{item}{$widget} = $item;
2534
2535 $self->{vbox}->add ($widget);
2536 }
2537
2538 $self
2539}
2540
2541# popup given the event (must be a mouse button down event currently)
2542sub popup {
2543 my ($self, $ev) = @_;
2544
2545 $self->emit ("popdown");
2546
2547 # maybe save $GRAB? must be careful about events...
2548 $GRAB = $self;
2549 $self->{button} = $ev->{button};
2550
2551 $self->show;
2552 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2553}
2554
2555sub mouse_motion {
2556 my ($self, $ev, $x, $y) = @_;
2557
2558 # TODO: should use vbox->find_widget or so
2559 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2560 $self->{hover} = $self->{item}{$HOVER};
2561}
2562
2563sub button_up {
2564 my ($self, $ev, $x, $y) = @_;
2565
2566 if ($ev->{button} == $self->{button}) {
2567 undef $GRAB;
2568 $self->hide;
2569
2570 $self->emit ("popdown");
2571 $self->{hover}[1]->() if $self->{hover};
2572 }
2573}
2574
2575#############################################################################
2576
2577package CFClient::UI::Root;
2578
2579our @ISA = CFClient::UI::Container::;
2580
2581use CFClient::OpenGL;
2582
2583sub check_size {
2584 my ($self) = @_;
2585
2586 $self->configure (0, 0, $::WIDTH, $::HEIGHT);
2587}
2588
2589sub size_request {
2590 ($::WIDTH, $::HEIGHT)
2591}
2592
2593sub configure {
2594 my ($self, $x, $y, $w, $h) = @_;
2595
2596 $self->SUPER::configure ($x, $y, $w, $h);
2597
2598 for my $child (@{$self->{children}}) {
2599 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2600
2601 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2602 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2603 $child->configure ($X, $Y, $W,$H);
2604 }
2605}
2606
2607sub _topleft {
2608 my ($self, $x, $y) = @_;
2609
2610 ($x, $y)
2611}
2612
2613sub update {
2614 my ($self) = @_;
2615
2616 $self->check_size;
2617 ::refresh ();
2618}
2619
2620sub add {
2621 my ($self, $child) = @_;
2622
2623 # integerize window positions
2624 $child->{x} = int $child->{x};
2625 $child->{y} = int $child->{y};
2626
2627 $self->SUPER::add ($child);
2628}
2629
2630sub on_refresh {
2631 my ($self, $id, $cb) = @_;
2632
2633 $self->{refresh_hook}{$id} = $cb;
2634}
2635
2636sub draw {
2637 my ($self) = @_;
2638
2639 while (my $rcb = delete $self->{refresh_hook}) {
2640 $_->() for values %$rcb;
2641 }
2642
2643 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2644 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2645 glClear GL_COLOR_BUFFER_BIT;
2646
2647 glMatrixMode GL_PROJECTION;
2648 glLoadIdentity;
2649 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2650 glMatrixMode GL_MODELVIEW;
2651 glLoadIdentity;
2652
2653 $self->_draw;
2654} 2533}
2655 2534
2656############################################################################# 2535#############################################################################
2657 2536
2658package CFClient::UI::InventoryItem; 2537package CFClient::UI::InventoryItem;
2758 ($self->{req_w}, $self->{req_h}); 2637 ($self->{req_w}, $self->{req_h});
2759} 2638}
2760 2639
2761############################################################################# 2640#############################################################################
2762 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
2763package CFClient::UI; 2820package CFClient::UI;
2764 2821
2765$ROOT = new CFClient::UI::Root; 2822$ROOT = new CFClient::UI::Root;
2766$TOOLTIP = new CFClient::UI::Tooltip; 2823$TOOLTIP = new CFClient::UI::Tooltip;
2767 2824

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines