… | |
… | |
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 | |
… | |
… | |
1322 | |
1324 | |
1323 | delete $self->{texture}; |
1325 | delete $self->{texture}; |
1324 | $self->SUPER::update; |
1326 | $self->SUPER::update; |
1325 | } |
1327 | } |
1326 | |
1328 | |
1327 | sub reconfigure { |
|
|
1328 | my ($self) = @_; |
|
|
1329 | |
|
|
1330 | delete $self->{texture}; |
|
|
1331 | } |
|
|
1332 | |
|
|
1333 | sub set_text { |
1329 | sub set_text { |
1334 | my ($self, $text) = @_; |
1330 | my ($self, $text) = @_; |
1335 | |
1331 | |
1336 | return if $self->{text} eq "T$text"; |
1332 | return if $self->{text} eq "T$text"; |
1337 | $self->{text} = "T$text"; |
1333 | $self->{text} = "T$text"; |
… | |
… | |
1390 | sub set_fontsize { |
1386 | sub set_fontsize { |
1391 | my ($self, $fontsize) = @_; |
1387 | my ($self, $fontsize) = @_; |
1392 | |
1388 | |
1393 | $self->{fontsize} = $fontsize; |
1389 | $self->{fontsize} = $fontsize; |
1394 | delete $self->{texture}; |
1390 | delete $self->{texture}; |
|
|
1391 | |
|
|
1392 | $self->update; |
1395 | $self->check_size; |
1393 | $self->check_size; |
1396 | $self->update; |
|
|
1397 | } |
1394 | } |
1398 | |
1395 | |
1399 | sub _draw { |
1396 | sub _draw { |
1400 | my ($self) = @_; |
1397 | my ($self) = @_; |
1401 | |
1398 | |
… | |
… | |
2483 | } |
2480 | } |
2484 | |
2481 | |
2485 | sub _draw { |
2482 | sub _draw { |
2486 | my ($self) = @_; |
2483 | my ($self) = @_; |
2487 | |
2484 | |
|
|
2485 | return unless $::CONN;#d# manage and cache textures differently |
2488 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2486 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$self->{face}]]; |
2489 | |
2487 | |
2490 | # TODO animation |
2488 | # TODO animation |
2491 | if ($tex) { |
2489 | if ($tex) { |
2492 | glEnable GL_BLEND; |
2490 | glEnable GL_BLEND; |
… | |
… | |
2496 | glColor 1, 1, 1, 1; |
2494 | glColor 1, 1, 1, 1; |
2497 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2495 | $tex->draw_quad (0, 0, $self->{w}, $self->{h}); |
2498 | glDisable GL_TEXTURE_2D; |
2496 | glDisable GL_TEXTURE_2D; |
2499 | glDisable GL_BLEND; |
2497 | glDisable GL_BLEND; |
2500 | } |
2498 | } |
2501 | } |
|
|
2502 | |
|
|
2503 | ############################################################################# |
|
|
2504 | |
|
|
2505 | package CFClient::UI::Menu; |
|
|
2506 | |
|
|
2507 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2508 | |
|
|
2509 | use CFClient::OpenGL; |
|
|
2510 | |
|
|
2511 | sub 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) |
|
|
2542 | sub 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 | |
|
|
2555 | sub 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 | |
|
|
2563 | sub 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 | |
|
|
2577 | package CFClient::UI::Root; |
|
|
2578 | |
|
|
2579 | our @ISA = CFClient::UI::Container::; |
|
|
2580 | |
|
|
2581 | use CFClient::OpenGL; |
|
|
2582 | |
|
|
2583 | sub check_size { |
|
|
2584 | my ($self) = @_; |
|
|
2585 | |
|
|
2586 | $self->configure (0, 0, $::WIDTH, $::HEIGHT); |
|
|
2587 | } |
|
|
2588 | |
|
|
2589 | sub size_request { |
|
|
2590 | ($::WIDTH, $::HEIGHT) |
|
|
2591 | } |
|
|
2592 | |
|
|
2593 | sub 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 | |
|
|
2607 | sub _topleft { |
|
|
2608 | my ($self, $x, $y) = @_; |
|
|
2609 | |
|
|
2610 | ($x, $y) |
|
|
2611 | } |
|
|
2612 | |
|
|
2613 | sub update { |
|
|
2614 | my ($self) = @_; |
|
|
2615 | |
|
|
2616 | $self->check_size; |
|
|
2617 | ::refresh (); |
|
|
2618 | } |
|
|
2619 | |
|
|
2620 | sub 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 | |
|
|
2630 | sub on_refresh { |
|
|
2631 | my ($self, $id, $cb) = @_; |
|
|
2632 | |
|
|
2633 | $self->{refresh_hook}{$id} = $cb; |
|
|
2634 | } |
|
|
2635 | |
|
|
2636 | sub 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 | } |
2499 | } |
2655 | |
2500 | |
2656 | ############################################################################# |
2501 | ############################################################################# |
2657 | |
2502 | |
2658 | package CFClient::UI::InventoryItem; |
2503 | package CFClient::UI::InventoryItem; |
… | |
… | |
2758 | ($self->{req_w}, $self->{req_h}); |
2603 | ($self->{req_w}, $self->{req_h}); |
2759 | } |
2604 | } |
2760 | |
2605 | |
2761 | ############################################################################# |
2606 | ############################################################################# |
2762 | |
2607 | |
|
|
2608 | package CFClient::UI::Menu; |
|
|
2609 | |
|
|
2610 | our @ISA = CFClient::UI::FancyFrame::; |
|
|
2611 | |
|
|
2612 | use CFClient::OpenGL; |
|
|
2613 | |
|
|
2614 | sub new { |
|
|
2615 | my $class = shift; |
|
|
2616 | |
|
|
2617 | my $self = $class->SUPER::new ( |
|
|
2618 | items => [], |
|
|
2619 | z => 100, |
|
|
2620 | @_, |
|
|
2621 | ); |
|
|
2622 | |
|
|
2623 | $self->add ($self->{vbox} = new CFClient::UI::VBox); |
|
|
2624 | |
|
|
2625 | for my $item (@{ $self->{items} }) { |
|
|
2626 | my ($widget, $cb) = @$item; |
|
|
2627 | |
|
|
2628 | # handle various types of items, only text for now |
|
|
2629 | if (!ref $widget) { |
|
|
2630 | $widget = new CFClient::UI::Label |
|
|
2631 | can_hover => 1, |
|
|
2632 | can_events => 1, |
|
|
2633 | text => $widget; |
|
|
2634 | } |
|
|
2635 | |
|
|
2636 | $self->{item}{$widget} = $item; |
|
|
2637 | |
|
|
2638 | $self->{vbox}->add ($widget); |
|
|
2639 | } |
|
|
2640 | |
|
|
2641 | $self |
|
|
2642 | } |
|
|
2643 | |
|
|
2644 | # popup given the event (must be a mouse button down event currently) |
|
|
2645 | sub popup { |
|
|
2646 | my ($self, $ev) = @_; |
|
|
2647 | |
|
|
2648 | $self->emit ("popdown"); |
|
|
2649 | |
|
|
2650 | # maybe save $GRAB? must be careful about events... |
|
|
2651 | $GRAB = $self; |
|
|
2652 | $self->{button} = $ev->{button}; |
|
|
2653 | |
|
|
2654 | $self->show; |
|
|
2655 | $self->move ($ev->{x} - $self->{w} * 0.5, $ev->{y} - $self->{h} * 0.5); |
|
|
2656 | } |
|
|
2657 | |
|
|
2658 | sub mouse_motion { |
|
|
2659 | my ($self, $ev, $x, $y) = @_; |
|
|
2660 | |
|
|
2661 | # TODO: should use vbox->find_widget or so |
|
|
2662 | $HOVER = $ROOT->find_widget ($ev->{x}, $ev->{y}); |
|
|
2663 | $self->{hover} = $self->{item}{$HOVER}; |
|
|
2664 | } |
|
|
2665 | |
|
|
2666 | sub button_up { |
|
|
2667 | my ($self, $ev, $x, $y) = @_; |
|
|
2668 | |
|
|
2669 | if ($ev->{button} == $self->{button}) { |
|
|
2670 | undef $GRAB; |
|
|
2671 | $self->hide; |
|
|
2672 | |
|
|
2673 | $self->emit ("popdown"); |
|
|
2674 | $self->{hover}[1]->() if $self->{hover}; |
|
|
2675 | } |
|
|
2676 | } |
|
|
2677 | |
|
|
2678 | ############################################################################# |
|
|
2679 | |
|
|
2680 | package CFClient::UI::Root; |
|
|
2681 | |
|
|
2682 | our @ISA = CFClient::UI::Container::; |
|
|
2683 | |
|
|
2684 | use CFClient::OpenGL; |
|
|
2685 | |
|
|
2686 | sub check_size { |
|
|
2687 | my ($self) = @_; |
|
|
2688 | |
|
|
2689 | $self->configure (0, 0, $self->{w}, $self->{h}); |
|
|
2690 | } |
|
|
2691 | |
|
|
2692 | sub size_request { |
|
|
2693 | my ($self) = @_; |
|
|
2694 | |
|
|
2695 | ($self->{w}, $self->{h}) |
|
|
2696 | } |
|
|
2697 | |
|
|
2698 | sub size_allocate { |
|
|
2699 | my ($self, $w, $h) = @_; |
|
|
2700 | |
|
|
2701 | my $old_w = $self->{old_w}; |
|
|
2702 | my $old_h = $self->{old_h}; |
|
|
2703 | |
|
|
2704 | if ($old_w && $old_h) { |
|
|
2705 | for my $child ($self->children) { |
|
|
2706 | $child->{x} = int 0.5 + $child->{x} * $w / $old_w; |
|
|
2707 | $child->{w} = int 0.5 + $child->{req_w} * $w / $old_w; |
|
|
2708 | $child->{req_w} = int 0.5 + $child->{req_w} * $w / $old_w if exists $child->{req_w}; |
|
|
2709 | $child->{user_w} = int 0.5 + $child->{user_w} * $w / $old_w if exists $child->{user_w}; |
|
|
2710 | $child->{y} = int 0.5 + $child->{y} * $h / $old_h; |
|
|
2711 | $child->{h} = int 0.5 + $child->{h} * $h / $old_h; |
|
|
2712 | $child->{req_h} = int 0.5 + $child->{req_h} * $h / $old_h if exists $child->{req_h}; |
|
|
2713 | $child->{user_h} = int 0.5 + $child->{user_h} * $h / $old_h if exists $child->{user_h}; |
|
|
2714 | } |
|
|
2715 | } |
|
|
2716 | |
|
|
2717 | $self->{old_w} = $w; |
|
|
2718 | $self->{old_h} = $h; |
|
|
2719 | } |
|
|
2720 | |
|
|
2721 | sub configure { |
|
|
2722 | my ($self, $x, $y, $w, $h) = @_; |
|
|
2723 | |
|
|
2724 | $self->SUPER::configure ($x, $y, $w, $h); |
|
|
2725 | |
|
|
2726 | for my $child ($self->children) { |
|
|
2727 | my ($X, $Y, $W, $H) = @$child{qw(x y req_w req_h)}; |
|
|
2728 | |
|
|
2729 | $X = List::Util::max 0, List::Util::min $w - $W, $X; |
|
|
2730 | $Y = List::Util::max 0, List::Util::min $h - $H, $Y; |
|
|
2731 | $child->configure ($X, $Y, $W, $H); |
|
|
2732 | } |
|
|
2733 | } |
|
|
2734 | |
|
|
2735 | sub _topleft { |
|
|
2736 | my ($self, $x, $y) = @_; |
|
|
2737 | |
|
|
2738 | ($x, $y) |
|
|
2739 | } |
|
|
2740 | |
|
|
2741 | sub update { |
|
|
2742 | my ($self) = @_; |
|
|
2743 | |
|
|
2744 | $self->check_size; |
|
|
2745 | ::refresh (); |
|
|
2746 | } |
|
|
2747 | |
|
|
2748 | sub add { |
|
|
2749 | my ($self, $child) = @_; |
|
|
2750 | |
|
|
2751 | # integerize window positions |
|
|
2752 | $child->{x} = int $child->{x}; |
|
|
2753 | $child->{y} = int $child->{y}; |
|
|
2754 | |
|
|
2755 | $self->SUPER::add ($child); |
|
|
2756 | } |
|
|
2757 | |
|
|
2758 | sub on_refresh { |
|
|
2759 | my ($self, $id, $cb) = @_; |
|
|
2760 | |
|
|
2761 | $self->{refresh_hook}{$id} = $cb; |
|
|
2762 | } |
|
|
2763 | |
|
|
2764 | sub draw { |
|
|
2765 | my ($self) = @_; |
|
|
2766 | |
|
|
2767 | while (my $rcb = delete $self->{refresh_hook}) { |
|
|
2768 | $_->() for values %$rcb; |
|
|
2769 | } |
|
|
2770 | |
|
|
2771 | glViewport 0, 0, $::WIDTH, $::HEIGHT; |
|
|
2772 | glClearColor +($::CFG->{fow_intensity}) x 3, 1; |
|
|
2773 | glClear GL_COLOR_BUFFER_BIT; |
|
|
2774 | |
|
|
2775 | glMatrixMode GL_PROJECTION; |
|
|
2776 | glLoadIdentity; |
|
|
2777 | glOrtho 0, $::WIDTH, $::HEIGHT, 0, -10000 , 10000; |
|
|
2778 | glMatrixMode GL_MODELVIEW; |
|
|
2779 | glLoadIdentity; |
|
|
2780 | |
|
|
2781 | $self->_draw; |
|
|
2782 | } |
|
|
2783 | |
|
|
2784 | ############################################################################# |
|
|
2785 | |
2763 | package CFClient::UI; |
2786 | package CFClient::UI; |
2764 | |
2787 | |
2765 | $ROOT = new CFClient::UI::Root; |
2788 | $ROOT = new CFClient::UI::Root; |
2766 | $TOOLTIP = new CFClient::UI::Tooltip; |
2789 | $TOOLTIP = new CFClient::UI::Tooltip; |
2767 | |
2790 | |