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