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.190 by root, Mon May 8 22:17:38 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 #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};
428
406 $self->{parent} 429 $self->{parent}
407 or return 1; 430 or return 1;
408 431
409 my ($w, $h) = $self->{user_w} && $self->{user_h} 432 my ($w, $h) = $self->{user_w} && $self->{user_h}
410 ? @$self{qw(user_w user_h)} 433 ? @$self{qw(user_w user_h)}
412 435
413 if ($w != $self->{req_w} || $h != $self->{req_h}) { 436 if ($w != $self->{req_w} || $h != $self->{req_h}) {
414 $self->{req_w} = $w; 437 $self->{req_w} = $w;
415 $self->{req_h} = $h; 438 $self->{req_h} = $h;
416 439
417 $self->{parent}->check_size 440 $self->{parent}->_check_size
418 or $self->size_allocate ( 441 or $self->size_allocate (
419 (List::Util::max $self->{w}, $w), 442 (List::Util::max $self->{w}, $w),
420 (List::Util::max $self->{h}, $h), 443 (List::Util::max $self->{h}, $h),
421 ); 444 );
422 445
427} 450}
428 451
429sub update { 452sub update {
430 my ($self) = @_; 453 my ($self) = @_;
431 454
432 $self->{parent}->update 455 my $update = $CFClient::UI::ROOT->{_update} ||= {};
433 if $self->{parent}; 456
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 });
434} 468}
435 469
436sub connect { 470sub connect {
437 my ($self, $signal, $cb) = @_; 471 my ($self, $signal, $cb) = @_;
438 472
528 562
529 $self 563 $self
530} 564}
531 565
532sub add { 566sub add {
533 my ($self, $child) = @_; 567 my ($self, @widgets) = @_;
534 568
535 $child->set_parent ($self); 569 $_->set_parent ($self)
570 for @widgets;
536 571
537 use sort 'stable'; 572 use sort 'stable';
538 573
539 $self->{children} = [ 574 $self->{children} = [
540 sort { $a->{z} <=> $b->{z} } 575 sort { $a->{z} <=> $b->{z} }
541 @{$self->{children}}, $child 576 @{$self->{children}}, @widgets
542 ]; 577 ];
543 578
544 $child->check_size; 579 $_->check_size
580 for @widgets;
581
545 $self->update; 582 $self->update;
546} 583}
547 584
548sub children { 585sub children {
549 @{ $_[0]{children} } 586 @{ $_[0]{children} }
1322 1359
1323 delete $self->{texture}; 1360 delete $self->{texture};
1324 $self->SUPER::update; 1361 $self->SUPER::update;
1325} 1362}
1326 1363
1327sub reconfigure {
1328 my ($self) = @_;
1329
1330 delete $self->{texture};
1331}
1332
1333sub set_text { 1364sub set_text {
1334 my ($self, $text) = @_; 1365 my ($self, $text) = @_;
1335 1366
1336 return if $self->{text} eq "T$text"; 1367 return if $self->{text} eq "T$text";
1337 $self->{text} = "T$text"; 1368 $self->{text} = "T$text";
1390sub set_fontsize { 1421sub set_fontsize {
1391 my ($self, $fontsize) = @_; 1422 my ($self, $fontsize) = @_;
1392 1423
1393 $self->{fontsize} = $fontsize; 1424 $self->{fontsize} = $fontsize;
1394 delete $self->{texture}; 1425 delete $self->{texture};
1426
1427 $self->update;
1395 $self->check_size; 1428 $self->check_size;
1396 $self->update;
1397} 1429}
1398 1430
1399sub _draw { 1431sub _draw {
1400 my ($self) = @_; 1432 my ($self) = @_;
1401 1433
2483} 2515}
2484 2516
2485sub _draw { 2517sub _draw {
2486 my ($self) = @_; 2518 my ($self) = @_;
2487 2519
2520 return unless $::CONN;#d# manage and cache textures differently
2488 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; 2521 my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]];
2489 2522
2490 # TODO animation 2523 # TODO animation
2491 if ($tex) { 2524 if ($tex) {
2492 glEnable GL_BLEND; 2525 glEnable GL_BLEND;
2496 glColor 1, 1, 1, 1; 2529 glColor 1, 1, 1, 1;
2497 $tex->draw_quad (0, 0, $self->{w}, $self->{h}); 2530 $tex->draw_quad (0, 0, $self->{w}, $self->{h});
2498 glDisable GL_TEXTURE_2D; 2531 glDisable GL_TEXTURE_2D;
2499 glDisable GL_BLEND; 2532 glDisable GL_BLEND;
2500 } 2533 }
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} 2534}
2655 2535
2656############################################################################# 2536#############################################################################
2657 2537
2658package CFClient::UI::InventoryItem; 2538package CFClient::UI::InventoryItem;
2758 ($self->{req_w}, $self->{req_h}); 2638 ($self->{req_w}, $self->{req_h});
2759} 2639}
2760 2640
2761############################################################################# 2641#############################################################################
2762 2642
2643package CFClient::UI::Menu;
2644
2645our @ISA = CFClient::UI::FancyFrame::;
2646
2647use CFClient::OpenGL;
2648
2649sub new {
2650 my $class = shift;
2651
2652 my $self = $class->SUPER::new (
2653 items => [],
2654 z => 100,
2655 @_,
2656 );
2657
2658 $self->add ($self->{vbox} = new CFClient::UI::VBox);
2659
2660 for my $item (@{ $self->{items} }) {
2661 my ($widget, $cb) = @$item;
2662
2663 # handle various types of items, only text for now
2664 if (!ref $widget) {
2665 $widget = new CFClient::UI::Label
2666 can_hover => 1,
2667 can_events => 1,
2668 text => $widget;
2669 }
2670
2671 $self->{item}{$widget} = $item;
2672
2673 $self->{vbox}->add ($widget);
2674 }
2675
2676 $self
2677}
2678
2679# popup given the event (must be a mouse button down event currently)
2680sub popup {
2681 my ($self, $ev) = @_;
2682
2683 $self->emit ("popdown");
2684
2685 # maybe save $GRAB? must be careful about events...
2686 $GRAB = $self;
2687 $self->{button} = $ev->{button};
2688
2689 $self->show;
2690 $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5);
2691}
2692
2693sub mouse_motion {
2694 my ($self, $ev, $x, $y) = @_;
2695
2696 # TODO: should use vbox->find_widget or so
2697 $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y});
2698 $self->{hover} = $self->{item}{$HOVER};
2699}
2700
2701sub button_up {
2702 my ($self, $ev, $x, $y) = @_;
2703
2704 if ($ev->{button} == $self->{button}) {
2705 undef $GRAB;
2706 $self->hide;
2707
2708 $self->emit ("popdown");
2709 $self->{hover}[1]->() if $self->{hover};
2710 }
2711}
2712
2713#############################################################################
2714
2715package CFClient::UI::Root;
2716
2717our @ISA = CFClient::UI::Container::;
2718
2719use CFClient::OpenGL;
2720
2721sub check_size {
2722 my ($self) = @_;
2723
2724 $self->configure (0, 0, $self->{w}, $self->{h});
2725}
2726
2727sub size_request {
2728 my ($self) = @_;
2729
2730 ($self->{w}, $self->{h})
2731}
2732
2733sub size_allocate {
2734 my ($self, $w, $h) = @_;
2735
2736 my $old_w = $self->{old_w};
2737 my $old_h = $self->{old_h};
2738
2739 if ($old_w && $old_h) {
2740 for my $child ($self->children) {
2741 $child->{x} = int 0.5 + $child->{x} * $w / $old_w;
2742 $child->{w} = int 0.5 + $child->{w} * $w / $old_w;
2743 $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w};
2744 $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w};
2745 $child->{y} = int 0.5 + $child->{y} * $h / $old_h;
2746 $child->{h} = int 0.5 + $child->{h} * $h / $old_h;
2747 $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};
2749 }
2750 }
2751
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) {
2762 my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)};
2763
2764 $X = List::Util::max 0, List::Util::min $w - $W, $X;
2765 $Y = List::Util::max 0, List::Util::min $h - $H, $Y;
2766 $child->configure ($X, $Y, $W, $H);
2767 }
2768}
2769
2770sub _topleft {
2771 my ($self, $x, $y) = @_;
2772
2773 ($x, $y)
2774}
2775
2776sub update {
2777 my ($self) = @_;
2778
2779 $self->check_size;
2780 ::refresh ();
2781}
2782
2783sub add {
2784 my ($self, $child) = @_;
2785
2786 # integerize window positions
2787 $child->{x} = int $child->{x};
2788 $child->{y} = int $child->{y};
2789
2790 $self->SUPER::add ($child);
2791}
2792
2793sub on_refresh {
2794 my ($self, $id, $cb) = @_;
2795
2796 $self->{refresh_hook}{$id} = $cb;
2797}
2798
2799sub draw {
2800 my ($self) = @_;
2801
2802 while (my $rcb = delete $self->{refresh_hook}) {
2803 $_->() for values %$rcb;
2804 }
2805
2806 glViewport 0, 0, $::WIDTH, $::HEIGHT;
2807 glClearColor +($::CFG->{fow_intensity}) x 3, 1;
2808 glClear GL_COLOR_BUFFER_BIT;
2809
2810 glMatrixMode GL_PROJECTION;
2811 glLoadIdentity;
2812 glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000;
2813 glMatrixMode GL_MODELVIEW;
2814 glLoadIdentity;
2815
2816 $self->_draw;
2817}
2818
2819#############################################################################
2820
2763package CFClient::UI; 2821package CFClient::UI;
2764 2822
2765$ROOT = new CFClient::UI::Root; 2823$ROOT = new CFClient::UI::Root;
2766$TOOLTIP = new CFClient::UI::Tooltip; 2824$TOOLTIP = new CFClient::UI::Tooltip;
2767 2825

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines