… | |
… | |
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 | |
263 | sub set_max_size { |
265 | sub set_max_size { |
264 | my ($self, $w, $h) = @_; |
266 | my ($self, $w, $h) = @_; |
265 | |
267 | |
… | |
… | |
528 | |
530 | |
529 | $self |
531 | $self |
530 | } |
532 | } |
531 | |
533 | |
532 | sub add { |
534 | sub add { |
533 | my ($self, $child) = @_; |
535 | my ($self, @widgets) = @_; |
534 | |
536 | |
535 | $child->set_parent ($self); |
537 | $_->set_parent ($self) |
|
|
538 | for @widgets; |
536 | |
539 | |
537 | use sort 'stable'; |
540 | use sort 'stable'; |
538 | |
541 | |
539 | $self->{children} = [ |
542 | $self->{children} = [ |
540 | sort { $a->{z} <=> $b->{z} } |
543 | sort { $a->{z} <=> $b->{z} } |
541 | @{$self->{children}}, $child |
544 | @{$self->{children}}, @widgets |
542 | ]; |
545 | ]; |
543 | |
546 | |
544 | $child->check_size; |
547 | $_->check_size |
|
|
548 | for @widgets; |
|
|
549 | |
545 | $self->update; |
550 | $self->update; |
546 | } |
551 | } |
547 | |
552 | |
548 | sub children { |
553 | sub children { |
549 | @{ $_[0]{children} } |
554 | @{ $_[0]{children} } |
… | |
… | |
737 | |
742 | |
738 | $self->{view_x} = int $x; |
743 | $self->{view_x} = int $x; |
739 | $self->{view_y} = int $y; |
744 | $self->{view_y} = int $y; |
740 | |
745 | |
741 | $self->update; |
746 | $self->update; |
|
|
747 | } |
|
|
748 | |
|
|
749 | # hmm, this does not work for topleft of $self... but we should not aks for that |
|
|
750 | sub _topleft { |
|
|
751 | my ($self, $x, $y) = @_; |
|
|
752 | |
|
|
753 | $self->SUPER::_topleft ($x - $self->{view_x}, $y - $self->{view_y}) |
|
|
754 | } |
|
|
755 | |
|
|
756 | sub find_widget { |
|
|
757 | my ($self, $x, $y) = @_; |
|
|
758 | |
|
|
759 | if ( $x >= $self->{x} && $x < $self->{x} + $self->{w} |
|
|
760 | && $y >= $self->{y} && $y < $self->{y} + $self->{h} |
|
|
761 | ) { |
|
|
762 | $self->child->find_widget ($x + $self->{view_x}, $y + $self->{view_y}) |
|
|
763 | } else { |
|
|
764 | $self->CFClient::UI::Base::find_widget ($x, $y) |
|
|
765 | } |
742 | } |
766 | } |
743 | |
767 | |
744 | sub _render { |
768 | sub _render { |
745 | my ($self) = @_; |
769 | my ($self) = @_; |
746 | |
770 | |
… | |
… | |
1303 | |
1327 | |
1304 | delete $self->{texture}; |
1328 | delete $self->{texture}; |
1305 | $self->SUPER::update; |
1329 | $self->SUPER::update; |
1306 | } |
1330 | } |
1307 | |
1331 | |
1308 | sub reconfigure { |
|
|
1309 | my ($self) = @_; |
|
|
1310 | |
|
|
1311 | delete $self->{texture}; |
|
|
1312 | } |
|
|
1313 | |
|
|
1314 | sub set_text { |
1332 | sub set_text { |
1315 | my ($self, $text) = @_; |
1333 | my ($self, $text) = @_; |
1316 | |
1334 | |
1317 | return if $self->{text} eq "T$text"; |
1335 | return if $self->{text} eq "T$text"; |
1318 | $self->{text} = "T$text"; |
1336 | $self->{text} = "T$text"; |
… | |
… | |
1371 | sub set_fontsize { |
1389 | sub set_fontsize { |
1372 | my ($self, $fontsize) = @_; |
1390 | my ($self, $fontsize) = @_; |
1373 | |
1391 | |
1374 | $self->{fontsize} = $fontsize; |
1392 | $self->{fontsize} = $fontsize; |
1375 | delete $self->{texture}; |
1393 | delete $self->{texture}; |
|
|
1394 | |
|
|
1395 | $self->update; |
1376 | $self->check_size; |
1396 | $self->check_size; |
1377 | $self->update; |
|
|
1378 | } |
1397 | } |
1379 | |
1398 | |
1380 | sub _draw { |
1399 | sub _draw { |
1381 | my ($self) = @_; |
1400 | my ($self) = @_; |
1382 | |
1401 | |
… | |
… | |
2464 | } |
2483 | } |
2465 | |
2484 | |
2466 | sub _draw { |
2485 | sub _draw { |
2467 | my ($self) = @_; |
2486 | my ($self) = @_; |
2468 | |
2487 | |
|
|
2488 | return unless $::CONN;#d# manage and cache textures differently |
2469 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2489 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2470 | |
2490 | |
2471 | # TODO animation |
2491 | # TODO animation |
2472 | if ($tex) { |
2492 | if ($tex) { |
2473 | glEnable GL_BLEND; |
2493 | glEnable GL_BLEND; |
… | |
… | |
2477 | glColor 1, 1, 1, 1; |
2497 | glColor 1, 1, 1, 1; |
2478 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2498 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2479 | glDisable GL_TEXTURE_2D; |
2499 | glDisable GL_TEXTURE_2D; |
2480 | glDisable GL_BLEND; |
2500 | glDisable GL_BLEND; |
2481 | } |
2501 | } |
2482 | } |
|
|
2483 | |
|
|
2484 | ############################################################################# |
|
|
2485 | |
|
|
2486 | package CFClient::UI::Menu; |
|
|
2487 | |
|
|
2488 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2489 | |
|
|
2490 | use CFClient::OpenGL; |
|
|
2491 | |
|
|
2492 | sub new { |
|
|
2493 | my $class = shift; |
|
|
2494 | |
|
|
2495 | my $self = $class->SUPER::new ( |
|
|
2496 | items => [], |
|
|
2497 | z => 100, |
|
|
2498 | @_, |
|
|
2499 | ); |
|
|
2500 | |
|
|
2501 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2502 | |
|
|
2503 | for my $item (@{ $self->{items} }) { |
|
|
2504 | my ($widget, $cb) = @$item; |
|
|
2505 | |
|
|
2506 | # handle various types of items, only text for now |
|
|
2507 | if (!ref $widget) { |
|
|
2508 | $widget = new CFClient::UI::Label |
|
|
2509 | can_hover => 1, |
|
|
2510 | can_events => 1, |
|
|
2511 | text => $widget; |
|
|
2512 | } |
|
|
2513 | |
|
|
2514 | $self->{item}{$widget} = $item; |
|
|
2515 | |
|
|
2516 | $self->{vbox}->add ($widget); |
|
|
2517 | } |
|
|
2518 | |
|
|
2519 | $self |
|
|
2520 | } |
|
|
2521 | |
|
|
2522 | # popup given the event (must be a mouse button down event currently) |
|
|
2523 | sub popup { |
|
|
2524 | my ($self, $ev) = @_; |
|
|
2525 | |
|
|
2526 | $self->emit ("popdown"); |
|
|
2527 | |
|
|
2528 | # maybe save $GRAB? must be careful about events... |
|
|
2529 | $GRAB = $self; |
|
|
2530 | $self->{button} = $ev->{button}; |
|
|
2531 | |
|
|
2532 | $self->show; |
|
|
2533 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2534 | } |
|
|
2535 | |
|
|
2536 | sub mouse_motion { |
|
|
2537 | my ($self, $ev, $x, $y) = @_; |
|
|
2538 | |
|
|
2539 | # TODO: should use vbox->find_widget or so |
|
|
2540 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2541 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2542 | } |
|
|
2543 | |
|
|
2544 | sub button_up { |
|
|
2545 | my ($self, $ev, $x, $y) = @_; |
|
|
2546 | |
|
|
2547 | if ($ev->{button} == $self->{button}) { |
|
|
2548 | undef $GRAB; |
|
|
2549 | $self->hide; |
|
|
2550 | |
|
|
2551 | $self->emit ("popdown"); |
|
|
2552 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2553 | } |
|
|
2554 | } |
|
|
2555 | |
|
|
2556 | ############################################################################# |
|
|
2557 | |
|
|
2558 | package CFClient::UI::Root; |
|
|
2559 | |
|
|
2560 | our @ISA = CFClient::UI::Container::; |
|
|
2561 | |
|
|
2562 | use CFClient::OpenGL; |
|
|
2563 | |
|
|
2564 | sub check_size { |
|
|
2565 | my ($self) = @_; |
|
|
2566 | |
|
|
2567 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2568 | } |
|
|
2569 | |
|
|
2570 | sub size_request { |
|
|
2571 | ($::WIDTH, $::HEIGHT) |
|
|
2572 | } |
|
|
2573 | |
|
|
2574 | sub configure { |
|
|
2575 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2576 | |
|
|
2577 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2578 | |
|
|
2579 | for my $child (@{$self->{children}}) { |
|
|
2580 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2581 | |
|
|
2582 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2583 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2584 | $child->configure ($X, $Y, $W,$H); |
|
|
2585 | } |
|
|
2586 | } |
|
|
2587 | |
|
|
2588 | sub _topleft { |
|
|
2589 | my ($self, $x, $y) = @_; |
|
|
2590 | |
|
|
2591 | ($x, $y) |
|
|
2592 | } |
|
|
2593 | |
|
|
2594 | sub update { |
|
|
2595 | my ($self) = @_; |
|
|
2596 | |
|
|
2597 | $self->check_size; |
|
|
2598 | ::refresh (); |
|
|
2599 | } |
|
|
2600 | |
|
|
2601 | sub add { |
|
|
2602 | my ($self, $child) = @_; |
|
|
2603 | |
|
|
2604 | # integerize window positions |
|
|
2605 | $child->{x} = int $child->{x}; |
|
|
2606 | $child->{y} = int $child->{y}; |
|
|
2607 | |
|
|
2608 | $self->SUPER::add ($child); |
|
|
2609 | } |
|
|
2610 | |
|
|
2611 | sub on_refresh { |
|
|
2612 | my ($self, $id, $cb) = @_; |
|
|
2613 | |
|
|
2614 | $self->{refresh_hook}{$id} = $cb; |
|
|
2615 | } |
|
|
2616 | |
|
|
2617 | sub draw { |
|
|
2618 | my ($self) = @_; |
|
|
2619 | |
|
|
2620 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2621 | $_->() for values %$rcb; |
|
|
2622 | } |
|
|
2623 | |
|
|
2624 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2625 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2626 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2627 | |
|
|
2628 | glMatrixMode GL_PROJECTION; |
|
|
2629 | glLoadIdentity; |
|
|
2630 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2631 | glMatrixMode GL_MODELVIEW; |
|
|
2632 | glLoadIdentity; |
|
|
2633 | |
|
|
2634 | $self->_draw; |
|
|
2635 | } |
2502 | } |
2636 | |
2503 | |
2637 | ############################################################################# |
2504 | ############################################################################# |
2638 | |
2505 | |
2639 | package CFClient::UI::InventoryItem; |
2506 | package CFClient::UI::InventoryItem; |
… | |
… | |
2739 | ($self->{req_w}, $self->{req_h}); |
2606 | ($self->{req_w}, $self->{req_h}); |
2740 | } |
2607 | } |
2741 | |
2608 | |
2742 | ############################################################################# |
2609 | ############################################################################# |
2743 | |
2610 | |
|
|
2611 | package CFClient::UI::Menu; |
|
|
2612 | |
|
|
2613 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2614 | |
|
|
2615 | use CFClient::OpenGL; |
|
|
2616 | |
|
|
2617 | sub new { |
|
|
2618 | my $class = shift; |
|
|
2619 | |
|
|
2620 | my $self = $class->SUPER::new ( |
|
|
2621 | items => [], |
|
|
2622 | z => 100, |
|
|
2623 | @_, |
|
|
2624 | ); |
|
|
2625 | |
|
|
2626 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2627 | |
|
|
2628 | for my $item (@{ $self->{items} }) { |
|
|
2629 | my ($widget, $cb) = @$item; |
|
|
2630 | |
|
|
2631 | # handle various types of items, only text for now |
|
|
2632 | if (!ref $widget) { |
|
|
2633 | $widget = new CFClient::UI::Label |
|
|
2634 | can_hover => 1, |
|
|
2635 | can_events => 1, |
|
|
2636 | text => $widget; |
|
|
2637 | } |
|
|
2638 | |
|
|
2639 | $self->{item}{$widget} = $item; |
|
|
2640 | |
|
|
2641 | $self->{vbox}->add ($widget); |
|
|
2642 | } |
|
|
2643 | |
|
|
2644 | $self |
|
|
2645 | } |
|
|
2646 | |
|
|
2647 | # popup given the event (must be a mouse button down event currently) |
|
|
2648 | sub popup { |
|
|
2649 | my ($self, $ev) = @_; |
|
|
2650 | |
|
|
2651 | $self->emit ("popdown"); |
|
|
2652 | |
|
|
2653 | # maybe save $GRAB? must be careful about events... |
|
|
2654 | $GRAB = $self; |
|
|
2655 | $self->{button} = $ev->{button}; |
|
|
2656 | |
|
|
2657 | $self->show; |
|
|
2658 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2659 | } |
|
|
2660 | |
|
|
2661 | sub mouse_motion { |
|
|
2662 | my ($self, $ev, $x, $y) = @_; |
|
|
2663 | |
|
|
2664 | # TODO: should use vbox->find_widget or so |
|
|
2665 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2666 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2667 | } |
|
|
2668 | |
|
|
2669 | sub button_up { |
|
|
2670 | my ($self, $ev, $x, $y) = @_; |
|
|
2671 | |
|
|
2672 | if ($ev->{button} == $self->{button}) { |
|
|
2673 | undef $GRAB; |
|
|
2674 | $self->hide; |
|
|
2675 | |
|
|
2676 | $self->emit ("popdown"); |
|
|
2677 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2678 | } |
|
|
2679 | } |
|
|
2680 | |
|
|
2681 | ############################################################################# |
|
|
2682 | |
|
|
2683 | package CFClient::UI::Root; |
|
|
2684 | |
|
|
2685 | our @ISA = CFClient::UI::Container::; |
|
|
2686 | |
|
|
2687 | use CFClient::OpenGL; |
|
|
2688 | |
|
|
2689 | sub check_size { |
|
|
2690 | my ($self) = @_; |
|
|
2691 | |
|
|
2692 | $self->configure (0, 0, $self->{w}, $self->{h}); |
|
|
2693 | } |
|
|
2694 | |
|
|
2695 | sub size_request { |
|
|
2696 | my ($self) = @_; |
|
|
2697 | |
|
|
2698 | ($self->{w}, $self->{h}) |
|
|
2699 | } |
|
|
2700 | |
|
|
2701 | sub size_allocate { |
|
|
2702 | my ($self, $w, $h) = @_; |
|
|
2703 | |
|
|
2704 | my $old_w = $self->{old_w}; |
|
|
2705 | my $old_h = $self->{old_h}; |
|
|
2706 | |
|
|
2707 | if ($old_w && $old_h) { |
|
|
2708 | for my $child ($self->children) { |
|
|
2709 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2710 | $child->{w} = int 0.5 + $child->{w} * $w / $old_w; |
|
|
2711 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2712 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2713 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2714 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2715 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2716 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2717 | } |
|
|
2718 | } |
|
|
2719 | |
|
|
2720 | $self->{old_w} = $w; |
|
|
2721 | $self->{old_h} = $h; |
|
|
2722 | } |
|
|
2723 | |
|
|
2724 | sub configure { |
|
|
2725 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2726 | |
|
|
2727 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2728 | |
|
|
2729 | for my $child ($self->children) { |
|
|
2730 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2731 | |
|
|
2732 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2733 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2734 | $child->configure ($X, $Y, $W, $H); |
|
|
2735 | } |
|
|
2736 | } |
|
|
2737 | |
|
|
2738 | sub _topleft { |
|
|
2739 | my ($self, $x, $y) = @_; |
|
|
2740 | |
|
|
2741 | ($x, $y) |
|
|
2742 | } |
|
|
2743 | |
|
|
2744 | sub update { |
|
|
2745 | my ($self) = @_; |
|
|
2746 | |
|
|
2747 | $self->check_size; |
|
|
2748 | ::refresh (); |
|
|
2749 | } |
|
|
2750 | |
|
|
2751 | sub add { |
|
|
2752 | my ($self, $child) = @_; |
|
|
2753 | |
|
|
2754 | # integerize window positions |
|
|
2755 | $child->{x} = int $child->{x}; |
|
|
2756 | $child->{y} = int $child->{y}; |
|
|
2757 | |
|
|
2758 | $self->SUPER::add ($child); |
|
|
2759 | } |
|
|
2760 | |
|
|
2761 | sub on_refresh { |
|
|
2762 | my ($self, $id, $cb) = @_; |
|
|
2763 | |
|
|
2764 | $self->{refresh_hook}{$id} = $cb; |
|
|
2765 | } |
|
|
2766 | |
|
|
2767 | sub draw { |
|
|
2768 | my ($self) = @_; |
|
|
2769 | |
|
|
2770 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2771 | $_->() for values %$rcb; |
|
|
2772 | } |
|
|
2773 | |
|
|
2774 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2775 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2776 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2777 | |
|
|
2778 | glMatrixMode GL_PROJECTION; |
|
|
2779 | glLoadIdentity; |
|
|
2780 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2781 | glMatrixMode GL_MODELVIEW; |
|
|
2782 | glLoadIdentity; |
|
|
2783 | |
|
|
2784 | $self->_draw; |
|
|
2785 | } |
|
|
2786 | |
|
|
2787 | ############################################################################# |
|
|
2788 | |
2744 | package CFClient::UI; |
2789 | package CFClient::UI; |
2745 | |
2790 | |
2746 | $ROOT = new CFClient::UI::Root; |
2791 | $ROOT = new CFClient::UI::Root; |
2747 | $TOOLTIP = new CFClient::UI::Tooltip; |
2792 | $TOOLTIP = new CFClient::UI::Tooltip; |
2748 | |
2793 | |