| 1 | package Gtk3::ImageView::Tool::Selector; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use base 'Gtk3::ImageView::Tool'; | ||
| 6 | use POSIX qw(round); | ||
| 7 | use Glib qw(TRUE FALSE); # To get TRUE and FALSE | ||
| 8 | use List::Util qw(min); | ||
| 9 | use Readonly; | ||
| 10 | Readonly my $CURSOR_PIXELS => 5; | ||
| 11 | Readonly my $RIGHT_BUTTON => 3; | ||
| 12 | |||
| 13 | our $VERSION = 8; | ||
| 14 | |||
| 15 | my %cursorhash = ( | ||
| 16 | lower => { | ||
| 17 | lower => 'nw-resize', | ||
| 18 | mid => 'w-resize', | ||
| 19 | upper => 'sw-resize', | ||
| 20 | }, | ||
| 21 | mid => { | ||
| 22 | lower => 'n-resize', | ||
| 23 | mid => 'crosshair', | ||
| 24 | upper => 's-resize', | ||
| 25 | }, | ||
| 26 | upper => { | ||
| 27 | lower => 'ne-resize', | ||
| 28 | mid => 'e-resize', | ||
| 29 | upper => 'se-resize', | ||
| 30 | }, | ||
| 31 | ); | ||
| 32 | |||
| 33 | sub button_pressed { | ||
| 34 | my $self = shift; | ||
| 35 | my $event = shift; | ||
| 36 | |||
| 37 | # Don't block context menu | ||
| 38 | if ( $event->button == $RIGHT_BUTTON ) { | ||
| 39 | return FALSE; | ||
| 40 | } | ||
| 41 | |||
| 42 | $self->{drag_start} = { x => undef, y => undef }; | ||
| 43 | $self->{dragging} = TRUE; | ||
| 44 | $self->view->update_cursor( $event->x, $event->y ); | ||
| 45 | $self->_update_selection($event); | ||
| 46 | return TRUE; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub button_released { | ||
| 50 | my $self = shift; | ||
| 51 | my $event = shift; | ||
| 52 | $self->{dragging} = FALSE; | ||
| 53 | $self->view->update_cursor( $event->x, $event->y ); | ||
| 54 | $self->_update_selection($event); | ||
| 55 | return; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub motion { | ||
| 59 | my $self = shift; | ||
| 60 | my $event = shift; | ||
| 61 | if ( not $self->{dragging} ) { return FALSE } | ||
| 62 | $self->_update_selection($event); | ||
| 63 | return; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub _update_selection { | ||
| 67 | my ( $self, $event ) = @_; | ||
| 68 | my ( $x, $y, $x2, $y2, $x_old, $y_old, $x2_old, $y2_old ); | ||
| 69 | if ( not defined $self->{h_edge} ) { $self->{h_edge} = 'mid' } | ||
| 70 | if ( not defined $self->{v_edge} ) { $self->{v_edge} = 'mid' } | ||
| 71 | if ( $self->{h_edge} eq 'lower' ) { | ||
| 72 | $x = $event->x; | ||
| 73 | $x2 = $self->{drag_start}{x}; | ||
| 74 | } | ||
| 75 | elsif ( $self->{h_edge} eq 'upper' ) { | ||
| 76 | $x = $self->{drag_start}{x}; | ||
| 77 | $x2 = $event->x; | ||
| 78 | } | ||
| 79 | if ( $self->{v_edge} eq 'lower' ) { | ||
| 80 | $y = $event->y; | ||
| 81 | $y2 = $self->{drag_start}{y}; | ||
| 82 | } | ||
| 83 | elsif ( $self->{v_edge} eq 'upper' ) { | ||
| 84 | $y = $self->{drag_start}{y}; | ||
| 85 | $y2 = $event->y; | ||
| 86 | } | ||
| 87 | if ( $self->{h_edge} eq 'mid' and $self->{v_edge} eq 'mid' ) { | ||
| 88 | $x = $self->{drag_start}{x}; | ||
| 89 | $y = $self->{drag_start}{y}; | ||
| 90 | $x2 = $event->x; | ||
| 91 | $y2 = $event->y; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | my $selection = $self->view->get_selection; | ||
| 95 | if ( not defined $x or not defined $y ) { | ||
| 96 | ( $x_old, $y_old ) = | ||
| 97 | $self->view->to_widget_coords( $selection->{x}, $selection->{y} ); | ||
| 98 | } | ||
| 99 | if ( not defined $x2 or not defined $y2 ) { | ||
| 100 | ( $x2_old, $y2_old ) = $self->view->to_widget_coords( | ||
| 101 | $selection->{x} + $selection->{width}, | ||
| 102 | $selection->{y} + $selection->{height} | ||
| 103 | ); | ||
| 104 | } | ||
| 105 | if ( not defined $x ) { | ||
| 106 | $x = $x_old; | ||
| 107 | } | ||
| 108 | if ( not defined $x2 ) { | ||
| 109 | $x2 = $x2_old; | ||
| 110 | } | ||
| 111 | if ( not defined $y ) { | ||
| 112 | $y = $y_old; | ||
| 113 | } | ||
| 114 | if ( not defined $y2 ) { | ||
| 115 | $y2 = $y2_old; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | my ( $w, $h ) = | ||
| 119 | $self->view->to_image_distance( abs $x2 - $x, abs $y2 - $y ); | ||
| 120 | ( $x, $y ) = | ||
| 121 | $self->view->to_image_coords( min( $x, $x2 ), min( $y, $y2 ) ); | ||
| 122 | $self->view->set_selection( | ||
| 123 | { | ||
| 124 | x => round($x), | ||
| 125 | y => round($y), | ||
| 126 | width => round($w), | ||
| 127 | height => round($h) | ||
| 128 | } | ||
| 129 | ); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | |||
| 133 | sub cursor_type_at_point { | ||
| 134 | my ( $self, $x, $y ) = @_; | ||
| 135 | |||
| 136 | my $selection = $self->view->get_selection; | ||
| 137 | if ( defined $selection ) { | ||
| 138 | my ( $sx1, $sy1 ) = | ||
| 139 | $self->view->to_widget_coords( $selection->{x}, $selection->{y} ); | ||
| 140 | my ( $sx2, $sy2 ) = $self->view->to_widget_coords( | ||
| 141 | $selection->{x} + $selection->{width}, | ||
| 142 | $selection->{y} + $selection->{height} | ||
| 143 | ); | ||
| 144 | |||
| 145 | # If we are dragging, a corner cursor must stay as a corner cursor, | ||
| 146 | # a left/right cursor must stay as left/right, | ||
| 147 | # and a top/bottom cursor must stay as top/bottom | ||
| 148 | if ( $self->{dragging} ) { | ||
| 149 | $self->_update_dragged_edge( 'x', $x, $sx1, $sx2 ); | ||
| 150 | $self->_update_dragged_edge( 'y', $y, $sy1, $sy2 ); | ||
| 151 | if ( $self->{h_edge} eq 'mid' ) { | ||
| 152 | if ( $self->{v_edge} eq 'mid' ) { | ||
| 153 | $self->{h_edge} = 'upper'; | ||
| 154 | $self->{v_edge} = 'upper'; | ||
| 155 | $self->{drag_start} = { x => $x, y => $y }; | ||
| 156 | } | ||
| 157 | else { | ||
| 158 | if ( not defined $self->{drag_start}{x} ) { | ||
| 159 | $self->{drag_start}{x} = | ||
| 160 | $self->{v_edge} eq 'lower' ? $sx2 : $sx1; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | } | ||
| 164 | elsif ( $self->{v_edge} eq 'mid' ) { | ||
| 165 | if ( not defined $self->{drag_start}{y} ) { | ||
| 166 | $self->{drag_start}{y} = | ||
| 167 | $self->{h_edge} eq 'lower' ? $sy2 : $sy1; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | } | ||
| 171 | else { | ||
| 172 | $self->_update_undragged_edge( 'h_edge', $x, $y, $sx1, $sy1, $sx2, | ||
| 173 | $sy2 ); | ||
| 174 | $self->_update_undragged_edge( 'v_edge', $y, $x, $sy1, $sx1, $sy2, | ||
| 175 | $sx2 ); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | if ( $self->{dragging} ) { | ||
| 180 | $self->{drag_start} = { x => $x, y => $y }; | ||
| 181 | ( $self->{h_edge}, $self->{v_edge} ) = qw( upper upper ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | ( $self->{h_edge}, $self->{v_edge} ) = qw( mid mid ); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | return $cursorhash{ $self->{h_edge} }{ $self->{v_edge} }; | ||
| 188 | } | ||
| 189 | |||
| 190 | sub _update_dragged_edge { | ||
| 191 | my ( $self, $direction, $s, $s1, $s2 ) = @_; | ||
| 192 | my $edge = ( $direction eq 'x' ? 'h' : 'v' ) . '_edge'; | ||
| 193 | if ( $self->{$edge} eq 'lower' ) { | ||
| 194 | if ( defined $self->{drag_start}{$direction} ) { | ||
| 195 | if ( $s > $self->{drag_start}{$direction} ) { | ||
| 196 | $self->{$edge} = 'upper'; | ||
| 197 | } | ||
| 198 | else { | ||
| 199 | $self->{$edge} = 'lower'; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | $self->{drag_start}{$direction} = $s2; | ||
| 204 | $self->{$edge} = 'lower'; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | elsif ( $self->{$edge} eq 'upper' ) { | ||
| 208 | if ( defined $self->{drag_start}{$direction} ) { | ||
| 209 | if ( $s < $self->{drag_start}{$direction} ) { | ||
| 210 | $self->{$edge} = 'lower'; | ||
| 211 | } | ||
| 212 | else { | ||
| 213 | $self->{$edge} = 'upper'; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | else { | ||
| 217 | $self->{drag_start}{$direction} = $s1; | ||
| 218 | $self->{$edge} = 'upper'; | ||
| 219 | } | ||
| 220 | } | ||
| 221 | return; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub _update_undragged_edge { | ||
| 225 | my ( $self, $edge, @coords ) = @_; | ||
| 226 | my ( $x, $y, $sx1, $sy1, $sx2, $sy2 ) = @coords; | ||
| 227 | $self->{$edge} = 'mid'; | ||
| 228 | if ( _between( $y, $sy1, $sy2 ) ) { | ||
| 229 | if ( _between( $x, $sx1 - $CURSOR_PIXELS, $sx1 + $CURSOR_PIXELS ) ) { | ||
| 230 | $self->{$edge} = 'lower'; | ||
| 231 | } | ||
| 232 | elsif ( _between( $x, $sx2 - $CURSOR_PIXELS, $sx2 + $CURSOR_PIXELS ) ) { | ||
| 233 | $self->{$edge} = 'upper'; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | return; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub _between { | ||
| 240 | my ( $value, $lower, $upper ) = @_; | ||
| 241 | return ( $value > $lower and $value < $upper ); | ||
| 242 | } | ||
| 243 | |||
| 244 | # compatibility layer | ||
| 245 | |||
| 246 | sub get_selection { | ||
| 247 | my $self = shift; | ||
| 248 | return $self->view->get_selection; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub set_selection { | ||
| 252 | my ( $self, @args ) = @_; | ||
| 253 | $self->view->set_selection(@args); | ||
| 254 | return; | ||
| 255 | } | ||
| 256 | |||
| 257 | 1; |