| 1 | |||
| 2 | package App::Asciio::Text ; | ||
| 3 | |||
| 4 | $|++ ; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | |||
| 9 | use Data::TreeDumper ; | ||
| 10 | use IO::Prompter ; | ||
| 11 | |||
| 12 | #------------------------------------------------------------------------------------------------------ | ||
| 13 | |||
| 14 | sub display_popup_menu | ||
| 15 | { | ||
| 16 | my ($self, $event) = @_; | ||
| 17 | |||
| 18 | my ($popup_x, $popup_y) = @{$self}{'MOUSE_X', 'MOUSE_Y'} ; | ||
| 19 | |||
| 20 | my @menu_items ; | ||
| 21 | |||
| 22 | for my $element (@{$self->{ELEMENT_TYPES}}) | ||
| 23 | { | ||
| 24 | (my $name_with_underscore = $element->{NAME}) =~ s/_/__/g ; | ||
| 25 | $name_with_underscore = ucfirst $name_with_underscore ; | ||
| 26 | |||
| 27 | push @menu_items, [ "/$name_with_underscore", undef , insert_generator($self, $element, $popup_x, $popup_y), 0 , '<Item>', undef], | ||
| 28 | } | ||
| 29 | |||
| 30 | for my $menu_entry (@{$self->get_context_menu_entries($popup_x, $popup_y)}) | ||
| 31 | { | ||
| 32 | my($name, $sub, $data) = @{$menu_entry} ; | ||
| 33 | (my $name_with_underscore = $name) =~ s/_/__/g ; | ||
| 34 | |||
| 35 | push @menu_items, [ $name_with_underscore, undef , $self->menu_entry_wrapper($sub, $data), 0, '<Item>', undef], | ||
| 36 | } | ||
| 37 | |||
| 38 | push @menu_items, | ||
| 39 | ( | ||
| 40 | ['/File/open', undef , sub {$self->run_actions_by_name('Open') ;}, 0 , '<Item>', undef], | ||
| 41 | ['/File/save', undef , sub {$self->run_actions_by_name('Save') ;}, 0 , '<Item>', undef], | ||
| 42 | [ '/File/save as', undef , sub {$self->run_actions_by_name(['Save', 1]) ;}, 0 , '<Item>', undef], | ||
| 43 | ) ; | ||
| 44 | |||
| 45 | use App::Asciio::Io ; | ||
| 46 | if($self->get_selected_elements(1) == 1) | ||
| 47 | { | ||
| 48 | push @menu_items, [ '/File/save stencil', undef , $self->menu_entry_wrapper(\&App::Asciio::save_stencil), 0 , '<Item>', undef ] ; | ||
| 49 | } | ||
| 50 | |||
| 51 | print "\e[2J\e[H\e[?25h" ; | ||
| 52 | |||
| 53 | my ($menu, $menu_lookup) = ({}, {}) ; | ||
| 54 | |||
| 55 | insert_menu_items($menu, \@menu_items, $menu_lookup) ; | ||
| 56 | |||
| 57 | my $result = prompt -1, 'popup menu ...', -menu => $menu, '> '; | ||
| 58 | |||
| 59 | if($result) | ||
| 60 | { | ||
| 61 | my $action = $menu_lookup->{"$result"} ; | ||
| 62 | my (undef, undef, $sub) = @$action ; | ||
| 63 | |||
| 64 | $sub->() ; | ||
| 65 | } | ||
| 66 | |||
| 67 | $self->update_display() ; | ||
| 68 | } | ||
| 69 | |||
| 70 | sub insert_menu_items | ||
| 71 | { | ||
| 72 | my ($root, $menu_entry_definitions, $menu_lookup) = @_ ; | ||
| 73 | |||
| 74 | my $lookup_index = 0 ; | ||
| 75 | |||
| 76 | for my $menu_entry_definition (map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, $_->[0]] } @$menu_entry_definitions) | ||
| 77 | { | ||
| 78 | $lookup_index++ ; | ||
| 79 | |||
| 80 | my ($path, undef, $sub, undef, $item, undef) = @$menu_entry_definition ; | ||
| 81 | |||
| 82 | $path =~ s~^/~~ or die "Menu path doesn't start at root" ; | ||
| 83 | my @path_elements = split m~/~, $path ; | ||
| 84 | my $name = pop @path_elements ; | ||
| 85 | |||
| 86 | my $parent = my $container = $root ; | ||
| 87 | my $last_path_element = $path_elements[-1] // $path ; | ||
| 88 | |||
| 89 | for my $path_element (@path_elements) | ||
| 90 | { | ||
| 91 | last unless 'HASH' eq ref $container ; | ||
| 92 | |||
| 93 | if(exists $container->{$path_element}) | ||
| 94 | { | ||
| 95 | $parent = $container ; | ||
| 96 | $container = $container->{$path_element} ; | ||
| 97 | } | ||
| 98 | else | ||
| 99 | { | ||
| 100 | $parent = $container ; | ||
| 101 | $container = $container->{$path_element} = {} ; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | $menu_lookup->{"$name [$lookup_index]"} = $menu_entry_definition ; | ||
| 106 | |||
| 107 | if('ARRAY' eq ref $parent->{$last_path_element}) | ||
| 108 | { | ||
| 109 | push @{$parent->{$last_path_element}}, "$name [$lookup_index]" | ||
| 110 | } | ||
| 111 | elsif('HASH' eq ref $parent->{$last_path_element}) | ||
| 112 | { | ||
| 113 | $parent->{$last_path_element} = [ "$name [$lookup_index]"] ; | ||
| 114 | } | ||
| 115 | else | ||
| 116 | { | ||
| 117 | $parent->{"$name [$lookup_index]"} = "$name [$lookup_index]" ; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | sub insert_generator | ||
| 123 | { | ||
| 124 | my ($self, $element, $x, $y) = @_ ; | ||
| 125 | |||
| 126 | return sub | ||
| 127 | { | ||
| 128 | my $new_element = $self->add_new_element_of_type($element, $x, $y) ; | ||
| 129 | $self->deselect_all_elements() ; | ||
| 130 | $self->select_elements(1, $new_element) ; | ||
| 131 | $self->update_display(); | ||
| 132 | } ; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub menu_entry_wrapper | ||
| 136 | { | ||
| 137 | my ($self, $sub, $data) = @_ ; | ||
| 138 | |||
| 139 | return sub { $sub->($self, $data) ; } ; | ||
| 140 | } | ||
| 141 | |||
| 142 | #------------------------------------------------------------------------------------------------------ | ||
| 143 | |||
| 144 | sub get_context_menu_entries | ||
| 145 | { | ||
| 146 | my ($self, $popup_x, $popup_y) = @_ ; | ||
| 147 | my @context_menu_entries ; | ||
| 148 | |||
| 149 | for my $context_menu_handler | ||
| 150 | ( | ||
| 151 | map {$self->{CURRENT_ACTIONS}{$_}} | ||
| 152 | grep | ||
| 153 | { | ||
| 154 | 'HASH' eq ref $self->{CURRENT_ACTIONS}{$_} # not a sub actions definition | ||
| 155 | && defined $self->{CURRENT_ACTIONS}{$_}{CONTEXT_MENU_SUB} | ||
| 156 | } sort keys %{$self->{CURRENT_ACTIONS}} | ||
| 157 | ) | ||
| 158 | { | ||
| 159 | # print "Adding context menu from action '$context_menu_handler->{NAME}'.\n" ; | ||
| 160 | |||
| 161 | if(defined $context_menu_handler->{CONTEXT_MENU_ARGUMENTS}) | ||
| 162 | { | ||
| 163 | push @context_menu_entries, | ||
| 164 | $context_menu_handler->{CONTEXT_MENU_SUB}-> | ||
| 165 | ( | ||
| 166 | $self, | ||
| 167 | $context_menu_handler->{CONTEXT_MENU_ARGUMENTS}, | ||
| 168 | $popup_x, $popup_y, | ||
| 169 | ) ; | ||
| 170 | } | ||
| 171 | else | ||
| 172 | { | ||
| 173 | push @context_menu_entries, $context_menu_handler->{CONTEXT_MENU_SUB}->($self, $popup_x, $popup_y) ; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | return(\@context_menu_entries) ; | ||
| 178 | } | ||
| 179 | |||
| 180 | #------------------------------------------------------------------------------------------------------ | ||
| 181 | |||
| 182 | 1 ; |