1 | package CFPlus::UI; |
1 | package CFPlus::UI; |
2 | |
2 | |
3 | use utf8; |
3 | use utf8; |
4 | use strict; |
4 | use strict; |
5 | |
5 | |
6 | use Scalar::Util (); |
|
|
7 | use List::Util (); |
6 | use List::Util (); |
8 | use Event; |
7 | use Event; |
9 | |
8 | |
10 | use CFPlus; |
9 | use CFPlus; |
11 | use CFPlus::Pod; |
10 | use CFPlus::Pod; |
… | |
… | |
23 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
22 | our $TOOLTIP_WATCHER = Event->idle (min => 1/60, cb => sub { |
24 | if (!$GRAB) { |
23 | if (!$GRAB) { |
25 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
24 | for (my $widget = $HOVER; $widget; $widget = $widget->{parent}) { |
26 | if (length $widget->{tooltip}) { |
25 | if (length $widget->{tooltip}) { |
27 | if ($TOOLTIP->{owner} != $widget) { |
26 | if ($TOOLTIP->{owner} != $widget) { |
|
|
27 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
28 | $TOOLTIP->hide; |
28 | $TOOLTIP->hide; |
29 | |
29 | |
30 | $TOOLTIP->{owner} = $widget; |
30 | $TOOLTIP->{owner} = $widget; |
|
|
31 | $TOOLTIP->{owner}->emit ("tooltip_show") if $TOOLTIP->{owner}; |
31 | |
32 | |
32 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | return if $ENV{CFPLUS_DEBUG} & 8; |
33 | |
34 | |
34 | my $tip = $widget->{tooltip}; |
35 | my $tip = $widget->{tooltip}; |
35 | |
36 | |
… | |
… | |
43 | } |
44 | } |
44 | } |
45 | } |
45 | } |
46 | } |
46 | |
47 | |
47 | $TOOLTIP->hide; |
48 | $TOOLTIP->hide; |
|
|
49 | $TOOLTIP->{owner}->emit ("tooltip_hide") if $TOOLTIP->{owner}; |
48 | delete $TOOLTIP->{owner}; |
50 | delete $TOOLTIP->{owner}; |
49 | }); |
51 | }); |
50 | |
52 | |
51 | sub get_layout { |
53 | sub get_layout { |
52 | my $layout; |
54 | my $layout; |
… | |
… | |
235 | h => undef, |
237 | h => undef, |
236 | can_events => 1, |
238 | can_events => 1, |
237 | @_ |
239 | @_ |
238 | }, $class; |
240 | }, $class; |
239 | |
241 | |
240 | Scalar::Util::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
242 | CFPlus::weaken ($CFPlus::UI::WIDGET{$self+0} = $self); |
241 | |
243 | |
242 | for (keys %$self) { |
244 | for (keys %$self) { |
243 | if (/^on_(.*)$/) { |
245 | if (/^on_(.*)$/) { |
244 | $self->connect ($1 => delete $self->{$_}); |
246 | $self->connect ($1 => delete $self->{$_}); |
245 | } |
247 | } |
… | |
… | |
262 | |
264 | |
263 | sub destroy { |
265 | sub destroy { |
264 | my ($self) = @_; |
266 | my ($self) = @_; |
265 | |
267 | |
266 | $self->hide; |
268 | $self->hide; |
|
|
269 | $self->emit ("destroy"); |
267 | %$self = (); |
270 | %$self = (); |
|
|
271 | } |
|
|
272 | |
|
|
273 | sub TO_JSON { |
|
|
274 | { __widget_ref__ => $_[0]{s_id} } |
268 | } |
275 | } |
269 | |
276 | |
270 | sub show { |
277 | sub show { |
271 | my ($self) = @_; |
278 | my ($self) = @_; |
272 | |
279 | |
… | |
… | |
313 | sub set_visibility { |
320 | sub set_visibility { |
314 | my ($self, $visible) = @_; |
321 | my ($self, $visible) = @_; |
315 | |
322 | |
316 | return if $self->{visible} == $visible; |
323 | return if $self->{visible} == $visible; |
317 | |
324 | |
318 | $visible ? $self->hide |
325 | $visible ? $self->show |
319 | : $self->show; |
326 | : $self->hide; |
320 | } |
327 | } |
321 | |
328 | |
322 | sub toggle_visibility { |
329 | sub toggle_visibility { |
323 | my ($self) = @_; |
330 | my ($self) = @_; |
324 | |
331 | |
… | |
… | |
502 | |
509 | |
503 | defined wantarray and CFPlus::guard { |
510 | defined wantarray and CFPlus::guard { |
504 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
511 | @{ $self->{signal_cb}{$signal} } = grep $_ != $cb, |
505 | @{ $self->{signal_cb}{$signal} }; |
512 | @{ $self->{signal_cb}{$signal} }; |
506 | } |
513 | } |
|
|
514 | } |
|
|
515 | |
|
|
516 | sub disconnect_all { |
|
|
517 | my ($self, $signal) = @_; |
|
|
518 | |
|
|
519 | delete $self->{signal_cb}{$signal}; |
507 | } |
520 | } |
508 | |
521 | |
509 | my %has_coords = ( |
522 | my %has_coords = ( |
510 | button_down => 1, |
523 | button_down => 1, |
511 | button_up => 1, |
524 | button_up => 1, |
… | |
… | |
523 | ? $args[0]->xy ($self) |
536 | ? $args[0]->xy ($self) |
524 | : (); |
537 | : (); |
525 | |
538 | |
526 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
539 | #warn +(caller(1))[3] . "emit $signal on $self (parent $self->{parent})\n";#d# |
527 | |
540 | |
528 | if ($self->{tooltip} =~ /Apply.*audio/) { |
541 | for my $cb ( |
529 | warn "2 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d# |
542 | @{$self->{signal_cb}{$signal} || []}, # before |
530 | warn "3 $self->{parent}\n";#d# |
543 | ($self->can ("invoke_$signal") || sub { 1 }), # closure |
|
|
544 | ) { |
|
|
545 | return $cb->($self, @args, @append) || next; |
531 | } |
546 | } |
532 | |
547 | |
533 | #d##TODO# stop propagating at first true, do not use sum |
548 | # parent |
534 | my $res = |
|
|
535 | (List::Util::sum map $_->($self, @args, @append), @{$self->{signal_cb}{$signal} || []}) # before |
|
|
536 | || ($self->can ("invoke_$signal") || sub { 1 })->($self, @args, @append) # closure |
|
|
537 | || ($self->{parent} && $self->{parent}->emit ($signal, @args)) # parent |
549 | $self->{parent} && $self->{parent}->emit ($signal, @args) |
538 | ; |
|
|
539 | if ($self->{tooltip} =~ /Apply.*audio/) { |
|
|
540 | warn "4 ", join ":", @{ $self->{signal_cb}{$signal} || []}, "\n";#d# |
|
|
541 | warn "5 $self->{parent}\n";#d# |
|
|
542 | } |
|
|
543 | |
|
|
544 | $res |
|
|
545 | } |
550 | } |
546 | |
551 | |
547 | sub find_widget { |
552 | sub find_widget { |
548 | my ($self, $x, $y) = @_; |
553 | my ($self, $x, $y) = @_; |
549 | |
554 | |
… | |
… | |
557 | } |
562 | } |
558 | |
563 | |
559 | sub set_parent { |
564 | sub set_parent { |
560 | my ($self, $parent) = @_; |
565 | my ($self, $parent) = @_; |
561 | |
566 | |
562 | Scalar::Util::weaken ($self->{parent} = $parent); |
567 | CFPlus::weaken ($self->{parent} = $parent); |
563 | $self->set_visible if $parent->{visible}; |
568 | $self->set_visible if $parent->{visible}; |
564 | } |
569 | } |
565 | |
570 | |
566 | sub realloc { |
571 | sub realloc { |
567 | my ($self) = @_; |
572 | my ($self) = @_; |
… | |
… | |
646 | my ($self) = @_; |
651 | my ($self) = @_; |
647 | |
652 | |
648 | warn "no draw defined for $self\n"; |
653 | warn "no draw defined for $self\n"; |
649 | } |
654 | } |
650 | |
655 | |
|
|
656 | my $cntx;#d# |
651 | sub DESTROY { |
657 | sub DESTROY { |
652 | my ($self) = @_; |
658 | my ($self) = @_; |
653 | |
659 | |
654 | return if CFPlus::in_destruct; |
660 | return if CFPlus::in_destruct; |
655 | |
|
|
656 | delete $WIDGET{$self+0}; |
|
|
657 | |
661 | |
658 | eval { $self->destroy }; |
662 | eval { $self->destroy }; |
659 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
663 | warn "exception during widget destruction: $@" if $@ & $@ != /during global destruction/; |
|
|
664 | |
|
|
665 | delete $WIDGET{$self+0}; |
660 | } |
666 | } |
661 | |
667 | |
662 | ############################################################################# |
668 | ############################################################################# |
663 | |
669 | |
664 | package CFPlus::UI::DrawBG; |
670 | package CFPlus::UI::DrawBG; |
… | |
… | |
1027 | |
1033 | |
1028 | ############################################################################# |
1034 | ############################################################################# |
1029 | |
1035 | |
1030 | package CFPlus::UI::ScrolledWindow; |
1036 | package CFPlus::UI::ScrolledWindow; |
1031 | |
1037 | |
1032 | our @ISA = CFPlus::UI::HBox::; |
1038 | our @ISA = CFPlus::UI::Table::; |
1033 | |
1039 | |
1034 | sub new { |
1040 | sub new { |
1035 | my ($class, %arg) = @_; |
1041 | my ($class, %arg) = @_; |
1036 | |
1042 | |
1037 | my $child = delete $arg{child}; |
1043 | my $child = delete $arg{child}; |
1038 | |
1044 | |
1039 | my $self; |
1045 | my $self; |
1040 | |
1046 | |
1041 | my $slider = new CFPlus::UI::Slider |
1047 | my $hslider = new CFPlus::UI::Slider |
|
|
1048 | vertical => 0, |
|
|
1049 | range => [0, 0, 1, 0.01], # HACK fix |
|
|
1050 | on_changed => sub { |
|
|
1051 | $self->{hpos} = $_[1]; |
|
|
1052 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
|
|
1053 | }, |
|
|
1054 | ; |
|
|
1055 | |
|
|
1056 | my $vslider = new CFPlus::UI::Slider |
1042 | vertical => 1, |
1057 | vertical => 1, |
1043 | range => [0, 0, 1, 0.01], # HACK fix |
1058 | range => [0, 0, 1, 0.01], # HACK fix |
1044 | on_changed => sub { |
1059 | on_changed => sub { |
1045 | $self->{vp}->set_offset (0, $_[1]); |
1060 | $self->{vpos} = $_[1]; |
|
|
1061 | $self->{vp}->set_offset ($self->{hpos}, $self->{vpos}); |
1046 | }, |
1062 | }, |
1047 | ; |
1063 | ; |
1048 | |
1064 | |
1049 | $self = $class->SUPER::new ( |
1065 | $self = $class->SUPER::new ( |
1050 | vp => (new CFPlus::UI::ViewPort expand => 1), |
1066 | scroll_x => 0, |
|
|
1067 | scroll_y => 1, |
1051 | can_events => 1, |
1068 | can_events => 1, |
1052 | slider => $slider, |
1069 | hslider => $hslider, |
|
|
1070 | vslider => $vslider, |
|
|
1071 | col_expand => [1, 0], |
|
|
1072 | row_expand => [1, 0], |
1053 | %arg, |
1073 | %arg, |
1054 | ); |
1074 | ); |
1055 | |
1075 | |
|
|
1076 | $self->{vp} = new CFPlus::UI::ViewPort |
|
|
1077 | expand => 1, |
|
|
1078 | scroll_x => $self->{scroll_x}, |
|
|
1079 | scroll_y => $self->{scroll_y}, |
|
|
1080 | ; |
|
|
1081 | |
|
|
1082 | $self->SUPER::add (0, 0, $self->{vp}); |
1056 | $self->SUPER::add ($self->{vp}, $self->{slider}); |
1083 | $self->SUPER::add (1, 0, $self->{vslider}) if $self->{scroll_y}; |
|
|
1084 | $self->SUPER::add (0, 1, $self->{hslider}) if $self->{scroll_x}; |
|
|
1085 | |
1057 | $self->add ($child) if $child; |
1086 | $self->add ($child) if $child; |
1058 | |
1087 | |
1059 | $self |
1088 | $self |
1060 | } |
1089 | } |
1061 | |
1090 | |
… | |
… | |
1070 | sub invoke_mouse_wheel { |
1099 | sub invoke_mouse_wheel { |
1071 | my ($self, $ev) = @_; |
1100 | my ($self, $ev) = @_; |
1072 | |
1101 | |
1073 | return 0 unless $ev->{dy}; # only vertical movements |
1102 | return 0 unless $ev->{dy}; # only vertical movements |
1074 | |
1103 | |
1075 | $self->{slider}->emit (mouse_wheel => $ev); |
1104 | $self->{vslider}->emit (mouse_wheel => $ev); |
1076 | |
1105 | |
1077 | 1 |
1106 | 1 |
1078 | } |
1107 | } |
1079 | |
1108 | |
1080 | sub update_slider { |
1109 | sub update_slider { |
1081 | my ($self) = @_; |
1110 | my ($self) = @_; |
1082 | |
1111 | |
|
|
1112 | my $child = ($self->{vp} or return)->child; |
|
|
1113 | |
|
|
1114 | my ($w1, $w2) = ($child->{w}, $self->{vp}{w}); |
1083 | $self->{slider}->set_range ([$self->{slider}{range}[0], 0, $self->{vp}->child->{h}, $self->{vp}{h}, 1]); |
1115 | $self->{hslider}->set_range ([$self->{hslider}{range}[0], 0, $w1, $w2, 1]); |
|
|
1116 | #$self->{hslider}->set_visibility ($w1 != $w2); |
|
|
1117 | |
|
|
1118 | my ($h1, $h2) = ($child->{h}, $self->{vp}{h}); |
|
|
1119 | $self->{vslider}->set_range ([$self->{vslider}{range}[0], 0, $h1, $h2, 1]); |
|
|
1120 | #$self->{vslider}->set_visibility ($h1 != $h2); |
1084 | } |
1121 | } |
1085 | |
1122 | |
1086 | sub update { |
1123 | sub update { |
1087 | my ($self) = @_; |
1124 | my ($self) = @_; |
1088 | |
1125 | |
1089 | $self->SUPER::update; |
1126 | $self->SUPER::update; |
1090 | |
|
|
1091 | $self->update_slider; |
1127 | $self->update_slider; |
1092 | } |
1128 | } |
1093 | |
1129 | |
1094 | sub invoke_size_allocate { |
1130 | sub invoke_size_allocate { |
1095 | my ($self, $w, $h) = @_; |
1131 | my ($self, $w, $h) = @_; |
1096 | |
1132 | |
1097 | $self->update_slider; |
1133 | $self->update_slider; |
1098 | |
|
|
1099 | $self->SUPER::invoke_size_allocate ($w, $h) |
1134 | $self->SUPER::invoke_size_allocate ($w, $h) |
1100 | } |
1135 | } |
1101 | |
1136 | |
1102 | ############################################################################# |
1137 | ############################################################################# |
1103 | |
1138 | |
… | |
… | |
1475 | sub new { |
1510 | sub new { |
1476 | my $class = shift; |
1511 | my $class = shift; |
1477 | |
1512 | |
1478 | $class->SUPER::new ( |
1513 | $class->SUPER::new ( |
1479 | col_expand => [], |
1514 | col_expand => [], |
|
|
1515 | row_expand => [], |
1480 | @_, |
1516 | @_, |
1481 | ) |
1517 | ) |
1482 | } |
1518 | } |
1483 | |
1519 | |
1484 | sub children { |
1520 | sub children { |
… | |
… | |
1559 | my ($ws, $hs) = $self->get_wh; |
1595 | my ($ws, $hs) = $self->get_wh; |
1560 | |
1596 | |
1561 | my $req_w = (sum @$ws) || 1; |
1597 | my $req_w = (sum @$ws) || 1; |
1562 | my $req_h = (sum @$hs) || 1; |
1598 | my $req_h = (sum @$hs) || 1; |
1563 | |
1599 | |
1564 | # TODO: nicer code && do row_expand |
1600 | # TODO: nicer code |
1565 | my @col_expand = @{$self->{col_expand}}; |
1601 | my @col_expand = @{$self->{col_expand}}; |
1566 | @col_expand = (1) x @$ws unless @col_expand; |
1602 | @col_expand = (1) x @$ws unless @col_expand; |
1567 | my $col_expand = (sum @col_expand) || 1; |
1603 | my $col_expand = (sum @col_expand) || 1; |
1568 | |
1604 | |
1569 | # linearly scale sizes |
|
|
1570 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1605 | $ws->[$_] += $col_expand[$_] / $col_expand * ($w - $req_w) for 0 .. $#$ws; |
1571 | $hs->[$_] *= 1 * $h / $req_h for 0 .. $#$hs; |
|
|
1572 | |
1606 | |
1573 | CFPlus::UI::harmonize $ws; |
1607 | CFPlus::UI::harmonize $ws; |
|
|
1608 | |
|
|
1609 | my @row_expand = @{$self->{row_expand}}; |
|
|
1610 | @row_expand = (1) x @$ws unless @row_expand; |
|
|
1611 | my $row_expand = (sum @row_expand) || 1; |
|
|
1612 | |
|
|
1613 | $hs->[$_] += $row_expand[$_] / $row_expand * ($h - $req_h) for 0 .. $#$hs; |
|
|
1614 | |
1574 | CFPlus::UI::harmonize $hs; |
1615 | CFPlus::UI::harmonize $hs; |
1575 | |
1616 | |
1576 | my $y; |
1617 | my $y; |
1577 | |
1618 | |
1578 | for my $r (0 .. $#{$self->{children}}) { |
1619 | for my $r (0 .. $#{$self->{children}}) { |
… | |
… | |
1913 | active_fg => [0, 0, 0], |
1954 | active_fg => [0, 0, 0], |
1914 | can_hover => 1, |
1955 | can_hover => 1, |
1915 | can_focus => 1, |
1956 | can_focus => 1, |
1916 | valign => 0, |
1957 | valign => 0, |
1917 | can_events => 1, |
1958 | can_events => 1, |
|
|
1959 | ellipsise => 0, |
1918 | #text => ... |
1960 | #text => ... |
1919 | #hidden => "*", |
1961 | #hidden => "*", |
1920 | @_ |
1962 | @_ |
1921 | ) |
1963 | ) |
1922 | } |
1964 | } |
… | |
… | |
1966 | my $mod = $ev->{mod}; |
2008 | my $mod = $ev->{mod}; |
1967 | my $sym = $ev->{sym}; |
2009 | my $sym = $ev->{sym}; |
1968 | my $uni = $ev->{unicode}; |
2010 | my $uni = $ev->{unicode}; |
1969 | |
2011 | |
1970 | my $text = $self->get_text; |
2012 | my $text = $self->get_text; |
|
|
2013 | |
|
|
2014 | $self->{cursor} = List::Util::max 0, List::Util::min $self->{cursor}, length $text; |
1971 | |
2015 | |
1972 | if ($uni == 8) { |
2016 | if ($uni == 8) { |
1973 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
2017 | substr $text, --$self->{cursor}, 1, "" if $self->{cursor}; |
1974 | } elsif ($uni == 127) { |
2018 | } elsif ($uni == 127) { |
1975 | substr $text, $self->{cursor}, 1, ""; |
2019 | substr $text, $self->{cursor}, 1, ""; |
1976 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
2020 | } elsif ($sym == CFPlus::SDLK_LEFT) { |
1977 | --$self->{cursor} if $self->{cursor}; |
2021 | --$self->{cursor} if $self->{cursor}; |
1978 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
2022 | } elsif ($sym == CFPlus::SDLK_RIGHT) { |
1979 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
2023 | ++$self->{cursor} if $self->{cursor} < length $self->{text}; |
1980 | } elsif ($sym == CFPlus::SDLK_HOME) { |
2024 | } elsif ($sym == CFPlus::SDLK_HOME) { |
|
|
2025 | # what a hack |
1981 | $self->{cursor} = 0; |
2026 | $self->{cursor} = |
|
|
2027 | (substr $self->{text}, 0, $self->{cursor}) =~ /^(.*\012)/ |
|
|
2028 | ? length $1 |
|
|
2029 | : 0; |
1982 | } elsif ($sym == CFPlus::SDLK_END) { |
2030 | } elsif ($sym == CFPlus::SDLK_END) { |
|
|
2031 | # uh, again |
|
|
2032 | $self->{cursor} = |
|
|
2033 | (substr $self->{text}, $self->{cursor}) =~ /^([^\012]*)\012/ |
1983 | $self->{cursor} = length $text; |
2034 | ? $self->{cursor} + length $1 |
|
|
2035 | : length $self->{text}; |
1984 | } elsif ($uni == 21) { # ctrl-u |
2036 | } elsif ($uni == 21) { # ctrl-u |
1985 | $text = ""; |
2037 | $text = ""; |
1986 | $self->{cursor} = 0; |
2038 | $self->{cursor} = 0; |
1987 | } elsif ($uni == 27) { |
2039 | } elsif ($uni == 27) { |
1988 | $self->emit ('escape'); |
2040 | $self->emit ('escape'); |
1989 | } elsif ($uni) { |
2041 | } elsif ($uni == 0x0d) { |
|
|
2042 | substr $text, $self->{cursor}++, 0, "\012"; |
|
|
2043 | } elsif ($uni >= 0x20) { |
1990 | substr $text, $self->{cursor}++, 0, chr $uni; |
2044 | substr $text, $self->{cursor}++, 0, chr $uni; |
1991 | } else { |
2045 | } else { |
1992 | return 0; |
2046 | return 0; |
1993 | } |
2047 | } |
1994 | |
2048 | |
1995 | $self->_set_text ($text); |
2049 | $self->_set_text ($text); |
1996 | |
2050 | |
1997 | $self->realloc; |
2051 | $self->realloc; |
|
|
2052 | $self->update; |
1998 | |
2053 | |
1999 | 1 |
2054 | 1 |
2000 | } |
2055 | } |
2001 | |
2056 | |
2002 | sub invoke_focus_in { |
2057 | sub invoke_focus_in { |
… | |
… | |
2071 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2126 | glVertex 0.5 + $self->{cur_x} + $self->{ox}, $self->{cur_y} + $self->{oy} + $self->{cur_h}; |
2072 | glEnd; |
2127 | glEnd; |
2073 | } |
2128 | } |
2074 | } |
2129 | } |
2075 | |
2130 | |
|
|
2131 | ############################################################################# |
|
|
2132 | |
2076 | package CFPlus::UI::Entry; |
2133 | package CFPlus::UI::Entry; |
2077 | |
2134 | |
2078 | our @ISA = CFPlus::UI::EntryBase::; |
2135 | our @ISA = CFPlus::UI::EntryBase::; |
2079 | |
2136 | |
2080 | use CFPlus::OpenGL; |
2137 | use CFPlus::OpenGL; |
… | |
… | |
2082 | sub invoke_key_down { |
2139 | sub invoke_key_down { |
2083 | my ($self, $ev) = @_; |
2140 | my ($self, $ev) = @_; |
2084 | |
2141 | |
2085 | my $sym = $ev->{sym}; |
2142 | my $sym = $ev->{sym}; |
2086 | |
2143 | |
2087 | if ($sym == 13) { |
2144 | if ($ev->{uni} == 0x0d || $sym == 13) { |
2088 | unshift @{$self->{history}}, |
2145 | unshift @{$self->{history}}, |
2089 | my $txt = $self->get_text; |
2146 | my $txt = $self->get_text; |
2090 | |
2147 | |
2091 | $self->{history_pointer} = -1; |
2148 | $self->{history_pointer} = -1; |
2092 | $self->{history_saveback} = ''; |
2149 | $self->{history_saveback} = ''; |
… | |
… | |
2122 | 1 |
2179 | 1 |
2123 | } |
2180 | } |
2124 | |
2181 | |
2125 | ############################################################################# |
2182 | ############################################################################# |
2126 | |
2183 | |
|
|
2184 | package CFPlus::UI::TextEdit; |
|
|
2185 | |
|
|
2186 | our @ISA = CFPlus::UI::EntryBase::; |
|
|
2187 | |
|
|
2188 | use CFPlus::OpenGL; |
|
|
2189 | |
|
|
2190 | sub move_cursor_ver { |
|
|
2191 | my ($self, $dy) = @_; |
|
|
2192 | |
|
|
2193 | my ($y, $x) = $self->{layout}->index_to_line_x ($self->{cursor}); |
|
|
2194 | |
|
|
2195 | $y += $dy; |
|
|
2196 | |
|
|
2197 | if (defined (my $index = $self->{layout}->line_x_to_index ($y, $x))) { |
|
|
2198 | $self->{cursor} = $index; |
|
|
2199 | delete $self->{cur_h}; |
|
|
2200 | $self->update; |
|
|
2201 | return; |
|
|
2202 | } |
|
|
2203 | } |
|
|
2204 | |
|
|
2205 | sub invoke_key_down { |
|
|
2206 | my ($self, $ev) = @_; |
|
|
2207 | |
|
|
2208 | my $sym = $ev->{sym}; |
|
|
2209 | |
|
|
2210 | if ($sym == CFPlus::SDLK_UP) { |
|
|
2211 | $self->move_cursor_ver (-1); |
|
|
2212 | } elsif ($sym == CFPlus::SDLK_DOWN) { |
|
|
2213 | $self->move_cursor_ver (+1); |
|
|
2214 | } else { |
|
|
2215 | return $self->SUPER::invoke_key_down ($ev) |
|
|
2216 | } |
|
|
2217 | |
|
|
2218 | 1 |
|
|
2219 | } |
|
|
2220 | |
|
|
2221 | ############################################################################# |
|
|
2222 | |
2127 | package CFPlus::UI::Button; |
2223 | package CFPlus::UI::Button; |
2128 | |
2224 | |
2129 | our @ISA = CFPlus::UI::Label::; |
2225 | our @ISA = CFPlus::UI::Label::; |
2130 | |
2226 | |
2131 | use CFPlus::OpenGL; |
2227 | use CFPlus::OpenGL; |
… | |
… | |
2273 | or Carp::croak "'path' or 'tex' attributes required"; |
2369 | or Carp::croak "'path' or 'tex' attributes required"; |
2274 | |
2370 | |
2275 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2371 | $self->{tex} ||= $texture_cache{$self->{path}} ||= |
2276 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2372 | new_from_file CFPlus::Texture CFPlus::find_rcfile $self->{path}, mipmap => 1; |
2277 | |
2373 | |
2278 | Scalar::Util::weaken $texture_cache{$self->{path}}; |
2374 | CFPlus::weaken $texture_cache{$self->{path}}; |
2279 | |
2375 | |
2280 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2376 | $self->{aspect} ||= $self->{tex}{w} / $self->{tex}{h}; |
2281 | |
2377 | |
2282 | $self |
2378 | $self |
2283 | } |
2379 | } |
… | |
… | |
2656 | sub invoke_mouse_wheel { |
2752 | sub invoke_mouse_wheel { |
2657 | my ($self, $ev) = @_; |
2753 | my ($self, $ev) = @_; |
2658 | |
2754 | |
2659 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2755 | my $delta = $self->{vertical} ? $ev->{dy} : $ev->{dx}; |
2660 | |
2756 | |
|
|
2757 | my $pagepart = $ev->{mod} & CFPlus::KMOD_SHIFT ? 1 : 0.2; |
|
|
2758 | |
2661 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * 0.2); |
2759 | $self->set_value ($self->{range}[0] + $delta * $self->{range}[3] * $pagepart); |
2662 | |
2760 | |
2663 | ! ! $delta |
2761 | ! ! $delta |
2664 | } |
2762 | } |
2665 | |
2763 | |
2666 | sub update { |
2764 | sub update { |
… | |
… | |
2776 | #font => default_font |
2874 | #font => default_font |
2777 | @_, |
2875 | @_, |
2778 | |
2876 | |
2779 | layout => (new CFPlus::Layout), |
2877 | layout => (new CFPlus::Layout), |
2780 | par => [], |
2878 | par => [], |
|
|
2879 | max_par => 0, |
2781 | height => 0, |
2880 | height => 0, |
2782 | children => [ |
2881 | children => [ |
2783 | (new CFPlus::UI::Empty expand => 1), |
2882 | (new CFPlus::UI::Empty expand => 1), |
2784 | (new CFPlus::UI::Slider vertical => 1), |
2883 | (new CFPlus::UI::Slider vertical => 1), |
2785 | ], |
2884 | ], |
… | |
… | |
2908 | wrapped => 1, |
3007 | wrapped => 1, |
2909 | }; |
3008 | }; |
2910 | |
3009 | |
2911 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
3010 | $self->add (@{ $para->{widget} }) if @{ $para->{widget} }; |
2912 | push @{$self->{par}}, $para; |
3011 | push @{$self->{par}}, $para; |
|
|
3012 | } |
|
|
3013 | |
|
|
3014 | if (my $max = $self->{max_par}) { |
|
|
3015 | shift @{$self->{par}} while @{$self->{par}} > $max; |
2913 | } |
3016 | } |
2914 | |
3017 | |
2915 | $self->{need_reflow}++; |
3018 | $self->{need_reflow}++; |
2916 | $self->update; |
3019 | $self->update; |
2917 | } |
3020 | } |
… | |
… | |
3231 | can_events => 0, |
3334 | can_events => 0, |
3232 | @_, |
3335 | @_, |
3233 | ); |
3336 | ); |
3234 | |
3337 | |
3235 | if ($self->{anim} && $self->{animspeed}) { |
3338 | if ($self->{anim} && $self->{animspeed}) { |
3236 | Scalar::Util::weaken (my $widget = $self); |
3339 | CFPlus::weaken (my $widget = $self); |
3237 | |
3340 | |
3238 | $self->{timer} = Event->timer ( |
3341 | $self->{timer} = Event->timer ( |
3239 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3342 | at => $self->{animspeed} * int $::NOW / $self->{animspeed}, |
3240 | hard => 1, |
3343 | hard => 1, |
3241 | interval => $self->{animspeed}, |
3344 | interval => $self->{animspeed}, |
3242 | cb => sub { |
3345 | cb => sub { |
|
|
3346 | delete $self->{wait_face}; |
3243 | ++$widget->{frame}; |
3347 | ++$widget->{frame}; |
3244 | $widget->update; |
3348 | $widget->update; |
3245 | }, |
3349 | }, |
3246 | ); |
3350 | ); |
3247 | } |
3351 | } |
… | |
… | |
3274 | my $anim = $::CONN->{anim}[$self->{anim}]; |
3378 | my $anim = $::CONN->{anim}[$self->{anim}]; |
3275 | |
3379 | |
3276 | $face = $anim->[ $self->{frame} % @$anim ] |
3380 | $face = $anim->[ $self->{frame} % @$anim ] |
3277 | if $anim && @$anim; |
3381 | if $anim && @$anim; |
3278 | } |
3382 | } |
|
|
3383 | |
|
|
3384 | my $faceid = $::CONN->{faceid}[$face || $self->{face}] |
|
|
3385 | or return; |
3279 | |
3386 | |
3280 | my $tex = $::CONN->{texture}[$::CONN->{faceid}[$face || $self->{face}]]; |
3387 | my $tex = $::CONN->{texture}[$faceid]; |
3281 | |
3388 | |
3282 | if ($tex) { |
3389 | if ($tex) { |
3283 | glEnable GL_TEXTURE_2D; |
3390 | glEnable GL_TEXTURE_2D; |
3284 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3391 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE; |
3285 | glColor 0, 0, 0, 1; |
3392 | glColor 0, 0, 0, 1; |
3286 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3393 | $tex->draw_quad_alpha (0, 0, $self->{w}, $self->{h}); |
3287 | glDisable GL_TEXTURE_2D; |
3394 | glDisable GL_TEXTURE_2D; |
|
|
3395 | } else { |
|
|
3396 | $self->{wait_face} ||= $::CONN->connect_face_update ($faceid, sub { |
|
|
3397 | $self->update; |
|
|
3398 | }); |
3288 | } |
3399 | } |
3289 | } |
3400 | } |
3290 | |
3401 | |
3291 | sub destroy { |
3402 | sub destroy { |
3292 | my ($self) = @_; |
3403 | my ($self) = @_; |
… | |
… | |
3495 | } |
3606 | } |
3496 | |
3607 | |
3497 | sub add { |
3608 | sub add { |
3498 | my ($self, $title, $widget, $tooltip) = @_; |
3609 | my ($self, $title, $widget, $tooltip) = @_; |
3499 | |
3610 | |
3500 | Scalar::Util::weaken $self; |
3611 | CFPlus::weaken $self; |
3501 | |
3612 | |
3502 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3613 | $self->{buttonbar}->add (new CFPlus::UI::Button |
3503 | markup => $title, |
3614 | markup => $title, |
3504 | tooltip => $tooltip, |
3615 | tooltip => $tooltip, |
3505 | on_activate => sub { $self->set_current_page ($widget) }, |
3616 | on_activate => sub { $self->set_current_page ($widget) }, |
… | |
… | |
3589 | my $self = $class->SUPER::new ( |
3700 | my $self = $class->SUPER::new ( |
3590 | fontsize => 0.8, |
3701 | fontsize => 0.8, |
3591 | @_, |
3702 | @_, |
3592 | ); |
3703 | ); |
3593 | |
3704 | |
3594 | Scalar::Util::weaken (my $this = $self); |
3705 | CFPlus::weaken (my $this = $self); |
3595 | |
3706 | |
3596 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3707 | $self->{timer} = Event->timer (after => 1, interval => 1, cb => sub { $this->reorder }); |
3597 | |
3708 | |
3598 | $self |
3709 | $self |
3599 | } |
3710 | } |
… | |
… | |
3722 | $self->SUPER::destroy; |
3833 | $self->SUPER::destroy; |
3723 | } |
3834 | } |
3724 | |
3835 | |
3725 | ############################################################################# |
3836 | ############################################################################# |
3726 | |
3837 | |
3727 | package CFPlus::UI::Inventory; |
|
|
3728 | |
|
|
3729 | our @ISA = CFPlus::UI::Table::; |
|
|
3730 | |
|
|
3731 | sub new { |
|
|
3732 | my $class = shift; |
|
|
3733 | |
|
|
3734 | my $self = $class->SUPER::new ( |
|
|
3735 | col_expand => [0, 1, 0], |
|
|
3736 | items => [], |
|
|
3737 | @_, |
|
|
3738 | ); |
|
|
3739 | |
|
|
3740 | $self->set_sort_order (undef); |
|
|
3741 | |
|
|
3742 | $self |
|
|
3743 | } |
|
|
3744 | |
|
|
3745 | sub update_items { |
|
|
3746 | my ($self) = @_; |
|
|
3747 | |
|
|
3748 | $self->clear; |
|
|
3749 | |
|
|
3750 | my @item = $self->{sort}->(@{ $self->{items} }); |
|
|
3751 | |
|
|
3752 | my @adds; |
|
|
3753 | my $row = 0; |
|
|
3754 | for my $item ($self->{sort}->(@{ $self->{items} })) { |
|
|
3755 | CFPlus::Item::update_widgets $item; |
|
|
3756 | |
|
|
3757 | push @adds, 0, $row, $item->{face_widget}; |
|
|
3758 | push @adds, 1, $row, $item->{desc_widget}; |
|
|
3759 | push @adds, 2, $row, $item->{weight_widget}; |
|
|
3760 | |
|
|
3761 | $row++; |
|
|
3762 | } |
|
|
3763 | |
|
|
3764 | $self->add (@adds); |
|
|
3765 | } |
|
|
3766 | |
|
|
3767 | sub set_sort_order { |
|
|
3768 | my ($self, $order) = @_; |
|
|
3769 | |
|
|
3770 | $self->{sort} = $order ||= sub { |
|
|
3771 | sort { |
|
|
3772 | $a->{type} <=> $b->{type} |
|
|
3773 | or $a->{name} cmp $b->{name} |
|
|
3774 | } @_ |
|
|
3775 | }; |
|
|
3776 | |
|
|
3777 | $self->update_items; |
|
|
3778 | } |
|
|
3779 | |
|
|
3780 | sub set_items { |
|
|
3781 | my ($self, $items) = @_; |
|
|
3782 | |
|
|
3783 | $self->{items} = [$items ? values %$items : ()]; |
|
|
3784 | $self->update_items; |
|
|
3785 | } |
|
|
3786 | |
|
|
3787 | ############################################################################# |
|
|
3788 | |
|
|
3789 | package CFPlus::UI::SpellList; |
|
|
3790 | |
|
|
3791 | our @ISA = CFPlus::UI::Table::; |
|
|
3792 | |
|
|
3793 | sub new { |
|
|
3794 | my $class = shift; |
|
|
3795 | |
|
|
3796 | my $self = $class->SUPER::new ( |
|
|
3797 | binding => [], |
|
|
3798 | commands => [], |
|
|
3799 | @_, |
|
|
3800 | ) |
|
|
3801 | } |
|
|
3802 | |
|
|
3803 | my $TOOLTIP_ALL = "\n\n<small>Left click - ready spell\nMiddle click - invoke spell\nRight click - further options</small>"; |
|
|
3804 | |
|
|
3805 | my @TOOLTIP_NAME = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3806 | "<b>Name</b>. The name of the spell.$TOOLTIP_ALL"); |
|
|
3807 | my @TOOLTIP_SKILL = (align => -1, can_events => 1, can_hover => 1, tooltip => |
|
|
3808 | "<b>Skill</b>. The skill (or magic school) required to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3809 | my @TOOLTIP_LVL = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3810 | "<b>Level</b>. Minimum level the caster needs in the associated skill to be able to attempt casting this spell.$TOOLTIP_ALL"); |
|
|
3811 | my @TOOLTIP_SP = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3812 | "<b>Spell points / Grace points</b>. Amount of spell or grace points used by each invocation.$TOOLTIP_ALL"); |
|
|
3813 | my @TOOLTIP_DMG = (align => 1, can_events => 1, can_hover => 1, tooltip => |
|
|
3814 | "<b>Damage</b>. The amount of damage the spell deals when it hits.$TOOLTIP_ALL"); |
|
|
3815 | |
|
|
3816 | sub rebuild_spell_list { |
|
|
3817 | my ($self) = @_; |
|
|
3818 | |
|
|
3819 | $CFPlus::UI::ROOT->on_refresh ($self => sub { |
|
|
3820 | $self->clear; |
|
|
3821 | |
|
|
3822 | return unless $::CONN; |
|
|
3823 | |
|
|
3824 | $self->add (1, 0, new CFPlus::UI::Label text => "Spell Name", @TOOLTIP_NAME); |
|
|
3825 | $self->add (2, 0, new CFPlus::UI::Label text => "Skill", @TOOLTIP_SKILL); |
|
|
3826 | $self->add (3, 0, new CFPlus::UI::Label text => "Lvl" , @TOOLTIP_LVL); |
|
|
3827 | $self->add (4, 0, new CFPlus::UI::Label text => "Sp/Gp", @TOOLTIP_SP); |
|
|
3828 | $self->add (5, 0, new CFPlus::UI::Label text => "Dmg" , @TOOLTIP_DMG); |
|
|
3829 | |
|
|
3830 | my $row = 0; |
|
|
3831 | |
|
|
3832 | for (sort { $a cmp $b } keys %{ $self->{spell} }) { |
|
|
3833 | my $spell = $self->{spell}{$_}; |
|
|
3834 | |
|
|
3835 | $row++; |
|
|
3836 | |
|
|
3837 | my $spell_cb = sub { |
|
|
3838 | my ($widget, $ev) = @_; |
|
|
3839 | |
|
|
3840 | if ($ev->{button} == 1) { |
|
|
3841 | $::CONN->user_send ("cast $spell->{name}"); |
|
|
3842 | } elsif ($ev->{button} == 2) { |
|
|
3843 | $::CONN->user_send ("invoke $spell->{name}"); |
|
|
3844 | } elsif ($ev->{button} == 3) { |
|
|
3845 | (new CFPlus::UI::Menu |
|
|
3846 | items => [ |
|
|
3847 | ["bind <i>cast $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["cast $spell->{name}"]) }], |
|
|
3848 | ["bind <i>invoke $spell->{name}</i> to a key" => sub { $::BIND_EDITOR->do_quick_binding (["invoke $spell->{name}"]) }], |
|
|
3849 | ], |
|
|
3850 | )->popup ($ev); |
|
|
3851 | } else { |
|
|
3852 | return 0; |
|
|
3853 | } |
|
|
3854 | |
|
|
3855 | 1 |
|
|
3856 | }; |
|
|
3857 | |
|
|
3858 | my $tooltip = (CFPlus::asxml $spell->{message}) . $TOOLTIP_ALL; |
|
|
3859 | |
|
|
3860 | #TODO: add path info to tooltip |
|
|
3861 | #$self->add (6, $row, new CFPlus::UI::Label text => $spell->{path}); |
|
|
3862 | |
|
|
3863 | $self->add (0, $row, new CFPlus::UI::Face |
|
|
3864 | face => $spell->{face}, |
|
|
3865 | can_hover => 1, |
|
|
3866 | can_events => 1, |
|
|
3867 | tooltip => $tooltip, |
|
|
3868 | on_button_down => $spell_cb, |
|
|
3869 | ); |
|
|
3870 | |
|
|
3871 | $self->add (1, $row, new CFPlus::UI::Label |
|
|
3872 | expand => 1, |
|
|
3873 | text => $spell->{name}, |
|
|
3874 | can_hover => 1, |
|
|
3875 | can_events => 1, |
|
|
3876 | tooltip => $tooltip, |
|
|
3877 | on_button_down => $spell_cb, |
|
|
3878 | ); |
|
|
3879 | |
|
|
3880 | $self->add (2, $row, new CFPlus::UI::Label text => $::CONN->{skill_info}{$spell->{skill}}, @TOOLTIP_SKILL); |
|
|
3881 | $self->add (3, $row, new CFPlus::UI::Label text => $spell->{level}, @TOOLTIP_LVL); |
|
|
3882 | $self->add (4, $row, new CFPlus::UI::Label text => $spell->{mana} || $spell->{grace}, @TOOLTIP_SP); |
|
|
3883 | $self->add (5, $row, new CFPlus::UI::Label text => $spell->{damage}, @TOOLTIP_DMG); |
|
|
3884 | } |
|
|
3885 | }); |
|
|
3886 | } |
|
|
3887 | |
|
|
3888 | sub add_spell { |
|
|
3889 | my ($self, $spell) = @_; |
|
|
3890 | |
|
|
3891 | $self->{spell}->{$spell->{name}} = $spell; |
|
|
3892 | $self->rebuild_spell_list; |
|
|
3893 | } |
|
|
3894 | |
|
|
3895 | sub remove_spell { |
|
|
3896 | my ($self, $spell) = @_; |
|
|
3897 | |
|
|
3898 | delete $self->{spell}->{$spell->{name}}; |
|
|
3899 | $self->rebuild_spell_list; |
|
|
3900 | } |
|
|
3901 | |
|
|
3902 | sub clear_spells { |
|
|
3903 | my ($self) = @_; |
|
|
3904 | |
|
|
3905 | $self->{spell} = {}; |
|
|
3906 | $self->rebuild_spell_list; |
|
|
3907 | } |
|
|
3908 | |
|
|
3909 | ############################################################################# |
|
|
3910 | |
|
|
3911 | package CFPlus::UI::Root; |
3838 | package CFPlus::UI::Root; |
3912 | |
3839 | |
3913 | our @ISA = CFPlus::UI::Container::; |
3840 | our @ISA = CFPlus::UI::Container::; |
3914 | |
3841 | |
3915 | use List::Util qw(min max); |
3842 | use List::Util qw(min max); |
… | |
… | |
3922 | my $self = $class->SUPER::new ( |
3849 | my $self = $class->SUPER::new ( |
3923 | visible => 1, |
3850 | visible => 1, |
3924 | @_, |
3851 | @_, |
3925 | ); |
3852 | ); |
3926 | |
3853 | |
3927 | Scalar::Util::weaken ($self->{root} = $self); |
3854 | CFPlus::weaken ($self->{root} = $self); |
3928 | |
3855 | |
3929 | $self |
3856 | $self |
3930 | } |
3857 | } |
3931 | |
3858 | |
3932 | sub size_request { |
3859 | sub size_request { |