| 1 | |||
| 2 | package App::Asciio::Actions::Git ; | ||
| 3 | |||
| 4 | #---------------------------------------------------------------------------------------------- | ||
| 5 | |||
| 6 | use strict ; | ||
| 7 | use warnings ; | ||
| 8 | |||
| 9 | use List::MoreUtils qw(first_value) ; | ||
| 10 | |||
| 11 | #---------------------------------------------------------------------------------------------- | ||
| 12 | { | ||
| 13 | |||
| 14 | my $git_connector_char = '*' ; | ||
| 15 | |||
| 16 | sub set_default_connector { my ($self, $char) = @_ ; $git_connector_char = $char ; } | ||
| 17 | |||
| 18 | sub quick_link | ||
| 19 | { | ||
| 20 | my ($self) = @_ ; | ||
| 21 | my ($x, $y) = @{$self}{'MOUSE_X', 'MOUSE_Y'} ; | ||
| 22 | |||
| 23 | $self->create_undo_snapshot() ; | ||
| 24 | |||
| 25 | my ($destination_element) = first_value {$self->is_over_element($_, $x, $y)} reverse @{$self->{ELEMENTS}} ; | ||
| 26 | |||
| 27 | if($destination_element) | ||
| 28 | { | ||
| 29 | connect_to_destination_element($self, $destination_element, $x, $y) ; | ||
| 30 | } | ||
| 31 | else | ||
| 32 | { | ||
| 33 | my $new_connector = $self->add_new_element_named('Asciio/connector2', $x, $y) ; | ||
| 34 | $new_connector->set_text('', $git_connector_char) ; | ||
| 35 | connect_to_destination_element($self, $new_connector, $x, $y) ; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | } | ||
| 40 | |||
| 41 | #---------------------------------------------------------------------------------------------- | ||
| 42 | |||
| 43 | sub connect_to_destination_element | ||
| 44 | { | ||
| 45 | my ($self, $destination_element, $x, $y) = @_ ; | ||
| 46 | |||
| 47 | my @destination_connections = grep {$_->{NAME} ne 'resize'} $destination_element->get_connection_points() ; | ||
| 48 | |||
| 49 | if(@destination_connections) | ||
| 50 | { | ||
| 51 | my @selected_elements = grep {$_ != $destination_element} $self->get_selected_elements(1) ; | ||
| 52 | my $destination_connection = $destination_connections[0] ; | ||
| 53 | |||
| 54 | if(@selected_elements) | ||
| 55 | { | ||
| 56 | $self->deselect_all_elements() ; | ||
| 57 | |||
| 58 | for my $element (@selected_elements) | ||
| 59 | { | ||
| 60 | # link $element to $destination_element | ||
| 61 | my @source_connections = grep {$_->{NAME} ne 'resize'} $element->get_connection_points() ; | ||
| 62 | |||
| 63 | if(@source_connections) | ||
| 64 | { | ||
| 65 | connect_from_connector($self, $element, $source_connections[0], $destination_element, $destination_connection) ; | ||
| 66 | } | ||
| 67 | else | ||
| 68 | { | ||
| 69 | connect_from_arrow($self, $element, $destination_element, $destination_connection) ; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | $self->select_elements(0, @selected_elements) ; | ||
| 74 | $self->select_elements(1, $destination_element) ; | ||
| 75 | } | ||
| 76 | else | ||
| 77 | { | ||
| 78 | $self->select_elements(1, $destination_element) ; | ||
| 79 | } | ||
| 80 | |||
| 81 | $self->update_display() ; # will also canonize the connections | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | #---------------------------------------------------------------------------------------------- | ||
| 86 | { | ||
| 87 | |||
| 88 | my $git_arrow = | ||
| 89 | [ | ||
| 90 | # name: start, body, connection, body_2, end, vertical, diagonal_connection | ||
| 91 | ['origin' , '*', '?', '?', '?', '?', '?', '?', 1], | ||
| 92 | ['up' , "'", '|', '?', '?', '.', '?', '?', 1], | ||
| 93 | ['down' , '.', '|', '?', '?', "'", '?', '?', 1], | ||
| 94 | ['left' , '-', '-', '?', '?', '-', '?', '?', 1], | ||
| 95 | ['right' , '-', '-', '?', '?', '-', '?', '?', 1], | ||
| 96 | ['up-left' , "'", '\\', '.', '-', '-', '|', "'", 1], | ||
| 97 | ['left-up' , '-', '\\', "'", '-', '.', '|', "'", 1], | ||
| 98 | ['down-left' , '.', '/', "'", '-', '-', '|', "'", 1], | ||
| 99 | ['left-down' , '-', '/', '.', '-', "'", '|', "'", 1], | ||
| 100 | ['up-right' , "'", '/', '.', '-', '-', '|', "'", 1], | ||
| 101 | ['right-up' , '-', '/', "'", '-', '.', '|', "'", 1], | ||
| 102 | ['down-right' , '.', '\\', "'", '-', '-', '|', "'", 1], | ||
| 103 | ['right-down' , '-', '\\', '.', '-', "'", '|', "'", 1], | ||
| 104 | ] ; | ||
| 105 | |||
| 106 | sub set_default_arrow { my ($self, $type) = @_ ; $git_arrow = App::Asciio::Arrows::clone($type) ; } | ||
| 107 | |||
| 108 | #---------------------------------------------------------------------------------------------- | ||
| 109 | |||
| 110 | sub connect_from_connector | ||
| 111 | { | ||
| 112 | my ($self, $element, $source_connection, $destination_element, $destination_connection) = @_ ; | ||
| 113 | |||
| 114 | my $direction = $element->{X} < $destination_element->{X} | ||
| 115 | ? $element->{Y} < $destination_element->{Y} | ||
| 116 | ? 'down-right' | ||
| 117 | : 'up-right' | ||
| 118 | : $element->{Y} < $destination_element->{Y} | ||
| 119 | ? 'down-left' | ||
| 120 | : 'up-left' ; | ||
| 121 | |||
| 122 | my $angled_arrow = new App::Asciio::stripes::angled_arrow | ||
| 123 | ({ | ||
| 124 | END_X => ($destination_element->{X} + $destination_connection->{X}) | ||
| 125 | - ($element->{X} + $source_connection->{X}), | ||
| 126 | |||
| 127 | END_Y => ($destination_element->{Y} + $destination_connection->{Y}) | ||
| 128 | - ($element->{Y} + $source_connection->{Y}), | ||
| 129 | |||
| 130 | DIRECTION => $direction, | ||
| 131 | |||
| 132 | ALLOW_DIAGONAL_LINES => 0, | ||
| 133 | EDITABLE => 1, | ||
| 134 | RESIZABLE => 1, | ||
| 135 | }) ; | ||
| 136 | |||
| 137 | $angled_arrow->set_arrow_type($git_arrow) ; | ||
| 138 | $angled_arrow->enable_autoconnect(0) ; | ||
| 139 | |||
| 140 | $self->add_element_at_no_connection | ||
| 141 | ( | ||
| 142 | $angled_arrow, | ||
| 143 | $element->{X} + $source_connection->{X}, | ||
| 144 | $element->{Y} + $source_connection->{Y} + 1, | ||
| 145 | ) ; | ||
| 146 | |||
| 147 | $self->add_connections | ||
| 148 | ({ | ||
| 149 | CONNECTED => $angled_arrow, | ||
| 150 | CONNECTOR => $angled_arrow->get_named_connection('start'), | ||
| 151 | CONNECTEE => $element, | ||
| 152 | CONNECTION => $source_connection, | ||
| 153 | }) ; | ||
| 154 | |||
| 155 | $self->add_connections | ||
| 156 | ({ | ||
| 157 | CONNECTED => $angled_arrow, | ||
| 158 | CONNECTOR => $angled_arrow->get_named_connection('end'), | ||
| 159 | CONNECTEE => $destination_element, | ||
| 160 | CONNECTION => $destination_connection, | ||
| 161 | }) ; | ||
| 162 | |||
| 163 | $self->move_elements_to_back($angled_arrow) ; | ||
| 164 | } | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | |||
| 169 | #---------------------------------------------------------------------------------------------- | ||
| 170 | |||
| 171 | sub connect_from_arrow | ||
| 172 | { | ||
| 173 | my ($self, $element, $destination_element, $destination_connection) = @_ ; | ||
| 174 | |||
| 175 | my %source_connectors = map {$_->{NAME} => $_} grep {$_->{NAME} ne 'resize'} $element->get_connector_points() ; | ||
| 176 | |||
| 177 | for(grep {$_->{CONNECTED} == $element } @{$self->{CONNECTIONS}}) | ||
| 178 | { | ||
| 179 | delete $source_connectors{$_->{CONNECTOR}{NAME}} ; | ||
| 180 | } | ||
| 181 | |||
| 182 | my ($unconnected_connector_name) = reverse sort keys %source_connectors ; | ||
| 183 | |||
| 184 | if($unconnected_connector_name) | ||
| 185 | { | ||
| 186 | my $unconnected_connector = $source_connectors{$unconnected_connector_name} ; | ||
| 187 | |||
| 188 | my ($x_offset, $y_offset) = | ||
| 189 | $element->move_connector | ||
| 190 | ( | ||
| 191 | $unconnected_connector_name, | ||
| 192 | |||
| 193 | ($destination_element->{X} + $destination_connection->{X}) - ($element->{X} + $unconnected_connector->{X}), | ||
| 194 | ($destination_element->{Y} + $destination_connection->{Y}) - ($element->{Y} + $unconnected_connector->{Y}), | ||
| 195 | ) ; | ||
| 196 | |||
| 197 | $element->{X} += $x_offset ; | ||
| 198 | $element->{Y} += $y_offset ; | ||
| 199 | |||
| 200 | my $new_connection = | ||
| 201 | { | ||
| 202 | CONNECTED => $element, | ||
| 203 | CONNECTOR =>$unconnected_connector, | ||
| 204 | CONNECTEE => $destination_element, | ||
| 205 | CONNECTION => $destination_connection, | ||
| 206 | } ; | ||
| 207 | |||
| 208 | $self->add_connections($new_connection) ; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | #---------------------------------------------------------------------------------------------- | ||
| 213 | |||
| 214 | use App::Asciio::Actions::ElementsManipulation ; | ||
| 215 | |||
| 216 | sub edit_selected_element | ||
| 217 | { | ||
| 218 | my ($self, $alternate_mode) = @_ ; | ||
| 219 | |||
| 220 | my @selected_elements = $self->get_selected_elements(1) ; | ||
| 221 | |||
| 222 | App::Asciio::Actions::ElementsManipulation::edit_selected_element($self, $alternate_mode) | ||
| 223 | if @selected_elements == 1 && $selected_elements[0]->isa('App::Asciio::stripes::editable_box2') ; | ||
| 224 | } | ||
| 225 | |||
| 226 | #---------------------------------------------------------------------------------------------- | ||
| 227 | |||
| 228 | 1 ; | ||
| 229 |