File: Selector.pm

package info (click to toggle)
libgtk3-imageview-perl 10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 228 kB
  • sloc: perl: 1,163; makefile: 4
file content (211 lines) | stat: -rw-r--r-- 5,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package Gtk3::ImageView::Tool::Selector;

use warnings;
use strict;
use base 'Gtk3::ImageView::Tool';
use Glib qw(TRUE FALSE);    # To get TRUE and FALSE
use List::Util qw(min);
use Readonly;
Readonly my $RIGHT_BUTTON => 3;
Readonly my $EDGE_WIDTH   => 5;

our $VERSION = '10';

sub button_pressed {
    my ( $self, $event ) = @_;
    if ( $event->button == $RIGHT_BUTTON ) {
        return FALSE;
    }
    my $type = $self->cursor_type_at_point( $event->x, $event->y );
    if ( $type eq 'grab' ) {
        $type                 = 'grabbing';
        $self->{drag_start_x} = int( $event->x + 0.5 );
        $self->{drag_start_y} = int( $event->y + 0.5 );
    }
    $self->{dragging} = $type;
    $self->_update_selection( $event->x, $event->y );
    return FALSE;
}

sub button_released {
    my ( $self, $event ) = @_;
    if ( $event->button == $RIGHT_BUTTON ) {
        return FALSE;
    }
    if ( $self->{dragging} ) {
        $self->_update_selection( $event->x, $event->y );
    }
    $self->{dragging} = undef;
    $self->view->update_cursor( $event->x, $event->y );
    return FALSE;
}

sub motion {
    my ( $self, $event ) = @_;
    if ( $self->{dragging} ) {
        $self->_update_selection( $event->x, $event->y );
    }
    return FALSE;
}

sub cursor_type_at_point {    ## no critic (ProhibitExcessComplexity);
    my ( $self, $x, $y ) = @_;
    if ( $self->{dragging} ) {
        return $self->{dragging};
    }
    my $selection = $self->view->get_selection;
    if ( !defined $selection ) {
        return 'crosshair';
    }
    my $edge_width = $EDGE_WIDTH * $self->view->get('scale-factor');
    my ( $sx1, $sy1 ) =
      $self->view->to_widget_coords( $selection->{x}, $selection->{y} );
    my ( $sx2, $sy2 ) = $self->view->to_widget_coords(
        $selection->{x} + $selection->{width},
        $selection->{y} + $selection->{height}
    );
    if (   $x < $sx1 - $edge_width
        || $x > $sx2 + $edge_width
        || $y < $sy1 - $edge_width
        || $y > $sy2 + $edge_width )
    {
        return 'crosshair';
    }
    if (   $x > $sx1 + $edge_width
        && $x < $sx2 - $edge_width
        && $y > $sy1 + $edge_width
        && $y < $sy2 - $edge_width )
    {
        return 'grab';
    }

# This makes it possible for the selection to be smaller than edge_width and still be resizeable in all directions
    my $leftish = $x < ( $sx1 + $sx2 ) / 2;
    my $topish  = $y < ( $sy1 + $sy2 ) / 2;
    if ( $y > $sy1 + $edge_width && $y < $sy2 - $edge_width ) {
        if ($leftish) {
            return 'w-resize';
        }
        else {
            return 'e-resize';
        }
    }
    if ( $x > $sx1 + $edge_width && $x < $sx2 - $edge_width ) {
        if ($topish) {
            return 'n-resize';
        }
        else {
            return 's-resize';
        }
    }
    if ($leftish) {
        if ($topish) {
            return 'nw-resize';
        }
        else {
            return 'sw-resize';
        }
    }
    else {
        if ($topish) {
            return 'ne-resize';
        }
        else {
            return 'se-resize';
        }
    }
}

sub _update_selection {
    my ( $self, $x, $y ) = @_;
    my $selection = $self->view->get('selection-float') // {
        x      => 0,
        y      => 0,
        width  => 0,
        height => 0,
    };
    my ( $sel_x1, $sel_y1 ) =
      $self->view->to_widget_coords( $selection->{x}, $selection->{y} );
    my ( $sel_x2, $sel_y2 ) = $self->view->to_widget_coords(
        $selection->{x} + $selection->{width},
        $selection->{y} + $selection->{height}
    );
    my $type = $self->{dragging};
    if ( $type eq 'grabbing' ) {
        my $off_x = $x - $self->{drag_start_x};
        my $off_y = $y - $self->{drag_start_y};
        $sel_x1 += $off_x;
        $sel_x2 += $off_x;
        $sel_y1 += $off_y;
        $sel_y2 += $off_y;
        $self->{drag_start_x} = $x;
        $self->{drag_start_y} = $y;
    }
    if ( $type eq 'crosshair' ) {
        $sel_x1 = $x;
        $sel_x2 = $x;
        $sel_y1 = $y;
        $sel_y2 = $y;
        $type   = 'se-resize';
    }
    my $flip_we = 0;
    my $flip_ns = 0;
    if ( $type =~ /w-resize/smx ) {
        $sel_x1 = $x;
        if ( $x > $sel_x2 ) { $flip_we = 'e' }
    }
    if ( $type =~ /e-resize/smx ) {
        $sel_x2 = $x;
        if ( $x < $sel_x1 ) { $flip_we = 'w' }
    }
    if ( $type =~ /n.?-resize/smx ) {
        $sel_y1 = $y;
        if ( $y > $sel_y2 ) { $flip_ns = 's' }
    }
    if ( $type =~ /s.?-resize/smx ) {
        $sel_y2 = $y;
        if ( $y < $sel_y1 ) { $flip_ns = 'n' }
    }
    my ( $w, $h ) = $self->view->to_image_distance( abs( $sel_x2 - $sel_x1 ),
        abs( $sel_y2 - $sel_y1 ) );
    my ( $img_x, $img_y ) =
      $self->view->to_image_coords( min( $sel_x1, $sel_x2 ),
        min( $sel_y1, $sel_y2 ) );
    $self->view->set_selection(
        {
            x      => $img_x,
            y      => $img_y,
            width  => $w,
            height => $h,
        }
    );

    # Prepare for next mouse event
    # If we are dragging, a corner cursor must stay as a corner cursor,
    # a left/right cursor must stay as left/right,
    # and a top/bottom cursor must stay as top/bottom
    if ($flip_we) {
        $type =~ s/[we]-/$flip_we-/smx;
    }
    if ($flip_ns) {
        $type =~ s/^[ns]/$flip_ns/smx;
    }
    $self->{dragging} = $type;
    $self->view->update_cursor( $x, $y );
    return;
}

# compatibility layer

sub get_selection {
    my $self = shift;
    return $self->view->get_selection;
}

sub set_selection {
    my ( $self, @args ) = @_;
    $self->view->set_selection(@args);
    return;
}

1;