… | |
… | |
452 | |
452 | |
453 | $FOCUS->emit ("focus_out") if $FOCUS; |
453 | $FOCUS->emit ("focus_out") if $FOCUS; |
454 | $self->emit ("focus_in"); |
454 | $self->emit ("focus_in"); |
455 | } |
455 | } |
456 | |
456 | |
457 | sub invoke_mouse_motion { 1 } |
457 | sub invoke_mouse_motion { 0 } |
458 | sub invoke_button_up { 1 } |
458 | sub invoke_button_up { 0 } |
459 | sub invoke_key_down { 1 } |
459 | sub invoke_key_down { 0 } |
460 | sub invoke_key_up { 1 } |
460 | sub invoke_key_up { 0 } |
461 | |
461 | |
462 | sub invoke_button_down { |
462 | sub invoke_button_down { |
463 | my ($self, $ev, $x, $y) = @_; |
463 | my ($self, $ev, $x, $y) = @_; |
464 | |
464 | |
465 | $self->grab_focus; |
465 | $self->grab_focus; |
466 | |
466 | |
467 | 1 |
467 | 0 |
468 | } |
468 | } |
469 | |
469 | |
470 | sub connect { |
470 | sub connect { |
471 | my ($self, $signal, $cb) = @_; |
471 | my ($self, $signal, $cb) = @_; |
472 | |
472 | |
473 | push @{ $self->{signal_cb}{$signal} }, $cb; |
473 | push @{ $self->{signal_cb}{$signal} }, $cb; |
474 | } |
474 | } |
475 | |
475 | |
476 | sub emit { |
476 | sub emit { |
477 | my ($self, $signal, @args) = @_; |
477 | my ($self, $signal, @args) = @_; |
|
|
478 | |
|
|
479 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
478 | |
480 | |
479 | #d##TODO# stop propagating at first true, do not use sum |
481 | #d##TODO# stop propagating at first true, do not use sum |
480 | (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before |
482 | (List::Util::sum map $_->($self, @args), @{$self->{signal_cb}{$signal} || []}) # before |
481 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure |
483 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args) # closure |
482 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
484 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
… | |
… | |
587 | } |
589 | } |
588 | |
590 | |
589 | sub DESTROY { |
591 | sub DESTROY { |
590 | my ($self) = @_; |
592 | my ($self) = @_; |
591 | |
593 | |
|
|
594 | return if CFClient::in_destruct; |
|
|
595 | |
592 | delete $WIDGET{$self+0}; |
596 | delete $WIDGET{$self+0}; |
593 | |
597 | |
594 | eval { $self->destroy }; |
598 | eval { $self->destroy }; |
595 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
599 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
596 | } |
600 | } |
… | |
… | |
994 | my ($self, $widget) = @_; |
998 | my ($self, $widget) = @_; |
995 | |
999 | |
996 | $self->{vp}->add ($self->{child} = $widget); |
1000 | $self->{vp}->add ($self->{child} = $widget); |
997 | } |
1001 | } |
998 | |
1002 | |
|
|
1003 | sub invoke_button_up { |
|
|
1004 | my ($self, $ev) = @_; |
|
|
1005 | |
|
|
1006 | warn "button up $ev->{button}\n";#d# |
|
|
1007 | |
|
|
1008 | 0 |
|
|
1009 | } |
|
|
1010 | |
999 | sub update_slider { |
1011 | sub update_slider { |
1000 | my ($self) = @_; |
1012 | my ($self) = @_; |
1001 | |
1013 | |
1002 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); |
1014 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); |
1003 | } |
1015 | } |
… | |
… | |
1080 | my $self = $class->SUPER::new ( |
1092 | my $self = $class->SUPER::new ( |
1081 | bg => [1, 1, 1, 1], |
1093 | bg => [1, 1, 1, 1], |
1082 | border_bg => [1, 1, 1, 1], |
1094 | border_bg => [1, 1, 1, 1], |
1083 | border => 0.6, |
1095 | border => 0.6, |
1084 | can_events => 1, |
1096 | can_events => 1, |
1085 | min_w => 16, |
1097 | min_w => 64, |
1086 | min_h => 16, |
1098 | min_h => 32, |
1087 | %arg, |
1099 | %arg, |
1088 | ); |
1100 | ); |
1089 | |
1101 | |
1090 | $self->{title_widget} = new CFClient::UI::Label |
1102 | $self->{title_widget} = new CFClient::UI::Label |
1091 | align => 0, |
1103 | align => 0, |
… | |
… | |
1536 | #font => default_font |
1548 | #font => default_font |
1537 | #text => initial text |
1549 | #text => initial text |
1538 | #markup => initial narkup |
1550 | #markup => initial narkup |
1539 | #max_w => maximum pixel width |
1551 | #max_w => maximum pixel width |
1540 | ellipsise => 3, # end |
1552 | ellipsise => 3, # end |
1541 | layout => (new CFClient::Layout 2), |
1553 | layout => (new CFClient::Layout), |
1542 | fontsize => 1, |
1554 | fontsize => 1, |
1543 | align => -1, |
1555 | align => -1, |
1544 | valign => -1, |
1556 | valign => -1, |
1545 | padding_x => 2, |
1557 | padding_x => 2, |
1546 | padding_y => 2, |
1558 | padding_y => 2, |
1547 | can_events => 0, |
1559 | can_events => 0, |
1548 | %arg |
1560 | %arg |
1549 | ); |
1561 | ); |
1550 | |
1562 | |
1551 | if (exists $self->{template}) { |
1563 | if (exists $self->{template}) { |
1552 | my $layout = new CFClient::Layout 2; |
1564 | my $layout = new CFClient::Layout; |
1553 | $layout->set_text (delete $self->{template}); |
1565 | $layout->set_text (delete $self->{template}); |
1554 | $self->{template} = $layout; |
1566 | $self->{template} = $layout; |
1555 | } |
1567 | } |
1556 | |
1568 | |
1557 | if (exists $self->{markup}) { |
1569 | if (exists $self->{markup}) { |
… | |
… | |
1627 | |
1639 | |
1628 | my ($w, $h) = $self->{layout}->size; |
1640 | my ($w, $h) = $self->{layout}->size; |
1629 | |
1641 | |
1630 | if (exists $self->{template}) { |
1642 | if (exists $self->{template}) { |
1631 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
1643 | $self->{template}->set_font ($self->{font}) if $self->{font}; |
|
|
1644 | $self->{template}->set_width ($self->{max_w} || -1); |
1632 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1645 | $self->{template}->set_height ($self->{fontsize} * $::FONTSIZE); |
1633 | |
1646 | |
1634 | my ($w2, $h2) = $self->{template}->size; |
1647 | my ($w2, $h2) = $self->{template}->size; |
1635 | |
1648 | |
1636 | $w = List::Util::max $w, $w2; |
1649 | $w = List::Util::max $w, $w2; |
… | |
… | |
1688 | $self->{layout}->set_width ($self->{w}); |
1701 | $self->{layout}->set_width ($self->{w}); |
1689 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1702 | $self->{layout}->set_ellipsise ($self->{ellipsise}); |
1690 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1703 | $self->{layout}->set_single_paragraph_mode ($self->{ellipsise}); |
1691 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1704 | $self->{layout}->set_height ($self->{fontsize} * $::FONTSIZE); |
1692 | |
1705 | |
1693 | $self->{size_req} |
1706 | [$self->{layout}->size] |
1694 | }; |
1707 | }; |
1695 | |
1708 | |
1696 | unless (exists $self->{ox}) { |
1709 | unless (exists $self->{ox}) { |
1697 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1710 | $self->{ox} = int ($self->{align} < 0 ? $self->{padding_x} |
1698 | : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} |
1711 | : $self->{align} > 0 ? $self->{w} - $size->[0] - $self->{padding_x} |
… | |
… | |
1825 | |
1838 | |
1826 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1839 | my $idx = $self->{layout}->xy_to_index ($x, $y); |
1827 | |
1840 | |
1828 | # byte-index to char-index |
1841 | # byte-index to char-index |
1829 | my $text = $self->{text}; |
1842 | my $text = $self->{text}; |
1830 | utf8::encode $text; |
1843 | utf8::encode $text; $text = substr $text, 0, $idx; utf8::decode $text; |
1831 | $self->{cursor} = length substr $text, 0, $idx; |
1844 | $self->{cursor} = length $text; |
1832 | |
1845 | |
1833 | $self->_set_text ($self->{text}); |
1846 | $self->_set_text ($self->{text}); |
1834 | $self->update; |
1847 | $self->update; |
1835 | |
1848 | |
1836 | 1 |
1849 | 1 |
… | |
… | |
2115 | my $self = $class->SUPER::new ( |
2128 | my $self = $class->SUPER::new ( |
2116 | can_events => 0, |
2129 | can_events => 0, |
2117 | @_, |
2130 | @_, |
2118 | ); |
2131 | ); |
2119 | |
2132 | |
2120 | $self->{path} |
2133 | $self->{path} || $self->{tex} |
2121 | or Carp::croak "required attribute 'path' not set"; |
2134 | or Carp::croak "'path' or 'tex' attributes required"; |
2122 | |
2135 | |
2123 | $self->{tex} = $texture_cache{$self->{path}} ||= |
2136 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2124 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
2137 | new_from_file CFClient::Texture CFClient::find_rcfile $self->{path}, mipmap => 1; |
2125 | |
2138 | |
2126 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2139 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2127 | |
2140 | |
2128 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2141 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
… | |
… | |
2561 | can_events => 0, |
2574 | can_events => 0, |
2562 | indent => 0, |
2575 | indent => 0, |
2563 | #font => default_font |
2576 | #font => default_font |
2564 | @_, |
2577 | @_, |
2565 | |
2578 | |
2566 | layout => (new CFClient::Layout 2), |
2579 | layout => (new CFClient::Layout), |
2567 | par => [], |
2580 | par => [], |
2568 | height => 0, |
2581 | height => 0, |
2569 | children => [ |
2582 | children => [ |
2570 | (new CFClient::UI::Empty expand => 1), |
2583 | (new CFClient::UI::Empty expand => 1), |
2571 | (new CFClient::UI::Slider vertical => 1), |
2584 | (new CFClient::UI::Slider vertical => 1), |
… | |
… | |
3062 | for my $item (@{ $self->{items} }) { |
3075 | for my $item (@{ $self->{items} }) { |
3063 | my ($widget, $cb, $tooltip) = @$item; |
3076 | my ($widget, $cb, $tooltip) = @$item; |
3064 | |
3077 | |
3065 | # handle various types of items, only text for now |
3078 | # handle various types of items, only text for now |
3066 | if (!ref $widget) { |
3079 | if (!ref $widget) { |
|
|
3080 | if ($widget =~ /\t/) { |
|
|
3081 | my ($left, $right) = split /\t/, $widget, 2; |
|
|
3082 | |
|
|
3083 | $widget = new CFClient::UI::HBox |
|
|
3084 | can_hover => 1, |
|
|
3085 | can_events => 1, |
|
|
3086 | tooltip => $tooltip, |
|
|
3087 | children => [ |
|
|
3088 | (new CFClient::UI::Label markup => $left, expand => 1), |
|
|
3089 | (new CFClient::UI::Label markup => $right, align => +1), |
|
|
3090 | ], |
|
|
3091 | ; |
|
|
3092 | |
|
|
3093 | } else { |
3067 | $widget = new CFClient::UI::Label |
3094 | $widget = new CFClient::UI::Label |
3068 | can_hover => 1, |
3095 | can_hover => 1, |
3069 | can_events => 1, |
3096 | can_events => 1, |
3070 | markup => $widget, |
3097 | markup => $widget, |
3071 | tooltip => $tooltip |
3098 | tooltip => $tooltip; |
|
|
3099 | } |
3072 | } |
3100 | } |
3073 | |
3101 | |
3074 | $self->{item}{$widget} = $item; |
3102 | $self->{item}{$widget} = $item; |
3075 | |
3103 | |
3076 | $self->{vbox}->add ($widget); |
3104 | $self->{vbox}->add ($widget); |
… | |
… | |
3798 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3826 | my ($w, $h) = @$widget{qw(alloc_w alloc_h)}; |
3799 | |
3827 | |
3800 | $w = 0 if $w < 0; |
3828 | $w = 0 if $w < 0; |
3801 | $h = 0 if $h < 0; |
3829 | $h = 0 if $h < 0; |
3802 | |
3830 | |
|
|
3831 | $w = max $widget->{min_w}, $w; |
|
|
3832 | $h = max $widget->{min_h}, $h; |
|
|
3833 | |
|
|
3834 | $w = min $widget->{max_w}, $w if exists $widget->{max_w}; |
|
|
3835 | $h = min $widget->{max_h}, $h if exists $widget->{max_h}; |
|
|
3836 | |
3803 | $w = int $w + 0.5; |
3837 | $w = int $w + 0.5; |
3804 | $h = int $h + 0.5; |
3838 | $h = int $h + 0.5; |
3805 | |
3839 | |
3806 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3840 | if ($widget->{w} != $w || $widget->{h} != $h || delete $widget->{force_size_alloc}) { |
3807 | $widget->{old_w} = $widget->{w}; |
3841 | $widget->{old_w} = $widget->{w}; |
… | |
… | |
3837 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
3871 | ($draw_x, $draw_y, $draw_w, $draw_h) = |
3838 | (0, 0, $self->{w}, $self->{h}); |
3872 | (0, 0, $self->{w}, $self->{h}); |
3839 | } |
3873 | } |
3840 | |
3874 | |
3841 | $self->_draw; |
3875 | $self->_draw; |
3842 | |
|
|
3843 | #TODO#d# display texture cache |
|
|
3844 | { |
|
|
3845 | glEnable GL_TEXTURE_2D; |
|
|
3846 | glBindTexture GL_TEXTURE_2D, 41; |
|
|
3847 | glColor 1, 1, 1, 1; |
|
|
3848 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
|
|
3849 | glEnable GL_BLEND; |
|
|
3850 | glBlendFunc GL_SRC_ALPHA, GL_ZERO; |
|
|
3851 | glBegin GL_QUADS; |
|
|
3852 | glTexCoord 0,1; glVertex 0,0; |
|
|
3853 | glTexCoord 1,1; glVertex 255,0; |
|
|
3854 | glTexCoord 1,0; glVertex 255,255; |
|
|
3855 | glTexCoord 0,0; glVertex 0,255; |
|
|
3856 | glEnd; |
|
|
3857 | glDisable GL_BLEND; |
|
|
3858 | glDisable GL_TEXTURE_2D; |
|
|
3859 | } |
|
|
3860 | |
|
|
3861 | } |
3876 | } |
3862 | |
3877 | |
3863 | ############################################################################# |
3878 | ############################################################################# |
3864 | |
3879 | |
3865 | package CFClient::UI; |
3880 | package CFClient::UI; |