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.310 by root, Fri Jun 23 20:28:20 2006 UTC vs.
Revision 1.316 by root, Sun Jul 2 13:57:58 2006 UTC

335sub size_request { 335sub size_request {
336 require Carp; 336 require Carp;
337 Carp::confess "size_request is abstract"; 337 Carp::confess "size_request is abstract";
338} 338}
339 339
340sub baseline_shift {
341 0
342}
343
340sub configure { 344sub configure {
341 my ($self, $x, $y, $w, $h) = @_; 345 my ($self, $x, $y, $w, $h) = @_;
342 346
343 if ($self->{aspect}) { 347 if ($self->{aspect}) {
344 my ($ow, $oh) = ($w, $h); 348 my ($ow, $oh) = ($w, $h);
535 return unless $self->{h} && $self->{w}; 539 return unless $self->{h} && $self->{w};
536 540
537 # update screen rectangle 541 # update screen rectangle
538 local $draw_x = $draw_x + $self->{x}; 542 local $draw_x = $draw_x + $self->{x};
539 local $draw_y = $draw_y + $self->{y}; 543 local $draw_y = $draw_y + $self->{y};
540 local $draw_w = $draw_x + $self->{w};
541 local $draw_h = $draw_y + $self->{h};
542 544
543 # skip widgets that are entirely outside the drawing area 545 # skip widgets that are entirely outside the drawing area
544 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w) 546 return if ($draw_x + $self->{w} < 0) || ($draw_x >= $draw_w)
545 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h); 547 || ($draw_y + $self->{h} < 0) || ($draw_y >= $draw_h);
546 548
984 $self->add ($child) if $child; 986 $self->add ($child) if $child;
985 987
986 $self 988 $self
987} 989}
988 990
991#TODO# update range on size_allocate depending on child
992
989sub add { 993sub add {
990 my ($self, $widget) = @_; 994 my ($self, $widget) = @_;
991 995
992 $self->{vp}->add ($self->{child} = $widget); 996 $self->{vp}->add ($self->{child} = $widget);
993} 997}
994 998
999sub update_slider {
1000 my ($self) = @_;
1001
1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]);
1003}
1004
995sub update { 1005sub update {
996 my ($self) = @_; 1006 my ($self) = @_;
997 1007
998 $self->SUPER::update; 1008 $self->SUPER::update;
999 1009
1000 # todo: overwrite size_allocate of child 1010 $self->update_slider;
1001 my $child = $self->{vp}->child;
1002 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1003} 1011}
1004 1012
1005sub invoke_size_allocate { 1013sub invoke_size_allocate {
1006 my ($self, $w, $h) = @_; 1014 my ($self, $w, $h) = @_;
1007 1015
1008 my $child = $self->{vp}->child; 1016 $self->update_slider;
1009 $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $child->{h}, $self->{vp}{h}, 1]);
1010 1017
1011 $self->SUPER::invoke_size_allocate ($w, $h) 1018 $self->SUPER::invoke_size_allocate ($w, $h)
1012} 1019}
1013
1014#TODO# update range on size_allocate depending on child
1015# update viewport offset on scroll
1016 1020
1017############################################################################# 1021#############################################################################
1018 1022
1019package CFClient::UI::Frame; 1023package CFClient::UI::Frame;
1020 1024
1631 }; 1635 };
1632 1636
1633 @{ $self->{size_req} } 1637 @{ $self->{size_req} }
1634} 1638}
1635 1639
1640sub baseline_shift {
1641 $_[0]{layout}->descent
1642}
1643
1636sub invoke_size_allocate { 1644sub invoke_size_allocate {
1637 my ($self, $w, $h) = @_; 1645 my ($self, $w, $h) = @_;
1638 1646
1639 delete $self->{ox}; 1647 delete $self->{ox};
1640 1648
1646 1654
1647sub set_fontsize { 1655sub set_fontsize {
1648 my ($self, $fontsize) = @_; 1656 my ($self, $fontsize) = @_;
1649 1657
1650 $self->{fontsize} = $fontsize; 1658 $self->{fontsize} = $fontsize;
1659 delete $self->{size_req};
1651 delete $self->{texture}; 1660 delete $self->{texture};
1652 1661
1653 $self->realloc; 1662 $self->realloc;
1654} 1663}
1655 1664
2567 2576
2568 $self->{fontsize} = $fontsize; 2577 $self->{fontsize} = $fontsize;
2569 $self->reflow; 2578 $self->reflow;
2570} 2579}
2571 2580
2572sub visible_children { 2581sub size_request {
2573 my ($self) = @_; 2582 my ($self) = @_;
2574 2583
2575 @{$self->{children}}[0,1] 2584 my ($empty, $slider) = @{ $self->{children} };
2585
2586 local $self->{children} = [$empty, $slider];
2587 $self->SUPER::size_request
2576} 2588}
2577 2589
2578sub invoke_size_allocate { 2590sub invoke_size_allocate {
2579 my ($self, $w, $h) = @_; 2591 my ($self, $w, $h) = @_;
2580 2592
2581 my (undef, undef, @other) = @{ $self->{children} }; 2593 my ($empty, $slider, @other) = @{ $self->{children} };
2582 $_->configure (@$_{qw(x y req_w req_h)}) for @other; 2594 $_->configure (@$_{qw(x y req_w req_h)}) for @other;
2583 2595
2584 $self->{layout}->set_font ($self->{font}) if $self->{font}; 2596 $self->{layout}->set_font ($self->{font}) if $self->{font};
2585 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); 2597 $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE);
2586 $self->{layout}->set_width ($self->{children}[0]{w}); 2598 $self->{layout}->set_width ($empty->{w});
2587 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2599 $self->{layout}->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2588 2600
2589 $self->reflow; 2601 $self->reflow;
2590 2602
2603 local $self->{children} = [$empty, $slider];
2591 $self->SUPER::invoke_size_allocate ($w, $h) 2604 $self->SUPER::invoke_size_allocate ($w, $h)
2592} 2605}
2593 2606
2594sub get_layout { 2607sub get_layout {
2595 my ($self, $para) = @_; 2608 my ($self, $para) = @_;
2596 2609
2597 my $layout = $self->{layout}; 2610 my $layout = $self->{layout};
2598 2611
2599 $layout->set_font ($self->{font}) if $self->{font}; 2612 $layout->set_font ($self->{font}) if $self->{font};
2613 $layout->set_foreground (@{$para->{fg}});
2600 $layout->set_height ($self->{fontsize} * $::FONTSIZE); 2614 $layout->set_height ($self->{fontsize} * $::FONTSIZE);
2601 $layout->set_width ($self->{children}[0]{w} - $para->{indent}); 2615 $layout->set_width ($self->{children}[0]{w} - $para->{indent});
2602 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent}); 2616 $layout->set_indent ($self->{fontsize} * $::FONTSIZE * $self->{indent});
2603 $layout->set_markup ($para->{markup}); 2617 $layout->set_markup ($para->{markup});
2604 $layout->set_shapes (map +($_->{req_w}, $_->{req_h}), @{$para->{widget}}); 2618
2619 $layout->set_shapes (
2620 map
2621 +(0, $_->baseline_shift +$_->{padding_y} - $_->{h}, $_->{w}, $_->{h}),
2622 @{$para->{widget}}
2623 );
2605 2624
2606 $layout 2625 $layout
2607} 2626}
2608 2627
2609sub reflow { 2628sub reflow {
2677 for my $para (@{$self->{par}}) { 2696 for my $para (@{$self->{par}}) {
2678 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) { 2697 if ($para->{w} != $W && ($para->{wrapped} || $para->{w} > $W)) {
2679 my $layout = $self->get_layout ($para); 2698 my $layout = $self->get_layout ($para);
2680 my ($w, $h) = $layout->size; 2699 my ($w, $h) = $layout->size;
2681 2700
2682 $para->{w} = $w + $para->{indent}; 2701 $para->{w} = $w + $para->{indent};
2683 $para->{h} = $h; 2702 $para->{h} = $h;
2684 $para->{wrapped} = $layout->has_wrapped; 2703 $para->{wrapped} = $layout->has_wrapped;
2685 } 2704 }
2686 2705
2687 $height += $para->{h}; 2706 $height += $para->{h};
2688 } 2707 }
2726 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data; 2745 glDrawPixels $w, $h, $format, GL_UNSIGNED_BYTE, $data;
2727 2746
2728 if (my @w = @{ $para->{widget} }) { 2747 if (my @w = @{ $para->{widget} }) {
2729 my @s = $layout->get_shapes; 2748 my @s = $layout->get_shapes;
2730 2749
2750 glDisable GL_BLEND;
2731 for (@w) { 2751 for (@w) {
2732 my ($dx, $dy) = splice @s, 0, 2, (); 2752 my ($dx, $dy) = splice @s, 0, 2, ();
2733 2753
2734 $_->{x} = $dx + $para->{indent}; 2754 $_->{x} = $dx + $para->{indent};
2735 $_->{y} = $dy + $y - $y0; 2755 $_->{y} = $dy + $y - $y0;
2736 2756
2737 $_->draw; 2757 $_->draw;
2738 } 2758 }
2759 glEnable GL_BLEND;
2760 glBlendFunc GL_ONE, GL_ONE_MINUS_SRC_ALPHA;
2739 } 2761 }
2740 } 2762 }
2741 2763
2742 $y += $h; 2764 $y += $h;
2743 } 2765 }
3389 count => 1, 3411 count => 1,
3390 %arg, 3412 %arg,
3391 }; 3413 };
3392 } 3414 }
3393 3415
3416 $ROOT->on_refresh (reorder => sub {
3394 $self->reorder; 3417 $self->reorder;
3418 });
3395} 3419}
3396 3420
3397sub reconfigure { 3421sub reconfigure {
3398 my ($self) = @_; 3422 my ($self) = @_;
3399 3423
3436 return unless $items; 3460 return unless $items;
3437 3461
3438 my @items = sort { 3462 my @items = sort {
3439 ($a->{type} <=> $b->{type}) 3463 ($a->{type} <=> $b->{type})
3440 or ($a->{name} cmp $b->{name}) 3464 or ($a->{name} cmp $b->{name})
3441 } @$items; 3465 } values %$items;
3442 3466
3443 $self->{real_items} = \@items; 3467 $self->{real_items} = \@items;
3444 3468
3445 my $row = 0; 3469 my $row = 0;
3446 for my $item (@items) { 3470 for my $item (@items) {
3449 $self->{child}->add (0, $row, $item->{face_widget}); 3473 $self->{child}->add (0, $row, $item->{face_widget});
3450 $self->{child}->add (1, $row, $item->{desc_widget}); 3474 $self->{child}->add (1, $row, $item->{desc_widget});
3451 $self->{child}->add (2, $row, $item->{weight_widget}); 3475 $self->{child}->add (2, $row, $item->{weight_widget});
3452 3476
3453 $row++; 3477 $row++;
3454 }
3455}
3456
3457#############################################################################
3458
3459package CFClient::UI::BindEditor;
3460
3461our @ISA = CFClient::UI::FancyFrame::;
3462
3463sub new {
3464 my $class = shift;
3465
3466 my $self = $class->SUPER::new (binding => [], commands => [], @_);
3467
3468 $self->add (my $vb = new CFClient::UI::VBox);
3469
3470
3471 $vb->add ($self->{rec_btn} = new CFClient::UI::Button
3472 text => "start recording",
3473 tooltip => "Start/Stops recording of actions."
3474 ."All subsequent actions after the recording started will be captured."
3475 ."The actions are displayed after the record was stopped."
3476 ."To bind the action you have to click on the 'Bind' button",
3477 on_activate => sub {
3478 unless ($self->{recording}) {
3479 $self->start;
3480 } else {
3481 $self->stop;
3482 }
3483 });
3484
3485 $vb->add (new CFClient::UI::Label text => "Actions:");
3486 $vb->add ($self->{cmdbox} = new CFClient::UI::VBox);
3487
3488 $vb->add (new CFClient::UI::Label text => "Bound to: ");
3489 $vb->add (my $hb = new CFClient::UI::HBox);
3490 $hb->add ($self->{keylbl} = new CFClient::UI::Label expand => 1);
3491 $hb->add (new CFClient::UI::Button
3492 text => "bind",
3493 tooltip => "This opens a query where you have to press the key combination to bind the recorded actions",
3494 on_activate => sub {
3495 $self->ask_for_bind;
3496 });
3497
3498 $vb->add (my $hb = new CFClient::UI::HBox);
3499 $hb->add (new CFClient::UI::Button
3500 text => "ok",
3501 expand => 1,
3502 tooltip => "This closes the binding editor and saves the binding",
3503 on_activate => sub {
3504 $self->hide;
3505 $self->commit;
3506 });
3507
3508 $hb->add (new CFClient::UI::Button
3509 text => "cancel",
3510 expand => 1,
3511 tooltip => "This closes the binding editor without saving",
3512 on_activate => sub {
3513 $self->hide;
3514 $self->{binding_cancel}->()
3515 if $self->{binding_cancel};
3516 });
3517
3518 $self->update_binding_widgets;
3519
3520 $self
3521}
3522
3523sub cfg_bind {
3524 my ($self, $mod, $sym, $cmds) = @_;
3525 $::CFG->{profile}{default}{bindings}{$mod}{$sym} = $cmds;
3526 ::update_bindings ();
3527}
3528
3529sub cfg_unbind {
3530 my ($self, $mod, $sym, $cmds) = @_;
3531 delete $::CFG->{profile}{default}{bindings}{$mod}{$sym};
3532 ::update_bindings ();
3533}
3534
3535sub commit {
3536 my ($self) = @_;
3537 my ($mod, $sym, $cmds) = $self->get_binding;
3538 if ($sym != 0 && @$cmds > 0) {
3539 $::STATUSBOX->add ("Bound actions to '".CFClient::Binder::keycombo_to_name ($mod, $sym)
3540 ."'. Don't forget 'Save Config'!");
3541 $self->{binding_change}->($mod, $sym, $cmds)
3542 if $self->{binding_change};
3543 } else {
3544 $::STATUSBOX->add ("No action bound, no key or action specified!");
3545 $self->{binding_cancel}->()
3546 if $self->{binding_cancel};
3547 }
3548}
3549
3550sub start {
3551 my ($self) = @_;
3552
3553 $self->{rec_btn}->set_text ("stop recording");
3554 $self->{recording} = 1;
3555 $self->clear_command_list;
3556 $::CONN->start_record if $::CONN;
3557}
3558
3559sub stop {
3560 my ($self) = @_;
3561
3562 $self->{rec_btn}->set_text ("start recording");
3563 $self->{recording} = 0;
3564
3565 my $rec;
3566 $rec = $::CONN->stop_record if $::CONN;
3567 return unless ref $rec eq 'ARRAY';
3568 $self->set_command_list ($rec);
3569}
3570
3571
3572sub ask_for_bind_and_commit {
3573 my ($self) = @_;
3574 $self->ask_for_bind (1);
3575}
3576
3577sub ask_for_bind {
3578 my ($self, $commit, $end_cb) = @_;
3579
3580 CFClient::Binder::open_binding_dialog (sub {
3581 my ($mod, $sym) = @_;
3582 $self->{binding} = [$mod, $sym]; # XXX: how to stop that memleak?
3583 $self->update_binding_widgets;
3584 $self->commit if $commit;
3585 $end_cb->() if $end_cb;
3586 });
3587}
3588
3589# $mod and $sym are the modifiers and key symbol
3590# $cmds is a array ref of strings (the commands)
3591# $cb is the callback that is executed on OK
3592# $ccb is the callback that is executed on CANCEL and
3593# when the binding was unsuccessful on OK
3594sub set_binding {
3595 my ($self, $mod, $sym, $cmds, $cb, $ccb) = @_;
3596
3597 $self->clear_command_list;
3598 $self->{recording} = 0;
3599 $self->{rec_btn}->set_text ("start recording");
3600
3601 $self->{binding} = [$mod, $sym];
3602 $self->{commands} = $cmds;
3603
3604 $self->{binding_change} = $cb;
3605 $self->{binding_cancel} = $ccb;
3606
3607 $self->update_binding_widgets;
3608}
3609
3610# this is a shortcut method that asks for a binding
3611# and then just binds it.
3612sub do_quick_binding {
3613 my ($self, $cmds, $end_cb) = @_;
3614 $self->set_binding (undef, undef, $cmds, sub { $self->cfg_bind (@_) });
3615 $self->ask_for_bind (1, $end_cb);
3616}
3617
3618sub update_binding_widgets {
3619 my ($self) = @_;
3620 my ($mod, $sym, $cmds) = $self->get_binding;
3621 $self->{keylbl}->set_text (CFClient::Binder::keycombo_to_name ($mod, $sym));
3622 $self->set_command_list ($cmds);
3623}
3624
3625sub get_binding {
3626 my ($self) = @_;
3627 return (
3628 $self->{binding}->[0],
3629 $self->{binding}->[1],
3630 [ grep { defined $_ } @{$self->{commands}} ]
3631 );
3632}
3633
3634sub clear_command_list {
3635 my ($self) = @_;
3636 $self->{cmdbox}->clear ();
3637}
3638
3639sub set_command_list {
3640 my ($self, $cmds) = @_;
3641
3642 $self->{cmdbox}->clear ();
3643 $self->{commands} = $cmds;
3644
3645 my $idx = 0;
3646
3647 for (@$cmds) {
3648 $self->{cmdbox}->add (my $hb = new CFClient::UI::HBox);
3649
3650 my $i = $idx;
3651 $hb->add (new CFClient::UI::Label text => $_);
3652 $hb->add (new CFClient::UI::Button
3653 text => "delete",
3654 tooltip => "Deletes the action from the record",
3655 on_activate => sub {
3656 $self->{cmdbox}->remove ($hb);
3657 $cmds->[$i] = undef;
3658 });
3659
3660
3661 $idx++
3662 } 3478 }
3663} 3479}
3664 3480
3665############################################################################# 3481#############################################################################
3666 3482
3720 } elsif ($ev->{button} == 2) { 3536 } elsif ($ev->{button} == 2) {
3721 $::CONN->user_send ("invoke $spell->{name}"); 3537 $::CONN->user_send ("invoke $spell->{name}");
3722 } elsif ($ev->{button} == 3) { 3538 } elsif ($ev->{button} == 3) {
3723 (new CFClient::UI::Menu 3539 (new CFClient::UI::Menu
3724 items => [ 3540 items => [
3725 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], 3541 ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }],
3726 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], 3542 ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }],
3727 ], 3543 ],
3728 )->popup ($ev); 3544 )->popup ($ev);
3729 } else { 3545 } else {
3730 return 0; 3546 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines