Przeglądaj źródła

fixed having a small box appear in screenshots when clicking on windows for windowshot

naelstrof 10 lat temu
rodzic
commit
5ddafa3bb9
1 zmienionych plików z 37 dodań i 24 usunięć
  1. 37
    24
      main.cpp

+ 37
- 24
main.cpp Wyświetl plik

@@ -10,7 +10,7 @@ int main( int argc, char** argv ) {
10 10
     }
11 11
     int state = 0;
12 12
     bool running = true;
13
-    slop::Rectangle* selection;
13
+    slop::Rectangle* selection = NULL;
14 14
     slop::Rectangle* windowselection = NULL;
15 15
     Window window = None;
16 16
     std::string xdisplay = options->m_xdisplay;
@@ -20,6 +20,8 @@ int main( int argc, char** argv ) {
20 20
     float r = options->m_red;
21 21
     float g = options->m_green;
22 22
     float b = options->m_blue;
23
+    int cx = 0;
24
+    int cy = 0;
23 25
 
24 26
     // First we set up the x interface and grab the mouse,
25 27
     // if we fail for either we exit immediately.
@@ -91,10 +93,9 @@ int main( int argc, char** argv ) {
91 93
                 break;
92 94
             }
93 95
             case 1: {
94
-                // Simply create a new rectangle at the mouse position and move on
95
-                // to the next state.
96
-                selection = new slop::Rectangle( xengine->m_mousex, xengine->m_mousey, 0, 0, borderSize, padding, r, g, b );
97
-                xengine->addRect( selection );
96
+                // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
97
+                cx = xengine->m_mousex;
98
+                cy = xengine->m_mousey;
98 99
                 state++;
99 100
                 break;
100 101
             }
@@ -105,10 +106,19 @@ int main( int argc, char** argv ) {
105 106
                     state++;
106 107
                     break;
107 108
                 }
109
+                // Check to make sure the user actually wants to drag for a selection before creating a rectangle.
110
+                int w = xengine->m_mousex - cx;
111
+                int h = xengine->m_mousey - cy;
112
+                if ( ( std::abs( w ) > tolerance || std::abs( h ) > tolerance ) && !selection ) {
113
+                    selection = new slop::Rectangle( cx, cy, 0, 0, borderSize, padding, r, g, b );
114
+                    xengine->addRect( selection );
115
+                } else if ( std::abs( w ) <= tolerance && std::abs( h ) <= tolerance ) {
116
+                    continue;
117
+                }
108 118
                 // Set the selection rectangle's dimensions to mouse movement.
109 119
                 // We use the function setDim since rectangles can't have negative widths,
110 120
                 // and because the rectangles have borders and padding to worry about.
111
-                selection->setDim( xengine->m_mousex - selection->m_x, xengine->m_mousey - selection->m_y );
121
+                selection->setDim( w, h );
112 122
                 // We also detect which way the user is pulling and set the mouse icon accordingly.
113 123
                 bool x = selection->m_flippedx;
114 124
                 bool y = selection->m_flippedy;
@@ -125,26 +135,29 @@ int main( int argc, char** argv ) {
125 135
                 break;
126 136
             }
127 137
             case 3: {
128
-                // We pull the dimensions and positions from the selection rectangle.
129
-                // The selection rectangle automatically converts the positions and
130
-                // dimensions to absolute coordinates when we set them earilier.
131
-                int x = selection->m_x+selection->m_xoffset;
132
-                int y = selection->m_y+selection->m_yoffset;
133
-                int w = selection->m_width;
134
-                int h = selection->m_height;
135
-                // Delete the rectangle.
136
-                xengine->removeRect( selection );
138
+                int x, y, w, h;
137 139
                 // Exit the utility after this state runs once.
138 140
                 running = false;
139
-                // If the user simply clicked (and thus made the width and height smaller than
140
-                // our tolerance) or if we're not hovering over a window, just print the selection
141
-                // rectangle's stuff.
142
-                if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
143
-                    printf( "X=%i\n", x );
144
-                    printf( "Y=%i\n", y );
145
-                    printf( "W=%i\n", w + 1 );
146
-                    printf( "H=%i\n", h + 1 );
147
-                    break;
141
+                if ( selection ) {
142
+                    // We pull the dimensions and positions from the selection rectangle.
143
+                    // The selection rectangle automatically converts the positions and
144
+                    // dimensions to absolute coordinates when we set them earilier.
145
+                    x = selection->m_x+selection->m_xoffset;
146
+                    y = selection->m_y+selection->m_yoffset;
147
+                    w = selection->m_width;
148
+                    h = selection->m_height;
149
+                    // Delete the rectangle.
150
+                    xengine->removeRect( selection );
151
+                    // If the user simply clicked (and thus made the width and height smaller than
152
+                    // our tolerance) or if we're not hovering over a window, just print the selection
153
+                    // rectangle's stuff.
154
+                    if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
155
+                        printf( "X=%i\n", x );
156
+                        printf( "Y=%i\n", y );
157
+                        printf( "W=%i\n", w + 1 );
158
+                        printf( "H=%i\n", h + 1 );
159
+                        break;
160
+                    }
148 161
                 }
149 162
                 // Otherwise lets grab the window's dimensions and use those (with padding).
150 163
                 slop::WindowRectangle t = xengine->m_hoverWindow;